Source for file InfModelF.php
Documentation is available at InfModelF.php
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------
* A InfModelF extends the InfModel Class, with a forward chaining algorithm.
* If a new statement is added, it is enferd at
* once and all the entailed statements are added too.
* When adding or removing a statement, that produced a new inference rule,
* all entailed statements are discarded and the whole base model is infered
* The InfModelF is safe for loops in Ontologies, that would cause infinite loops.
* @version $Id: fsource_infModel__infModelInfModelF.php.html 443 2007-06-01 16:25:38Z cax $
* @author Daniel Westphal <mail at d-westphal dot de>
* Array that holds the position of the infered statements in the model.
* Variable that influences the habbit when adding statements.
* Used by the loadModel method to increase performance.
* You can supply a base_uri.
$this->inferenceEnabled=
true;
* Adds a new triple to the MemModel without checking if the statement
* is already in the MemModel.
* So if you want a duplicate free MemModel use the addWithoutDuplicates()
* function (which is slower then add())
* The statement is infered and all entailed statements are added.
* @param object Statement $statement
function add ($statement)
if ($this->inferenceEnabled)
//a addWithoutDublicates construct
//save the position of the infered statements
$this->infPos[]=
key($this->triples);
//apply the complete inference to the model, if the added statement was able to add a rule
if (in_array($statement->getLabelPredicate(),$this->supportedInference))
* Checks if a new statement is already in the MemModel and adds
* the statement, if it is not in the MemModel.
* addWithoutDuplicates() is significantly slower then add().
* Retruns TRUE if the statement is added.
* The statement is infered and all entailed statements are added.
* @param object Statement $statement
if ($this->inferenceEnabled)
//save the position of the infered statements
$this->infPos[]=
key($this->triples);
if (in_array($statement->getLabelPredicate(),$this->supportedInference))
* Entails every statement and adds the entailments if not already
function applyInference()
//check every statement in the model
foreach ($this->triples as $statement)
//gat all statements, that it recursively entails
//add the InfStatement position to the index
$this->infPos[]=
key($this->triples);
* Entails a statement by recursively using the _entailStatementRec
* @param object Statement $statement
* @return array of statements
$infStatementsIndex=
array();
return $this->_entailStatementRec($statement,$infStatementsIndex);
* Recursive method, that checks the statement with the trigger of
* every rule. If the trigger matches and entails new statements,
* those statements are recursively infered too.
* The $infStatementsIndex array holds lready infered statements
* to prevent infinite loops.
* @param object Statement $statement
* @param array $infStatementsIndex
* @return array of statements
function _entailStatementRec ( $statement,& $infStatementsIndex)
$infStatements =
array();
//dont entail statements about the supported inference-schema
if (!in_array($statement->getLabelPredicate(),$this->supportedInference))
//check only the rules, that were returned by the index
foreach ($this->_findRuleTriggerInIndex($statement) as $key )
$infRule=
$this->infRules[$key];
//If the statement wasn't infered before
if (!in_array($stateString,$infStatementsIndex))
$infStatementsIndex[]=
$stateString;
//Check, if the Statements triggers this rule
if($infRule->checkTrigger($statement))
$infStatement=
$infRule->entail($statement);
#if(!$this->contains($infStatement))
$this->_entailStatementRec($infStatement,
* Removes all infered statements from the model but keeps the
$indexTmp=
$this->indexed;
foreach ($this->infPos as $key)
unset
($this->triples[$key]);
* 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
* If the model is not empty, the contents of the file is added to
* While loading the model, the inference entailing is disabled, but
* new inference rules are added to increase performance.
* @param string $filename
function load($filename, $type =
NULL)
//Disable entailing to increase performance
$this->inferenceEnabled=
false;
parent::load($filename, $type);
$this->inferenceEnabled=
true;
* Short Dump of the InfModelF.
return 'InfModelF[baseURI=' .
$this->getBaseURI() .
';
size=' .
$this->size(true) .
']';
* Create a MemModel containing all the triples (including inferred
* statements) of the current InfModelF.
* @return object MemModel
$return->setBaseURI($this->baseURI);
foreach ($this->triples as $statement)
$return->add($statement);
* Create a MemModel containing only the base triples
* (without inferred statements) of the current InfModelF.
* @return object MemModel
$return->setBaseURI($this->baseURI);
foreach ($this->triples as $key =>
$statement)
$return->add($statement);
* 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 the statement is in the model
$inferenceRulesWereTouched=
false;
//If the statement was able to add inference rules
if (in_array($statement->getLabelPredicate(),$this->supportedInference))
$statementPositions=
$this->_removeFromInference($statement);
$inferenceRulesWereTouched=
true;
//get the position of all matching statements
$statementPositions=
array();
//find the positions of the statements
$this->findFirstMatchOff($statement->getSubject(),
$statement->getPredicate(),
if ($statementPosition!=-
1)
$statementPositions[]=
$statementPosition;
} while ($statementPosition != -
1);
//remove matching statements
parent::remove($statement);
foreach ($statementPositions as $statementPosition)
//if the statement was infered, remove it from the index of the infered statements.
if (in_array($statementPosition,$this->infPos))
unset
($this->infPos[$statementPosition]);
if ($inferenceRulesWereTouched)
//remove the statement and re-entail the 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
//Disable entailing to increase performance
$this->inferenceEnabled=
false;
parent::addModel($model);
$this->inferenceEnabled=
true;
Documentation generated on Fri, 1 Jun 2007 16:49:31 +0200 by phpDocumentor 1.3.2