Source for file Statement.php

Documentation is available at Statement.php

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

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