Source for file OntModel.php

Documentation is available at OntModel.php

  1. <?PHP
  2.  
  3. /**
  4. * ----------------------------------------------------------------------------------
  5. * Class: OntModel
  6. * ----------------------------------------------------------------------------------
  7. *
  8. * @package ontModel
  9. */
  10.  
  11.  
  12. /**
  13. * Enhanced view of the model that is known to contain ontology data, under a
  14. * given ontology vocabulary (such as RDFS). OntModel together with OntClass
  15. * and OntResource provide ontology specific methods like addSubClass(),
  16. * listSubClasses(), hasSuperProperty(), addDomain() and listInstances(). This class does not by
  17. * itself compute the deductive extension of the graph under the semantic
  18. * rules of the language. Instead, we wrap an underlying model with this
  19. * ontology interface, that presents a convenience syntax for accessing the
  20. * language elements. Depending on the inference capability of the underlying
  21. * model, the OntModel will appear to contain more or less triples.
  22. * For example, if this class is used to wrap a MemModel or DBModel, only the
  23. * relationships asserted by the document will be reported through this
  24. * convenience API.
  25. * Alternatively, if the OntModel wraps an InfModel (InfModelF / InfModelB),
  26. * the inferred triples from the extension will be reported as well.
  27. *
  28. * <BR><BR>History:
  29. * <LI>10-05-2004 : First version of this class.</LI>
  30. * <LI>01-17-2005 : bugfix in createOntProperty.</LI>
  31. *
  32. * @version V0.9.1
  33. * @author Daniel Westphal <mail at d-westphal dot de>
  34. *
  35. *
  36. * @package ontModel
  37. * @access public
  38. ***/
  39. class OntModel extends ResModel
  40. {
  41. /**
  42. * Holds a reference to the assoiated vocabulary.
  43. * @var object
  44. * @access private
  45. */
  46. var $vocabulary;
  47. /**
  48. * Constructor.
  49. * You have to supply a memmodel/dbmodel/infmodel to save the statements and a vocabulary
  50. *
  51. * @param object Model $model
  52. * @access public
  53. */
  54. function OntModel(& $model,& $vocabulary)
  55. {
  56. parent::ResModel($model);
  57. $this->vocabulary = & $vocabulary;
  58. }
  59. /**
  60. * Answer a resource that represents a class description node in this model.
  61. * If a resource with the given uri exists in the model, it will be re-used.
  62. * If not, a new one is created in the updateable sub-model of the ontology model.
  63. *
  64. * @param string $uri
  65. * @return object OntClass
  66. * @access public
  67. */
  68. function createOntClass($uri = null)
  69. {
  70. $class = new OntClass($uri);
  71. $class->setAssociatedModel($this);
  72. $class->setVocabulary($this->vocabulary);
  73. $class->setInstanceRdfType($this->vocabulary->ONTCLASS());
  74. return $class;
  75. }
  76. /**
  77. * Answer a resource that represents an Individual node in this model.
  78. * If a resource with the given uri exists in the model, it will be re-used.
  79. * If not, a new one is created in the updateable sub-model of the ontology model.
  80. *
  81. * @param string $uri
  82. * @return object Individual
  83. * @access public
  84. */
  85. function createIndividual($uri = null)
  86. {
  87. $individual = new Individual($uri);
  88. $individual->setAssociatedModel($this);
  89. $individual->setVocabulary($this->vocabulary);
  90. return $individual;
  91. }
  92. /**
  93. * Answer a resource that represents an OntProperty node in this model.
  94. * If a resource with the given uri exists in the model, it will be re-used.
  95. * If not, a new one is created in the updateable sub-model of the ontology model.
  96. *
  97. * @param string $uri
  98. * @return object OntProperty
  99. * @access public
  100. */
  101. function createOntProperty($uri = null)
  102. {
  103. $ontProperty = new OntProperty($uri);
  104. $ontProperty->setAssociatedModel($this);
  105. $ontProperty->setVocabulary($this->vocabulary);
  106. $ontProperty->setInstanceRdfType($this->createResource(RDF_NAMESPACE_URI.RDF_PROPERTY));
  107. return $ontProperty;
  108. }
  109. /**
  110. * Answer an array that ranges over all of the various forms of class
  111. * description resource in this model.
  112. * Class descriptions include domain/range definitions, named classes and subClass constructs.
  113. *
  114. * @param string $uri
  115. * @return array of object ResResource
  116. * @access public
  117. */
  118. function listClasses()
  119. {
  120. //get all statements, with an rdf:type as property
  121. $statements= $this->find(null,$this->vocabulary->TYPE(),null);
  122. $return = array();
  123. $returnIndex=array();
  124. foreach ($statements as $statement)
  125. {
  126. $objectLabel=$statement->getLabelObject();
  127. //if it's about a typed Individual
  128. if ($objectLabel!=RDF_SCHEMA_URI.RDFS_CLASS)
  129. {
  130. if (!in_array($objectLabel,$returnIndex))
  131. {
  132. $returnIndex[]=$objectLabel;
  133. $return[]=$statement->getObject();
  134. }
  135. } else
  136. //if it's a "class1 rdf:type rdf:class" construct
  137. {
  138. $subjectLabel=$statement->getLabelSubject();
  139. if (!in_array($subjectLabel,$returnIndex))
  140. {
  141. $returnIndex[]=$subjectLabel;
  142. $return[]=$statement->getSubject();
  143. }
  144. }
  145. }
  146. //find all statements about SubClassConstructs
  147. $statements= $this->find(null,$this->vocabulary->SUB_CLASS_OF(),null);
  148. foreach ($statements as $statement)
  149. {
  150. //add the statements object to the result
  151. $objectLabel=$statement->getLabelObject();
  152. if (!in_array($objectLabel,$returnIndex))
  153. {
  154. $returnIndex[]=$objectLabel;
  155. $return[]=$statement->getObject();
  156. }
  157. }
  158. foreach ($statements as $statement)
  159. {
  160. //add the statements subject to the result
  161. $objectLabel=$statement->getLabelSubject();
  162. if (!in_array($objectLabel,$returnIndex))
  163. {
  164. $returnIndex[]=$objectLabel;
  165. $return[]=$statement->getSubject();
  166. }
  167. }
  168. return $return;
  169. }
  170. }
  171. ?>

Documentation generated on Thu, 7 Jul 2005 13:42:22 +0200 by phpDocumentor 1.3.0RC3