PageRenderTime 46ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/rdfapi-php/api/model/Statement.php

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