Source for file Statement.php

Documentation is available at Statement.php

  1. <?php
  2.  
  3. // ----------------------------------------------------------------------------------
  4. // Class: Statement
  5. // ----------------------------------------------------------------------------------
  6.  
  7.  
  8.  
  9. /**
  10. * An RDF statement.
  11. * In this implementation, a statement is not itself a resource.
  12. * If you want to use a a statement as subject or object of other statements,
  13. * you have to reify it first.
  14. *
  15. * <BR><BR>History:<UL>
  16. * <LI>05-30-2003 : Changed function compare(), added statementsorter($a,$b).</LI>
  17. * <LI>02-21-2003 : getLabelSubject(), getLabelPredicate(), getLabelObject(), toStringSubject(), toStringPredicate(), toStringObject() added.</LI>
  18. * <LI>09-10-2002 : First version of this class.</LI>
  19. *
  20. * @author Chris Bizer <chris@bizer.de>
  21. * @version V0.9.1
  22. * @package model
  23. */
  24. class Statement extends Object {
  25. /**
  26. * Subject of the statement
  27. *
  28. * @var object resource
  29. * @access private
  30. */
  31. var $subj;
  32.  
  33. /**
  34. * Predicate of the statement
  35. *
  36. * @var object resource
  37. * @access private
  38. */
  39. var $pred;
  40. /**
  41. * Object of the statement
  42. *
  43. * @var object node
  44. * @access private
  45. */
  46. var $obj;
  47.  
  48. /**
  49. * The parameters to constructor are instances of classes and not just strings
  50. *
  51. * @param object node $subj
  52. * @param object node $pred
  53. * @param object node $obj
  54. * @throws PhpError
  55. */
  56. function Statement($subj, $pred, $obj) {
  57.  
  58. if (!is_a($subj, 'Resource')) {
  59. $errmsg = RDFAPI_ERROR .
  60. '(class: Statement; method: new): Resource expected as subject.';
  61. trigger_error($errmsg, E_USER_ERROR);
  62. }
  63. if (!is_a($pred, 'Resource') || is_a($pred, 'BlankNode')) {
  64. $errmsg = RDFAPI_ERROR .
  65. '(class: Statement; method: new): Resource expected as predicate, no blank node allowed.';
  66. trigger_error($errmsg, E_USER_ERROR);
  67. }
  68. if (!(is_a($obj, 'Resource') or is_a($obj, 'Literal'))) {
  69. $errmsg = RDFAPI_ERROR .
  70. '(class: Statement; method: new): Resource or Literal expected as object.';
  71. trigger_error($errmsg, E_USER_ERROR);
  72. }
  73. $this->pred = $pred;
  74. $this->subj = $subj;
  75. $this->obj = $obj;
  76. }
  77.  
  78. /**
  79. * Returns the subject of the triple.
  80. * @access public
  81. * @return object node
  82. */
  83. function getSubject() {
  84. return $this->subj;
  85. }
  86.  
  87. /**
  88. * Returns the predicate of the triple.
  89. * @access public
  90. * @return object node
  91. */
  92. function getPredicate() {
  93. return $this->pred;
  94. }
  95.  
  96. /**
  97. * Returns the object of the triple.
  98. * @access public
  99. * @return object node
  100. */
  101. function getObject() {
  102. return $this->obj;
  103. }
  104. /**
  105. * Alias for getSubject()
  106. * @access public
  107. * @return object node
  108. */
  109. function subject() {
  110. return $this->subj;
  111. }
  112.  
  113. /**
  114. * Alias for getPredicate()
  115. * @access public
  116. * @return object node
  117. */
  118. function predicate() {
  119. return $this->pred;
  120. }
  121.  
  122. /**
  123. * Alias for getObject()
  124. * @access public
  125. * @return object node
  126. */
  127. function object() {
  128. return $this->obj;
  129. }
  130. /**
  131. * Retruns the hash code of the triple.
  132. * @access public
  133. * @return string
  134. */
  135. function hashCode() {
  136. return md5($this->subj->getLabel() . $this->pred->getLabel() . $this->obj->getLabel());
  137. }
  138.  
  139. /**
  140. * Dumps the triple.
  141. * @access public
  142. * @return string
  143. */
  144.  
  145. function toString() {
  146. return 'Triple(' . $this->subj->toString() . ', ' . $this->pred->toString() . ', ' . $this->obj->toString() . ')';
  147. }
  148.  
  149. /**
  150. * Returns a toString() serialization of the statements's subject.
  151. *
  152. * @access public
  153. * @return string
  154. */
  155. function toStringSubject() {
  156. return $this->subj->toString();
  157. }
  158.  
  159. /**
  160. * Returns a toString() serialization of the statements's predicate.
  161. *
  162. * @access public
  163. * @return string
  164. */
  165. function toStringPredicate() {
  166. return $this->pred->toString();
  167. }
  168.  
  169. /**
  170. * Reurns a toString() serialization of the statements's object.
  171. *
  172. * @access public
  173. * @return string
  174. */
  175. function toStringObject() {
  176. return $this->obj->toString();
  177. }
  178.  
  179. /**
  180. * Returns the URI or bNode identifier of the statements's subject.
  181. *
  182. * @access public
  183. * @return string
  184. */
  185. function getLabelSubject() {
  186. return $this->subj->getLabel();
  187. }
  188.  
  189. /**
  190. * Returns the URI of the statements's predicate.
  191. *
  192. * @access public
  193. * @return string
  194. */
  195. function getLabelPredicate() {
  196. return $this->pred->getLabel();
  197. }
  198.  
  199. /**
  200. * Reurns the URI, text or bNode identifier of the statements's object.
  201. *
  202. * @access public
  203. * @return string
  204. */
  205. function getLabelObject() {
  206. return $this->obj->getLabel();
  207. }
  208. /**
  209. * Checks if two statements are equal.
  210. * Two statements are considered to be equal if they have the
  211. * same subject, predicate and object. A statement can only be equal
  212. * to another statement object.
  213. * @access public
  214. * @param object statement $that
  215. * @return boolean
  216. */
  217.  
  218. function equals ($that) {
  219. if ($this == $that) {
  220. return true;
  221. }
  222. if ($that == NULL || !(is_a($that, 'Statement'))) {
  223. return false;
  224. }
  225. return
  226. $this->subj->equals($that->subject()) &&
  227. $this->pred->equals($that->predicate()) &&
  228. $this->obj->equals($that->object());
  229. }
  230.  
  231. /**
  232. * Compares two statements and returns integer less than, equal to, or greater than zero.
  233. * Can be used for writing sorting function for models or with the PHP function usort().
  234. *
  235. * @access public
  236. * @param object statement &$that
  237. * @return boolean
  238. */
  239.  
  240. function compare(&$that) {
  241. return statementsorter($this, $that);
  242. // statementsorter function see below
  243. }
  244. /**
  245. * Reifies a statement.
  246. * Returns a new MemModel that is the reification of the statement.
  247. * For naming the statement's bNode a Model or bNodeID must be passed to the method.
  248. *
  249. * @access public
  250. * @param mixed &$model_or_bNodeID
  251. * @return object model
  252. */
  253.  
  254. function & reify(&$model_or_bNodeID) {
  255. if (is_a($model_or_bNodeID, 'MemModel')) {
  256. // parameter is model
  257. $statementModel = new MemModel($model_or_bNodeID->getBaseURI());
  258. $thisStatement = new BlankNode($model_or_bNodeID);
  259. } else {
  260. // parameter is bNodeID
  261. $statementModel = new MemModel();
  262. $thisStatement = new BlankNode($model_or_bNodeID);
  263. }
  264. $RDFstatement = new Resource(RDF_NAMESPACE_URI . RDF_STATEMENT);
  265. $RDFtype = new Resource(RDF_NAMESPACE_URI . RDF_TYPE);
  266. $RDFsubject = new Resource(RDF_NAMESPACE_URI . RDF_SUBJECT);
  267. $RDFpredicate = new Resource(RDF_NAMESPACE_URI . RDF_PREDICATE);
  268. $RDFobject = new Resource(RDF_NAMESPACE_URI . RDF_OBJECT);
  269. $statementModel->add(new Statement($thisStatement, $RDFtype, $RDFstatement));
  270. $statementModel->add(new Statement($thisStatement, $RDFsubject, $this->getSubject()));
  271. $statementModel->add(new Statement($thisStatement, $RDFpredicate, $this->getPredicate()));
  272. $statementModel->add(new Statement($thisStatement, $RDFobject, $this->Object()));
  273. return $statementModel;
  274. }
  275. } // end: Statement
  276.  
  277.  
  278. /**
  279. * Comparison function for comparing two statements.
  280. * statementsorter() is used by the PHP function usort ( array array, callback cmp_function)
  281. *
  282. * @access private
  283. * @param object Statement $a
  284. * @param object Statement $b
  285. * @return integer less than, equal to, or greater than zero
  286. * @throws phpErrpr
  287. */
  288. function statementsorter($a,$b) {
  289. //Compare subjects
  290. $x=$a->getSubject();
  291. $y=$b->getSubject();
  292. $r=strcmp($x->getLabel(),$y->getLabel());
  293. if ($r!=0) return $r;
  294. //Compare predicates
  295. $x=$a->getPredicate();
  296. $y=$b->getPredicate();
  297. $r=strcmp($x->getURI(),$y->getURI());
  298. if ($r!=0) return $r;
  299. //Final resort, compare objects
  300. $x=$a->getObject();
  301. $y=$b->getObject();
  302. return strcmp($x->toString(),$y->toString());
  303. }
  304. ?>

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