Source for file InfRule.php

Documentation is available at InfRule.php

  1. <?php
  2. // ----------------------------------------------------------------------------------
  3. // Class: InfRule
  4. // ----------------------------------------------------------------------------------
  5.  
  6. /**
  7. * This class represents a single rule in a RDFS inference model.
  8. * It primary constists of a trigger and an entailment.
  9. * In the forward-chaining mode (RDFSFModel) a statement is checked,
  10. * if it satisfies the trigger. If it does, a new statement is returned.
  11. * In the backward-chaining mode (RDFSBModel) a find-query is checked
  12. * with the entailment. If this entailment could satify the find-query,
  13. * a new find-query is returned, that searches for statements that
  14. * satisfy the trigger of this rule.
  15. *
  16. * <BR><BR>History:<UL>
  17. * <LI>09-10-2004 : First version of this class.</LI>
  18. *
  19. * @version V0.9.3
  20. * @author Daniel Westphal <mail at d-westphal dot de>
  21.  
  22. *
  23. * @package infModel
  24. * @access public
  25. ***/
  26. class InfRule
  27. {
  28. /**
  29. * Array, that hold the trigger subject in key [''s''], the trigger
  30. * predicate in [''p''], and the trigger object in [''o''].
  31. * The array values can be NULL to match anything or be a node that
  32. * has to be matched.
  33. * @var array
  34. * @access private
  35. */
  36. var $trigger;
  37.  
  38. /**
  39. * Array, that hold the entailment subject in key [''s''], the
  40. * entailment predicate in [''p''], and the entailment object in [''o''].
  41. * The array values can be a node that will be inserted in the
  42. * returning statement, or ''<s>'' to insert the subject,''<p>'' to insert
  43. * the predicate, or ''<o>'' to insert the object of the checked statement
  44. * to this position in the new returned statement.
  45. * @var array
  46. * @access private
  47. */
  48. var $entailment;
  49. /**
  50. * Constructor
  51. *
  52. *
  53. * @access public
  54. */
  55. function infRule()
  56. {
  57. //initialising vars
  58. $this->trigger=array();
  59. $this->entailment=array();
  60. }
  61. /**
  62. * Sets the trigger of this rule
  63. * The values can be NULL to match anything or be a node that has to
  64. * be matched.
  65. * @param object Node OR NULL $subject
  66. * @param object Node OR NULL $predicate
  67. * @param object Node OR NULL $object
  68. * @access public
  69. * @throws PhpError
  70. */
  71. function setTrigger ($subject, $predicate, $object)
  72. {
  73. //throw an error if subject, predicate, or object are neither
  74. //node, nor null.
  75. if(!is_a($subject,''Node'') && $subject != null)
  76. trigger_error(RDFAPI_ERROR . ''(class: Infrule; method:
  77. setTrigger): $subject has to be null or of class Node''
  78. , E_USER_ERROR);
  79. if(!is_a($predicate,''Node'') && $predicate != null)
  80. trigger_error(RDFAPI_ERROR . ''(class: Infrule; method:
  81. setTrigger): $predicate has to be null or of class Node''
  82. , E_USER_ERROR);
  83. if(!is_a($object,''Node'') && $object != null)
  84. trigger_error(RDFAPI_ERROR . ''(class: Infrule; method:
  85. setTrigger): $object has to be null or of class Node''
  86. , E_USER_ERROR);
  87. //set the trigger
  88. $this->trigger[''s'']=$subject;
  89. $this->trigger[''p'']=$predicate;
  90. $this->trigger[''o'']=$object;
  91. }
  92.  
  93. /**
  94. * Sets the entailment of this rule
  95. * The values can be NULL to match anything or be a node that has to
  96. * be matched.
  97. * @param object Node OR NULL $subject
  98. * @param object Node OR NULL $predicate
  99. * @param object Node OR NULL $object
  100. * @access public
  101. * @throws PhpError
  102. */
  103. function setEntailment($subject,$predicate,$object)
  104. {
  105. //throw an error if subject, predicate, or object are neither node,
  106. //nor <s>,<p>,<o>.
  107. if(!is_a($subject,''Node'') && !ereg(''<[spo]>'', $subject))
  108. trigger_error(RDFAPI_ERROR . ''(class: Infrule; method:
  109. setEntailment): $subject has to be <s>,<p>,or <o> or of class Node''
  110. , E_USER_ERROR);
  111. if(!is_a($predicate,''Node'') && !ereg(''<[spo]>'', $predicate))
  112. trigger_error(RDFAPI_ERROR . ''(class: Infrule; method:
  113. setEntailment): $predicate has to be <s>,<p>,or <o> or of class Node''
  114. , E_USER_ERROR);
  115. if(!is_a($object,''Node'') && !ereg(''<[spo]>'', $object))
  116. trigger_error(RDFAPI_ERROR . ''(class: Infrule; method:
  117. setEntailment): $object has to be <s>,<p>,or <o> or of class Node''
  118. , E_USER_ERROR);
  119. $this->entailment[''s'']=$subject;
  120. $this->entailment[''p'']=$predicate;
  121. $this->entailment[''o'']=$object;
  122. }
  123. /**
  124. * Checks, if the statement satisfies the trigger
  125. * @param object Statement
  126. * @return boolean
  127. * @access public
  128. * @throws PhpError
  129. */
  130. function checkTrigger(& $statement)
  131. {
  132. //is true, if the trigger is null to match anything
  133. //or equals the statement''s subject
  134. $shouldFireS = $this->trigger[''s''] == null ||
  135. $this->trigger[''s'']->equals($statement->getSubject());
  136. //is true, if the trigger is null to match anything
  137. //or equals the statement''s predicate
  138. $shouldFireP = $this->trigger[''p''] == null ||
  139. $this->trigger[''p'']->equals($statement->getPredicate());
  140.  
  141. //is true, if the trigger is null to match anything
  142. //or equals the statement''s object
  143. $shouldFireO = $this->trigger[''o''] == null ||
  144. $this->trigger[''o'']->equals($statement->getObject());
  145.  
  146. //returns true, if ALL are true
  147. return $shouldFireS && $shouldFireP && $shouldFireO;
  148. }
  149. /**
  150. * Checks, if this rule could entail a statement that matches
  151. * a find of $subject,$predicate,$object
  152. * @param object Statement
  153. * @return boolean
  154. * @access public
  155. * @throws PhpError
  156. */
  157. function checkEntailment ($subject, $predicate, $object)
  158. {
  159. //true, if $subject is null, the entailment''s subject matches
  160. //anything, or the $subject equals the entailment-subject.
  161. $matchesS= $subject == null ||
  162. !is_a($this->entailment[''s''],''Node'') ||
  163. $this->entailment[''s'']->equals($subject);
  164.  
  165. //true, if $predicate is null, the entailment''s predicate matches
  166. //anything, or the $predicate equals the entailment-predicate.
  167. $matchesP= $predicate == null ||
  168. !is_a($this->entailment[''p''],''Node'') ||
  169. $this->entailment[''p'']->equals($predicate);
  170.  
  171. //true, if $object is null, the entailment''s object matches
  172. //anything, or the $object equals the entailment-object.
  173. $matchesO= $object == null ||
  174. !is_a($this->entailment[''o''],''Node'') ||
  175. $this->entailment[''o'']->equals($object);
  176. //returns true, if ALL are true
  177. return $matchesS && $matchesP && $matchesO;
  178. }
  179. /**
  180. * Returns a infered InfStatement by evaluating the statement with
  181. * the entailment rule.
  182. * @param object Statement
  183. * @return object InfStatement
  184. * @access public
  185. * @throws PhpError
  186. */
  187. function entail(& $statement)
  188. {
  189. //if the entailment''s subject is <s>,<p>,or <o>, put the statements
  190. //subject,predicate,or object into the subject of the
  191. //entailed statement. If the entailment''s subject is a node,
  192. //add that node to the statement.
  193. switch ($this->entailment[''s''])
  194. {
  195. case ''<s>'':
  196. $entailedSubject=$statement->getSubject();
  197. break;
  198. case ''<p>'':
  199. $entailedSubject=$statement->getPredicate();
  200. break;
  201. case ''<o>'':
  202. $entailedSubject=$statement->getObject();
  203. break;
  204. default:
  205. $entailedSubject=$this->entailment[''s''];
  206. };
  207. //if the entailment''s predicate is <s>,<p>,or <o>, put the
  208. //statements subject,predicate,or object into the predicate of
  209. //the entailed statement. If the entailment''s predicate is a node,
  210. //add that node to the statement.
  211. switch ($this->entailment[''p''])
  212. {
  213. case ''<s>'':
  214. $entailedPredicate=$statement->getSubject();
  215. break;
  216. case ''<p>'':
  217. $entailedPredicate=$statement->getPredicate();
  218. break;
  219. case ''<o>'':
  220. $entailedPredicate=$statement->getObject();
  221. break;
  222. default:
  223. $entailedPredicate=$this->entailment[''p''];
  224. };
  225. //if the entailment''s object is <s>,<p>,or <o>, put the
  226. //statements subject,predicate,or object into the object of
  227. //the entailed statement. If the entailment''s object is a node,
  228. //add that node to the statement.
  229. switch ($this->entailment[''o''])
  230. {
  231. case ''<s>'':
  232. $entailedObject=$statement->getSubject();
  233. break;
  234. case ''<p>'':
  235. $entailedObject=$statement->getPredicate();
  236. break;
  237. case ''<o>'':
  238. $entailedObject=$statement->getObject();
  239. break;
  240. default:
  241. $entailedObject=$this->entailment[''o''];
  242. };
  243. //return the infered statement
  244. return (new InfStatement($entailedSubject,$entailedPredicate,$entailedObject));
  245. }
  246. /**
  247. * Returns a find-query that matches statements, whose entailed
  248. * statements would match the supplied find query.
  249. * @param Node OR null $subject
  250. * @param Node OR null $predicate
  251. * @param Node OR null $object
  252. * @return array
  253. * @access public
  254. * @throws PhpError
  255. */
  256. function getModifiedFind( $subject, $predicate, $object)
  257. {
  258. $findSubject=$this->trigger[''s''];
  259. $findPredicate=$this->trigger[''p''];
  260. $findObject=$this->trigger[''o''];
  261. switch ($this->entailment[''s''])
  262. {
  263. case ''<s>'':
  264. $findSubject=$subject;
  265. break;
  266. case ''<p>'':
  267. $findPredicate=$subject;
  268. break;
  269. case ''<o>'':
  270. $findObject=$subject;
  271. break;
  272. };
  273. switch ($this->entailment[''p''])
  274. {
  275. case ''<s>'':
  276. $findSubject=$predicate;
  277. break;
  278. case ''<p>'':
  279. $findPredicate=$predicate;
  280. break;
  281. case ''<o>'':
  282. $findObject=$predicate;
  283. break;
  284. };
  285. switch ($this->entailment[''o''])
  286. {
  287. case ''<s>'':
  288. $findSubject=$object;
  289. break;
  290. case ''<p>'':
  291. $findPredicate=$object;
  292. break;
  293. case ''<o>'':
  294. $findObject=$object;
  295. break;
  296. };
  297. return array(''s'' => $findSubject,
  298. ''p'' => $findPredicate,
  299. ''o'' => $findObject );
  300. }
  301. function getTrigger()
  302. {
  303. return array ( ''s'' => $this->trigger[''s''],
  304. ''p'' => $this->trigger[''p''],
  305. ''o'' => $this->trigger[''o''],
  306. );
  307. }
  308. function getEntailment()
  309. {
  310. return array ( ''s'' => $this->entailment[''s''],
  311. ''p'' => $this->entailment[''p''],
  312. ''o'' => $this->entailment[''o''],
  313. );
  314. }
  315. }
  316. ?>

Documentation generated on Fri, 13 Jan 2006 07:48:25 +0100 by phpDocumentor 1.3.0RC4