Source for file OntResource.php

Documentation is available at OntResource.php

  1. <?php
  2. /**
  3. * ----------------------------------------------------------------------------------
  4. * Class: OntResource
  5. * ----------------------------------------------------------------------------------
  6. *
  7. * @package ontModel
  8. */
  9.  
  10.  
  11. /**
  12. * Provides a common super-type for all of the abstractions in
  13. * this ontology representation package.
  14. *
  15. * <BR><BR>History:
  16. * <LI>10-05-2004 : First version of this class.</LI>
  17. *
  18. * @version V0.9.1
  19. * @author Daniel Westphal <mail at d-westphal dot de>
  20. *
  21. *
  22. * @package ontModel
  23. * @access public
  24. ***/
  25. class OntResource extends ResResource
  26. {
  27. /**
  28. * Holds a reference to the assoiated vocabulary
  29. * @var object
  30. * @access private
  31. */
  32. var $vocabulary;
  33. /**
  34. * Holds a resResource of the type, which is this ontResource of.
  35. * If this value is set, the ontModel will add an additional
  36. * statement about this resource and the fiven rdf:type
  37. * @var object
  38. * @access private
  39. */
  40. var $rdfType;
  41. /**
  42. * Constructor
  43. * You can supply a uri
  44. *
  45. * @param string $uri
  46. * @access public
  47. */
  48. function OntResource($uri = null)
  49. {
  50. $this->rdfType=false;
  51. parent::ResResource($uri);
  52. }
  53. /**
  54. * Sets the reference to the assoiated vocabulary
  55. *
  56. * @param object OntVocabulary $vocabulary
  57. * @access public
  58. */
  59. function setVocabulary(& $vocabulary)
  60. {
  61. $this->vocabulary = & $vocabulary;
  62. }
  63. /**
  64. * Add the given comment to this resource.
  65. *
  66. * @param object ResLiteral $comment
  67. * @return boolean
  68. * @access public
  69. */
  70. function addComment($comment)
  71. {
  72. return $this->addProperty($this->vocabulary->COMMENT(),$comment);
  73. }
  74. /**
  75. * Answer the comment string for this object. If there is more than one such resource, an arbitrary selection is made.
  76. *
  77. * @return object ResLiteral or NULL
  78. * @access public
  79. */
  80. function getComment()
  81. {
  82. return $this->getPropertyValue($this->vocabulary->COMMENT());
  83. }
  84. /**
  85. * Add a resource that is declared to provide a definition of this resource.
  86. *
  87. * @param object ResResource $resResource
  88. * @return boolean
  89. * @access public
  90. */
  91. function addIsDefinedBy($resResource)
  92. {
  93. return $this->addProperty($this->vocabulary->IS_DEFINED_BY(),$resResource);
  94. }
  95. /**
  96. * Answer a resource that is declared to provide a definition of this resource.
  97. * If there is more than one such resource, an arbitrary selection is made.
  98. *
  99. * @return object ResResource
  100. * @access public
  101. */
  102. function getIsDefinedBy()
  103. {
  104. return $this->getPropertyValue($this->vocabulary->IS_DEFINED_BY());
  105. }
  106. /**
  107. * Add the given Label to this resource
  108. *
  109. * @param object ResLiteral $resLiteral
  110. * @return boolean
  111. * @access public
  112. */
  113. function addLabelProperty($resLiteral)
  114. {
  115. return $this->addProperty($this->vocabulary->LABEL(),$resLiteral);
  116. }
  117. /**
  118. * Answer the label ResLiteral for this object.
  119. * If there is more than one such resource, an arbitrary selection is made.
  120. *
  121. * @param string $uri
  122. * @return object ResResource
  123. * @access public
  124. */
  125. function getLabelProperty()
  126. {
  127. return $this->getPropertyValue($this->vocabulary->LABEL());
  128. }
  129. /**
  130. * Add the given class as one of the rdf:type's for this resource.
  131. *
  132. * @param object ResResource $resResource
  133. * @return boolean
  134. * @access public
  135. */
  136. function addRDFType($resResource)
  137. {
  138. return $this->addProperty($this->vocabulary->TYPE(),$resResource);
  139. }
  140. /**
  141. * Answer the rdf:type (ie the class) of this resource.
  142. * If there is more than one type for this resource, the return value will
  143. * be one of the values, but it is not specified which one
  144. * (nor that it will consistently be the same one each time).
  145. *
  146. * @return object ResResource
  147. * @access public
  148. */
  149. function getRDFType()
  150. {
  151. return $this->getPropertyValue($this->vocabulary->TYPE());
  152. }
  153. /**
  154. * Add a resource that is declared to provided additional
  155. * information about the definition of this resource.
  156. *
  157. * @param object ResResource $resResource
  158. * @return boolean
  159. * @access public
  160. */
  161. function addSeeAlso($resResource)
  162. {
  163. return $this->addProperty($this->vocabulary->SEE_ALSO(),$resResource);
  164. }
  165. /**
  166. * Answer a resource that provides additional information about this resource.
  167. * If more than one such resource is defined, make an arbitrary choice.
  168. *
  169. * @return object ResResource
  170. * @access public
  171. */
  172. function getSeeAlso()
  173. {
  174. return $this->getPropertyValue($this->vocabulary->SEE_ALSO());
  175. }
  176. /**
  177. * Answer a view of this resource as a class
  178. *
  179. * @return object OntClass
  180. * @access public
  181. */
  182. function asClass()
  183. {
  184. return $this->model->createOntClass($this->uri);
  185. }
  186. /**
  187. * Answer a view of this resource as an Individual
  188. *
  189. * @return object Individual
  190. * @access public
  191. */
  192. function asIndividual()
  193. {
  194. return $this->model->createIndividual($this->uri);
  195. }
  196. /**
  197. * Answer a view of this resource as a property
  198. *
  199. * @return object OntProperty
  200. * @access public
  201. */
  202. function asOntProperty()
  203. {
  204. return $this->model->createOntProperty($this->uri);
  205. }
  206. /**
  207. * Answer a reference to the ontology language profile that governs the
  208. * ontology model to which this ontology resource is attached.
  209. *
  210. * @param string $uri
  211. * @return object OntClass
  212. * @access public
  213. */
  214. function getVocabulary()
  215. {
  216. return $this->vocabulary ;
  217. }
  218. /**
  219. * Answer the value of a given RDF property for this resource as $returnType, or null
  220. * if it doesn't have one. If there is more than one RDF statement with
  221. * the given property for the current value, it is not defined which of
  222. * the values will be returned.
  223. * The following return Types are supported: 'OntClass', 'OntProperty', 'Individual', and 'ResResource'
  224. * Default is 'ResResource'
  225. *
  226. * @param object ResResource $property
  227. * @param string $returnType
  228. * @return object OntClass
  229. * @access public
  230. */
  231. function getPropertyValue($property, $returnType = 'ResResource')
  232. {
  233. $statement=$this->getProperty($property);
  234. if ($statement===null)
  235. return null;
  236. switch ($returnType)
  237. {
  238. case 'OntClass':
  239. return $this->model->createOntClass($statement->getLabelObject());
  240. break;
  241. case 'OntProperty':
  242. return $this->model->createOntProperty($statement->getLabelObject());
  243. break;
  244. case 'Individual':
  245. return $this->model->createIndividual($statement->getLabelObject());
  246. break;
  247. default:
  248. return $statement->getObject();
  249. break;
  250. }
  251. }
  252. /**
  253. * Answer true if this resource has the given comment.
  254. *
  255. * @param object ResLiteral $resLiteral
  256. * @return boolean
  257. * @access public
  258. */
  259. function hasComment($resLiteral)
  260. {
  261. return $this->hasProperty($this->vocabulary->COMMENT(),$resLiteral);
  262. }
  263. /**
  264. * Answer true if this resource has the given label.
  265. *
  266. * @param object ResLiteral $resLiteral
  267. * @return boolean
  268. * @access public
  269. */
  270. function hasLabelProperty($resLiteral)
  271. {
  272. return $this->hasProperty($this->vocabulary->LABEL(),$resLiteral);
  273. }
  274. /**
  275. * Answer true if this resource has the given rdf:type.
  276. *
  277. * @param object ResResource $ontClass
  278. * @return boolean
  279. * @access public
  280. */
  281. function hasRDFType($ontClass)
  282. {
  283. return $this->hasProperty($this->vocabulary->TYPE(),$ontClass);
  284. }
  285. /**
  286. * Answer true if this resource has the given resource as a source
  287. * of additional information.
  288. *
  289. * @param object ResResource $resResource
  290. * @return boolean
  291. * @access public
  292. */
  293. function hasSeeAlso($resResource)
  294. {
  295. return $this->hasProperty($this->vocabulary->SEE_ALSO(),$resResource);
  296. }
  297. /**
  298. * Answer true if this resource is defined by the given resource.
  299. *
  300. * @param object ResResource $resResource
  301. * @return boolean
  302. * @access public
  303. */
  304. function isDefinedBy($resResource)
  305. {
  306. return $this->hasProperty($this->vocabulary->IS_DEFINED_BY(),$resResource);
  307. }
  308. /**
  309. * Answer an array of all of the comment literals for this resource.
  310. *
  311. * @param string $language
  312. * @return array
  313. * @access public
  314. */
  315. function listComments($language)
  316. {
  317. $return=$this->listProperty($this->vocabulary->COMMENT());
  318. if ($language === false)
  319. return $return;
  320. foreach ($return as $key => $resLiteral)
  321. {
  322. if (!is_a($resLiteral,'ResLiteral') || $resLiteral->getLanguage() != $language)
  323. unset ($return[$key]);
  324. }
  325. return $return;
  326. }
  327. /**
  328. * Answer an array of all of the resources that are declared to define this resource.
  329. *
  330. * @return array
  331. * @access public
  332. */
  333. function listIsDefinedBy()
  334. {
  335. return $this->listProperty($this->vocabulary->IS_DEFINED_BY());
  336. }
  337. /**
  338. * Answer an array of all of the label literals for this resource, with the given
  339. * language, if $language is set.
  340. *
  341. * @return array
  342. * @access public
  343. */
  344. function listLabelProperties($language = false)
  345. {
  346. $return=$this->listProperty($this->vocabulary->LABEL());
  347. if ($language === false)
  348. return $return;
  349. foreach ($return as $key => $resLiteral)
  350. {
  351. if (!is_a($resLiteral,'ResLiteral') || $resLiteral->getLanguage() != $language)
  352. unset ($return[$key]);
  353. }
  354. return $return;
  355. }
  356. /**
  357. * Answer an array of the RDF classes to which this resource belongs.
  358. * If $direct is true, only answer those resources that are direct types of
  359. * this resource, not the super-classes of the class etc.
  360. *
  361. * @param boolean $direct
  362. * @return array Array of ResResources
  363. * @access public
  364. */
  365. function listRDFTypes($direct = true)
  366. {
  367. return $this->listProperty($this->vocabulary->TYPE());
  368. }
  369. /**
  370. * Answer an array of all of the resources that are declared to
  371. * provide addition information about this resource.
  372. *
  373. * @return array Array of ResResources
  374. * @access public
  375. */
  376. function listSeeAlso()
  377. {
  378. return $this->listProperty($this->vocabulary->SEE_ALSO());
  379. }
  380. /**
  381. * Answer an array of values of a given RDF property for this resource as $returnType, or null
  382. * if it doesn't have one.
  383. * The following return Types are supported: 'OntClass', 'OntProperty', 'Individual', and 'ResResource'
  384. * Default is 'ResResource'
  385. *
  386. * @param object ResResource $property
  387. * @param string $returnType
  388. * @return array of ResResources
  389. * @access public
  390. */
  391. function listProperty($property, $returnType = 'OntResource')
  392. {
  393. $return=array();
  394. $resArray = $this->listProperties($property);
  395. foreach ($resArray as $statement)
  396. {
  397. switch ($returnType)
  398. {
  399. case 'OntClass':
  400. $return[]=$this->model->createOntClass($statement->getLabelObject());
  401. break;
  402. case 'OntProperty':
  403. $return[]=$this->model->createOntProperty($statement->getLabelObject());
  404. break;
  405. case 'Individual':
  406. $return[]=$this->model->createIndividual($statement->getLabelObject());
  407. break;
  408. default:
  409. $return[]=$statement->getObject();
  410. break;
  411. }
  412. }
  413. return $return;
  414. }
  415. /**
  416. * Remove the statement that the given ResLiteral is a comment on this resource.
  417. * Returns true, if a statement was removed
  418. *
  419. * @param object ResLiteral $resLiteral
  420. * @return boolean
  421. * @access public
  422. */
  423. function removeComment($resLiteral)
  424. {
  425. return $this->removeProperty($this->vocabulary->COMMENT(),$resLiteral);
  426. }
  427. /**
  428. * Remove the statement that this resource is defined by the given resource.
  429. *
  430. * @param object ResResource $resResource
  431. * @return boolean
  432. * @access public
  433. */
  434. function removeDefinedBy($resResource)
  435. {
  436. return $this->removeProperty($this->vocabulary->IS_DEFINED_BY(),$resResource);
  437. }
  438. /**
  439. * Remove the statement that the given ResLiteral is a label on this resource.
  440. * Returns true, if a statement was removed
  441. *
  442. * @param object ResLiteral $resLiteral
  443. * @return boolean
  444. * @access public
  445. */
  446. function removeLabelProperty($resLiteral)
  447. {
  448. return $this->removeProperty($this->vocabulary->LABEL(),$resLiteral);
  449. }
  450. /**
  451. * Remove the specific property-value pair from this resource.
  452. *
  453. * @param object ResResource $property
  454. * @param object ResResource $value
  455. * @return boolean
  456. * @access public
  457. */
  458. function removeProperty($property, $value)
  459. {
  460. return $this->model->remove(new Statement($this,$property,$value));
  461. }
  462. /**
  463. * Remove the statement that this resource is of the given RDF type.
  464. *
  465. * @param object ResResource $resResource
  466. * @return boolean
  467. * @access public
  468. */
  469. function removeRDFType($resResource)
  470. {
  471. return $this->removeProperty($this->vocabulary->TYPE(),$resResource);
  472. }
  473. /**
  474. * Remove the statement indicating the given resource as a source of
  475. * additional information about this resource.
  476. *
  477. * @param object ResResource $resResource
  478. * @return boolean
  479. * @access public
  480. */
  481. function removeSeeAlso($resResource)
  482. {
  483. return $this->removeProperty($this->vocabulary->SEE_ALSO(),$resResource);
  484. }
  485. /**
  486. * Assert that the given string is the comment on this resource.
  487. * Any existing statements for comment will be removed.
  488. *
  489. * @param object ResLiteral $resLiteral
  490. * @access public
  491. */
  492. function setComment($resLiteral)
  493. {
  494. $this->setPropertyValue($this->vocabulary->COMMENT(),$resLiteral);
  495. }
  496. /**
  497. * Assert that the given resource provides a source of definitions about this resource.
  498. * Any existing statements for isDefinedBy will be removed.
  499. *
  500. * @param object ResResource $resResource
  501. * @access public
  502. */
  503. function setIsDefinedBy($resResource)
  504. {
  505. $this->setPropertyValue($this->vocabulary->IS_DEFINED_BY(),$resResource);
  506. }
  507. /**
  508. * Assert that the given string is the label on this resource.
  509. * Any existing statements for comment will be removed.
  510. *
  511. * @param object ResLiteral $resLiteral
  512. * @access public
  513. */
  514. function setLabelProperty($resLiteral)
  515. {
  516. $this->setPropertyValue($this->vocabulary->LABEL(),$resLiteral);
  517. }
  518. /**
  519. * Set the value of the given property of this ontology resource to the given value.
  520. * Maintains the invariant that there is at most one value of the property for a
  521. * given resource, so existing property values are first removed.
  522. * To add multiple properties, use addProperty.
  523. *
  524. * @param object ResResource $property
  525. * @param object ResResource $value
  526. * @access public
  527. */
  528. function setPropertyValue($property, $value)
  529. {
  530. $this->removeAll($property);
  531. $this->addProperty($property,$value);
  532. }
  533. /**
  534. * Set the RDF type (ie the class) for this resource,
  535. * replacing any existing rdf:type property. Any existing statements
  536. * for the RDF type will first be removed.
  537. *
  538. * @param object ResResource $resResource
  539. * @access public
  540. */
  541. function setRDFType($resResource)
  542. {
  543. $this->setPropertyValue($this->vocabulary->TYPE(),$resResource);
  544. }
  545. /**
  546. * Add a resource that is declared to provided additional information
  547. * about the definition of this resource
  548. *
  549. * @param object ResResource $resResource
  550. * @access public
  551. */
  552. function setSeeAlso($resResource)
  553. {
  554. $this->setPropertyValue($this->vocabulary->SEE_ALSO(),$resResource);
  555. }
  556. /**
  557. * Returns an array of ResResources that are recursively connected by $attribute
  558. * in superProperty direction.
  559. * If $onlyFindThisResResource is set to a ResResource, this function returns boolean
  560. * if this distinct resource recursively is connected to the $startResource.
  561. *
  562. * @param object ResResource $startResource
  563. * @param object ResResource $attribute
  564. * @param array $attribute
  565. * @param object ResResource $onlyFindThisResResource
  566. * @return array OR boolean
  567. * @access private
  568. */
  569. function _getSuperAttributeStatementsRec(& $startResource,& $attribute,& $attributeIndex, $onlyFindThisResResource = false)
  570. {
  571.  
  572. $return = $startResource->listProperties($attribute);
  573. if ($onlyFindThisResResource)
  574. {
  575. foreach ($return as $statement)
  576. {
  577. if ($onlyFindThisResResource->equals($statement->getObject()))
  578. return true;
  579. }
  580. }
  581. foreach ($return as $statement)
  582. {
  583. $attributeLabel=$statement->getLabelObject();
  584. if (!in_array($attributeLabel,$attributeIndex))
  585. {
  586. $attributeIndex[]=$attributeLabel;
  587. $subReturn = $this->_getSuperAttributeStatementsRec($statement->getObject(), $attribute, $attributeIndex, $onlyFindThisResResource);
  588. }
  589. }
  590. if (isset($subReturn))
  591. {
  592. if ($subReturn === true)
  593. return true;
  594. return array_merge($return,$subReturn);
  595. }
  596. return $return;
  597. }
  598. /**
  599. * Returns an array of ResResources that are recursively connected by $attribute
  600. * in subProperty direction.
  601. * If $onlyFindThisResResource is set to a ResResource, this function returns boolean
  602. * if this distinct resource recursively is connected to the $startResource.
  603. *
  604. * @param object ResResource $startResource
  605. * @param object ResResource $attribute
  606. * @param array $attribute
  607. * @param object ResResource $onlyFindThisResResource
  608. * @return array OR boolean
  609. * @access private
  610. */
  611. function _getSubAttributeStatementsRec(& $startResource,& $attribute,& $attributeIndex, $onlyFindThisResResource = false)
  612. {
  613.  
  614. $return = $this->model->find(null,$attribute,$startResource);
  615. if ($onlyFindThisResResource)
  616. {
  617. foreach ($return as $statement)
  618. {
  619. if ($onlyFindThisResResource->equals($statement->getSubject()))
  620. return true;
  621. }
  622. }
  623. foreach ($return as $statement)
  624. {
  625. $attributeLabel=$statement->getLabelSubject();
  626. if (!in_array($attributeLabel,$attributeIndex))
  627. {
  628. $attributeIndex[]=$attributeLabel;
  629. $subReturn = $this->_getSubAttributeStatementsRec($statement->getSubject(), $attribute, $attributeIndex, $onlyFindThisResResource);
  630. }
  631. }
  632. if (isset($subReturn))
  633. {
  634. if ($subReturn === true)
  635. return true;
  636. return array_merge($return,$subReturn);
  637. }
  638. return $return;
  639. }
  640. /**
  641. * Add a property to this resource.
  642. * A statement with this resource as the subject, p as the predicate and o
  643. * as the object is added to the model associated with this resource.
  644. * If $this->rdfType is set, an additional statement about it's type
  645. * is added.
  646. *
  647. * @param ResResource $property
  648. * @param ResResource/ResLiteral $object
  649. * @return object ResResource
  650. * @access public
  651. */
  652. function addProperty($property,$object)
  653. {
  654. if ($this->rdfType !== false)
  655. if (!$this->hasRDFType($this->rdfType))
  656. $this->model->add(new Statement($this,$this->vocabulary->TYPE(),$this->rdfType));
  657. return parent:: addProperty($property,$object);
  658. }
  659. /**
  660. * Sets the rdf:type, that this distinct resource is instance of.
  661. * If this value is set, the ontModel will add an additional
  662. * statement about this resource and the fiven rdf:type
  663. *
  664. * @param object ResResource $resResource
  665. * @access public
  666. */
  667. function setInstanceRdfType($resResource)
  668. {
  669. $this->rdfType = $resResource;
  670. }
  671. /**
  672. * returns the rdf:type, that this distinct resource is instance of.
  673. *
  674. * @return object ResResource $resResource
  675. * @access public
  676. */
  677. function getInstanceRdfType()
  678. {
  679. return $this->rdfType;
  680. }
  681. }
  682. ?>

Documentation generated on Fri, 17 Dec 2004 16:16:36 +0100 by phpDocumentor 1.3.0RC3