source: Dev/branches/rest-dojo-ui/jos-branch/server/rdfapi/ontModel/OntResource.php @ 312

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