Example #1
0
 /**
  * It gets all statements of a given graph which match the following conditions:
  * - statement's subject is either equal to the subject of the same statement of the graph or
  *   it is null.
  * - statement's predicate is either equal to the predicate of the same statement of the graph or
  *   it is null.
  * - statement's object is either equal to the object of a statement of the graph or it is null.
  *
  * @param  Statement         $Statement          It can be either a concrete or pattern-statement.
  * @param  Node              $graph     optional Overrides target graph. If set, you will get all
  *                                               matching statements of that graph.
  * @param  array             $options   optional It contains key-value pairs and should provide
  *                                               additional introductions for the store and/or its
  *                                               adapter(s).
  * @return StatementIterator It contains Statement instances  of all matching statements of the
  *                           given graph.
  * @todo check if graph URI is valid
  * @todo make it possible to read graphUri from $statement, if given $graphUri is null
  */
 public function getMatchingStatements(Statement $statement, Node $graph = null, array $options = array())
 {
     // otherwise check, if graph was set in the statement and it is a named node and use it, if so.
     if (null === $graph && $statement->isQuad()) {
         $graph = $statement->getGraph();
     }
     /*
      * Build query
      */
     $query = 'SELECT ?s ?p ?o { ?s ?p ?o ';
     if ($graph !== null) {
         $query = 'SELECT ?s ?p ?o ?g { graph ?g { ?s ?p ?o } ';
     }
     // create shortcuts for S, P and O
     $subject = $statement->getSubject();
     $predicate = $statement->getPredicate();
     $object = $statement->getObject();
     // add filter, if subject is a named node or literal
     if (!$subject->isVariable()) {
         $query .= 'FILTER (?s = ' . $subject->toNQuads() . ') ';
     }
     // add filter, if predicate is a named node or literal
     if (!$predicate->isVariable()) {
         $query .= 'FILTER (?p = ' . $predicate->toNQuads() . ') ';
     }
     // add filter, if object is a named node or literal
     if (!$object->isVariable()) {
         $query .= 'FILTER (?o = ' . $object->toNQuads() . ') ';
     }
     // add filter, if graph is a named node or literal
     if ($graph !== null && !$graph->isVariable()) {
         $query .= 'FILTER (?g = ' . $graph->toNQuads() . ') ';
     }
     $query .= '}';
     // execute query and save result
     // TODO transform getMatchingStatements into lazy loading, so a batch loading is possible
     $result = $this->query($query, $options);
     /*
      * Transform SetResult entries to Statement instances.
      */
     $statementList = array();
     if ($graph !== null) {
         foreach ($result as $entry) {
             $statementList[] = $this->statementFactory->createStatement($entry['s'], $entry['p'], $entry['o'], $entry['g']);
         }
     } else {
         foreach ($result as $entry) {
             $statementList[] = $this->statementFactory->createStatement($entry['s'], $entry['p'], $entry['o']);
         }
     }
     // return a StatementIterator which contains the matching statements
     return $this->statementIteratorFactory->createIteratorFromArray($statementList);
 }
Example #2
0
 /**
  * Transforms a statement array given by EasyRdf to a Saft StatementIterator instance.
  *
  * @param  array             $rdfPhp
  * @return StatementIterator
  */
 protected function rdfPhpToStatementIterator(array $rdfPhp)
 {
     $statements = array();
     // go through all subjects
     foreach ($rdfPhp as $subject => $predicates) {
         // predicates associated with the subject
         foreach ($predicates as $property => $objects) {
             // object(s)
             foreach ($objects as $object) {
                 /**
                  * Create subject node
                  */
                 if (true === NodeUtils::simpleCheckURI($subject)) {
                     $s = $this->nodeFactory->createNamedNode($subject);
                 } else {
                     $s = $this->nodeFactory->createLiteral($subject);
                 }
                 /**
                  * Create predicate node
                  */
                 if (true === NodeUtils::simpleCheckURI($property)) {
                     $p = $this->nodeFactory->createNamedNode($property);
                 } else {
                     $p = $this->nodeFactory->createLiteral($property);
                 }
                 /*
                  * Create object node
                  */
                 // URI
                 if (NodeUtils::simpleCheckURI($object['value'])) {
                     $o = $this->nodeFactory->createNamedNode($object['value']);
                     // datatype set
                 } elseif (isset($object['datatype'])) {
                     $o = $this->nodeFactory->createLiteral($object['value'], $object['datatype']);
                     // lang set
                 } elseif (isset($object['lang'])) {
                     $o = $this->nodeFactory->createLiteral($object['value'], 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString', $object['lang']);
                 } else {
                     $o = $this->nodeFactory->createLiteral($object['value']);
                 }
                 // build and add statement
                 $statements[] = $this->statementFactory->createStatement($s, $p, $o);
             }
         }
     }
     return new ArrayStatementIteratorImpl($statements);
 }
Example #3
0
 /**
  * Removes all statements from a (default-) graph which match with given statement.
  *
  * @param  Statement $statement          It can be either a concrete or pattern-statement.
  * @param  Node      $graph     optional Overrides target graph. If set, all statements will
  *                                       be delete in that graph.
  * @param  array     $options   optional Key-value pairs which provide additional introductions
  *                                       for the store and/or its adapter(s).
  */
 public function deleteMatchingStatements(Statement $statement, Node $graph = null, array $options = array())
 {
     // given $graph forces usage of it and not the graph from the statement instance
     if (null !== $graph) {
         // use given $graph
         // use graphUri from statement
     } elseif (null === $graph && null !== $statement->getGraph()) {
         $graph = $statement->getGraph();
     }
     // create triple statement, because we have to handle the graph extra
     $tripleStatement = $this->statementFactory->createStatement($statement->getSubject(), $statement->getPredicate(), $statement->getObject());
     $statementIterator = $this->statementIteratorFactory->createIteratorFromArray(array($tripleStatement));
     $triple = $this->sparqlFormat($statementIterator);
     $query = 'DELETE ';
     if (null !== $graph) {
         $query .= 'FROM <' . $graph->getUri() . '> ';
     }
     $query .= '{' . $triple . '} WHERE {' . $triple . '}';
     $this->query($query);
 }
 /**
  * Create statements from query.
  *
  * @param  Query             $queryObject Query object which represents a SPARQL query.
  * @return StatementIterator StatementIterator object
  */
 protected function getStatements(Query $queryObject)
 {
     $queryParts = $queryObject->getQueryParts();
     $statementArray = array();
     // if only triples, but no quads
     if (true === isset($queryParts['triple_pattern']) && false === isset($queryParts['quad_pattern'])) {
         foreach ($queryParts['triple_pattern'] as $pattern) {
             /**
              * Create Node instances for S, P and O to build a Statement instance later on
              */
             $s = $this->createNodeByValueAndType($pattern['s'], $pattern['s_type']);
             $p = $this->createNodeByValueAndType($pattern['p'], $pattern['p_type']);
             $o = $this->createNodeByValueAndType($pattern['o'], $pattern['o_type']);
             $g = null;
             $statementArray[] = $this->statementFactory->createStatement($s, $p, $o, $g);
         }
         // if only quads, but not triples
     } elseif (false === isset($queryParts['triple_pattern']) && true === isset($queryParts['quad_pattern'])) {
         foreach ($queryParts['quad_pattern'] as $pattern) {
             /**
              * Create Node instances for S, P and O to build a Statement instance later on
              */
             $s = $this->createNodeByValueAndType($pattern['s'], $pattern['s_type']);
             $p = $this->createNodeByValueAndType($pattern['p'], $pattern['p_type']);
             $o = $this->createNodeByValueAndType($pattern['o'], $pattern['o_type']);
             $g = $this->createNodeByValueAndType($pattern['g'], $pattern['g_type']);
             $statementArray[] = $this->statementFactory->createStatement($s, $p, $o, $g);
         }
         // found quads and triples
     } elseif (true === isset($queryParts['triple_pattern']) && true === isset($queryParts['quad_pattern'])) {
         throw new \Exception('Query contains quads and triples. That is not supported yet.');
         // neither quads nor triples
     } else {
         throw new \Exception('Query contains neither quads nor triples.');
     }
     return $this->statementIteratorFactory->createIteratorFromArray($statementArray);
 }
 /**
  * It basically returns all stored statements.
  *
  * @param  Statement         $Statement          It can be either a concrete or pattern-statement.
  * @param  Node              $graph     optional Overrides target graph. If set, you will get all
  *                                               matching statements of that graph.
  * @param  array             $options   optional It contains key-value pairs and should provide additional
  *                                               introductions for the store and/or its adapter(s).
  * @return StatementIterator It contains Statement instances  of all matching statements of the given
  *                           graph.
  */
 public function getMatchingStatements(Statement $statement, Node $graph = null, array $options = array())
 {
     if (null !== $graph) {
         $graphUri = $graph->getUri();
         // no graph information given, use default graph
     } elseif (null === $graph && null === $statement->getGraph()) {
         $graphUri = 'http://saft/defaultGraph/';
         // no graph given, use graph information from $statement
     } elseif (null === $graph && $statement->getGraph()->isNamed()) {
         $graphUri = $statement->getGraph()->getUri();
         // no graph given, use graph information from $statement
     } elseif (null === $graph && false == $statement->getGraph()->isNamed()) {
         $graphUri = 'http://saft/defaultGraph/';
     }
     if (false == isset($this->statements[$graphUri])) {
         $this->statements[$graphUri] = array();
     }
     // if not default graph was requested
     if ('http://saft/defaultGraph/' != $graphUri) {
         return new ArrayStatementIteratorImpl($this->statements[$graphUri]);
         // if default graph was requested, return matching statements from all graphs
     } else {
         $_statements = array();
         foreach ($this->statements as $graphUri => $statements) {
             foreach ($statements as $statement) {
                 if ('http://saft/defaultGraph/' == $graphUri) {
                     $graph = null;
                 } else {
                     $graph = $this->nodeFactory->createNamedNode($graphUri);
                 }
                 $_statements[] = $this->statementFactory->createStatement($statement->getSubject(), $statement->getPredicate(), $statement->getObject(), $graph);
             }
         }
         return new ArrayStatementIteratorImpl($_statements);
     }
 }