PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/rdfapi-php/api/infModel/InfRule.php

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