KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > ontology > impl > OntPropertyImpl


1 /*****************************************************************************
2  * Source code information
3  * -----------------------
4  * Original author Ian Dickinson, HP Labs Bristol
5  * Author email Ian.Dickinson@hp.com
6  * Package Jena 2
7  * Web http://sourceforge.net/projects/jena/
8  * Created 31-Mar-2003
9  * Filename $RCSfile: OntPropertyImpl.java,v $
10  * Revision $Revision: 1.21 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2005/04/11 16:41:41 $
14  * by $Author: ian_dickinson $
15  *
16  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
17  * (see footer for full conditions)
18  *****************************************************************************/

19
20 // Package
21
///////////////
22
package com.hp.hpl.jena.ontology.impl;
23
24
25 // Imports
26
///////////////
27
import java.util.*;
28
29 import com.hp.hpl.jena.enhanced.*;
30 import com.hp.hpl.jena.graph.*;
31 import com.hp.hpl.jena.ontology.*;
32 import com.hp.hpl.jena.rdf.model.*;
33 import com.hp.hpl.jena.util.iterator.*;
34 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
35 import com.hp.hpl.jena.util.iterator.Filter;
36
37
38
39 /**
40  * <p>
41  * Implementation of the abstraction representing a general ontology property.
42  * </p>
43  *
44  * @author Ian Dickinson, HP Labs
45  * (<a HREF="mailto:Ian.Dickinson@hp.com" >email</a>)
46  * @version CVS $Id: OntPropertyImpl.java,v 1.21 2005/04/11 16:41:41 ian_dickinson Exp $
47  */

48 public class OntPropertyImpl
49     extends OntResourceImpl
50     implements OntProperty
51 {
52     // Constants
53
//////////////////////////////////
54

55     // Static variables
56
//////////////////////////////////
57

58     /**
59      * A factory for generating OntProperty facets from nodes in enhanced graphs.
60      * Note: should not be invoked directly by user code: use
61      * {@link com.hp.hpl.jena.rdf.model.RDFNode#as as()} instead.
62      */

63     public static Implementation factory = new Implementation() {
64         public EnhNode wrap( Node n, EnhGraph eg ) {
65             if (canWrap( n, eg )) {
66                 return new OntPropertyImpl( n, eg );
67             }
68             else {
69                 throw new ConversionException( "Cannot convert node " + n + " to OntProperty");
70             }
71         }
72
73         public boolean canWrap( Node node, EnhGraph eg ) {
74             // node will support being an OntProperty facet if it has rdf:type owl:Property or equivalent
75
Profile profile = (eg instanceof OntModel) ? ((OntModel) eg).getProfile() : null;
76             return (profile != null) && profile.isSupported( node, eg, OntProperty.class );
77         }
78     };
79
80
81     // Instance variables
82
//////////////////////////////////
83

84     // Constructors
85
//////////////////////////////////
86

87     /**
88      * <p>
89      * Construct an ontology property represented by the given node in the given graph.
90      * </p>
91      *
92      * @param n The node that represents the resource
93      * @param g The enh graph that contains n
94      */

95     public OntPropertyImpl( Node n, EnhGraph g ) {
96         super( n, g );
97     }
98
99
100     // External signature methods
101
//////////////////////////////////
102

103     /**
104      * <p>
105      * Answer true to indicate that this resource is an RDF property.
106      * </p>
107      *
108      * @return True.
109      */

110     public boolean isProperty() {
111         return true;
112     }
113
114
115     /**
116      * @see Property#getOrdinal()
117      */

118     public int getOrdinal() {
119         return ((Property) as( Property.class )).getOrdinal();
120     }
121
122
123     // subPropertyOf
124

125     /**
126      * <p>Assert that this property is sub-property of the given property. Any existing
127      * statements for <code>subPropertyOf</code> will be removed.</p>
128      * @param prop The property that this property is a sub-property of
129      * @exception OntProfileException If the {@link Profile#SUB_PROPERTY_OF()} property is not supported in the current language profile.
130      */

131     public void setSuperProperty( Property prop ) {
132         setPropertyValue( getProfile().SUB_PROPERTY_OF(), "SUB_PROPERTY_OF", prop );
133     }
134
135     /**
136      * <p>Add a super-property of this property.</p>
137      * @param prop A property that is a super-property of this property.
138      * @exception OntProfileException If the {@link Profile#SUB_PROPERTY_OF()} property is not supported in the current language profile.
139      */

140     public void addSuperProperty( Property prop ) {
141         addPropertyValue( getProfile().SUB_PROPERTY_OF(), "SUB_PROPERTY_OF", prop );
142     }
143
144     /**
145      * <p>Answer a property that is the super-property of this property. If there is
146      * more than one such property, an arbitrary selection is made.</p>
147      * @return A super-property of this property
148      * @exception OntProfileException If the {@link Profile#SUB_PROPERTY_OF()} property is not supported in the current language profile.
149      */

150     public OntProperty getSuperProperty() {
151         return objectAsProperty( getProfile().SUB_PROPERTY_OF(), "SUB_PROPERTY_OF" );
152     }
153
154     /**
155      * <p>Answer an iterator over all of the properties that are declared to be super-properties of
156      * this property. Each elemeent of the iterator will be an {@link OntProperty}.</p>
157      * @return An iterator over the super-properties of this property.
158      * @exception OntProfileException If the {@link Profile#SUB_PROPERTY_OF()} property is not supported in the current language profile.
159      */

160     public ExtendedIterator listSuperProperties() {
161         return listSuperProperties( false );
162     }
163
164     /**
165      * <p>Answer an iterator over all of the properties that are declared to be super-properties of
166      * this property. Each elemeent of the iterator will be an {@link OntProperty}.</p>
167      * @param direct If true, only answer the direcly adjacent properties in the
168      * property hierarchy: i&#046;e&#046; eliminate any property for which there is a longer route
169      * to reach that child under the super-property relation.
170      * @return An iterator over the super-properties of this property.
171      * @exception OntProfileException If the {@link Profile#SUB_PROPERTY_OF()} property is not supported in the current language profile.
172      */

173     public ExtendedIterator listSuperProperties( boolean direct ) {
174         return listDirectPropertyValues( getProfile().SUB_PROPERTY_OF(), "SUB_PROPERTY_OF", OntProperty.class, getProfile().SUB_PROPERTY_OF(), direct, false );
175     }
176
177     /**
178      * <p>Answer true if the given property is a super-property of this property.</p>
179      * @param prop A property to test.
180      * @param direct If true, only consider the direcly adjacent properties in the
181      * property hierarchy
182      * @return True if the given property is a super-property of this property.
183      */

184     public boolean hasSuperProperty( Property prop, boolean direct ) {
185         return hasPropertyValue( getProfile().SUB_PROPERTY_OF(), "SUB_PROPERTY_OF", prop );
186     }
187
188     /**
189      * <p>Remove the given property from the super-properties of this property. If this statement
190      * is not true of the current model, nothing happens.</p>
191      * @param prop A property to be removed from the super-properties of this property
192      * @exception OntProfileException If the {@link Profile#SUB_PROPERTY_OF()} property is not supported in the current language profile.
193      */

194     public void removeSuperProperty( Property prop ) {
195         removePropertyValue( getProfile().SUB_PROPERTY_OF(), "SUB_PROPERTY_OF", prop );
196     }
197
198
199     /**
200      * <p>Assert that this property is super-property of the given property. Any existing
201      * statements for <code>subPropertyOf</code> on <code>prop</code> will be removed.</p>
202      * @param prop The property that is a sub-property of this property
203      * @exception OntProfileException If the {@link Profile#SUB_PROPERTY_OF()} property is not supported in the current language profile.
204      */

205     public void setSubProperty( Property prop ) {
206         // first we have to remove all of the inverse sub-prop links
207
checkProfile( getProfile().SUB_PROPERTY_OF(), "SUB_PROPERTY_OF" );
208         for (StmtIterator i = getModel().listStatements( null, getProfile().SUB_PROPERTY_OF(), this ); i.hasNext(); ) {
209             i.removeNext();
210         }
211
212         ((OntProperty) prop.as( OntProperty.class )).addSuperProperty( this );
213     }
214
215     /**
216      * <p>Add a sub-property of this property.</p>
217      * @param prop A property that is a sub-property of this property.
218      * @exception OntProfileException If the {@link Profile#SUB_PROPERTY_OF()} property is not supported in the current language profile.
219      */

220     public void addSubProperty( Property prop ) {
221         ((OntProperty) prop.as( OntProperty.class )).addSuperProperty( this );
222     }
223
224     /**
225      * <p>Answer a property that is the sub-property of this property. If there is
226      * more than one such property, an arbitrary selection is made.</p>
227      * @return A sub-property of this property
228      * @exception OntProfileException If the {@link Profile#SUB_PROPERTY_OF()} property is not supported in the current language profile.
229      */

230     public OntProperty getSubProperty() {
231         checkProfile( getProfile().SUB_PROPERTY_OF(), "SUB_PROPERTY_OF" );
232         return (OntProperty) getModel().listStatements( null, getProfile().SUB_PROPERTY_OF(), this )
233                              .nextStatement()
234                              .getSubject()
235                              .as( OntProperty.class );
236     }
237
238     /**
239      * <p>Answer an iterator over all of the properties that are declared to be sub-properties of
240      * this property. Each element of the iterator will be an {@link OntProperty}.</p>
241      * @return An iterator over the sub-properties of this property.
242      * @exception OntProfileException If the {@link Profile#SUB_PROPERTY_OF()} property is not supported in the current language profile.
243      */

244     public ExtendedIterator listSubProperties() {
245         return listSubProperties( false );
246     }
247
248     /**
249      * <p>Answer an iterator over all of the properties that are declared to be sub-properties of
250      * this property. Each element of the iterator will be an {@link OntProperty}.</p>
251      * @param direct If true, only answer the direcly adjacent properties in the
252      * property hierarchy: i&#046;e&#046; eliminate any property for which there is a longer route
253      * to reach that child under the sub-property relation.
254      * @return An iterator over the sub-properties of this property.
255      * @exception OntProfileException If the {@link Profile#SUB_PROPERTY_OF()} property is not supported in the current language profile.
256      */

257     public ExtendedIterator listSubProperties( boolean direct ) {
258         return listDirectPropertyValues( getProfile().SUB_PROPERTY_OF(), "SUB_PROPERTY_OF", OntProperty.class, getProfile().SUB_PROPERTY_OF(), direct, true );
259     }
260
261     /**
262      * <p>Answer true if the given property is a sub-property of this property.</p>
263      * @param prop A property to test.
264      * @param direct If true, only consider the direcly adjacent properties in the
265      * property hierarchy
266      * @return True if the given property is a sub-property of this property.
267      */

268     public boolean hasSubProperty( Property prop, boolean direct ) {
269         return ((OntProperty) prop.as( OntProperty.class )).hasSuperProperty( this, direct );
270     }
271
272     /**
273      * <p>Remove the given property from the sub-properties of this property. If this statement
274      * is not true of the current model, nothing happens.</p>
275      * @param prop A property to be removed from the sub-properties of this property
276      * @exception OntProfileException If the {@link Profile#SUB_PROPERTY_OF()} property is not supported in the current language profile.
277      */

278     public void removeSubProperty( Property prop ) {
279         ((OntProperty) prop.as( OntProperty.class )).removeSuperProperty( this );
280     }
281
282     // domain
283

284     /**
285      * <p>Assert that the given resource represents the class of individuals that form the
286      * domain of this property. Any existing <code>domain</code> statements for this property are removed.</p>
287      * @param res The resource that represents the domain class for this property.
288      * @exception OntProfileException If the {@link Profile#DOMAIN()} property is not supported in the current language profile.
289      */

290     public void setDomain( Resource res ) {
291         setPropertyValue( getProfile().DOMAIN(), "DOMAIN", res );
292     }
293
294     /**
295      * <p>Add a resource representing the domain of this property.</p>
296      * @param res A resource that represents a domain class for this property.
297      * @exception OntProfileException If the {@link Profile#DOMAIN()} property is not supported in the current language profile.
298      */

299     public void addDomain( Resource res ) {
300         addPropertyValue( getProfile().DOMAIN(), "DOMAIN", res );
301     }
302
303     /**
304      * <p>Answer a resource that represents the domain class of this property. If there is
305      * more than one such resource, an arbitrary selection is made.</p>
306      * @return An resource representing the class that forms the domain of this property
307      * @exception OntProfileException If the {@link Profile#DOMAIN()} property is not supported in the current language profile.
308      */

309     public OntResource getDomain() {
310         return objectAsResource( getProfile().DOMAIN(), "DOMAIN" );
311     }
312
313     /**
314      * <p>Answer an iterator over all of the declared domain classes of this property.
315      * Each elemeent of the iterator will be an {@link OntResource}.</p>
316      * @return An iterator over the classes that form the domain of this property.
317      * @exception OntProfileException If the {@link Profile#DOMAIN()} property is not supported in the current language profile.
318      */

319     public ExtendedIterator listDomain() {
320         return listAs( getProfile().DOMAIN(), "DOMAIN", OntClass.class );
321     }
322
323     /**
324      * <p>Answer true if the given resource a class specifying the domain of this property.</p>
325      * @param res A resource representing a class
326      * @return True if the given resource is one of the domain classes of this property.
327      */

328     public boolean hasDomain( Resource res ) {
329         return hasPropertyValue( getProfile().DOMAIN(), "DOMAIN", res );
330     }
331
332     /**
333      * <p>Remove the given class from the stated domain(s) of this property. If this statement
334      * is not true of the current model, nothing happens.</p>
335      * @param cls A class to be removed from the declared domain(s) of this property
336      * @exception OntProfileException If the {@link Profile#DOMAIN()} property is not supported in the current language profile.
337      */

338     public void removeDomain( Resource cls ) {
339         removePropertyValue( getProfile().DOMAIN(), "DOMAIN", cls );
340     }
341
342
343     // range
344

345     /**
346      * <p>Assert that the given resource represents the class of individuals that form the
347      * range of this property. Any existing <code>range</code> statements for this property are removed.</p>
348      * @param res The resource that represents the range class for this property.
349      * @exception OntProfileException If the {@link Profile#RANGE()} property is not supported in the current language profile.
350      */

351     public void setRange( Resource res ) {
352         setPropertyValue( getProfile().RANGE(), "RANGE", res );
353     }
354
355     /**
356      * <p>Add a resource representing the range of this property.</p>
357      * @param res A resource that represents a range class for this property.
358      * @exception OntProfileException If the {@link Profile#RANGE()} property is not supported in the current language profile.
359      */

360     public void addRange( Resource res ) {
361         addPropertyValue( getProfile().RANGE(), "RANGE", res );
362     }
363
364     /**
365      * <p>Answer a resource that represents the range class of this property. If there is
366      * more than one such resource, an arbitrary selection is made.</p>
367      * @return An resource representing the class that forms the range of this property
368      * @exception OntProfileException If the {@link Profile#RANGE()} property is not supported in the current language profile.
369      */

370     public OntResource getRange() {
371         return objectAsResource( getProfile().RANGE(), "RANGE" );
372     }
373
374     /**
375      * <p>Answer an iterator over all of the declared range classes of this property.
376      * Each elemeent of the iterator will be an {@link OntResource}.</p>
377      * @return An iterator over the classes that form the range of this property.
378      * @exception OntProfileException If the {@link Profile#RANGE()} property is not supported in the current language profile.
379      */

380     public ExtendedIterator listRange() {
381         return listAs( getProfile().RANGE(), "RANGE", OntClass.class );
382     }
383
384     /**
385      * <p>Answer true if the given resource a class specifying the range of this property.</p>
386      * @param res A resource representing a class
387      * @return True if the given resource is one of the range classes of this property.
388      */

389     public boolean hasRange( Resource res ) {
390         return hasPropertyValue( getProfile().RANGE(), "RANGE", res );
391     }
392
393     /**
394      * <p>Remove the given class from the stated range(s) of this property. If this statement
395      * is not true of the current model, nothing happens.</p>
396      * @param cls A class to be removed from the declared range(s) of this property
397      * @exception OntProfileException If the {@link Profile#RANGE()} property is not supported in the current language profile.
398      */

399     public void removeRange( Resource cls ) {
400         removePropertyValue( getProfile().RANGE(), "RANGE", cls );
401     }
402
403
404     // relationships between properties
405

406     // equivalentProperty
407

408     /**
409      * <p>Assert that the given property is equivalent to this property. Any existing
410      * statements for <code>equivalentProperty</code> will be removed.</p>
411      * @param prop The property that this property is a equivalent to.
412      * @exception OntProfileException If the {@link Profile#EQUIVALENT_PROPERTY()} property is not supported in the current language profile.
413      */

414     public void setEquivalentProperty( Property prop ) {
415         setPropertyValue( getProfile().EQUIVALENT_PROPERTY(), "EQUIVALENT_PROPERTY", prop );
416     }
417
418     /**
419      * <p>Add a property that is equivalent to this property.</p>
420      * @param prop A property that is equivalent to this property.
421      * @exception OntProfileException If the {@link Profile#EQUIVALENT_PROPERTY()} property is not supported in the current language profile.
422      */

423     public void addEquivalentProperty( Property prop ) {
424         addPropertyValue( getProfile().EQUIVALENT_PROPERTY(), "EQUIVALENT_PROPERTY", prop );
425     }
426
427     /**
428      * <p>Answer a property that is equivalent to this property. If there is
429      * more than one such property, an arbitrary selection is made.</p>
430      * @return A property equivalent to this property
431      * @exception OntProfileException If the {@link Profile#EQUIVALENT_PROPERTY()} property is not supported in the current language profile.
432      */

433     public OntProperty getEquivalentProperty() {
434         return objectAsProperty( getProfile().EQUIVALENT_PROPERTY(), "EQUIVALENT_PROPERTY" );
435     }
436
437     /**
438      * <p>Answer an iterator over all of the properties that are declared to be equivalent properties to
439      * this property. Each elemeent of the iterator will be an {@link OntProperty}.</p>
440      * @return An iterator over the properties equivalent to this property.
441      * @exception OntProfileException If the {@link Profile#EQUIVALENT_PROPERTY()} property is not supported in the current language profile.
442      */

443     public ExtendedIterator listEquivalentProperties() {
444         return listAs( getProfile().EQUIVALENT_PROPERTY(), "EQUIVALENT_PROPERTY", OntProperty.class );
445     }
446
447     /**
448      * <p>Answer true if the given property is equivalent to this property.</p>
449      * @param prop A property to test for
450      * @return True if the given property is equivalent to this property.
451      */

452     public boolean hasEquivalentProperty( Property prop ) {
453         return hasPropertyValue( getProfile().EQUIVALENT_PROPERTY(), "EQUIVALENT_PROPERTY", prop );
454     }
455
456     /**
457      * <p>Remove the statement that this property and the given property are
458      * equivalent. If this statement
459      * is not true of the current model, nothing happens.</p>
460      * @param prop A property that may be declared to be equivalent to this property
461      * @exception OntProfileException If the {@link Profile#EQUIVALENT_PROPERTY()} property is not supported in the current language profile.
462      */

463     public void removeEquivalentProperty( Property prop ) {
464         removePropertyValue( getProfile().EQUIVALENT_PROPERTY(), "EQUIVALENT_PROPERTY", prop );
465     }
466
467     // inverseProperty
468

469     /**
470      * <p>Assert that the given property is the inverse of this property. Any existing
471      * statements for <code>inverseOf</code> will be removed.</p>
472      * @param prop The property that this property is a inverse to.
473      * @exception OntProfileException If the {@link Profile#INVERSE_OF()} property is not supported in the current language profile.
474      */

475     public void setInverseOf( Property prop ) {
476         setPropertyValue( getProfile().INVERSE_OF(), "INVERSE_OF", prop );
477     }
478
479     /**
480      * <p>Add a property that is the inverse of this property.</p>
481      * @param prop A property that is the inverse of this property.
482      * @exception OntProfileException If the {@link Profile#INVERSE_OF()} property is not supported in the current language profile.
483      */

484     public void addInverseOf( Property prop ) {
485         addPropertyValue( getProfile().INVERSE_OF(), "INVERSE_OF", prop );
486     }
487
488     /**
489      * <p>Answer a property that is an inverse of this property. If there is
490      * more than one such property, an arbitrary selection is made.</p>
491      * @return A property inverse to this property
492      * @exception OntProfileException If the {@link Profile#INVERSE_OF()} property is not supported in the current language profile.
493      */

494     public OntProperty getInverseOf() {
495         return objectAsProperty( getProfile().INVERSE_OF(), "INVERSE_OF" );
496     }
497
498     /**
499      * <p>Answer an iterator over all of the properties that are declared to be inverse properties of
500      * this property. Each elemeent of the iterator will be an {@link OntProperty}.</p>
501      * @return An iterator over the properties inverse to this property.
502      * @exception OntProfileException If the {@link Profile#INVERSE_OF()} property is not supported in the current language profile.
503      */

504     public ExtendedIterator listInverseOf() {
505         return listAs( getProfile().INVERSE_OF(), "INVERSE_OF", OntProperty.class );
506     }
507
508     /**
509      * <p>Answer true if this property is the inverse of the given property.</p>
510      * @param prop A property to test for
511      * @return True if the this property is the inverse of the the given property.
512      */

513     public boolean isInverseOf( Property prop ) {
514         return hasPropertyValue( getProfile().INVERSE_OF(), "INVERSE_OF", prop );
515     }
516
517     /**
518      * <p>Remove the statement that this property is the inverse of the given property. If this statement
519      * is not true of the current model, nothing happens.</p>
520      * @param prop A property that may be declared to be inverse to this property
521      * @exception OntProfileException If the {@link Profile#INVERSE_OF()} property is not supported in the current language profile.
522      */

523     public void removeInverseProperty( Property prop ) {
524         removePropertyValue( getProfile().INVERSE_OF(), "INVERSE_OF", prop );
525     }
526
527
528     /**
529      * <p>Answer a view of this property as a functional property</p>
530      * @return This property, but viewed as a FunctionalProperty node
531      * @exception ConversionException if the resource cannot be converted to a functional property
532      * given the lanuage profile and the current state of the underlying model.
533      */

534     public FunctionalProperty asFunctionalProperty() {
535         return (FunctionalProperty) as( FunctionalProperty.class );
536     }
537
538     /**
539      * <p>Answer a view of this property as a datatype property</p>
540      * @return This property, but viewed as a DatatypeProperty node
541      * @exception ConversionException if the resource cannot be converted to a datatype property
542      * given the lanuage profile and the current state of the underlying model.
543      */

544     public DatatypeProperty asDatatypeProperty() {
545         return (DatatypeProperty) as( DatatypeProperty.class );
546     }
547
548     /**
549      * <p>Answer a view of this property as an object property</p>
550      * @return This property, but viewed as an ObjectProperty node
551      * @exception ConversionException if the resource cannot be converted to an object property
552      * given the lanuage profile and the current state of the underlying model.
553      */

554     public ObjectProperty asObjectProperty() {
555         return (ObjectProperty) as( ObjectProperty.class );
556     }
557
558     /**
559      * <p>Answer a view of this property as a transitive property</p>
560      * @return This property, but viewed as a TransitiveProperty node
561      * @exception ConversionException if the resource cannot be converted to a transitive property
562      * given the lanuage profile and the current state of the underlying model.
563      */

564     public TransitiveProperty asTransitiveProperty() {
565         return (TransitiveProperty) as( TransitiveProperty.class );
566     }
567
568     /**
569      * <p>Answer a view of this property as an inverse functional property</p>
570      * @return This property, but viewed as an InverseFunctionalProperty node
571      * @exception ConversionException if the resource cannot be converted to an inverse functional property
572      * given the lanuage profile and the current state of the underlying model.
573      */

574     public InverseFunctionalProperty asInverseFunctionalProperty() {
575         return (InverseFunctionalProperty) as( InverseFunctionalProperty.class );
576     }
577
578     /**
579      * <p>Answer a view of this property as a symmetric property</p>
580      * @return This property, but viewed as a SymmetricProperty node
581      * @exception ConversionException if the resource cannot be converted to a symmetric property
582      * given the lanuage profile and the current state of the underlying model.
583      */

584     public SymmetricProperty asSymmetricProperty() {
585         return (SymmetricProperty) as( SymmetricProperty.class );
586     }
587
588     // conversion functions
589

590     /**
591      * <p>Answer a facet of this property as a functional property, adding additional information to the model if necessary.</p>
592      * @return This property, but converted to a FunctionalProperty facet
593      */

594     public FunctionalProperty convertToFunctionalProperty() {
595         return (FunctionalProperty) convertToType( getProfile().FUNCTIONAL_PROPERTY(), "FUNCTIONAL_PROPERTY", FunctionalProperty.class );
596     }
597
598     /**
599      * <p>Answer a facet of this property as a datatype property, adding additional information to the model if necessary.</p>
600      * @return This property, but converted to a DatatypeProperty facet
601      */

602     public DatatypeProperty convertToDatatypeProperty() {
603         return (DatatypeProperty) convertToType( getProfile().DATATYPE_PROPERTY(), "DATATYPE_PROPERTY", DatatypeProperty.class );
604     }
605
606     /**
607      * <p>Answer a facet of this property as an object property, adding additional information to the model if necessary.</p>
608      * @return This property, but converted to an ObjectProperty facet
609      */

610     public ObjectProperty convertToObjectProperty() {
611         return (ObjectProperty) convertToType( getProfile().OBJECT_PROPERTY(), "OBJECT_PROPERTY", ObjectProperty.class );
612     }
613
614     /**
615      * <p>Answer a facet of this property as a transitive property, adding additional information to the model if necessary.</p>
616      * @return This property, but converted to a TransitiveProperty facet
617      */

618     public TransitiveProperty convertToTransitiveProperty() {
619         return (TransitiveProperty) convertToType( getProfile().TRANSITIVE_PROPERTY(), "TRANSITIVE_PROPERTY", TransitiveProperty.class );
620     }
621
622     /**
623      * <p>Answer a facet of this property as an inverse functional property, adding additional information to the model if necessary.</p>
624      * @return This property, but converted to an InverseFunctionalProperty facet
625      */

626     public InverseFunctionalProperty convertToInverseFunctionalProperty() {
627         return (InverseFunctionalProperty) convertToType( getProfile().INVERSE_FUNCTIONAL_PROPERTY(), "INVERSE_FUNCTIONAL_PROPERTY", InverseFunctionalProperty.class );
628     }
629
630     /**
631      * <p>Answer a facet of this property as a symmetric property, adding additional information to the model if necessary.</p>
632      * @return This property, but converted to a SymmetricProperty facet
633      */

634     public SymmetricProperty convertToSymmetricProperty() {
635         return (SymmetricProperty) convertToType( getProfile().SYMMETRIC_PROPERTY(), "SYMMETRIC_PROPERTY", SymmetricProperty.class );
636     }
637
638
639     // tests on property sub-types
640

641     /**
642      * <p>Answer true if this property is a functional property</p>
643      * @return True if this this property has an <code>rdf:type</code> that defines it as a functional property.
644      */

645     public boolean isFunctionalProperty() {
646         return hasRDFType( getProfile().FUNCTIONAL_PROPERTY(), "FUNCTIONAL_PROPERTY", false );
647     }
648
649     /**
650      * <p>Answer true if this property is a datatype property</p>
651      * @return True if this this property has an <code>rdf:type</code> that defines it as a datatype property.
652      */

653     public boolean isDatatypeProperty() {
654         return hasRDFType( getProfile().DATATYPE_PROPERTY(), "DATATYPE_PROPERTY", false );
655     }
656
657     /**
658      * <p>Answer true if this property is an object property</p>
659      * @return True if this this property has an <code>rdf:type</code> that defines it as an object property.
660      */

661     public boolean isObjectProperty() {
662         return hasRDFType( getProfile().OBJECT_PROPERTY(), "OBJECT_PROPERTY", false );
663     }
664
665     /**
666      * <p>Answer true if this property is a transitive property</p>
667      * @return True if this this property has an <code>rdf:type</code> that defines it as a transitive property.
668      */

669     public boolean isTransitiveProperty() {
670         return hasRDFType( getProfile().TRANSITIVE_PROPERTY(), "TRANSITIVE_PROPERTY", false );
671     }
672
673     /**
674      * <p>Answer true if this property is an inverse functional property</p>
675      * @return True if this this property has an <code>rdf:type</code> that defines it as an inverse functional property.
676      */

677     public boolean isInverseFunctionalProperty() {
678         return hasRDFType( getProfile().INVERSE_FUNCTIONAL_PROPERTY(), "INVERSE_FUNCTIONAL_PROPERTY", false );
679     }
680
681     /**
682      * <p>Answer true if this property is a symmetric property</p>
683      * @return True if this this property has an <code>rdf:type</code> that defines it as a symmetric property.
684      */

685     public boolean isSymmetricProperty() {
686         return hasRDFType( getProfile().SYMMETRIC_PROPERTY(), "SYMMETRIC_PROPERTY", false );
687     }
688
689
690     /**
691      * <p>Answer the property that is the inverse of this property. If no such property is defined,
692      * return null. If more than one inverse is defined, return an abritrary selection.</p>
693      * @return The property that is the inverse of this property, or null.
694      */

695     public OntProperty getInverse() {
696         ExtendedIterator i = listInverse();
697         OntProperty p = i.hasNext() ? ((OntProperty) i.next()) : null;
698         i.close();
699
700         return p;
701     }
702
703     /**
704      * <p>Answer an iterator over the properties that are defined to be inverses of this property.</p>
705      * @return An iterator over the properties that declare themselves the <code>inverseOf</code> this property.
706      */

707     public ExtendedIterator listInverse() {
708         return getModel().listStatements( null, getProfile().INVERSE_OF(), this ).mapWith( new SubjectAsMapper( OntProperty.class ) );
709     }
710
711     /**
712      * <p>Answer true if there is at least one inverse property for this property.</p>
713      * @return True if property has an inverse.
714      */

715     public boolean hasInverse() {
716         ExtendedIterator i = listInverse();
717         boolean hasInv = i.hasNext();
718         i.close();
719
720         return hasInv;
721     }
722
723
724     /**
725      * <p>Answer an iterator of all of the classes in this ontology, such
726      * that each returned class has this property as one of its
727      * properties in {@link OntClass#listDeclaredProperties()}. This
728      * simulates a frame-like view of properties and classes; for more
729      * details see the <a HREF="../../../../../../how-to/rdf-frames.html">
730      * RDF frames howto</a>.</p>
731      * @return An iterator of the classes having this property as one
732      * of their declared properties
733      */

734     public ExtendedIterator listDeclaringClasses() {
735         return listDeclaringClasses( false );
736     }
737
738     /**
739      * <p>Answer an iterator of all of the classes in this ontology, such
740      * that each returned class has this property as one of its
741      * properties in {@link OntClass#listDeclaredProperties(boolean)}. This
742      * simulates a frame-like view of properties and classes; for more
743      * details see the <a HREF="../../../../../../how-to/rdf-frames.html">
744      * RDF frames howto</a>.</p>
745      * @param direct If true, use only </em>direct</em> associations between classes
746      * and properties
747      * @return An iterator of the classes having this property as one
748      * of their declared properties
749      */

750     public ExtendedIterator listDeclaringClasses( boolean direct ) {
751         // first list the candidate classes, which will also help us
752
// work out whether this is a "global" property or not
753
Set cands = new HashSet();
754         for (Iterator i = listDomain(); i.hasNext(); ) {
755             // the candidates include this class and it sub-classes
756
List q = new ArrayList();
757             q.add( i.next() );
758
759             while (!q.isEmpty()) {
760                 OntClass c = (OntClass) q.remove( 0 );
761
762                 if (!c.isOntLanguageTerm() && !cands.contains( c )) {
763                     // a new value that is not just a term from OWL or RDFS
764
cands.add( c );
765                     for (Iterator j = c.listSubClasses(); j.hasNext(); ) {
766                         q.add( j.next() );
767                     }
768                 }
769             }
770         }
771
772         if (cands.isEmpty()) {
773             // no declared non-global domain, so this is a global prop
774
if (!direct) {
775                 // in the non-direct case, global properties appear in the ldp
776
// of all classes, but we ignore the built-in classes
777
return ((OntModel) getModel()).listClasses()
778                                               .filterDrop( new Filter() {
779                                                 public boolean accept( Object JavaDoc o ) {
780                                                     return ((OntClass) o).isOntLanguageTerm();
781                                                 }} );
782             }
783             else {
784                 // in the direct case, global properties only attach to the
785
// local hierarchy roots
786
return ((OntModel) getModel()).listHierarchyRootClasses();
787             }
788         }
789         else {
790             // not a global property
791
// pick out classes from the domain for which this is a declared prop
792
return WrappedIterator.create( cands.iterator() )
793                                   .filterKeep( new FilterDeclaringClass( this, direct ));
794         }
795     }
796
797
798
799     // Internal implementation methods
800
//////////////////////////////////
801

802     /**
803      * <p>Answer a property that is attached to the given model, which will either
804      * be this property or a new property object with the same URI in the given
805      * model. If the given model is an ontology model, make the new property object
806      * an ontproperty.</p>
807      * @param m A model
808      * @return A property equal to this property that is attached to m.
809      */

810     public RDFNode inModel( Model m ) {
811         return (getModel() == m) ? this : m.createProperty( getURI() );
812     }
813
814
815
816     //==============================================================================
817
// Inner class definitions
818
//==============================================================================
819

820     /**
821      * <p>Filter that accepts classes which have the given property as one of
822      * their declared properties.</p>
823      */

824     private class FilterDeclaringClass
825         implements Filter
826     {
827         private boolean m_direct;
828         private Property m_prop;
829
830         private FilterDeclaringClass( Property prop, boolean direct ) {
831             m_prop = prop;
832             m_direct = direct;
833         }
834
835         public boolean accept( Object JavaDoc o ) {
836             return ((OntClass) o).hasDeclaredProperty( m_prop, m_direct );
837         }
838
839     }
840 }
841
842
843 /*
844     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
845     All rights reserved.
846
847     Redistribution and use in source and binary forms, with or without
848     modification, are permitted provided that the following conditions
849     are met:
850
851     1. Redistributions of source code must retain the above copyright
852        notice, this list of conditions and the following disclaimer.
853
854     2. Redistributions in binary form must reproduce the above copyright
855        notice, this list of conditions and the following disclaimer in the
856        documentation and/or other materials provided with the distribution.
857
858     3. The name of the author may not be used to endorse or promote products
859        derived from this software without specific prior written permission.
860
861     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
862     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
863     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
864     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
865     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
866     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
867     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
868     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
869     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
870     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
871 */

872
873
Popular Tags