001: /*****************************************************************************
002: * Source code information
003: * -----------------------
004: * Original author Ian Dickinson, HP Labs Bristol
005: * Author email Ian.Dickinson@hp.com
006: * Package Jena 2
007: * Web http://sourceforge.net/projects/jena/
008: * Created 31-Mar-2003
009: * Filename $RCSfile: IndividualImpl.java,v $
010: * Revision $Revision: 1.17 $
011: * Release status $State: Exp $
012: *
013: * Last modified on $Date: 2008/01/23 12:47:00 $
014: * by $Author: ian_dickinson $
015: *
016: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
017: * (see footer for full conditions)
018: *****************************************************************************/package com.hp.hpl.jena.ontology.impl;
019:
020: // Imports
021: ///////////////
022: import com.hp.hpl.jena.ontology.*;
023: import com.hp.hpl.jena.rdf.model.*;
024: import com.hp.hpl.jena.util.iterator.ExtendedIterator;
025: import com.hp.hpl.jena.enhanced.*;
026: import com.hp.hpl.jena.graph.*;
027:
028: /**
029: * <p>
030: * Implementation for the ontology abstraction representing ontology class descriptions.
031: * </p>
032: *
033: * @author Ian Dickinson, HP Labs
034: * (<a href="mailto:Ian.Dickinson@hp.com" >email</a>)
035: * @version CVS $Id: IndividualImpl.java,v 1.17 2008/01/23 12:47:00 ian_dickinson Exp $
036: */
037: public class IndividualImpl extends OntResourceImpl implements
038: Individual {
039: // Constants
040: //////////////////////////////////
041:
042: // Static variables
043: //////////////////////////////////
044:
045: /**
046: * A factory for generating Individual facets from nodes in enhanced graphs.
047: * Note: should not be invoked directly by user code: use
048: * {@link com.hp.hpl.jena.rdf.model.RDFNode#as as()} instead.
049: */
050: public static Implementation factory = new Implementation() {
051: public EnhNode wrap(Node n, EnhGraph eg) {
052: if (canWrap(n, eg)) {
053: return new IndividualImpl(n, eg);
054: } else {
055: throw new ConversionException("Cannot convert node "
056: + n.toString() + " to Individual");
057: }
058: }
059:
060: public boolean canWrap(Node node, EnhGraph eg) {
061: // node will support being an Individual facet if it is a URI node or bNode
062: Profile profile = (eg instanceof OntModel) ? ((OntModel) eg)
063: .getProfile()
064: : null;
065: return (profile != null)
066: && profile.isSupported(node, eg, Individual.class);
067: }
068: };
069:
070: // Instance variables
071: //////////////////////////////////
072:
073: // Constructors
074: //////////////////////////////////
075:
076: /**
077: * <p>
078: * Construct an individual represented by the given node in the given graph.
079: * </p>
080: *
081: * @param n The node that represents the resource
082: * @param g The enh graph that contains n
083: */
084: public IndividualImpl(Node n, EnhGraph g) {
085: super (n, g);
086: }
087:
088: // External signature methods
089: //////////////////////////////////
090:
091: /**
092: * <p>Set the ontology class for this individual, replacing any
093: * existing class membership. Class membership is encoded using the
094: * <code>rdf:type</code> property. Any existing statements for the RDF type
095: * will first be removed.</p>
096: *
097: * @param cls The RDF resource denoting the new class to which this individual belongs,
098: * which will replace any existing <code>rdf:type</code> property.
099: */
100: public void setOntClass(Resource cls) {
101: setRDFType(cls);
102: }
103:
104: /**
105: * <p>Add the given ontology class as one of the classes to which
106: * this individual belongs. Class membership is encoded using the
107: * <code>rdf:type</code> property. </p>
108: *
109: * @param cls An RDF resource denoting an additional class to which this individual
110: * belongs.
111: */
112: public void addOntClass(Resource cls) {
113: addRDFType(cls);
114: }
115:
116: /**
117: * <p>
118: * Answer an ontology class to which this individual belongs. If the individual
119: * belongs to more than one class, which is common in ontology models using
120: * a reasoner, then the return value will be one of
121: * the possible values but <strong>it is not specified which one</strong>.
122: * In the case of multiple classes, callers <strong>should not</strong> rely on
123: * the return value being consistent, e.g. across runs, since it may
124: * depend on the underlying hash indexes in the model. </p>
125: * <p>This method considers any ontology class for the individual, not just
126: * <em>direct</em> classes. It is equivalent to <code>getOntClass(false)</code>.
127: * </p>
128: *
129: * @return A resource denoting the ontology class for this individual, or one of them if
130: * more than one is defined.
131: * @exception ConversionException if the return value is known to be an
132: * ontology class, assuming strict type checking is turned on for the underlying
133: * <code>OntModel</code>. See {@link OntModel#setStrictMode(boolean)}
134: */
135: public OntClass getOntClass() {
136: return getOntClass(false);
137: }
138:
139: /**
140: * <p>
141: * Answer an ontology class to which this individual belongs. If the resource
142: * belongs to more than one class, which is common in ontology models using
143: * a reasoner, then the return value will be one of
144: * the possible values but <strong>it is not specified which one</strong>.
145: * In the case of multiple classes, callers <strong>should not</strong> rely on
146: * the return value being consistent, e.g. across runs, since it may
147: * depend on the underlying hash indexes in the model. </p>
148: *
149: * @param direct If <code>true</code>, only <em>direct</em> classes are considered.
150: * A class is a direct class of this <code>Individual</code> if and only if
151: * there is no other resource is both an <code>rdf:type</code> of this
152: * individual and a sub-class of the candidate class.
153: *
154: * @return A resource denoting the ontology class for this individual, or one of them if
155: * more than one is defined.
156: * @exception ConversionException if the return value is known to be an
157: * ontology class, assuming strict type checking is turned on for the underlying
158: * <code>OntModel</code>. See {@link OntModel#setStrictMode(boolean)}
159: */
160: public OntClass getOntClass(boolean direct) {
161: return (OntClass) (getRDFType(direct).as(OntClass.class));
162: }
163:
164: /**
165: * <p>
166: * Answer an iterator over the ontology classes to which this individual belongs.
167: * The members of the iterator will be {@link OntClass} objects.
168: * </p>
169: *
170: * @param direct If true, only answer those resources that are direct types
171: * of this individual, not the super-classes of the class etc.
172: * @return An iterator over the set of this individual's classes. Each member
173: * of the iteration will be an {@link OntClass}.
174: */
175: public ExtendedIterator listOntClasses(boolean direct) {
176: return listRDFTypes(direct).mapWith(
177: new AsMapper(OntClass.class));
178: }
179:
180: /**
181: * <p>
182: * Answer true if this individual is a member of the class denoted by the
183: * given class resource.
184: * </p>
185: *
186: * @param ontClass Denotes an ontology class to which this individual may belong
187: * @param direct If true, only consider the direct types of this individual, ignoring
188: * the super-classes of the stated types.
189: * @return True if this individual is a member of the given class, possibly taking the
190: * directness constraint into account.
191: */
192: public boolean hasOntClass(Resource ontClass, boolean direct) {
193: return hasRDFType(ontClass, direct);
194: }
195:
196: /**
197: * <p>
198: * Answer true if this individual is a member of the class denoted by the
199: * given ontology class resource. Not limited to only direct class relationships,
200: * so this is equivalent to:
201: * <code><pre>
202: * hasOntClass( ontClass, false );
203: * </pre></code>
204: * </p>
205: *
206: * @param ontClass Denotes a class to which this individual may belong
207: * @return True if this individual has the given class as one of its <code>rdf:type</code>'s.
208: */
209: public boolean hasOntClass(Resource ontClass) {
210: return hasOntClass(ontClass, false);
211: }
212:
213: /**
214: * <p>
215: * Answer true if this individual is a member of the class denoted by the
216: * given URI.</p>
217: *
218: * @param uri Denotes the URI of a class to which this value may belong
219: * @return True if this individual has the given class as one of its <code>rdf:type</code>'s.
220: */
221: public boolean hasOntClass(String uri) {
222: return hasRDFType(uri);
223: }
224:
225: /**
226: * <p>Attempt to remove this <code>individual</code> as a member of the
227: * given ontology class. This relationship is represented by a <code>rdf:type</code>
228: * statement in the underlying model. If this relationship was originally
229: * asserted, then removal will always succeed. However, if the <code>rdf:type</code>
230: * relationship is entailed by the action of an attached reasoner, it may not be
231: * possible to directly remove it. Callers should instead update the assertions
232: * and axioms that entail the class membership relationship, and ensure the
233: * reasoner gets chance to update the entailments.</p>
234: * <p>If this individual is not a member of the given class, the
235: * operation has no effect.</p>
236: *
237: * @param ontClass A resource denoting a class that that is to be removed from
238: * the set of classes to which this individual belongs
239: */
240: public void removeOntClass(Resource ontClass) {
241: removeRDFType(ontClass);
242: }
243:
244: // Internal implementation methods
245: //////////////////////////////////
246:
247: //==============================================================================
248: // Inner class definitions
249: //==============================================================================
250:
251: }
252:
253: /*
254: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
255: All rights reserved.
256:
257: Redistribution and use in source and binary forms, with or without
258: modification, are permitted provided that the following conditions
259: are met:
260:
261: 1. Redistributions of source code must retain the above copyright
262: notice, this list of conditions and the following disclaimer.
263:
264: 2. Redistributions in binary form must reproduce the above copyright
265: notice, this list of conditions and the following disclaimer in the
266: documentation and/or other materials provided with the distribution.
267:
268: 3. The name of the author may not be used to endorse or promote products
269: derived from this software without specific prior written permission.
270:
271: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
272: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
273: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
274: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
275: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
276: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
277: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
278: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
279: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
280: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281: */
|