KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > ReasonerRegistry


1 /******************************************************************
2  * File: ReasonerRegistry.java
3  * Created by: Dave Reynolds
4  * Created on: 16-Jan-03
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: ReasonerRegistry.java,v 1.27 2005/02/21 12:16:15 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner;
11
12 import com.hp.hpl.jena.rdf.model.*;
13 import com.hp.hpl.jena.vocabulary.*;
14 import com.hp.hpl.jena.graph.*;
15 import com.hp.hpl.jena.reasoner.dig.DIGReasoner;
16 import com.hp.hpl.jena.reasoner.dig.DIGReasonerFactory;
17 import com.hp.hpl.jena.reasoner.rulesys.DAMLMicroReasonerFactory;
18 import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasonerFactory;
19 import com.hp.hpl.jena.reasoner.rulesys.OWLFBRuleReasonerFactory;
20 import com.hp.hpl.jena.reasoner.rulesys.OWLMicroReasonerFactory;
21 import com.hp.hpl.jena.reasoner.rulesys.OWLMiniReasonerFactory;
22 import com.hp.hpl.jena.reasoner.rulesys.RDFSRuleReasonerFactory;
23 import com.hp.hpl.jena.reasoner.transitiveReasoner.TransitiveReasonerFactory;
24
25 import java.util.*;
26
27 /**
28  * A global registry of known reasoner modules. Modules are identified
29  * by a URI and can have associated capability and descriptive
30  * information, in RDF. Currently the schema/ontology for such
31  * descriptions is mostly open. However, we do ensure that each reasoner
32  * URI at least has an associated rdf:type statement to indicate that it
33  * is a reasoner.
34  * <p>
35  * It is up to each reasoner or some associated configuration system
36  * to register it in this registry. </p>
37  *
38  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
39  * @version $Revision: 1.27 $ on $Date: 2005/02/21 12:16:15 $
40  */

41 public class ReasonerRegistry {
42
43     /** Single glogal instance of the registry */
44     protected static ReasonerRegistry theRegistry;
45     
46     /** Map from reasoner URI to the associated factory */
47     protected Map reasonerFactories = new HashMap();
48     
49     /** Union of the all reasoner capability descriptions */
50     protected Model allDescriptions;
51
52     /**
53      * Constructor is hidden - go via theRegistry
54      */

55     private ReasonerRegistry() {
56         allDescriptions = ModelFactory.createDefaultModel();
57         // Preload the known Jena reasoers
58
register(TransitiveReasonerFactory.theInstance());
59         register(RDFSRuleReasonerFactory.theInstance());
60         register(OWLFBRuleReasonerFactory.theInstance());
61         register(GenericRuleReasonerFactory.theInstance());
62         register(DAMLMicroReasonerFactory.theInstance());
63         register(DIGReasonerFactory.theInstance());
64         register(OWLMicroReasonerFactory.theInstance());
65         register(OWLMiniReasonerFactory.theInstance());
66     }
67     
68     /**
69      * Return the single global instance of the registry
70      */

71     public static ReasonerRegistry theRegistry() {
72         if (theRegistry == null) {
73             theRegistry = new ReasonerRegistry();
74         }
75         return theRegistry;
76     }
77     
78     /**
79      * Register a Reasoner.
80      * @param factory an factory that can be used to create instances of the reasoner
81      */

82     public void register(ReasonerFactory factory) {
83         reasonerFactories.put(factory.getURI(), factory);
84         Model description = factory.getCapabilities();
85         if (description != null) {
86             allDescriptions.add(description);
87         }
88         allDescriptions.createResource(factory.getURI())
89                         .addProperty(RDF.type, ReasonerVocabulary.ReasonerClass);
90     }
91     
92     /**
93      * Register a Reasoner - simple case with no RDF description.
94      * @param factory an factory that can be used to create instances of the reasoner
95      * @param reasonerUri the URI used to label the reasoner, expressed as a
96      * simple string
97      */

98     public void register(String JavaDoc reasonerUri, ReasonerFactory factory) {
99         reasonerFactories.put(reasonerUri, factory);
100         allDescriptions.createResource(reasonerUri)
101                        .addProperty(RDF.type, ReasonerVocabulary.ReasonerClass);
102     }
103     
104     /**
105      * Return a composite set of RDF capability descriptions for all registered reasoners.
106      * Listing all Resources of type ReasonerClass in the returned model
107      * would enumerate all registered reasoners.
108      */

109     public Model getAllDescriptions() {
110         return allDescriptions;
111     }
112     
113     /**
114      * Return information on a given Reasoner.
115      * @param uri the URI of the reasoner
116      * @return a Resource representing the reasoner whose properties (as obtainable
117      * through listProperties etc) give a capability description of the reasoner;
118      * returns null if no such reasoner is registered.
119      */

120     public Resource getDescription(String JavaDoc uri) {
121         Resource reasonerURI = allDescriptions.getResource(uri);
122         if (allDescriptions.contains(reasonerURI, RDF.type, ReasonerVocabulary.ReasonerClass)) {
123             return reasonerURI;
124         } else {
125             return null;
126         }
127     }
128     
129     /**
130      * Return the factory for the given reasoner.
131      * @param uri the URI of the reasoner
132      * @return the ReasonerFactory instance for this reasoner
133      */

134     public ReasonerFactory getFactory(String JavaDoc uri) {
135         return (ReasonerFactory)reasonerFactories.get(uri);
136     }
137     
138     /**
139      * Create and return a new instance of the reasoner identified by
140      * the given uri.
141      * @param uri the uri of the reasoner to be created, expressed as a simple string
142      * @param configuration an optional set of configuration information encoded in RDF this
143      * parameter can be null if no configuration information is required.
144      * @return a reaoner instance
145      * @throws ReasonerException if there is not such reasoner or if there is
146      * some problem during instantiation
147      */

148     public Reasoner create(String JavaDoc uri, Resource configuration) throws ReasonerException {
149         ReasonerFactory factory = getFactory(uri);
150         if (factory != null) {
151             return factory.create(configuration);
152         } else {
153             throw new ReasonerException("Attempted to instantiate an unknown reasoner: " + uri);
154         }
155     }
156     
157     /**
158      * Return a property Node which represents the direct version of a
159      * transitively closed property.
160      *
161      * <p>Not clear what the right thing to do here is. Should not just invent
162      * a new local name in the same namespace because that might be a controlled
163      * namespace like RDF or RDFS. Can't even just extend the namespace slightly
164      * because that would be violating the web principles of namespace ownership.
165      * On the other hand, this solution results in staggeringly clumsy names.</p>
166      *
167      * @param node the node representing the transitive property
168      */

169     public static Node makeDirect(Node node) {
170         String JavaDoc directName = "urn:x-hp-direct-predicate:" + node.getURI().replace(':','_') ;
171         return Node.createURI(directName);
172     }
173     
174     /** Prebuilt standard configuration for the default RDFS reasoner. */
175     protected static Reasoner theRDFSReasoner = null;
176     
177     /**
178      * Return a prebuilt standard configuration for the default RDFS reasoner
179      */

180      public static Reasoner getRDFSReasoner() {
181          if (theRDFSReasoner == null) theRDFSReasoner = RDFSRuleReasonerFactory.theInstance().create(null);
182          return theRDFSReasoner;
183      }
184     
185     
186     /** Prebuilt standard configuration for the default RDFS reasoner. */
187     protected static Reasoner theRDFSSimpleReasoner = null;
188     
189     /**
190      * Return a prebuilt simplified configuration for the default RDFS reasoner
191      */

192      public static Reasoner getRDFSSimpleReasoner() {
193          if (theRDFSSimpleReasoner == null) {
194              theRDFSSimpleReasoner = RDFSRuleReasonerFactory.theInstance().create(null);
195              theRDFSSimpleReasoner.setParameter(ReasonerVocabulary.PROPsetRDFSLevel, ReasonerVocabulary.RDFS_SIMPLE);
196          }
197          return theRDFSSimpleReasoner;
198      }
199      
200     /** Prebuilt standard configuration for the default subclass/subproperty transitive closure reasoner. */
201     protected static Reasoner theTRANSITIVEReasoner;
202     
203     /**
204      * Return a prebuilt standard configuration for the default subclass/subproperty transitive closure reasoner.
205      */

206     public static Reasoner getTransitiveReasoner() {
207         if (theTRANSITIVEReasoner == null) theTRANSITIVEReasoner = TransitiveReasonerFactory.theInstance().create(null);
208         return theTRANSITIVEReasoner;
209     }
210     
211     /** Prebuilt standard configuration OWL reasoner */
212     protected static Reasoner theOWLReasoner;
213     
214     /**
215      * Prebuilt standard configuration for the default OWL reasoner. This configuration is
216      * hybrid forward/backward reasoner.
217      */

218     public static Reasoner getOWLReasoner() {
219         if (theOWLReasoner == null) theOWLReasoner = OWLFBRuleReasonerFactory.theInstance().create(null);
220         return theOWLReasoner;
221     }
222     
223     /** Prebuilt micro configuration OWL reasoner */
224     protected static Reasoner theOWLMicroReasoner;
225     
226     /**
227      * Prebuilt standard configuration a micro-OWL reasoner. This just supports property axioms,
228      * and class inheritance sufficient to power the OntAPI.
229      */

230     public static Reasoner getOWLMicroReasoner() {
231         if (theOWLMicroReasoner == null) theOWLMicroReasoner = OWLMicroReasonerFactory.theInstance().create(null);
232         return theOWLMicroReasoner;
233     }
234     
235     /** Prebuilt mini configuration OWL reasoner */
236     protected static Reasoner theOWLMiniReasoner;
237     
238     /**
239      * Prebuilt mini configuration for the default OWL reasoner. This omits bNode
240      * introduction rules which has significant performance gain in some cases and
241      * avoids breaking the find contract.
242      */

243     public static Reasoner getOWLMiniReasoner() {
244         if (theOWLMiniReasoner == null) theOWLMiniReasoner = OWLMiniReasonerFactory.theInstance().create(null);
245         return theOWLMiniReasoner;
246     }
247     
248     /**
249      * <p>Create a DIG reasoner for the specified language for the OWL
250      * language, with default settings (including the default URL for the
251      * DIG reasoner), and no axioms specified. </p>
252      * @return A new DIG reasoner
253      */

254     public static DIGReasoner getDIGReasoner() {
255         return getDIGReasoner( OWL.NAMESPACE, null );
256     }
257     
258     /**
259      * <p>Create a DIG reasoner for the specified language
260      * ({@linkplain DAML_OIL#NAMESPACE_DAML DAML} or
261      * {@linkplain OWL#NAMESPACE OWL}), without type axioms.</p>
262      * @param lang The ontology language, OWL or DAML, as a resource
263      * @param config Optional configuration root resource, or null
264      * @return A new DIG reasoner
265      */

266     public static DIGReasoner getDIGReasoner( Resource lang, Resource config ) {
267         return getDIGReasoner( lang, false, config );
268     }
269
270     /**
271      * <p>Create a DIG reasoner for the specified language
272      * ({@linkplain DAML_OIL#NAMESPACE_DAML DAML} or
273      * {@linkplain OWL#NAMESPACE OWL}), optionally with axioms that specify
274      * e.g. the types of OWL objects etc.</p>
275      * @param lang The ontology language, OWL or DAML, as a resource
276      * @param withAxioms If true, include the type axioms for the language
277      * @param config Optional configuration root resource, or null
278      * @return A new DIG reasoner
279      */

280     public static DIGReasoner getDIGReasoner( Resource lang, boolean withAxioms, Resource config ) {
281         if (!lang.equals( DAML_OIL.NAMESPACE_DAML ) && !lang.equals( OWL.NAMESPACE )) {
282             throw new ReasonerException( "Cannot create DIG reasoner for unknown language: " + lang );
283         }
284         
285         boolean owl = lang.equals( OWL.NAMESPACE );
286         String JavaDoc axioms = withAxioms ?
287                           (owl ? DIGReasonerFactory.DEFAULT_OWL_AXIOMS : DIGReasonerFactory.DEFAULT_DAML_AXIOMS) :
288                           null;
289         
290         return DIGReasonerFactory.theInstance().create( lang, axioms, config );
291     }
292 }
293
294 /*
295     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
296     All rights reserved.
297
298     Redistribution and use in source and binary forms, with or without
299     modification, are permitted provided that the following conditions
300     are met:
301
302     1. Redistributions of source code must retain the above copyright
303        notice, this list of conditions and the following disclaimer.
304
305     2. Redistributions in binary form must reproduce the above copyright
306        notice, this list of conditions and the following disclaimer in the
307        documentation and/or other materials provided with the distribution.
308
309     3. The name of the author may not be used to endorse or promote products
310        derived from this software without specific prior written permission.
311
312     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
313     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
314     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
315     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
316     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
317     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
318     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
319     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
320     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
321     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322 */

323
324
Popular Tags