KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************
2  * File: BasicBackwardRuleReasoner.java
3  * Created by: Dave Reynolds
4  * Created on: 29-Apr-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: BasicBackwardRuleReasoner.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 import com.hp.hpl.jena.rdf.model.*;
13 import com.hp.hpl.jena.reasoner.*;
14 import com.hp.hpl.jena.reasoner.rulesys.impl.RuleStore;
15 import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
16 import com.hp.hpl.jena.graph.*;
17
18 import java.util.*;
19
20 /**
21  * Reasoner implementation which augments or transforms an RDF graph
22  * according to a set of rules. The rules are processed using a
23  * tabled backchaining interpreter which is implemented by the
24  * relvant InfGraph class.
25  *
26  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
27  * @version $Revision: 1.8 $ on $Date: 2005/02/21 12:18:03 $
28  */

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

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

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

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

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

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

115     public boolean supportsProperty(Property property) {
116         if (factory == null) return false;
117         Model caps = factory.getCapabilities();
118         Resource root = caps.getResource(factory.getURI());
119         return caps.contains(root, ReasonerVocabulary.supportsP, property);
120     }
121     
122     /**
123      * Precompute the implications of a schema graph. The statements in the graph
124      * will be combined with the data when the final InfGraph is created.
125      */

126     public Reasoner bindSchema(Graph tbox) throws ReasonerException {
127         return new BasicBackwardRuleReasoner(this, tbox);
128     }
129     
130     /**
131      * Precompute the implications of a schema Model. The statements in the graph
132      * will be combined with the data when the final InfGraph is created.
133      */

134     public Reasoner bindSchema(Model tbox) throws ReasonerException {
135         return new BasicBackwardRuleReasoner(this, tbox.getGraph());
136     }
137     
138     /**
139      * Attach the reasoner to a set of RDF data to process.
140      * The reasoner may already have been bound to specific rules or ontology
141      * axioms (encoded in RDF) through earlier bindRuleset calls.
142      *
143      * @param data the RDF data to be processed, some reasoners may restrict
144      * the range of RDF which is legal here (e.g. syntactic restrictions in OWL).
145      * @return an inference graph through which the data+reasoner can be queried.
146      * @throws ReasonerException if the data is ill-formed according to the
147      * constraints imposed by this reasoner.
148      */

149     public InfGraph bind(Graph data) throws ReasonerException {
150         BasicBackwardRuleInfGraph graph = new BasicBackwardRuleInfGraph(this, ruleStore, data, schemaGraph);
151         graph.setDerivationLogging(recordDerivations);
152         return graph;
153     }
154     
155     /**
156      * Return the this of Rules used by this reasoner
157      * @return a List of Rule objects
158      */

159     public List getRules() {
160         return rules;
161     }
162    
163     /**
164      * Switch on/off drivation logging.
165      * If set to true then the InfGraph created from the bind operation will start
166      * life with recording of derivations switched on. This is currently only of relevance
167      * to rule-based reasoners.
168      * <p>
169      * Default - false.
170      */

171     public void setDerivationLogging(boolean logOn) {
172         recordDerivations = logOn;
173     }
174     
175     /**
176      * Set the state of the trace flag. If set to true then rule firings
177      * are logged out to the Log at "INFO" level.
178      */

179     public void setTraceOn(boolean state) {
180         traceOn = state;
181     }
182     
183     /**
184       * Set a configuration paramter for the reasoner. In the case of the this
185       * reasoner there are no configuration parameters and this method is simply
186       * here to meet the interfaces specification
187       *
188       * @param parameter the property identifying the parameter to be changed
189       * @param value the new value for the parameter, typically this is a wrapped
190       * java object like Boolean or Integer.
191       */

192      public void setParameter(Property parameter, Object JavaDoc value) {
193          throw new IllegalParameterException(parameter.toString());
194      }
195
196      /**
197       * Return the Jena Graph Capabilties that the inference graphs generated
198       * by this reasoner are expected to conform to.
199       */

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

240
241
Popular Tags