KickJava   Java API By Example, From Geeks To Geeks.

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


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: RestrictionImpl.java,v $
10  * Revision $Revision: 1.16 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2005/02/21 12:06:53 $
14  * by $Author: andy_seaborne $
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
26 // Imports
27
///////////////
28
import com.hp.hpl.jena.graph.*;
29 import com.hp.hpl.jena.enhanced.*;
30 import com.hp.hpl.jena.ontology.*;
31 import com.hp.hpl.jena.rdf.model.*;
32
33
34 /**
35  * <p>
36  * Implementation of the ontology abstraction representing restrictions.
37  * </p>
38  *
39  * @author Ian Dickinson, HP Labs
40  * (<a HREF="mailto:Ian.Dickinson@hp.com" >email</a>)
41  * @version CVS $Id: RestrictionImpl.java,v 1.16 2005/02/21 12:06:53 andy_seaborne Exp $
42  */

43 public class RestrictionImpl
44     extends OntClassImpl
45     implements Restriction
46 {
47     // Constants
48
//////////////////////////////////
49

50     // Static variables
51
//////////////////////////////////
52

53     /**
54      * A factory for generating Restriction facets from nodes in enhanced graphs.
55      * Note: should not be invoked directly by user code: use
56      * {@link com.hp.hpl.jena.rdf.model.RDFNode#as as()} instead.
57      */

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

79     // Constructors
80
//////////////////////////////////
81

82     /**
83      * <p>
84      * Construct a restriction node represented by the given node in the given graph.
85      * </p>
86      *
87      * @param n The node that represents the resource
88      * @param g The enh graph that contains n
89      */

90     public RestrictionImpl( Node n, EnhGraph g ) {
91         super( n, g );
92     }
93
94
95     // External signature methods
96
//////////////////////////////////
97

98     // onProperty
99

100     /**
101      * <p>Assert that the property that this restriction applies to is the given property. Any existing
102      * statements for <code>onProperty</code> will be removed.</p>
103      * @param prop The property that this restriction applies to
104      * @exception OntProfileException If the {@link Profile#ON_PROPERTY()} property is not supported in the current language profile.
105      */

106     public void setOnProperty( Property prop ) {
107         setPropertyValue( getProfile().ON_PROPERTY(), "ON_PROPERTY", prop );
108     }
109
110     /**
111      * <p>Answer the property that this property restriction applies to. If there is
112      * more than one such resource, an arbitrary selection is made (though well-defined property restrictions
113      * should not have more than one <code>onProperty</code> statement.</p>
114      * @return The property that this property restriction applies to
115      * @exception OntProfileException If the {@link Profile#ON_PROPERTY()} property is not supported in the current language profile.
116      */

117     public OntProperty getOnProperty() {
118         return (OntProperty) objectAs( getProfile().ON_PROPERTY(), "ON_PROPERTY", OntProperty.class );
119     }
120
121     /**
122      * <p>Answer true if this restriction is a property restriction on the given property.</p>
123      * @param prop A property to test against
124      * @return True if this restriction is a restriction on <code>prop</code>
125      * @exception OntProfileException If the {@link Profile#ON_PROPERTY()} property is not supported in the current language profile.
126      */

127     public boolean onProperty( Property prop ) {
128         return hasPropertyValue( getProfile().ON_PROPERTY(), "ON_PROPERTY", prop );
129     }
130     
131     /**
132      * <p>Remove the given property as the property that this restriction applies to. If this statement
133      * is not true of the current model, nothing happens.</p>
134      * @param prop The property to be removed as a the property that this restriction applies to
135      */

136     public void removeOnProperty( Property prop ) {
137         removePropertyValue( getProfile().ON_PROPERTY(), "ON_PROPERTY", prop );
138     }
139     
140
141     /**
142      * <p>Answer a view of this restriction as an all values from expression</p>
143      * @return This class, but viewed as an AllValuesFromRestriction node
144      * @exception ConversionException if the class cannot be converted to an all values from restriction
145      * given the lanuage profile and the current state of the underlying model.
146      */

147     public AllValuesFromRestriction asAllValuesFromRestriction() {
148         return (AllValuesFromRestriction) as( AllValuesFromRestriction.class );
149     }
150          
151     /**
152      * <p>Answer a view of this restriction as a some values from expression</p>
153      * @return This class, but viewed as a SomeValuesFromRestriction node
154      * @exception ConversionException if the class cannot be converted to an all values from restriction
155      * given the lanuage profile and the current state of the underlying model.
156      */

157     public SomeValuesFromRestriction asSomeValuesFromRestriction() {
158         return (SomeValuesFromRestriction) as( SomeValuesFromRestriction.class );
159     }
160          
161     /**
162      * <p>Answer a view of this restriction as a has value expression</p>
163      * @return This class, but viewed as a HasValueRestriction node
164      * @exception ConversionException if the class cannot be converted to a has value restriction
165      * given the lanuage profile and the current state of the underlying model.
166      */

167     public HasValueRestriction asHasValueRestriction() {
168         return (HasValueRestriction) as( HasValueRestriction.class );
169     }
170          
171     /**
172      * <p>Answer a view of this restriction as a cardinality restriction class expression</p>
173      * @return This class, but viewed as a CardinalityRestriction node
174      * @exception ConversionException if the class cannot be converted to a cardinality restriction
175      * given the lanuage profile and the current state of the underlying model.
176      */

177     public CardinalityRestriction asCardinalityRestriction() {
178         return (CardinalityRestriction) as( CardinalityRestriction.class );
179     }
180
181     /**
182      * <p>Answer a view of this restriction as a min cardinality restriction class expression</p>
183      * @return This class, but viewed as a MinCardinalityRestriction node
184      * @exception ConversionException if the class cannot be converted to a min cardinality restriction
185      * given the lanuage profile and the current state of the underlying model.
186      */

187     public MinCardinalityRestriction asMinCardinalityRestriction() {
188         return (MinCardinalityRestriction) as( MinCardinalityRestriction.class );
189     }
190
191     /**
192      * <p>Answer a view of this restriction as a max cardinality restriction class expression</p>
193      * @return This class, but viewed as a MaxCardinalityRestriction node
194      * @exception ConversionException if the class cannot be converted to a max cardinality restriction
195      * given the lanuage profile and the current state of the underlying model.
196      */

197     public MaxCardinalityRestriction asMaxCardinalityRestriction() {
198         return (MaxCardinalityRestriction) as( MaxCardinalityRestriction.class );
199     }
200
201
202     // type tests
203

204     /**
205      * <p>Answer true if this restriction is an all values from restriction</p>
206      * @return True if this is an allValuesFrom property restriction
207      * @exception ProfileException if {@link Profile#ALL_VALUES_FROM()} is not supported in the current profile
208      */

209     public boolean isAllValuesFromRestriction() {
210         checkProfile( getProfile().ALL_VALUES_FROM(), "ALL_VALUES_FROM" );
211         return hasProperty( getProfile().ALL_VALUES_FROM() );
212     }
213          
214     /**
215      * <p>Answer true if this restriction is a some values from restriction</p>
216      * @return True if this is a someValuesFrom property restriction
217      * @exception ProfileException if {@link Profile#SOME_VALUES_FROM()} is not supported in the current profile
218      */

219     public boolean isSomeValuesFromRestriction() {
220         checkProfile( getProfile().SOME_VALUES_FROM(), "SOME_VALUES_FROM" );
221         return hasProperty( getProfile().SOME_VALUES_FROM() );
222     }
223          
224     /**
225      * <p>Answer true if this restriction is a has value restriction</p>
226      * @return True if this is a hasValue property restriction
227      * @exception ProfileException if {@link Profile#HAS_VALUE()} is not supported in the current profile
228      */

229     public boolean isHasValueRestriction() {
230         checkProfile( getProfile().HAS_VALUE(), "HAS_VALUE" );
231         return hasProperty( getProfile().HAS_VALUE() );
232     }
233          
234     /**
235      * <p>Answer true if this restriction is a cardinality restriction (ie is a property restriction
236      * constructed with an <code>owl:cardinality</code> operator, or similar). This is not a test for
237      * a restriction that tests cardinalities in general.</p>
238      * @return True if this is a cardinality property restriction
239      * @exception ProfileException if {@link Profile#CARDINALITY()} is not supported in the current profile
240      */

241     public boolean isCardinalityRestriction() {
242         checkProfile( getProfile().CARDINALITY(), "CARDINALITY" );
243         return hasProperty( getProfile().CARDINALITY() );
244     }
245
246     /**
247      * <p>Answer true if this restriction is a min cardinality restriction (ie is a property restriction
248      * constructed with an <code>owl:minCardinality</code> operator, or similar). This is not a test for
249      * a restriction that tests cardinalities in general.</p>
250      * @return True if this is a minCardinality property restriction
251      * @exception ProfileException if {@link Profile#MIN_CARDINALITY()} is not supported in the current profile
252      */

253     public boolean isMinCardinalityRestriction() {
254         checkProfile( getProfile().MIN_CARDINALITY(), "MIN_CARDINALITY" );
255         return hasProperty( getProfile().MIN_CARDINALITY() );
256     }
257
258     /**
259      * <p>Answer true if this restriction is a max cardinality restriction (ie is a property restriction
260      * constructed with an <code>owl:maxCardinality</code> operator, or similar). This is not a test for
261      * a restriction that tests cardinalities in general.</p>
262      * @return True if this is a maxCardinality property restriction
263      * @exception ProfileException if {@link Profile#MAX_CARDINALITY()} is not supported in the current profile
264      */

265     public boolean isMaxCardinalityRestriction() {
266         checkProfile( getProfile().MAX_CARDINALITY(), "MAX_CARDINALITY" );
267         return hasProperty( getProfile().MAX_CARDINALITY() );
268     }
269
270
271     // conversions
272

273     /**
274      * <p>Convert this restriction to an all values from class expression.</p>
275      * @param cls The class to which all values of the restricted property must belong, to be in the
276      * extension of this restriction
277      * @return This class, but converted to a AllValuesFromRestriction class expression
278      * @exception ProfileException if {@link Profile#ALL_VALUES_FROM()} is not supported in the current profile
279      */

280     public AllValuesFromRestriction convertToAllValuesFromRestriction( Resource cls ) {
281         setPropertyValue( getProfile().ALL_VALUES_FROM(), "ALL_VALUES_FROM", cls );
282         return (AllValuesFromRestriction) as( AllValuesFromRestriction.class );
283     }
284          
285     /**
286      * <p>Convert this restriction to a some values from class expression</p>
287      * @param cls The class to which at least one value of the restricted property must belong, to be in the
288      * extension of this restriction
289      * @return This class, but converted to a SomeValuesFromRestriction node
290      * @exception ProfileException if {@link Profile#SOME_VALUES_FROM()} is not supported in the current profile
291      */

292     public SomeValuesFromRestriction convertToSomeValuesFromRestriction( Resource cls ) {
293         setPropertyValue( getProfile().SOME_VALUES_FROM(), "SOME_VALUES_FROM", cls );
294         return (SomeValuesFromRestriction) as( SomeValuesFromRestriction.class );
295     }
296          
297     /**
298      * <p>Convert this restriction to a has value class expression</p>
299      * @param value The value which the restricted property must have, for resource to be
300      * in the extension of this restriction
301      * @return This class, but converted to a HasValueRestriction
302      * @exception ProfileException if {@link Profile#HAS_VALUE()} is not supported in the current profile
303      */

304     public HasValueRestriction convertToHasValueRestriction( RDFNode value ) {
305         setPropertyValue( getProfile().HAS_VALUE(), "HAS_VALUE", value );
306         return (HasValueRestriction) as( HasValueRestriction.class );
307     }
308          
309     /**
310      * <p>Convert this restriction to a cardinality restriction class expression</p>
311      * @param cardinality The exact cardinality for the restricted property
312      * @return This class, but converted to a CardinalityRestriction node
313      * @exception ProfileException if {@link Profile#CARDINALITY()} is not supported in the current profile
314      */

315     public CardinalityRestriction convertToCardinalityRestriction( int cardinality ) {
316         setPropertyValue( getProfile().CARDINALITY(), "CARDINALITY", getModel().createTypedLiteral( cardinality ) );
317         return (CardinalityRestriction) as( CardinalityRestriction.class );
318     }
319
320     /**
321      * <p>Convert this restriction to a min cardinality restriction class expression</p>
322      * @param cardinality The minimum cardinality for the restricted property
323      * @return This class, but converted to a MinCardinalityRestriction node
324      * @exception ProfileException if {@link Profile#MIN_CARDINALITY()} is not supported in the current profile
325      */

326     public MinCardinalityRestriction convertToMinCardinalityRestriction( int cardinality ) {
327         setPropertyValue( getProfile().MIN_CARDINALITY(), "MIN_CARDINALITY", getModel().createTypedLiteral( cardinality ) );
328         return (MinCardinalityRestriction) as( MinCardinalityRestriction.class );
329     }
330
331     /**
332      * <p>Convert this restriction to a max cardinality restriction class expression</p>
333      * @param cardinality The maximum cardinality for the restricted property
334      * @return This class, but converted to a MaxCardinalityRestriction node
335      * @exception ProfileException if {@link Profile#MAX_CARDINALITY()} is not supported in the current profile
336      */

337     public MaxCardinalityRestriction convertToMaxCardinalityRestriction( int cardinality ) {
338         setPropertyValue( getProfile().MAX_CARDINALITY(), "MAX_CARDINALITY", getModel().createTypedLiteral( cardinality ) );
339         return (MaxCardinalityRestriction) as( MaxCardinalityRestriction.class );
340     }
341
342
343     // Internal implementation methods
344
//////////////////////////////////
345

346     //==============================================================================
347
// Inner class definitions
348
//==============================================================================
349

350 }
351
352
353 /*
354     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
355     All rights reserved.
356
357     Redistribution and use in source and binary forms, with or without
358     modification, are permitted provided that the following conditions
359     are met:
360
361     1. Redistributions of source code must retain the above copyright
362        notice, this list of conditions and the following disclaimer.
363
364     2. Redistributions in binary form must reproduce the above copyright
365        notice, this list of conditions and the following disclaimer in the
366        documentation and/or other materials provided with the distribution.
367
368     3. The name of the author may not be used to endorse or promote products
369        derived from this software without specific prior written permission.
370
371     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
372     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
373     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
374     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
375     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
376     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
377     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
378     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
379     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
380     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
381 */

382
383
384
Popular Tags