KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > transitiveReasoner > TransitiveReasoner


1 /******************************************************************
2  * File: TransitiveReasoner.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: TransitiveReasoner.java,v 1.20 2005/02/21 12:18:37 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.transitiveReasoner;
11
12 import com.hp.hpl.jena.rdf.model.*;
13 import com.hp.hpl.jena.reasoner.*;
14 import com.hp.hpl.jena.graph.*;
15 import com.hp.hpl.jena.vocabulary.RDFS;
16 import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
17
18 /**
19  * A simple "reasoner" used to help with API development.
20  * <p>This reasoner caches a transitive closure of the subClass and
21  * subProperty graphs. The generated infGraph allows both the direct
22  * and closed versions of these properties to be retrieved. The cache is
23  * built when the tbox is bound in but if the final data graph
24  * contains additional subProperty/subClass declarations then the
25  * cache has to be rebuilt.</p>
26  * <p>
27  * The triples in the tbox (if present) will also be included
28  * in any query. Any of tbox or data graph are allowed to be null.</p>
29  *
30  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
31  * @version $Revision: 1.20 $ on $Date: 2005/02/21 12:18:37 $
32  */

33 public class TransitiveReasoner implements Reasoner {
34
35     /** The precomputed cache of the subClass graph */
36     protected TransitiveGraphCache subClassCache;
37     
38     /** The precomputed cache of the subProperty graph */
39     protected TransitiveGraphCache subPropertyCache;
40     
41     /** The graph registered as the schema, if any */
42     protected Finder tbox = null;
43     
44     /** The direct (minimal) version of the subPropertyOf property */
45     public static Node directSubPropertyOf;
46     
47     /** The direct (minimal) version of the subClassOf property */
48     public static Node directSubClassOf;
49     
50     /** The normal subPropertyOf property */
51     public static Node subPropertyOf;
52     
53     /** The normal subClassOf property */
54     public static Node subClassOf;
55     
56     /** The graph capabilities of the infgraphs generated by this reasoner */
57     protected Capabilities capabilities;
58
59     // Static initializer
60
static {
61         directSubPropertyOf = ReasonerRegistry.makeDirect(RDFS.subPropertyOf.getNode());
62         directSubClassOf = ReasonerRegistry.makeDirect(RDFS.subClassOf.getNode());
63         subPropertyOf = RDFS.subPropertyOf.getNode();
64         subClassOf = RDFS.subClassOf.getNode();
65     }
66     
67     /** Constructor */
68     public TransitiveReasoner() {
69         subClassCache = new TransitiveGraphCache(directSubClassOf, subClassOf);
70         subPropertyCache = new TransitiveGraphCache(directSubPropertyOf, subPropertyOf);
71     }
72     
73     /**
74      * Private constructor used by bindSchema when
75      * returning a partially bound reasoner instance.
76      */

77     protected TransitiveReasoner(Finder tbox,
78                     TransitiveGraphCache subClassCache,
79                     TransitiveGraphCache subPropertyCache) {
80         this.tbox = tbox;
81         this.subClassCache = subClassCache;
82         this.subPropertyCache = subPropertyCache;
83     }
84
85     /**
86      * Return a description of the capabilities of this reasoner encoded in
87      * RDF. These capabilities may be static or may depend on configuration
88      * information supplied at construction time. May be null if there are
89      * no useful capabilities registered.
90      */

91     public Model getReasonerCapabilities() {
92         return TransitiveReasonerFactory.theInstance().getCapabilities();
93     }
94     
95     /**
96      * Add a configuration description for this reasoner into a partial
97      * configuration specification model.
98      * @param configSpec a Model into which the configuration information should be placed
99      * @param base the Resource to which the configuration parameters should be added.
100      */

101     public void addDescription(Model configSpec, Resource base) {
102         // No configuration
103
}
104
105     /**
106      * Determine whether the given property is recognized and treated specially
107      * by this reasoner. This is a convenience packaging of a special case of getCapabilities.
108      * @param property the property which we want to ask the reasoner about, given as a Node since
109      * this is part of the SPI rather than API
110      * @return true if the given property is handled specially by the reasoner.
111      */

112     public boolean supportsProperty(Property property) {
113         ReasonerFactory rf = TransitiveReasonerFactory.theInstance();
114         Model caps = rf.getCapabilities();
115         Resource root = caps.getResource(rf.getURI());
116         return caps.contains(root, ReasonerVocabulary.supportsP, property);
117     }
118      
119     /**
120      * Extracts all of the subClass and subProperty declarations from
121      * the given schema/tbox and caches the resultant graphs.
122      * It can only be used once, can't stack up multiple tboxes this way.
123      * This limitation could be lifted - the only difficulty is the need to
124      * reprocess all the earlier tboxes if a new subPropertyOf subPropertyOf
125      * subClassOf is discovered.
126      * @param tbox schema containing the property and class declarations
127      */

128     public Reasoner bindSchema(Graph tbox) throws ReasonerException {
129         return bindSchema(new FGraph(tbox));
130     }
131      
132     /**
133      * Extracts all of the subClass and subProperty declarations from
134      * the given schema/tbox and caches the resultant graphs.
135      * It can only be used once, can't stack up multiple tboxes this way.
136      * This limitation could be lifted - the only difficulty is the need to
137      * reprocess all the earlier tboxes if a new subPropertyOf subPropertyOf
138      * subClassOf is discovered.
139      * @param tbox schema containing the property and class declarations
140      */

141     public Reasoner bindSchema(Model tbox) throws ReasonerException {
142         return bindSchema(new FGraph(tbox.getGraph()));
143     }
144     
145      
146     /**
147      * Extracts all of the subClass and subProperty declarations from
148      * the given schema/tbox and caches the resultant graphs.
149      * It can only be used once, can't stack up multiple tboxes this way.
150      * This limitation could be lifted - the only difficulty is the need to
151      * reprocess all the earlier tboxes if a new subPropertyOf subPropertyOf
152      * subClassOf is discovered.
153      * @param tbox schema containing the property and class declarations
154      */

155     Reasoner bindSchema(Finder tbox) throws ReasonerException {
156         if (this.tbox != null) {
157             throw new ReasonerException("Attempt to bind multiple rulesets - disallowed for now");
158         }
159         TransitiveGraphCache sCc = new TransitiveGraphCache(directSubClassOf, subClassOf);
160         TransitiveGraphCache sPc = new TransitiveGraphCache(directSubPropertyOf, subPropertyOf);
161         TransitiveEngine.cacheSubPropUtility(tbox, sPc);
162         TransitiveEngine.cacheSubClassUtility(tbox, sPc, sCc);
163         
164         return new TransitiveReasoner(tbox, sCc, sPc);
165     }
166     
167     /**
168      * Attach the reasoner to a set of RDF ddata to process.
169      * The reasoner may already have been bound to specific rules or ontology
170      * axioms (encoded in RDF) through earlier bindRuleset calls.
171      * @param data the RDF data to be processed, some reasoners may restrict
172      * the range of RDF which is legal here (e.g. syntactic restrictions in OWL).
173      * @return an inference graph through which the data+reasoner can be queried.
174      * @throws ReasonerException if the data is ill-formed according to the
175      * constraints imposed by this reasoner.
176      */

177     public InfGraph bind(Graph data) throws ReasonerException {
178         return new TransitiveInfGraph(data, this);
179     }
180    
181     /**
182      * Switch on/off drivation logging.
183      * If set to true then the InfGraph created from the bind operation will start
184      * life with recording of derivations switched on. This is currently only of relevance
185      * to rule-based reasoners.
186      * <p>
187      * Default - false.
188      */

189     public void setDerivationLogging(boolean logOn) {
190         // Irrelevant to this reasoner
191
}
192     
193     /**
194       * Set a configuration paramter for the reasoner. In the case of the this
195       * reasoner there are no configuration parameters and this method is simply
196       * here to meet the interfaces specification
197       *
198       * @param parameter the property identifying the parameter to be changed
199       * @param value the new value for the parameter, typically this is a wrapped
200       * java object like Boolean or Integer.
201       */

202      public void setParameter(Property parameter, Object JavaDoc value) {
203          throw new IllegalParameterException(parameter.toString());
204      }
205     
206     /**
207      * Accessor used during infgraph construction - return the cached
208      * version of the subProperty lattice.
209      */

210     public TransitiveGraphCache getSubPropertyCache() {
211         return subPropertyCache;
212     }
213     
214     /**
215      * Accessor used during infgraph construction - return the cached
216      * version of the subClass lattice.
217      */

218     public TransitiveGraphCache getSubClassCache() {
219         return subClassCache;
220     }
221     
222     /**
223      * Accessor used during infgraph construction - return the partially
224      * bound tbox, if any.
225      */

226     public Finder getTbox() {
227         return tbox;
228     }
229
230     /**
231      * Return the Jena Graph Capabilties that the inference graphs generated
232      * by this reasoner are expected to conform to.
233      */

234     public Capabilities getGraphCapabilities() {
235         if (capabilities == null) {
236             capabilities = new BaseInfGraph.InfFindSafeCapabilities();
237         }
238         return capabilities;
239     }
240     
241 }
242
243 /*
244     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
245     All rights reserved.
246
247     Redistribution and use in source and binary forms, with or without
248     modification, are permitted provided that the following conditions
249     are met:
250
251     1. Redistributions of source code must retain the above copyright
252        notice, this list of conditions and the following disclaimer.
253
254     2. Redistributions in binary form must reproduce the above copyright
255        notice, this list of conditions and the following disclaimer in the
256        documentation and/or other materials provided with the distribution.
257
258     3. The name of the author may not be used to endorse or promote products
259        derived from this software without specific prior written permission.
260
261     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
262     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
263     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
264     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
265     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
266     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
267     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
268     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
269     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
270     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
271 */

272
273
Popular Tags