Source for file InfRule.php

Documentation is available at InfRule.php

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

Documentation generated on Fri, 17 Dec 2004 16:15:31 +0100 by phpDocumentor 1.3.0RC3