KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > LPBackwardRuleReasoner


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

10 package com.hp.hpl.jena.reasoner.rulesys;
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.impl.*;
16 import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
17 import com.hp.hpl.jena.graph.*;
18
19 import java.util.*;
20
21 /**
22  * Reasoner implementation which augments or transforms an RDF graph
23  * according to a set of rules. The rules are processed using a
24  * tabled LP backchaining interpreter which is implemented by the
25  * relvant InfGraph class.
26  *
27  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
28  * @version $Revision: 1.8 $ on $Date: 2005/02/21 12:17:00 $
29  */

30 public class LPBackwardRuleReasoner implements Reasoner {
31
32     /** The parent reasoner factory which is consulted to answer capability questions */
33     protected ReasonerFactory factory;
34     
35     /** The rules to be used by this instance of the backward engine */
36     protected List rules;
37     
38     /** Indexed, normalized copy of the rule list */
39     protected LPRuleStore ruleStore;
40     
41     /** A cache set of schema data used in partial binding chains */
42     protected Graph schemaGraph;
43     
44     /** Flag to set whether the inference class should record derivations */
45     protected boolean recordDerivations = false;
46     
47     /** Flag which, if true, enables tracing of rule actions to logger.info */
48     boolean traceOn = false;
49     
50     /** The graph capabilities of the infgraphs generated by this reasoner */
51     protected Capabilities capabilities;
52
53     /**
54      * Constructor. This is the raw version that does not reference a ReasonerFactory
55      * and so has no capabilities description.
56      * @param rules a list of Rule instances which defines the ruleset to process
57      */

58     public LPBackwardRuleReasoner(List rules) {
59         this.rules = rules;
60         ruleStore = new LPRuleStore(rules);
61     }
62
63     /**
64      * Constructor
65      * @param rules a list of Rule instances which defines the ruleset to process
66      * @param factory the parent reasoner factory which is consulted to answer capability questions
67      */

68     public LPBackwardRuleReasoner(List rules, ReasonerFactory factory) {
69         this.rules = rules;
70         this.factory = factory;
71         ruleStore = new LPRuleStore(rules);
72     }
73     
74     /**
75      * Internal constructor, used to generated a partial binding of a schema
76      * to a rule reasoner instance.
77      */

78     protected LPBackwardRuleReasoner(LPBackwardRuleReasoner parent, Graph schemaGraph) {
79         rules = parent.rules;
80         ruleStore = parent.ruleStore;
81         this.schemaGraph = schemaGraph;
82         this.factory = parent.factory;
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         if (factory != null) {
93             return factory.getCapabilities();
94         } else {
95             return null;
96         }
97     }
98     
99     /**
100      * Add a configuration description for this reasoner into a partial
101      * configuration specification model.
102      * @param configSpec a Model into which the configuration information should be placed
103      * @param base the Resource to which the configuration parameters should be added.
104      */

105     public void addDescription(Model configSpec, Resource base) {
106         // No configuration
107
}
108     
109     /**
110      * Register an RDF predicate as one whose presence in a goal should force
111      * the goal to be tabled.
112      */

113     public synchronized void tablePredicate(Node predicate) {
114         ruleStore.tablePredicate(predicate);
115     }
116     
117
118     /**
119      * Determine whether the given property is recognized and treated specially
120      * by this reasoner. This is a convenience packaging of a special case of getCapabilities.
121      * @param property the property which we want to ask the reasoner about, given as a Node since
122      * this is part of the SPI rather than API
123      * @return true if the given property is handled specially by the reasoner.
124      */

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

136     public Reasoner bindSchema(Graph tbox) throws ReasonerException {
137         return new LPBackwardRuleReasoner(this, tbox);
138     }
139     
140     /**
141      * Precompute the implications of a schema Model. The statements in the graph
142      * will be combined with the data when the final InfGraph is created.
143      */

144     public Reasoner bindSchema(Model tbox) throws ReasonerException {
145         return new LPBackwardRuleReasoner(this, tbox.getGraph());
146     }
147     
148     /**
149      * Attach the reasoner to a set of RDF data to process.
150      * The reasoner may already have been bound to specific rules or ontology
151      * axioms (encoded in RDF) through earlier bindRuleset calls.
152      *
153      * @param data the RDF data to be processed, some reasoners may restrict
154      * the range of RDF which is legal here (e.g. syntactic restrictions in OWL).
155      * @return an inference graph through which the data+reasoner can be queried.
156      * @throws ReasonerException if the data is ill-formed according to the
157      * constraints imposed by this reasoner.
158      */

159     public InfGraph bind(Graph data) throws ReasonerException {
160         LPBackwardRuleInfGraph graph = new LPBackwardRuleInfGraph(this, ruleStore, data, schemaGraph);
161         graph.setDerivationLogging(recordDerivations);
162         return graph;
163     }
164     
165     /**
166      * Return the this of Rules used by this reasoner
167      * @return a List of Rule objects
168      */

169     public List getRules() {
170         return rules;
171     }
172    
173     /**
174      * Switch on/off drivation logging.
175      * If set to true then the InfGraph created from the bind operation will start
176      * life with recording of derivations switched on. This is currently only of relevance
177      * to rule-based reasoners.
178      * <p>
179      * Default - false.
180      */

181     public void setDerivationLogging(boolean logOn) {
182         recordDerivations = logOn;
183     }
184     
185     /**
186      * Set the state of the trace flag. If set to true then rule firings
187      * are logged out to the Log at "INFO" level.
188      */

189     public void setTraceOn(boolean state) {
190         traceOn = state;
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      * Return the Jena Graph Capabilties that the inference graphs generated
208      * by this reasoner are expected to conform to.
209      */

210     public Capabilities getGraphCapabilities() {
211         if (capabilities == null) {
212             capabilities = new BaseInfGraph.InfCapabilities();
213         }
214         return capabilities;
215     }
216
217 }
218
219
220
221 /*
222     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
223     All rights reserved.
224
225     Redistribution and use in source and binary forms, with or without
226     modification, are permitted provided that the following conditions
227     are met:
228
229     1. Redistributions of source code must retain the above copyright
230        notice, this list of conditions and the following disclaimer.
231
232     2. Redistributions in binary form must reproduce the above copyright
233        notice, this list of conditions and the following disclaimer in the
234        documentation and/or other materials provided with the distribution.
235
236     3. The name of the author may not be used to endorse or promote products
237        derived from this software without specific prior written permission.
238
239     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
240     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
241     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
242     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
243     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
244     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
245     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
247     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
248     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
249 */
Popular Tags