KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > ontology > daml > impl > PropertyAccessorImpl


1 /*****************************************************************************
2  * Source code information
3  * -----------------------
4  * Original author Ian Dickinson, HP Labs Bristol
5  * Author email Ian.Dickinson@hp.com
6  * Package Jena
7  * Created 26 Jan 2001
8  * Filename $RCSfile: PropertyAccessorImpl.java,v $
9  * Revision $Revision: 1.13 $
10  * Release status Preview-release $State: Exp $
11  *
12  * Last modified on $Date: 2005/02/21 12:05:30 $
13  * by $Author: andy_seaborne $
14  *
15  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
16  * (see footer for full conditions)
17  *****************************************************************************/

18
19 // Package
20
///////////////
21
package com.hp.hpl.jena.ontology.daml.impl;
22
23
24 // Imports
25
///////////////
26
import com.hp.hpl.jena.rdf.model.*;
27 import com.hp.hpl.jena.rdf.model.impl.NodeIteratorImpl;
28 import com.hp.hpl.jena.util.iterator.*;
29 import com.hp.hpl.jena.ontology.*;
30 import com.hp.hpl.jena.ontology.daml.*;
31
32
33
34 /**
35  * <p>Encapsulates the standard methods of modifying a property on a DAML value.</p>
36  *
37  * @author Ian Dickinson, HP Labs (<a HREF="mailto:Ian.Dickinson@hp.com">email</a>)
38  * @version CVS info: $Id: PropertyAccessorImpl.java,v 1.13 2005/02/21 12:05:30 andy_seaborne Exp $
39  */

40 public class PropertyAccessorImpl
41     implements PropertyAccessor
42 {
43     // Constants
44
//////////////////////////////////
45

46
47     // Static variables
48
//////////////////////////////////
49

50
51     // Instance variables
52
//////////////////////////////////
53

54     /** The property that this accessor works on */
55     protected Property m_property = null;
56
57     /** The underlying value that this is an accessor to */
58     protected OntResource m_val = null;
59
60
61
62     // Constructors
63
//////////////////////////////////
64

65     /**
66      * <p>Construct a new accessor for the given property, which takes
67      * the given value type as it range.</p>
68      *
69      * @param property The property that this accessor works on
70      * @param val The underlying DAML value that this is an accessor to
71      */

72     public PropertyAccessorImpl( Property property, OntResource val ) {
73         m_property = property;
74         m_val = val;
75     }
76
77
78
79     // External signature methods
80
//////////////////////////////////
81

82     /**
83      * <p>Answer the property that this accessor works on</p>
84      *
85      * @return A property
86      */

87     public Property getProperty() {
88         return m_property;
89     }
90
91
92     /**
93      * <p>Answer the number of values that the encapsulated property has in the
94      * RDF model.</p>
95      *
96      * @return The number RDF statements for this property in the model.
97      */

98     public int count() {
99         return m_val.getCardinality( getProperty() );
100     }
101
102
103     /**
104      * <p>Answer an iteration over the DAML values that this property has in the
105      * RDF model.</p>
106      * <p><strong>Note:</strong> In Jena 1, this method took a paramter <code>closed</code>,
107      * to control whether the transitive closure of the class and property hierarchies was
108      * considered. Computing these entailments is now handled by the reaoner attached to the
109      * DAML or Ontology model, and is not controlled by a method parameter at the API level.
110      * Accordingly, this parameter has been removed. See the documentation for details on
111      * controlling the operation of the reasoners.</p>
112      *
113      * @return An iteration over the values of the encapsulated property.
114      */

115     public NodeIterator getAll() {
116         return new NodeIteratorImpl( UniqueExtendedIterator.create( m_val.listPropertyValues( getProperty() ) ), null );
117     }
118
119
120     /**
121      * <p>Answer a general value of the encapsulated property. If it has no values, answer
122      * null. If it has one value, answer that value. Otherwise, answer an undetermined
123      * member of the set of values. See also {@link #getDAMLValue}.
124      *
125      * @return A value for the encapsulated property in the RDF model, or null
126      * if the property has no value.
127      */

128     public RDFNode get() {
129         return m_val.getPropertyValue( getProperty() ).as( DAMLCommon.class );
130     }
131
132
133     /**
134      * <p>Answer the value of the encapsulated property, presented as a DAML list. If it has no values, answer
135      * null. If it has one value, answer that value (as a list). Otherwise, answer an undetermined
136      * member of the set of values. See also {@link #getDAMLValue}.
137      *
138      * @return A value for the encapsulated property in the RDF model, or null
139      * if the property has no value.
140      * @exception com.hp.hpl.jena.ontology.ConversionException if the value is not a list
141      */

142     public DAMLList getList() {
143         RDFNode n = get();
144         return (n == null) ? null : (DAMLList) n.as( DAMLList.class );
145     }
146
147
148     /**
149      * <p>Answer a value of the encapsulated property, converted to a DAML common value</p>
150      *
151      * @return A DAML value for the encapsulated property in the RDF model, or null
152      * if the property has no value.
153      */

154     public DAMLCommon getDAMLValue() {
155         NodeIterator i = null;
156         try {
157             for (i = getAll(); i.hasNext(); ) {
158                 RDFNode n = (RDFNode) i.next();
159                 
160                 if (n.canAs( DAMLCommon.class )) {
161                     return (DAMLCommon) n.as( DAMLCommon.class );
162                 }
163             }
164             
165             // no DAMLCommon value, so return null for compatability with Jena 1
166
return null;
167         }
168         finally {
169             if (i != null) {
170                 i.close();
171             }
172         }
173     }
174
175
176     /**
177      * <p>Add a value to the encapsulated property.</p>
178      *
179      * @param value The value to be added.
180      */

181     public void add( RDFNode value ) {
182         m_val.addProperty( getProperty(), value );
183     }
184
185
186     /**
187      * <p>Remove a value from the encapsulated property.</p>
188      *
189      * @param value The value to be removed.
190      */

191     public void remove( RDFNode value ) {
192         m_val.removeProperty( getProperty(), value );
193     }
194
195
196     /**
197      * <p>Answer true if the encapsulated property has the given value as one of its
198      * values.</p>
199      *
200      * @param value A value to test for
201      * @return True if the RDF model contains a statement giving a value for
202      * the encapsulated property matching the given value.
203      */

204     public boolean hasValue( RDFNode value ) {
205         return m_val.hasProperty( getProperty(), value );
206     }
207
208
209
210     // Internal implementation methods
211
//////////////////////////////////
212

213
214
215     //==============================================================================
216
// Inner class definitions
217
//==============================================================================
218

219
220 }
221
222
223 /*
224     (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
225     All rights reserved.
226
227     Redistribution and use in source and binary forms, with or without
228     modification, are permitted provided that the following conditions
229     are met:
230
231     1. Redistributions of source code must retain the above copyright
232        notice, this list of conditions and the following disclaimer.
233
234     2. Redistributions in binary form must reproduce the above copyright
235        notice, this list of conditions and the following disclaimer in the
236        documentation and/or other materials provided with the distribution.
237
238     3. The name of the author may not be used to endorse or promote products
239        derived from this software without specific prior written permission.
240
241     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
242     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
243     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
244     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
245     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
246     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
247     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
248     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
249     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
250     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251 */

252
253
Popular Tags