KickJava   Java API By Example, From Geeks To Geeks.

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


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 07-May-2003
9  * Filename $RCSfile: AllValuesFromRestrictionImpl.java,v $
10  * Revision $Revision: 1.9 $
11  * Release status $State: Exp $
12  *
13  * Last modified on $Date: 2005/02/21 12:06:15 $
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.enhanced.*;
29 import com.hp.hpl.jena.graph.*;
30 import com.hp.hpl.jena.ontology.*;
31 import com.hp.hpl.jena.rdf.model.Resource;
32
33
34 /**
35  * <p>
36  * Implementation of the allValuesFrom restriction abstraction.
37  * </p>
38  *
39  * @author Ian Dickinson, HP Labs
40  * (<a HREF="mailto:Ian.Dickinson@hp.com" >email</a>)
41  * @version CVS $Id: AllValuesFromRestrictionImpl.java,v 1.9 2005/02/21 12:06:15 andy_seaborne Exp $
42  */

43 public class AllValuesFromRestrictionImpl
44     extends RestrictionImpl
45     implements AllValuesFromRestriction
46 {
47     // Constants
48
//////////////////////////////////
49

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

53     /**
54      * A factory for generating AllValuesFromRestriction 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 AllValuesFromRestrictionImpl( n, eg );
62             }
63             else {
64                 throw new ConversionException( "Cannot convert node " + n + " to AllValuesFromRestriction");
65             }
66         }
67             
68         public boolean canWrap( Node node, EnhGraph eg ) {
69             // node will support being a AllValuesFromRestriction facet if it has rdf:type owl:Restriction or equivalent
70
// and the combination of owl:onProperty and owl:allValuesFrom (or equivalents)
71
Profile profile = (eg instanceof OntModel) ? ((OntModel) eg).getProfile() : null;
72             return (profile != null) && profile.isSupported( node, eg, AllValuesFromRestriction.class );
73         }
74     };
75
76
77     // Instance variables
78
//////////////////////////////////
79

80     // Constructors
81
//////////////////////////////////
82

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

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

99     // allValuesFrom
100

101     /**
102      * <p>Assert that this restriction restricts the property to have all values
103      * be members of the given class. Any existing statements for <code>allValuesFrom</code>
104      * will be removed.</p>
105      * @param cls The class that all values of the property must belong to
106      * @exception OntProfileException If the {@link Profile#ALL_VALUES_FROM()} property is not supported in the current language profile.
107      */

108     public void setAllValuesFrom( Resource cls ) {
109         setPropertyValue( getProfile().ALL_VALUES_FROM(), "ALL_VALUES_FROM", cls );
110     }
111
112     /**
113      * <p>Answer the resource characterising the constraint on all values of the restricted property. This may be
114      * a class, the URI of a concrete datatype, a DataRange object or the URI rdfs:Literal.</p>
115      * @return A resource, which will have been pre-converted to the appropriate Java value type
116      * ({@link OntClass} or {@link DataRange}) if appropriate.
117      * @exception OntProfileException If the {@link Profile#ALL_VALUES_FROM()} property is not supported in the current language profile.
118      */

119     public Resource getAllValuesFrom() {
120         checkProfile( getProfile().ALL_VALUES_FROM(), "ALL_VALUES_FROM" );
121         Resource r = (Resource) getRequiredProperty( getProfile().ALL_VALUES_FROM() ).getObject();
122         
123         boolean currentStrict = ((OntModel) getModel()).strictMode();
124         ((OntModel) getModel()).setStrictMode( true );
125         
126         try {
127             if (r.canAs( OntClass.class )) {
128                 // all values from a class
129
return (Resource) r.as( OntClass.class );
130             }
131             else if (r.canAs( DataRange.class )) {
132                 // all values from a given data range
133
return (Resource) r.as( DataRange.class );
134             }
135             else {
136                 // must be a datatype ID or rdfs:Literal
137
return r;
138             }
139         }
140         finally {
141             ((OntModel) getModel()).setStrictMode( currentStrict );
142         }
143     }
144
145     /**
146      * <p>Answer true if this property restriction has the given class as the class to which all
147      * values of the restricted property must belong.</p>
148      * @param cls A class to test
149      * @return True if the given class is the class to which all values must belong
150      * @exception OntProfileException If the {@link Profile#ALL_VALUES_FROM()} property is not supported in the current language profile.
151      */

152     public boolean hasAllValuesFrom( Resource cls ) {
153         return hasPropertyValue( getProfile().ALL_VALUES_FROM(), "ALL_VALUES_FROM", cls );
154     }
155     
156     /**
157      * <p>Remove the statement that this restriction has all values from the given class among
158      * the values for the restricted property. If this statement
159      * is not true of the current model, nothing happens.</p>
160      * @param cls A Resource the denotes the class to be removed from this restriction
161      */

162     public void removeAllValuesFrom( Resource cls ) {
163         removePropertyValue( getProfile().ALL_VALUES_FROM(), "ALL_VALUES_FROM", cls );
164     }
165     
166
167     // Internal implementation methods
168
//////////////////////////////////
169

170     //==============================================================================
171
// Inner class definitions
172
//==============================================================================
173

174 }
175
176
177 /*
178     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
179     All rights reserved.
180
181     Redistribution and use in source and binary forms, with or without
182     modification, are permitted provided that the following conditions
183     are met:
184
185     1. Redistributions of source code must retain the above copyright
186        notice, this list of conditions and the following disclaimer.
187
188     2. Redistributions in binary form must reproduce the above copyright
189        notice, this list of conditions and the following disclaimer in the
190        documentation and/or other materials provided with the distribution.
191
192     3. The name of the author may not be used to endorse or promote products
193        derived from this software without specific prior written permission.
194
195     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
196     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
197     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
198     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
199     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
200     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
201     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
202     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
203     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
204     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
205 */

206
207
208
Popular Tags