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