Example #1
0
 /**
  * Constructs a result graph.
  *
  * @param  array         $arVartable       A table containing the result vars and their bindings
  * @param  GraphPattern  $constructPattern The CONSTRUCT pattern
  * @return MemModel      The result graph which matches the CONSTRUCT pattern
  */
 function constructGraph($arVartable, $constructPattern)
 {
     $resultGraph = new MemModel();
     if (!$arVartable) {
         return $resultGraph;
     }
     $tp = $constructPattern->getTriplePatterns();
     $bnode = 0;
     foreach ($arVartable as $value) {
         foreach ($tp as $triple) {
             $sub = $triple->getSubject();
             $pred = $triple->getPredicate();
             $obj = $triple->getObject();
             if (is_string($sub) && $sub[1] == '_') {
                 $sub = new BlankNode("_bN" . $bnode);
             }
             if (is_string($pred) && $pred[1] == '_') {
                 $pred = new BlankNode("_bN" . $bnode);
             }
             if (is_string($obj) && $obj[1] == '_') {
                 $obj = new BlankNode("_bN" . $bnode);
             }
             if (is_string($sub)) {
                 $sub = $value[$sub];
             }
             if (is_string($pred)) {
                 $pred = $value[$pred];
             }
             if (is_string($obj)) {
                 $obj = $value[$obj];
             }
             if ($sub !== "" && $pred !== "" && $obj !== "") {
                 $resultGraph->add(new Statement($sub, $pred, $obj));
             }
         }
         $bnode++;
     }
     return $resultGraph;
 }
Example #2
0
 /**
  * Finds tuples that match one graph pattern.
  *
  * @param  Array        $patternlist list that contains the graphPatterns
  * @param  array        $graphlist   the graphlist
  * @param  GraphPattern $graphPattern the pattern which has to be matched
  * @return void
  */
 protected function matchPattern(&$patternlist, $graphlist, &$graphPattern)
 {
     // generate an empty result set
     $finalRes = null;
     // if the GraphPattern has triple patterns
     if (count($graphPattern->getTriplePatterns()) > 0) {
         // check if the pattern has a GRAPH clause and if this Iri is in $graphlist
         $newGraphList = $this->_checkGraphs($graphPattern, $graphlist);
         if ($newGraphList) {
             $qt = $graphPattern->getTriplePatterns();
             $resultSet = $this->findTuplesMatchingOnePattern($qt[0], $newGraphList);
             for ($i = 1; $i < count($qt); $i++) {
                 $rs = $this->findTuplesMatchingOnePattern($qt[$i], $newGraphList);
                 $resultSet = $this->joinTuples($resultSet, $rs);
                 if (!$resultSet) {
                     break;
                 }
             }
             if ($finalRes != null) {
                 $finalRes = $this->joinTuples($finalRes, $resultSet);
             } else {
                 $finalRes = $resultSet;
             }
         }
     }
     // dependencies between pattern results
     $patternlist[$graphPattern->getId()]['hasOptional'] = 0;
     $patternlist[$graphPattern->getId()]['hasUnion'] = 0;
     $patternlist[$graphPattern->getId()]['patternResult'] = $finalRes;
     $op = $graphPattern->getOptional();
     $un = $graphPattern->getUnion();
     $patternlist[$graphPattern->getId()]['optionalTo'] = $op;
     if (is_int($op)) {
         $patternlist[$op]['hasOptional']++;
     }
     $patternlist[$graphPattern->getId()]['unionWith'] = $un;
     if (is_int($un)) {
         $patternlist[$un]['hasUnion']++;
     }
     $constraint = $graphPattern->getConstraints();
     if (count($constraint) > 0) {
         foreach ($constraint as $constr) {
             if ($constr->isOuterFilter()) {
                 $patternlist[$graphPattern->getId()]['outerFilter'][] = $constr;
                 $patternlist[$graphPattern->getId()]['innerFilter'][] = null;
             } else {
                 $patternlist[$graphPattern->getId()]['innerFilter'][] = $constr;
                 $patternlist[$graphPattern->getId()]['outerFilter'][] = null;
             }
         }
     } else {
         $patternlist[$graphPattern->getId()]['innerFilter'] = null;
         $patternlist[$graphPattern->getId()]['outerFilter'] = null;
     }
 }