You are here

function ARC_sparql_parser::parse_Triples1 in Taxonomy import/export via XML 6

Same name and namespace in other branches
  1. 5.2 arc/ARC_sparql_parser.php \ARC_sparql_parser::parse_Triples1()
  2. 5 arc/ARC_sparql_parser.php \ARC_sparql_parser::parse_Triples1()
  3. 6.2 arc/ARC_sparql_parser.php \ARC_sparql_parser::parse_Triples1()
2 calls to ARC_sparql_parser::parse_Triples1()
ARC_sparql_parser::parse_ConstructTriples in arc/ARC_sparql_parser.php
ARC_sparql_parser::parse_Triples in arc/ARC_sparql_parser.php

File

arc/ARC_sparql_parser.php, line 945

Class

ARC_sparql_parser

Code

function parse_Triples1($val = "") {
  $nr = rand();
  $triples = array();
  $state = 1;

  /* expecting subject */
  $s_stack = array();
  $p_stack = array();
  $state_stack = array();
  do {
    $proceed = false;
    $blank_node_prop_list_start_found = false;

    /* [ */
    $term = false;

    /* var */
    if ($sub_result = $this
      ->parse_Var($val)) {
      $term = $sub_result;
      $val = $sub_result["unparsed_val"];
    }
    elseif ($sub_result = $this
      ->parse_IRIref($val)) {
      $term = $sub_result;
      $val = $sub_result["unparsed_val"];
    }
    elseif ($sub_result = $this
      ->parse_RDFLiteral($val)) {
      $term = $sub_result;
      $val = $sub_result["unparsed_val"];
    }
    elseif (preg_match("/^(\\+\\-)?(.*)\$/s", $val, $matches) && ($sub_result = $this
      ->parse_NumericLiteral(trim($matches[2])))) {
      $sub_result["negated"] = $matches[1] == "-" ? true : false;
      $term = $sub_result;
      $val = $sub_result["unparsed_val"];
    }
    elseif ($sub_result = $this
      ->parse_BooleanLiteral($val)) {
      $term = $sub_result;
      $val = $sub_result["unparsed_val"];
    }
    elseif ($sub_result = $this
      ->parse_BlankNode($val)) {
      $term = $sub_result;
      $val = $sub_result["unparsed_val"];
    }
    elseif (preg_match("/^\\(\\s*\\)(.*)\$/s", $val, $matches)) {
      $term = array(
        "type" => "iri",
        "val" => "http://www.w3.org/1999/02/22-rdf-syntax-ns#nil",
      );
      $val = trim($matches[1]);
    }
    elseif (preg_match("/^a\\s+(.*)\$/s", $val, $matches)) {
      $term = array(
        "type" => "iri",
        "val" => "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
      );
      $val = trim($matches[1]);
    }
    elseif (preg_match("/^=\\s*(.*)\$/s", $val, $matches)) {
      $term = array(
        "type" => "iri",
        "val" => "http://www.w3.org/2002/07/owl#sameAs",
      );
      $val = trim($matches[1]);
    }
    elseif ($sub_result = $this
      ->parse_Collection($val)) {
      $term = $sub_result;
      $val = $sub_result["unparsed_val"];
    }
    elseif (preg_match("/^\\[\\s*(.*)\$/s", $val, $matches)) {
      $id = $this
        ->get_next_bnode_id();
      if (!in_array(str_replace(":", "_", $id), $this->infos["vars"])) {
        $this->infos["vars"][] = str_replace(":", "_", $id);
      }
      $term = array(
        "type" => "bnode",
        "val" => $id,
      );
      $val = trim($matches[1]);
      $state_stack[] = $state;
      $blank_node_prop_list_start_found = true;
    }
    elseif (preg_match("/^\\]\\s*(.*)\$/s", $val, $matches)) {
      $val = trim($matches[1]);
      $proceed = true;

      /* pop s */
      $s_stack = $this
        ->pop($s_stack);

      /* pop p */
      $p_stack = $this
        ->pop($p_stack);

      /* state */
      $state = count($state_stack) ? $state_stack[count($state_stack) - 1] : 1;
      $state_stack = $this
        ->pop($state_stack);
    }
    elseif (preg_match("/^\\;\\s*(.*)\$/s", $val, $matches)) {
      $val = trim($matches[1]);
      $proceed = true;

      /* state */
      $state = 2;

      /* expecting predicate */

      /* pop p */
      $p_stack = $this
        ->pop($p_stack);
    }
    elseif (preg_match("/^\\,\\s*(.*)\$/s", $val, $matches)) {
      $val = trim($matches[1]);
      $proceed = true;

      /* state */
      $state = 3;

      /* expecting object */
    }
    if ($term) {

      //$this->logs[]="term ".$nr." : '".print_r($term, true)."'";
      unset($term["unparsed_val"]);
      $proceed = true;

      /* new s */
      if ($state == 1) {
        $s_stack[] = $term;
        $state = 2;
        $p_stack = $this
          ->pop($p_stack);
      }
      elseif ($state == 2) {
        $p_stack[] = $term;
        $state = 3;
      }
      elseif ($state == 3) {
        $triples[] = array(
          "s" => $s_stack[count($s_stack) - 1],
          "p" => $p_stack[count($p_stack) - 1],
          "o" => $term,
        );
        $this->logs[] = "adding triple '" . $s_stack[count($s_stack) - 1]["val"] . "' - '" . $p_stack[count($p_stack) - 1]["val"] . "' - '" . $term["val"] . "'";
        $state = 1;
        if ($blank_node_prop_list_start_found) {
          $state = 2;
          $s_stack[] = $term;
        }
        elseif (count($state_stack)) {

          /* still in [...] */
          $state = 2;
        }
        elseif (substr($val, 0, 1) === ".") {
          $val = trim(substr($val, 1));
        }
      }
    }
  } while ($proceed);
  return array(
    "type" => "triples",
    "triples" => $triples,
    "unparsed_val" => trim($val),
  );
}