/model/semantics/rdfapi-php/api/ontModel/OntClass.php

https://github.com/lievenjanssen/The-DataTank · PHP · 263 lines · 91 code · 24 blank · 148 comment · 8 complexity · 84aabde170963ce1915cf0d815b4819f MD5 · raw file

  1. <?php
  2. // ----------------------------------------------------------------------------------
  3. // Class: OntClass
  4. // ----------------------------------------------------------------------------------
  5. /**
  6. * Class that represents an ontology node characterising a class description.
  7. *
  8. * @version $Id: OntClass.php 320 2006-11-21 09:38:51Z tgauss $
  9. * @author Daniel Westphal <mail at d-westphal dot de>
  10. *
  11. *
  12. * @package ontModel
  13. * @access public
  14. * */
  15. class OntClass extends OntResource {
  16. /**
  17. * Constructor
  18. * You can supply a uri
  19. *
  20. * @param string $uri
  21. * @access public
  22. */
  23. public function OntClass($uri = null) {
  24. parent::OntResource($uri);
  25. }
  26. /**
  27. * Add a sub-class of this class.
  28. *
  29. * @param object ResResource $resResource
  30. * @return boolean
  31. * @access public
  32. */
  33. public function addSubClass($resResource) {
  34. return $resResource->addProperty($this->vocabulary->SUB_CLASS_OF(), $this);
  35. }
  36. /**
  37. * Add a super-class of this class.
  38. *
  39. * @param object ResResource $resResource
  40. * @return boolean
  41. * @access public
  42. */
  43. public function addSuperClass($resResource) {
  44. return $this->addProperty($this->vocabulary->SUB_CLASS_OF(), $resResource);
  45. }
  46. /**
  47. * Answer a class that is the sub-class of this class.
  48. * If there is more than one such class, an arbitrary selection is made.
  49. *
  50. * @return object OntClass or NULL
  51. * @access public
  52. */
  53. public function getSubClass() {
  54. $statement = $this->model->findFirstMatchingStatement(null, $this->vocabulary->SUB_CLASS_OF(), $this);
  55. if ($statement !== null)
  56. return $this->model->createOntClass($statement->getLabelSubject());
  57. return null;
  58. }
  59. /**
  60. * Answer a class that is the super-class of this class.
  61. * If there is more than one such class, an arbitrary selection is made.
  62. *
  63. * @return object OntClass or NULL
  64. * @access public
  65. */
  66. public function getSuperClass() {
  67. return $this->getPropertyValue($this->vocabulary->SUB_CLASS_OF(), 'OntClass');
  68. }
  69. /**
  70. * Answer true if the given class is a sub-class of this class.
  71. * $direct - If true, only search the classes that are directly
  72. * adjacent to this class in the class hierarchy.
  73. *
  74. * @param object ResResource $resResource
  75. * @param boolean $direct
  76. * @return boolean
  77. * @access public
  78. */
  79. public function hasSubclass($resResource, $direct = true) {
  80. if ($direct)
  81. return $resResource->hasProperty($this->vocabulary->SUB_CLASS_OF(), $this);
  82. $index = array();
  83. return ($this->_getSubAttributeStatementsRec($this, $this->vocabulary->SUB_CLASS_OF(), $index, $resResource) === true);
  84. }
  85. /**
  86. * Answer true if the given class is a super-class of this class.
  87. * $direct - If true, only search the classes that are directly
  88. * adjacent to this class in the class hierarchy.
  89. *
  90. * @param object ResResource $resResource
  91. * @param boolean $direct
  92. * @return boolean
  93. * @access public
  94. */
  95. public function hasSuperClass($resResource, $direct = true) {
  96. if ($direct)
  97. return $this->hasProperty($this->vocabulary->SUB_CLASS_OF(), $resResource);
  98. $index = array();
  99. return ($this->_getSuperAttributeStatementsRec($this, $this->vocabulary->SUB_CLASS_OF(), $index, $resResource) === true);
  100. }
  101. /**
  102. * Answer an ResIterator over the individuals in the model that have this class
  103. * among their types.
  104. *
  105. * @return object ResIterator
  106. * @access public
  107. */
  108. public function listInstances() {
  109. /*
  110. $statements= $this->model->find(null,$this->vocabulary->TYPE(),$this);
  111. $return = array();
  112. $returnIndex=array();
  113. foreach ($statements as $statement)
  114. {
  115. $subjectLabel=$statement->getLabelSubject();
  116. if (!in_array($subjectLabel,$returnIndex))
  117. {
  118. $returnIndex[]=$subjectLabel;
  119. $return[]=$statement->getSubject();
  120. }
  121. }
  122. return $return;
  123. */
  124. return new ResIterator(null, $this->vocabulary->TYPE(), $this, 's', $this->model, 'Individual');
  125. }
  126. /**
  127. * Answer an array over the classes that are declared to be sub-classes of this class.
  128. * Each element of the array will be an OntClass.
  129. * $direct - If true, only search the classes that are directly
  130. * adjacent to this class in the class hierarchy.
  131. *
  132. * @param boolean $direct
  133. * @return array
  134. * @access public
  135. */
  136. public function listSubClasses($direct = true) {
  137. $return = array();
  138. if ($direct) {
  139. $statements = $this->model->find(null, $this->vocabulary->SUB_CLASS_OF(), $this);
  140. } else {
  141. $index = array();
  142. $statements = $this->_getSubAttributeStatementsRec($this, $this->vocabulary->SUB_CLASS_OF(), $index);
  143. }
  144. $returnIndex = array();
  145. foreach ($statements as $statement) {
  146. $subjectLabel = $statement->getLabelSubject();
  147. if (!in_array($subjectLabel, $returnIndex)) {
  148. $returnIndex[] = $subjectLabel;
  149. $return[] = $this->model->createOntClass($subjectLabel);
  150. }
  151. }
  152. return $return;
  153. }
  154. /**
  155. * Answer an array over the classes that are declared to be super-classes of this class.
  156. * Each element of the array will be an OntClass.
  157. * $direct - If true, only search the classes that are directly
  158. * adjacent to this class in the class hierarchy.
  159. *
  160. * @param boolean $direct
  161. * @return array
  162. * @access public
  163. */
  164. public function listSuperClasses($direct = true) {
  165. $return = array();
  166. if ($direct)
  167. return $this->listProperty($this->vocabulary->SUB_CLASS_OF(), 'OntClass');
  168. $index = array();
  169. $statements = $this->_getSuperAttributeStatementsRec($this, $this->vocabulary->SUB_CLASS_OF(), $index);
  170. $returnIndex = array();
  171. foreach ($statements as $statement) {
  172. $objectLabel = $statement->getLabelObject();
  173. if (!in_array($objectLabel, $returnIndex)) {
  174. $returnIndex[] = $objectLabel;
  175. $return[] = $this->model->createOntClass($objectLabel);
  176. }
  177. }
  178. return $return;
  179. }
  180. /**
  181. * Remove the given class from the sub-classes of this class.
  182. *
  183. * @param object ResResource $resResource
  184. * @return boolean
  185. * @access public
  186. */
  187. public function removeSubClass($resResource) {
  188. return $this->model->remove(new Statement($resResource, $this->vocabulary->SUB_CLASS_OF(), $this));
  189. }
  190. /**
  191. * Remove the given class from the super-classes of this class.
  192. *
  193. * @param object ResResource $resResource
  194. * @return boolean
  195. * @access public
  196. */
  197. public function removeSuperClass($resResource) {
  198. return $this->removeProperty($this->vocabulary->SUB_CLASS_OF(), $resResource);
  199. }
  200. /**
  201. * Assert that this class is super-class of the given class.
  202. * Any existing statements for subClassOf on prop will be removed.
  203. *
  204. * @param object ResResource $resResource
  205. * @access public
  206. */
  207. public function setSubClass($resResource) {
  208. foreach ($this->listSubClasses() as $oldRes) {
  209. $this->removeSubClass($oldRes);
  210. }
  211. $this->addSubClass($resResource);
  212. }
  213. /**
  214. * Assert that this class is sub-class of the given class.
  215. * Any existing statements for subClassOf on prop will be removed.
  216. *
  217. * @param object ResResource $resResource
  218. * @access public
  219. */
  220. public function setSuperClass($resResource) {
  221. $this->setPropertyValue($this->vocabulary->SUB_CLASS_OF(), $resResource);
  222. }
  223. /**
  224. * Answer a resource that represents an instance of this OntClass and Individual
  225. * node in this model.
  226. * If a resource with the given uri exists in the model, it will be re-used.
  227. * If not, a new one is created in the updateable sub-model of the ontology model.
  228. *
  229. * @param string $uri
  230. * @return object Individual
  231. * @access public
  232. */
  233. public function createInstance($uri = null) {
  234. $instance = $this->model->createIndividual($uri);
  235. $instance->setInstanceRdfType($this);
  236. return $instance;
  237. }
  238. }
  239. ?>