KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > impl > oldCode > FBLPRuleReasoner


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

10 package com.hp.hpl.jena.reasoner.rulesys.impl.oldCode;
11
12
13 import com.hp.hpl.jena.rdf.model.*;
14 import com.hp.hpl.jena.reasoner.*;
15 import com.hp.hpl.jena.reasoner.rulesys.ClauseEntry;
16 import com.hp.hpl.jena.reasoner.rulesys.Functor;
17 import com.hp.hpl.jena.reasoner.rulesys.Rule;
18 import com.hp.hpl.jena.reasoner.rulesys.Util;
19 import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
20 import com.hp.hpl.jena.graph.*;
21
22 import java.util.*;
23
24 /**
25  * Rule-based reasoner interface. This is the default rule reasoner to use.
26  * It supports both forward reasoning and backward reasoning, including use
27  * of forward rules to generate and instantiate backward rules.
28  *
29  * This version is purely temporary during development of the LP engine and will get
30  * replaced by the upgraded FBRuleReasoner when the LP section is released.
31  *
32  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
33  * @version $Revision: 1.8 $ on $Date: 2005/02/21 12:18:03 $
34  */

35 public class FBLPRuleReasoner implements Reasoner {
36     
37     /** The parent reasoner factory which is consulted to answer capability questions */
38     protected ReasonerFactory factory;
39
40     /** The rules to be used by this instance of the forward engine */
41     protected List rules;
42     
43     /** A precomputed set of schema deductions */
44     protected Graph schemaGraph;
45     
46     /** Flag to set whether the inference class should record derivations */
47     protected boolean recordDerivations = false;
48
49     /** Flag which, if true, enables tracing of rule actions to logger.info */
50     protected boolean traceOn = false;
51
52     /** Flag, if true we cache the closure of the pure rule set with its axioms */
53     protected static final boolean cachePreload = true;
54     
55     /** The cached empty closure, if wanted */
56     protected InfGraph preload;
57     
58     /** The graph capabilities of the infgraphs generated by this reasoner */
59     protected Capabilities capabilities;
60      
61     /**
62      * Constructor. This is the raw version that does not reference a ReasonerFactory
63      * and so has no capabilities description.
64      * @param rules a list of Rule instances which defines the ruleset to process
65      */

66     public FBLPRuleReasoner(List rules) {
67         this.rules = rules;
68     }
69     
70     /**
71      * Constructor
72      * @param factory the parent reasoner factory which is consulted to answer capability questions
73      */

74     public FBLPRuleReasoner(ReasonerFactory factory) {
75         this(null, factory);
76     }
77     
78     /**
79      * Constructor
80      * @param rules a list of Rule instances which defines the ruleset to process
81      * @param factory the parent reasoner factory which is consulted to answer capability questions
82      */

83     public FBLPRuleReasoner(List rules, ReasonerFactory factory) {
84         this(rules);
85         this.factory = factory;
86     }
87     
88     /**
89      * Internal constructor, used to generated a partial binding of a schema
90      * to a rule reasoner instance.
91      */

92     protected FBLPRuleReasoner(List rules, Graph schemaGraph, ReasonerFactory factory) {
93         this(rules, factory);
94         this.schemaGraph = schemaGraph;
95     }
96
97     /**
98      * Return a description of the capabilities of this reasoner encoded in
99      * RDF. These capabilities may be static or may depend on configuration
100      * information supplied at construction time. May be null if there are
101      * no useful capabilities registered.
102      */

103     public Model getReasonerCapabilities() {
104         if (factory != null) {
105             return factory.getCapabilities();
106         } else {
107             return null;
108         }
109     }
110     
111     /**
112      * Add a configuration description for this reasoner into a partial
113      * configuration specification model.
114      * @param configSpec a Model into which the configuration information should be placed
115      * @param base the Resource to which the configuration parameters should be added.
116      */

117     public void addDescription(Model configSpec, Resource base) {
118         // No configuration
119
}
120
121     /**
122      * Determine whether the given property is recognized and treated specially
123      * by this reasoner. This is a convenience packaging of a special case of getCapabilities.
124      * @param property the property which we want to ask the reasoner about, given as a Node since
125      * this is part of the SPI rather than API
126      * @return true if the given property is handled specially by the reasoner.
127      */

128     public boolean supportsProperty(Property property) {
129         if (factory == null) return false;
130         Model caps = factory.getCapabilities();
131         Resource root = caps.getResource(factory.getURI());
132         return caps.contains(root, ReasonerVocabulary.supportsP, property);
133     }
134     
135     /**
136      * Precompute the implications of a schema graph. The statements in the graph
137      * will be combined with the data when the final InfGraph is created.
138      */

139     public Reasoner bindSchema(Graph tbox) throws ReasonerException {
140         if (schemaGraph != null) {
141             throw new ReasonerException("Can only bind one schema at a time to an OWLRuleReasoner");
142         }
143         FBLPRuleInfGraph graph = new FBLPRuleInfGraph(this, rules, getPreload(), tbox);
144         graph.prepare();
145         FBLPRuleReasoner fbr = new FBLPRuleReasoner(rules, graph, factory);
146         fbr.setDerivationLogging(recordDerivations);
147         fbr.setTraceOn(traceOn);
148         return fbr;
149     }
150     
151     /**
152      * Precompute the implications of a schema Model. The statements in the graph
153      * will be combined with the data when the final InfGraph is created.
154      */

155     public Reasoner bindSchema(Model tbox) throws ReasonerException {
156         return bindSchema(tbox.getGraph());
157     }
158     
159     /**
160      * Attach the reasoner to a set of RDF data to process.
161      * The reasoner may already have been bound to specific rules or ontology
162      * axioms (encoded in RDF) through earlier bindRuleset calls.
163      *
164      * @param data the RDF data to be processed, some reasoners may restrict
165      * the range of RDF which is legal here (e.g. syntactic restrictions in OWL).
166      * @return an inference graph through which the data+reasoner can be queried.
167      * @throws ReasonerException if the data is ill-formed according to the
168      * constraints imposed by this reasoner.
169      */

170     public InfGraph bind(Graph data) throws ReasonerException {
171         Graph schemaArg = schemaGraph == null ? getPreload() : (FBLPRuleInfGraph)schemaGraph;
172         FBLPRuleInfGraph graph = new FBLPRuleInfGraph(this, rules, schemaArg);
173         graph.setDerivationLogging(recordDerivations);
174         graph.setTraceOn(traceOn);
175         graph.rebind(data);
176         return graph;
177     }
178     
179     /**
180      * Set (or change) the rule set that this reasoner should execute.
181      * @param rules a list of Rule objects
182      */

183     public void setRules(List rules) {
184         this.rules = rules;
185         preload = null;
186     }
187     
188     /**
189      * Return the this of Rules used by this reasoner
190      * @return a List of Rule objects
191      */

192     public List getRules() {
193         return rules;
194     }
195     
196     /**
197      * Register an RDF predicate as one whose presence in a goal should force
198      * the goal to be tabled. This is better done directly in the rule set.
199      */

200     public synchronized void tablePredicate(Node predicate) {
201         // Create a dummy rule which tables the predicate ...
202
Rule tablePredicateRule = new Rule("",
203                 new ClauseEntry[]{
204                     new Functor("table", new Node[] { predicate })
205                 },
206                 new ClauseEntry[]{});
207         // ... end append the rule to the ruleset
208
rules.add(tablePredicateRule);
209     }
210     
211     /**
212      * Get the single static precomputed rule closure.
213      */

214     protected synchronized InfGraph getPreload() {
215         if (cachePreload && preload == null) {
216             preload = (new FBLPRuleInfGraph(this, rules, null));
217             preload.prepare();
218         }
219         return preload;
220     }
221        
222     /**
223      * Switch on/off drivation logging.
224      * If set to true then the InfGraph created from the bind operation will start
225      * life with recording of derivations switched on. This is currently only of relevance
226      * to rule-based reasoners.
227      * <p>
228      * Default - false.
229      */

230     public void setDerivationLogging(boolean logOn) {
231         recordDerivations = logOn;
232     }
233     
234     /**
235      * Set the state of the trace flag. If set to true then rule firings
236      * are logged out to the Log at "INFO" level.
237      */

238     public void setTraceOn(boolean state) {
239         traceOn = state;
240     }
241     
242     /**
243      * Set a configuration paramter for the reasoner. The supported parameters
244      * are:
245      * <ul>
246      * <li>PROPderivationLogging - set to true to enable recording all rule derivations</li>
247      * <li>PROPtraceOn - set to true to enable verbose trace information to be sent to the logger INFO channel</li>
248      * </ul>
249      *
250      * @param parameterUri the property identifying the parameter to be changed
251      * @param value the new value for the parameter, typically this is a wrapped
252      * java object like Boolean or Integer.
253      */

254     public void setParameter(Property parameter, Object JavaDoc value) {
255         if (parameter.equals(ReasonerVocabulary.PROPderivationLogging)) {
256             recordDerivations = Util.convertBooleanPredicateArg(parameter, value);
257         } else if (parameter.equals(ReasonerVocabulary.PROPtraceOn)) {
258             traceOn = Util.convertBooleanPredicateArg(parameter, value);
259         } else {
260             throw new IllegalParameterException("Don't recognize configuration parameter " + parameter + " for rule-based reasoner");
261         }
262     }
263
264     /**
265      * Return the Jena Graph Capabilties that the inference graphs generated
266      * by this reasoner are expected to conform to.
267      */

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