Source for file ResModel.php
Documentation is available at ResModel.php
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------
* A ResModel provides an resource centric view on an underlying RDF model.
* ResModels show information not as statements but as resources with
* properties, similar to Jena models. ResModels may create Resources [URI
* nodes and bnodes]. Creating a Resource does not make the Resource visible to
* the model; Resources are only "in" Models if Statements about them are added
* to the Model. Similarly the only way to "remove" a Resource from a Model is
* to remove all the Statements that mention it.
* When a Resource or Literal is created by a Model, the Model is free to re-use an existing
* Resource or Literal object with the correct values, or it may create a fresh one.
* @version $Id: fsource_resModel__resModelResModel.php.html 443 2007-06-01 16:25:38Z cax $
* @author Daniel Westphal <mail at d-westphal dot de>
* Holds a reference to the assoiated memmodel/dbmodel/infmodel
* You have to supply a memmodel/dbmodel/infmodel to save the statements.
* @param object model $model
if (!is_a($model,'Model'))
$model has to be object of class Model', E_USER_ERROR);
* Create a new resource associated with this model.
* If the uri string isn't set, this creates a bnode.
* Otherwise it creates a URI node.
* A URI resource is .equals() to any other URI Resource with the same URI
* (even in a different model - be warned).
* This method may return an existing Resource with the correct URI and model,
* or it may construct a fresh one, as it sees fit.
* Operations on the result Resource may change this model.
* @return object ResResource
//associate the resource with this model, and get a unique identifier
$resResource->setAssociatedModel($this);
* Create a new Property associated with this model.
* This method may return an existing property with the correct URI and model,
* or it may construct a fresh one, as it sees fit.
* Subsequent operations on the returned property may modify this model.
* @return object ResProperty
$resProperty->setAssociatedModel($this);
* Create an untyped literal from a String value with a specified language.
* If you want to type this literal, you have to set a datatype before
* adding it to the model.
* @param string $languageTag
* @return object ResLiteral
$resLiteral =
new ResLiteral($label,$languageTag);
$resLiteral->setAssociatedModel($this);
* General method to search for triples.
* NULL input for any parameter will match anything.
* Example: $result = $m->find( NULL, NULL, $node );
* Finds all Statements with $node as object.
* Returns an array of statements with ResResources.
* @param object ResResource $subject
* @param object ResResource $predicate
* @param object ResResource $object
function find($subject,$predicate, $object)
//convert ResResources to Resources and Blanknodes
$resmodel=
$this->model->find( $this->_resNode2Node($subject),
$this->_resNode2Node($predicate),
$this->_resNode2Node($object)
//convert Resources, Blanknodes to ResResources
foreach ($resmodel->triples as $statement)
$result[]=
new Statement($this->_node2ResNode($statement->getSubject()),
$this->_node2ResNode($statement->getPredicate(),true),
$this->_node2ResNode($statement->getObject())
* Searches for triples and returns the first matching statement.
* NULL input for any parameter will match anything.
* Example: $result = $m->findFirstMatchingStatement( NULL, NULL, $node );
* Returns the first statement with ResResources of the Model where the object equals $node.
* Returns an NULL if nothing is found.
* You can define an offset to search.
* @param object Node $subject
* @param object Node $predicate
* @param object Node $object
* @return object Statement
$statement =
$this->model->findFirstMatchingStatement( $this->_resNode2Node($subject),
$this->_resNode2Node($predicate),
$this->_resNode2Node($object),
return new Statement( $this->_node2ResNode($statement->getSubject()),
$this->_node2ResNode($statement->getPredicate(),true),
$this->_node2ResNode($statement->getObject())
* Adds a new triple to the Model without checking if the statement is already in the Model.
* So if you want a duplicate free Model use the addWithoutDuplicates() function (which is slower then add())
* Expects a statements with ResResources(ResLiterals)
* @param object Statement $statement
return $this->model->add(new Statement( $this->_resNode2Node($statement->getSubject()),
$this->_resNode2Node($statement->getPredicate()),
$this->_resNode2Node($statement->getObject()))
* Checks if a new statement is already in the Model and adds the statement, if it is not in the Model.
* addWithoutDuplicates() is significantly slower then add().
* Retruns TRUE if the statement is added.
* Expects a statements with ResResources(ResLiterals)
* @param object Statement $statement
return $this->model->addWithoutDuplicates(new Statement($this->_resNode2Node($statement->getSubject()),
$this->_resNode2Node($statement->getPredicate()),
$this->_resNode2Node($statement->getObject()))
* Tests if the Model contains the given statement.
* TRUE if the statement belongs to the model;
* Expects a statement of ResResources(ResLiterals)
* @param object Statement $statement
return $this->model->contains(new Statement($this->_resNode2Node($statement->getSubject()),
$this->_resNode2Node($statement->getPredicate()),
$this->_resNode2Node($statement->getObject()))
* Determine if all of the statements in a model are also contained in this model.
* True if all of the statements in $model are also contained in this model and false otherwise.
* @param object Model &$model
if (is_a($model,'ResModel'))
return $this->model->containsAll($model->getModel());
return $this->model->containsAll($model);
* Determine if any of the statements in a model are also contained in this model.
* True if any of the statements in $model are also contained in this model and false otherwise.
* @param object Model &$model
if (is_a($model,'ResModel'))
return $this->model->containsAny($model->getModel());
return $this->model->containsAny($model);
* Determine if the node (ResResource / ResLiteral) $node appears in any statement of this model.
* @param object Node &$node
* Create a literal from a String value with the $dtype Datatype
* An existing literal of the right value may be returned, or a fresh one created.
* @return object ResLiteral
$resLiteral->setDatatype($dtype);
$resLiteral->setAssociatedModel($this);
* Checks if two models are equal.
* Two models are equal if and only if the two RDF graphs they represent are isomorphic.
* Warning: This method doesn't work correct with models where the same blank node has different
* identifiers in the two models. We will correct this in a future version.
* @param object model &$that
if (is_a($that,'ResModel'))
return $this->model->equals($that->getModel());
return $this->model->equals($that);
* Returns a new model that is the subtraction of another model from this model.
* @param object Model $model
* @return object MemModel
if (is_a($model,'ResModel'))
return $this->model->subtract($model->getModel());
return $this->model->subtract($model);
* Answer a statement find(s, p, null) with ResResources(ResLiterals) from this model.
* If none exist, return null; if several exist, pick one arbitrarily.
* @param object ResResource $subject
* @param object ResResource $property
* @return object Statement
$statement=
$this->model->findFirstMatchingStatement( $this->_resNode2Node($subject),
$this->_resNode2Node($property),
return new Statement($this->_node2ResNode($statement->getSubject()),
$this->_node2ResNode($statement->getPredicate(),true),
$this->_node2ResNode($statement->getObject())
* Checks if MemModel is empty
return $this->model->isEmpty();
* Returns a ResIterator with all objects in a model.
* @return object ResIterator
* Returns a ResIterator with all objects with a given property and property value.
* @param object ResResource $property
* @param object ResResource $value
* @return object ResIterator
return new ResIterator(null,$property,$value,'o',$this);
* Returns a ResIterator with all subjects in a model.
* @return object ResIterator
* Returns a ResIterator with all subjects with a given property and property value.
* @param object ResResource $property
* @param object ResResource $value
* @return object ResIterator
return new ResIterator(null,$property,$value,'s',$this);
* Removes the statement of ResResources(ResTriples) from the MemModel.
* TRUE if the statement is removed.
* @param object Statement $statement
return $this->model->remove(new Statement( $this->_resNode2Node($statement->getSubject()),
$this->_resNode2Node($statement->getPredicate()),
$this->_resNode2Node($statement->getObject())
* Number of statements in the MemModel
return $this->model->size();
* Returns a new Model that is the set-union of the model with another model.
* Duplicate statements are removed. If you want to allow duplicates, use addModel() which is much faster.
* The result of taking the set-union of two or more RDF graphs (i.e. sets of triples)
* is another graph, which we will call the merge of the graphs.
* Each of the original graphs is a subgraph of the merged graph. Notice that when forming
* a merged graph, two occurrences of a given uriref or literal as nodes in two different
* graphs become a single node in the union graph (since by definition they are the same
* uriref or literal) but blank nodes are not 'merged' in this way; and arcs are of course
* never merged. In particular, this means that every blank node in a merged graph can be
* identified as coming from one particular graph in the original set of graphs.
* Notice that one does not, in general, obtain the merge of a set of graphs by concatenating
* their corresponding N-triples documents and constructing the graph described by the merged
* document, since if some of the documents use the same node identifiers, the merged document
* will describe a graph in which some of the blank nodes have been 'accidentally' merged.
* To merge Ntriples documents it is necessary to check if the same nodeID is used in two or
* more documents, and to replace it with a distinct nodeID in each of them, before merging the
* documents. (Not implemented yet !!!!!!!!!!!)
* @param object Model $model
* @return object MemModel
function & unite(& $model)
if (is_a($model,'ResModel'))
return $this->model->unite($model->getModel());
return $this->model->unite($model);
* Adds another model to this MemModel.
* Duplicate statements are not removed.
* If you don't want duplicates, use unite().
* If any statement of the model to be added to this model contains a blankNode
* with an identifier already existing in this model, a new blankNode is generated.
* @param object Model $model
if (is_a($model,'ResModel'))
return $this->model->addModel($model->getModel());
return $this->model->addModel($model);
* Create a new RDF Container from type rdf:Alt
* This method may return an existing container with the correct URI and model,
* or it may construct a fresh one, as it sees fit.
* Subsequent operations on the returned Container may modify this model.
* @return object ResProperty
$resAlt->setAssociatedModel($this);
* Create a new RDF Container from type rdf:Bag
* This method may return an existing container with the correct URI and model,
* or it may construct a fresh one, as it sees fit.
* Subsequent operations on the returned Container may modify this model.
* @return object ResProperty
$resBag->setAssociatedModel($this);
* Create a new RDF Container from type rdf:Seq
* This method may return an existing container with the correct URI and model,
* or it may construct a fresh one, as it sees fit.
* Subsequent operations on the returned Container may modify this model.
* @return object ResProperty
$resSeq->setAssociatedModel($this);
* Create a new RDF Collection from type rdf:List
* This method may return an existing container with the correct URI and model,
* or it may construct a fresh one, as it sees fit.
* Subsequent operations on the returned Container may modify this model.
* @return object ResProperty
$resList->setAssociatedModel($this);
* Returns a reference to the underlying model (Mem/DB/InfModel) that contains the statements
* Internal method, that returns a resource URI that is unique for the Model.
* URIs are generated using the base_uri of the Model, the prefix and a unique number.
* If no prefix is defined, the bNode prefix, defined in constants.php, is used.
function getUniqueResourceURI($bnodePrefix)
return $this->model->getUniqueResourceURI($bnodePrefix);
* Load a model from a file containing RDF, N3 or N-Triples.
* This function recognizes the suffix of the filename (.n3 or .rdf) and
* calls a suitable parser, if no $type is given as string ("rdf" "n3" "nt");
* If the model is not empty, the contents of the file is added to this DbModel.
* @param string $filename
function load($filename, $type =
NULL, $stream=
false)
$this->model->load($filename, $type =
NULL, $stream=
false);
* Return current baseURI.
return $this->model->getBaseURI();
* Saves the RDF,N3 or N-Triple serialization of the MemModel to a file.
* You can decide to which format the model should be serialized by using a
* corresponding suffix-string as $type parameter. If no $type parameter
* is placed this method will serialize the model to XML/RDF format.
* Returns FALSE if the MemModel couldn't be saved to the file.
* @param string $filename
function saveAs($filename, $type =
'rdf')
return $this->model->saveAs($filename, $type =
'rdf');
* Writes the RDF serialization of the MemModel as HTML table.
$this->model->writeAsHtmlTable();
* Returns a new model containing all the statements which are in both this model and another.
* @param object Model $model
* @return object MemModel
if (is_a($model,'ResModel'))
return $this->model->intersect($model->getModel());
return $this->model->intersect($model);
* converts a Resource,Blanknode,Literal into a ResResource, ResProperty, or ResLiteral
* @param object Node $node
* @param boolean $isProperty
* @return object ResResource / ResProperty / ResLiteral
function _node2ResNode($node, $isProperty =
false)
if (is_a($node,'Literal'))
$return=
new ResLiteral($node->getLabel(),$node->getLanguage());
$return->setDatatype($node->getDatatype());
$return->setAssociatedModel($this);
if (is_a($node,'Resource'))
$res->setAssociatedModel($this);
if (is_a($node,'Blanknode'))
* converts a ResResource, ResProperty, or ResLiteral into a Resource, Blanknode, or Literal
* @param object ResNode $resNode
function _resNode2Node($resNode)
if (is_a($resNode,'ResResource'))
if ($resNode->getIsAnon())
$return=
new Resource($resNode->getURI());
if (is_a($resNode,'ResLiteral'))
$literal=
new Literal($resNode->getLabel(),$resNode->getLanguage());
if ($resNode->getDatatype() !=
null)
$literal->setDatatype($resNode->getDatatype());
* Set a base URI for the MemModel.
* Affects creating of new resources and serialization syntax.
* If the URI doesn't end with # : or /, then a # is added to the URI.
$this->model->setBaseURI($uri);
* Writes the RDF serialization of the MemModel as HTML table.
return $this->model->writeRdfToString();
* Perform an RDQL query on this MemModel.
* This method returns an associative array of variable bindings.
* The values of the query variables can either be RAP's objects (instances of Node)
* if $returnNodes set to TRUE, or their string serialization.
* @param string $queryString
* @param boolean $returnNodes
* @return array [][?VARNAME] = object Node (if $returnNodes = TRUE)
* OR array [][?VARNAME] = string
function & rdqlQuery($queryString, $returnNodes =
TRUE)
return $this->model->rdqlQuery($queryString, $returnNodes);
* Perform an RDQL query on this MemModel.
* This method returns an RdqlResultIterator of variable bindings.
* The values of the query variables can either be RAP's objects (instances of Node)
* if $returnNodes set to TRUE, or their string serialization.
* @param string $queryString
* @param boolean $returnNodes
* @return object RdqlResultIterator = with values as object Node (if $returnNodes = TRUE)
* OR object RdqlResultIterator = with values as strings if (if $returnNodes = FALSE)
return $this->model->rdqlQueryAsIterator($queryString, $returnNodes);
* Returns the models namespaces.
* @author Tobias Gauß <tobias.gauss@web.de>
return $this->model->getParsedNamespaces();
* Adds the namespaces to the model. This method is called by
* the parser. !!!! addParsedNamespaces() not overwrites manual
* added namespaces in the model !!!!
* @author Tobias Gauß <tobias.gauss@web.de>
$this->model->addParsedNamespaces($newNs);
* Adds a namespace and prefix to the model.
* @author Tobias Gauß <tobias.gauss@web.de>
* @param String $prefix, String $nmsp
$this->model->addNamespace($prefix, $namespace);
* removes a single namespace from the model
* @author Tobias Gauß <tobias.gauss@web.de>
return $this->model->removeNamespace($nmsp);
Documentation generated on Fri, 1 Jun 2007 16:51:35 +0200 by phpDocumentor 1.3.2