Source for file InfModelB.php
Documentation is available at InfModelB.php
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------
* A InfModelB extends the InfModel Class, with a backward chaining algorithm.
* Only the loaded or added base-triples are stored.
* A find-query evaluates the inference rules and recursively tries to find the statements.
* InfModelB memorises "Dead-Ends" until the next add() command, thus
* makin a second find much faster.
* InfModelB is safe for loops in Ontologies, that would cause infinite loops.
* WARNING: A find(null,null,null) might take very long.
* @version $Id: fsource_infModel__infModelInfModelB.php.html 442 2007-06-01 16:19:26Z cax $
* @author Daniel Westphal <mail at d-westphal dot de>
* Array that holds combinations of inference rules with distinct
* find-querys, that don't lead to any inference.
* You can supply a base_uri
$this->findDeadEnds=
array();
* 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())
* @param object Statement $statement
//Reset the found dead-ends.
$this->findDeadEnds=
array();
* General method to search for triples.
* NULL input for any parameter will match anything.
* Example: $result = $m->find( NULL, NULL, $node );
* Finds all triples with $node as object.
* Returns an empty MemModel if nothing is found.
* To improve the search speed with big Models, call index(INDEX_TYPE)
* It recursively searches in the statements and rules to find
* @param object Node $subject
* @param object Node $predicate
* @param object Node $object
* @return object MemModel
function find($subject,$predicate,$object)
$searchStringIndex=
array();
//add all infered statements without duplicates to the result model
foreach ($this->_infFind($subject,$predicate,$object,array())as $statement)
$resultModel->addWithoutDuplicates($statement);
* This is the main inference method of the InfModelB
* The algorithm works as follows:
* Find all statements in the base model, that matches the current
* Check all rules, if they are able to deliver infered statements,
* that match the current find-query. Don't use rules with queries,
* that lead to dead-ends and don't use a rule-query-combination that
* was used before in this branch (ontology loops).
* If a rule is possible do deliver such statements, get a new
* find-query, that is possible to find those statements, that are able
* Call this _infFind method wirh the new find-query and entail the
* If this rule, wasn't able to return any statements with this distinct
* query, add this combination to the dead-ends.
* Return the statements from the base triples and those, which were infered.
* If $findOnlyFirstMatching is set to true, only the first match in
* the base-statements is entailed an returned (used in contains() and
* findFirstMatchingStatement() methods).
* You can set an offset to look for the first matching statement by setting the
* It recursively searches in the statements and rules to find matching
* @param object Node $subject
* @param object Node $predicate
* @param object Node $object
* @param array $searchStringIndex
* @param boolean $findOnlyFirstMatching
* @param integer $resultCount
* @return object array Statements
function _infFind ($subject,$predicate,$object, $searchStringIndex, $findOnlyFirstMatching =
false, $offset =
0,$resultCount =
0 )
//Find all matching statements in the base statements
$findResult=
parent::find($subject,$predicate,$object);
//For all found statements
foreach ($findResult->triples as $statement)
//Return, if only the firstMatchingStatement was wanted
if ($findOnlyFirstMatching &&
$resultCount >
$offset)
//Don't infer statements about the schema (rdfs:subClass, etc..)
if ($predicate ==
null ||
(is_a($predicate,'Node') &&
!in_array($predicate->getLabel(),$this->supportedInference))
//Check only Rules, that the EntailmentIndex returned.
foreach ($this->_findRuleEntailmentInIndex($subject,$predicate,$object) as $ruleKey)
$infRule=
$this->infRules[$ruleKey];
//If it is to ontology loop and no dead-end
if (!in_array($serializedRuleStatement, $searchStringIndex) &&
!in_array($serializedRuleStatement, $this->findDeadEnds))
//Keep this distinct rule query cobination for
//this branch to detect loops
$searchStringIndex[]=
$serializedRuleStatement;
//If the rule is able to deliver statements that match
if ($infRule->checkEntailment($subject,$predicate,$object))
//Get a modified find-query, that matches statements,
$modefiedFind=
$infRule->getModifiedFind($subject,$predicate,$object);
//Call this method with the new find-query
$infFindResult=
$this->_infFind($modefiedFind['s'],
//If it deliverd statements that matches the trigger
if (isset
($infFindResult[0]))
foreach ($infFindResult as $statement)
//Entail the statements and check, if they are not about the
$newStatement=
$infRule->entail($statement);
if (!in_array($newStatement->getLabelPredicate(),$this->supportedInference))
//Check if, the entailed statements are, what we are looking for
if($this->_nodeEqualsFind($subject,$newStatement->getSubject()) &&
$this->_nodeEqualsFind($predicate,$newStatement->getPredicate()) &&
$this->_nodeEqualsFind($object,$newStatement->getObject() ) )
if ($findOnlyFirstMatching &&
$resultCount >
$offset)
//If there were no results of the rule-query-combination,
//mark this combination as a dead-end.
$this->findDeadEnds[]=
$serializedRuleStatement;
//Return the array of the found statements
* Tests if the Model contains the given triple.
* TRUE if the triple belongs to the Model;
* @param object Statement &$statement
//throws an error, if $statement is not of class Statement
if(!is_a($statement,'Statement'))
$statement has to be object of class Statement', E_USER_ERROR);
//Call the _infFind method, but let it stop, if it finds the first match.
if (count( $this->_infFind($statement->getSubject(),
$statement->getPredicate(),
* 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 of the MemModel where the object equals $node.
* Returns an NULL if nothing is found.
* You can define an offset to search for. Default = 0
* @param object Node $subject
* @param object Node $predicate
* @param object Node $object
* @return object Statement
//Call the _infFind method, but let it stop, if it finds the
$res=
$this->_infFind($subject,$predicate,$object,array(),true,$offset);
if (isset
($res[$offset]))
* Returns a StatementIterator for traversing the Model.
* @return object StatementIterator
// Import Package Utility
// Gets a MemModel by executing a find(null,null,null) to get a
// WARNING: might be slow
* Number of all inferable triples in the Model.
* WARNING: uses a find(null,null,null) to find all statements! (might take a while)
// Gets a MemModel by executing a find(null,null,null) to get a
// WARNING: might be slow
* Create a MemModel containing all the triples (including inferred
* statements) of the current InfModelB.
* @return object MemModel
$return=
$this->find(null,null,null);
$return->setBaseURI($this->baseURI);
* Create a MemModel containing only the base triples (without inferred
* statements) of the current InfModelB.
* @return object MemModel
$return->setBaseURI($this->baseURI);
foreach ($this->triples as $statement)
$return->add($statement);
* Short Dump of the InfModelB.
return 'InfModelB[baseURI=' .
$this->getBaseURI() .
'; size=' .
$this->size(true) .
']';
* Dumps of the InfModelB including ALL inferable triples.
while($statement=
$stateIt->next())
$dump .=
$statement->toString() .
chr(13);
* Saves the RDF,N3 or N-Triple serialization of the full InfModelB
* (including inferred triples) 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
* Returns FALSE if the InfModelB couldn't be saved to the file.
* @param string $filename
function saveAs($filename, $type =
'rdf')
return $memmodel->saveAs($filename, $type);
* Writes the RDF serialization of the Model including ALL inferable
* Writes the RDF serialization of the Model including ALL inferable
// Import Package Utility
* Writes the RDF serialization of the Model including ALL inferable
* Removes the triple from the MemModel.
* TRUE if the triple is removed.
* Checks, if it touches any statements, that added inference rules
* @param object Statement $statement
if (parent::contains($statement))
if (in_array($statement->getLabelPredicate(),$this->supportedInference));
while (count($this->_removeFromInference($statement))>
0);
$this->findDeadEnds=
array();
return parent::remove($statement);
* Checks, if a single node matches a single find pattern.
* TRUE if the node matches.
* Checks, if it touches any statements, that added inference rules
* @param object Statement $statement
function _nodeEqualsFind(& $find, $node)
//If the find pattern is a node, use the nodes equal-method and
return $node->equals($find);
//Null-pattern matches anything.
* Returns a FindIterator for traversing the MemModel.
* @return object FindIterator
$errmsg =
RDFAPI_ERROR .
'(class: InfModelB; method: findAsIterator):
This function is disabled in the
Documentation generated on Fri, 1 Jun 2007 16:49:27 +0200 by phpDocumentor 1.3.2