Exemplo n.º 1
0
 /**
  * Find a statement in the model.
  *
  * A NULL argument for any of source, predicate or target is treated as
  * a wildcard.  If a context is given, only statements from that context
  * will be returned.  The result is an object that be used in foreach
  * iteration.  The returned iterator cannot be rewound.
  *
  * The search arguments can be either a (source, predicate target) triple
  * of LibRDF_Node objects or a LibRDF_Statement object.  Valid argument 
  * lists are (source, predicate, target, [context]) or
  * (statement, [context]).
  *
  * For more complex queries, see {@link LibRDF_Query}.
  *
  * @param   mixed       $statement  The statement to match or a source node
  * @param   LibRDF_Node $predicate  The predicate to match
  * @param   LibRDF_Node $target     The target to match
  * @param   LibRDF_URINode  $context    The context in which to search
  * @return  LibRDF_StreamIterator   An iterator over the matched statements
  * @access  public
  */
 public function findStatements()
 {
     $num_args = func_num_args();
     if ($num_args == 1 or $num_args == 2) {
         $statement = func_get_arg(0);
         if (!$statement instanceof LibRDF_Statement) {
             throw new LibRDF_Error("First argument must be a LibRDF_Statement");
         }
         if ($num_args == 2) {
             $context = func_get_arg(1);
             if (!$context instanceof LibRDF_URINode) {
                 throw new LibRDF_Error("Context must be LibRDF_URINode");
             }
         } else {
             $context = NULL;
         }
         $statement = $statement->getStatement();
     } elseif ($num_args == 3 or $num_args == 4) {
         $source = func_get_arg(0);
         $predicate = func_get_arg(1);
         $target = func_get_arg(2);
         if ($source !== NULL) {
             if (!$source instanceof LibRDF_Node) {
                 throw new LibRDF_Error("Argument 1 must be of type LibRDF_Node");
             } else {
                 $source = librdf_new_node_from_node($source->getNode());
             }
         }
         if ($predicate !== NULL) {
             if (!$predicate instanceof LibRDF_Node) {
                 throw new LibRDF_Error("Argument 2 must be of type LibRDF_Node");
             } else {
                 $predicate = librdf_new_node_from_node($predicate->getNode());
             }
         }
         if ($target !== NULL) {
             if (!$target instanceof LibRDF_Node) {
                 throw new LibRDF_Error("Argument 3 must be of type LibRDF_Node");
             } else {
                 $target = librdf_new_node_from_node($target->getNode());
             }
         }
         if ($num_args == 4) {
             $context = func_get_arg(3);
             if (!$context instanceof LibRDF_URINode) {
                 throw new LibRDF_Error("Context must be LibRDF_URINode");
             }
         } else {
             $context = NULL;
         }
         $statement = librdf_new_statement_from_nodes(librdf_php_get_world(), $source, $predicate, $target);
     } else {
         throw new LibRDF_Error("findStatements takes 2-4 arguments");
     }
     if ($context !== NULL) {
         $stream_resource = librdf_model_find_statements_in_context($this->model, $statement, $context->getNode());
     } else {
         $stream_resource = librdf_model_find_statements($this->model, $statement);
     }
     if ($num_args > 2) {
         librdf_free_statement($statement);
     }
     if (!$stream_resource) {
         throw new LibRDF_Error("Unable to create new statement iterator");
     }
     return new LibRDF_StreamIterator($stream_resource, $this);
 }
Exemplo n.º 2
0
 function remove_triple($triple)
 {
     if (!isset($triple['type']) || $triple['type'] != 'triple') {
         return 0;
     }
     $r = 0;
     $s = $triple['s'];
     $p = $triple['p'];
     $o = $triple['o'];
     if (!is_null($s)) {
         $s = $this->_uriNode($s);
     }
     if (!is_null($p)) {
         $p = $this->_uriNode($p);
     }
     if (!is_null($o)) {
         if ($triple['o_type'] == 'uri') {
             $o = $this->_uriNode($o);
         } elseif ($triple['o_type'] == 'literal') {
             $o = $this->_literalNode($o);
         }
     }
     $pattern = librdf_new_statement_from_nodes($this->_world, $s, $p, $o);
     $stream = librdf_model_find_statements($this->_model, $pattern);
     while (!librdf_stream_end($stream)) {
         $elt = librdf_stream_get_object($stream);
         $r += librdf_model_remove_statement($this->_model, $elt) ? 0 : 1;
         librdf_stream_next($stream);
     }
     librdf_free_stream($stream);
     //librdf_free_statement($pattern);
     //$s && librdf_free_node($s);
     //$p && librdf_free_node($p);
     return $r;
 }