KickJava   Java API By Example, From Geeks To Geeks.

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


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 7 Sept 2001
8  * Filename $RCSfile: DAMLHierarchy.java,v $
9  * Revision $Revision: 1.6 $
10  * Release status Preview-release $State: Exp $
11  *
12  * Last modified on $Date: 2005/02/21 12:05:20 $
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

27 import com.hp.hpl.jena.vocabulary.*;
28 import com.hp.hpl.jena.rdf.model.*;
29
30 import java.util.*;
31
32
33
34 /**
35  * Represents knowledge about the class and property hierarchies in the DAML spec,
36  * so that these can be available without forcing the user to import the DAML specification
37  * into their model each time. Uses Singleton pattern.
38  *
39  * @author Ian Dickinson, HP Labs (<a HREF="mailto:Ian.Dickinson@hp.com">email</a>)
40  * @version CVS info: $Id: DAMLHierarchy.java,v 1.6 2005/02/21 12:05:20 andy_seaborne Exp $
41  */

42 public class DAMLHierarchy
43 {
44     // Constants
45
//////////////////////////////////
46

47
48     // Static variables
49
//////////////////////////////////
50

51     /** The singleton instance */
52     private static DAMLHierarchy s_instance = new DAMLHierarchy();
53
54
55     // Instance variables
56
//////////////////////////////////
57

58     /**
59      * Map of DAML and RDF standard classes that are super-classes of other DAML standard classes.
60      * Assumes each class has at most one super-class.
61      */

62     public Resource[][] DAML_STANDARD_CLASS_HIERARCHY = new Resource[][] {
63         // sub-class super-class
64
{DAML_OIL.Class, RDFS.Class},
65         {DAML_OIL.Datatype, RDFS.Class},
66         {DAML_OIL.Restriction, DAML_OIL.Class},
67         {DAML_OIL.ObjectProperty, RDF.Property},
68         {DAML_OIL.DatatypeProperty, RDF.Property},
69         {DAML_OIL.TransitiveProperty, DAML_OIL.ObjectProperty},
70         {DAML_OIL.UnambiguousProperty, DAML_OIL.ObjectProperty},
71         {DAML_OIL.UniqueProperty, RDF.Property},
72         {DAML_OIL.List, RDF.Seq},
73     };
74
75
76     /**
77      * Known equivalences between DAML and RDFS values
78      */

79     public Resource[][] DAML_STANDARD_EQUIVALENCES = new Resource[][] {
80         {DAML_OIL.subClassOf, RDFS.subClassOf},
81         {DAML_OIL.Literal, RDFS.Literal},
82         {DAML_OIL.Property, RDF.Property},
83         {DAML_OIL.type, RDF.type},
84         {DAML_OIL.value, RDF.value},
85         {DAML_OIL.subPropertyOf, RDFS.subPropertyOf},
86         {DAML_OIL.domain, RDFS.domain},
87         {DAML_OIL.range, RDFS.range},
88         {DAML_OIL.label, RDFS.label},
89         {DAML_OIL.comment, RDFS.comment},
90         {DAML_OIL.seeAlso, RDFS.seeAlso},
91         {DAML_OIL.isDefinedBy, RDFS.isDefinedBy},
92     };
93
94
95     /** A list of well-known properties that are known a priori to be transitive */
96     public Property[] TRANSITIVE_PROPERTIES = new Property[] {
97         DAML_OIL.subClassOf,
98         DAML_OIL.subPropertyOf,
99         DAML_OIL.sameClassAs,
100         DAML_OIL.sameIndividualAs,
101         DAML_OIL.samePropertyAs,
102         DAML_OIL.equivalentTo,
103
104         RDFS.subClassOf,
105         RDFS.subPropertyOf
106     };
107
108     /** Super-class hierarchy hashmap */
109     protected Map m_classHierarchyMap = new HashMap();
110
111     /** Flag to show initialisation of static structures has taken place */
112     protected boolean m_initialised = false;
113
114     /** Equivalance map */
115     protected Map m_equivalenceMap = new HashMap();
116
117     /** Table of transitive properties */
118     protected Map m_transitiveProperties = new HashMap();
119
120
121
122     // Constructors
123
//////////////////////////////////
124

125     /**
126      * Constructor is private to enforce singleton design pattern.
127      */

128     private DAMLHierarchy() {
129     }
130
131
132     // External signature methods
133
//////////////////////////////////
134

135     /**
136      * Answer the singleton instance.
137      *
138      * @return the unique DAMLHierarchy
139      */

140     public static DAMLHierarchy getInstance() {
141         return s_instance;
142     }
143
144
145     /**
146      * Answer true if the first URI represents a class in the standard DAML ontology
147      * that is a super-class of the class denoted by the second URI. This is used to
148      * provide background knowledge from the standard DAML ontology without having
149      * to explicitly import it each time.
150      *
151      * @param uri0 A class URI
152      * @param uri1 A class URI
153      * @return true if the class denoted by uri0 is a sub-class of the class denoted by uri1
154      */

155     public boolean isDAMLSubClassOf( String JavaDoc uri0, String JavaDoc uri1 ) {
156         // only works for named classes, so if the uri's are null or empty (indicating
157
// anonymous resources) we skip
158
if ((uri0 == null) || (uri1 == null) ||
159             (uri0.length() == 0) || (uri1.length() == 0)) {
160             return false;
161         }
162
163         // ensure that the structure has been initialised
164
initialiseMaps();
165
166         // first, is uri0 directly a sub-class of uri1?
167
String JavaDoc superClass = (String JavaDoc) m_classHierarchyMap.get( uri0 );
168
169         if (uri1.equals( superClass )) {
170             // found a match
171
return true;
172         }
173         else {
174             // NOTE: assumes there is at most one super-class for each class, which is
175
// true in current releases of DAML but may not remain true forever.
176
return superClass == null ? false : isDAMLSubClassOf( superClass, uri1 );
177         }
178     }
179
180
181     /**
182      * Answer true if the given property is well-known to be transitive. That is,
183      * it is transitive even though it is not marked as being in the class of
184      * transitive properties defined by the TransitiveProperty class. Note: this
185      * test does not check for <code>rdf:type TransitiveProperty</code>, it <i>only</i>
186      * tests the table of well-known transitive properties.
187      *
188      * @param res A resource
189      * @return True if the resource is a Property, and it is a member of the set
190      * of well-known transitive properties.
191      */

192     public boolean isTransitiveProperty( Resource res ) {
193         return (res instanceof Property && m_transitiveProperties.containsKey( res ));
194     }
195
196
197     /**
198      * Answer an iteration of those values that are equivalent to the given resource.
199      *
200      * @param res A resource, that may have known equivalent values
201      * @return an iteration over the resource's equivalent values, which may be empty
202      * but will not be null.
203      */

204     public Iterator getEquivalentValues( Resource res ) {
205         // ensure that the cache structure has been initialised
206
initialiseMaps();
207
208         LinkedList equivs = (LinkedList) m_equivalenceMap.get( res );
209         return (equivs == null) ? new LinkedList().iterator() : equivs.iterator();
210     }
211
212
213
214     // Internal implementation methods
215
//////////////////////////////////
216

217     /**
218      * Initialise the static structures we need to process class hierarchy queries.
219      * Note that we don't do this as a static to avoid race conditions with setting
220      * up the vocabularies.
221      */

222     private void initialiseMaps() {
223         if (!m_initialised) {
224             // initialise the class hierachy map for known standard DAML classes
225
for (int i = 0; i < DAML_STANDARD_CLASS_HIERARCHY.length; i++) {
226                 String JavaDoc subClass = DAML_STANDARD_CLASS_HIERARCHY[i][0].getURI();
227                 String JavaDoc superClass = DAML_STANDARD_CLASS_HIERARCHY[i][1].getURI();
228
229                 m_classHierarchyMap.put( subClass, superClass );
230             }
231
232             // initialise the equivalence map - note symmetric relation, so we have to do both directions
233
for (int i = 0; i < DAML_STANDARD_EQUIVALENCES.length; i++) {
234                 // first do the forwards direction
235
LinkedList l = (LinkedList) m_equivalenceMap.get( DAML_STANDARD_EQUIVALENCES[i][0] );
236
237                 if (l == null) {
238                     // if no list of equivs yet, create one
239
l = new LinkedList();
240                     m_equivalenceMap.put( DAML_STANDARD_EQUIVALENCES[i][0], l );
241                 }
242
243                 l.add( DAML_STANDARD_EQUIVALENCES[i][1] );
244
245                 // now do the reverse direction
246
l = (LinkedList) m_equivalenceMap.get( DAML_STANDARD_EQUIVALENCES[i][1] );
247
248                 if (l == null) {
249                     // if no list of equivs yet, create one
250
l = new LinkedList();
251                     m_equivalenceMap.put( DAML_STANDARD_EQUIVALENCES[i][1], l );
252                 }
253
254                 l.add( DAML_STANDARD_EQUIVALENCES[i][0] );
255             }
256
257             // initialise the transitive properties map, for efficiency
258
for (int i = 0; i < TRANSITIVE_PROPERTIES.length; i++) {
259                 m_transitiveProperties.put( TRANSITIVE_PROPERTIES[i], Boolean.TRUE );
260             }
261
262             m_initialised = true;
263         }
264     }
265
266
267
268     //==============================================================================
269
// Inner class definitions
270
//==============================================================================
271

272
273 }
274
275
276 /*
277     (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
278     All rights reserved.
279
280     Redistribution and use in source and binary forms, with or without
281     modification, are permitted provided that the following conditions
282     are met:
283
284     1. Redistributions of source code must retain the above copyright
285        notice, this list of conditions and the following disclaimer.
286
287     2. Redistributions in binary form must reproduce the above copyright
288        notice, this list of conditions and the following disclaimer in the
289        documentation and/or other materials provided with the distribution.
290
291     3. The name of the author may not be used to endorse or promote products
292        derived from this software without specific prior written permission.
293
294     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
295     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
296     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
297     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
298     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
299     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
300     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
301     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
302     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
303     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
304 */

305
306
Popular Tags