KickJava   Java API By Example, From Geeks To Geeks.

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


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

10 package com.hp.hpl.jena.reasoner.rulesys;
11 import com.hp.hpl.jena.rdf.model.*;
12 import com.hp.hpl.jena.reasoner.*;
13 import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
14 import com.hp.hpl.jena.graph.*;
15
16 import java.util.*;
17
18 /** * Reasoner implementation which augments or transforms an RDF graph
19  * according to a set of rules. This trivial version does not support
20  * separate schema processing. The actual work is done in the inference
21  * graph implementation.
22  * * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a> * @version $Revision: 1.16 $ on $Date: 2005/02/21 12:16:55 $ */

23 public class BasicForwardRuleReasoner implements Reasoner {
24     
25     /** The parent reasoner factory which is consulted to answer capability questions */
26     protected ReasonerFactory factory;
27
28     /** The rules to be used by this instance of the forward engine */
29     protected List rules;
30     
31     /** A precomputed set of schema deductions */
32     protected InfGraph schemaGraph;
33     
34     /** Flag to set whether the inference class should record derivations */
35     protected boolean recordDerivations = false;
36     
37     /** Flag which, if true, enables tracing of rule actions to logger.info */
38     protected boolean traceOn = false;
39     
40     /** The graph capabilities of the infgraphs generated by this reasoner */
41     protected Capabilities capabilities;
42     
43     /**
44      * Constructor. This is the raw version that does not reference a ReasonerFactory
45      * and so has no capabilities description.
46      * @param rules a list of Rule instances which defines the ruleset to process
47      */

48     public BasicForwardRuleReasoner(List rules) {
49         this.rules = rules;
50     }
51     
52     /**
53      * Constructor
54      * @param rules a list of Rule instances which defines the ruleset to process
55      * @param factory the parent reasoner factory which is consulted to answer capability questions
56      */

57     public BasicForwardRuleReasoner(List rules, ReasonerFactory factory) {
58         this.rules = rules;
59         this.factory = factory;
60     }
61     
62     /**
63      * Internal constructor, used to generated a partial binding of a schema
64      * to a rule reasoner instance.
65      */

66     private BasicForwardRuleReasoner(List rules, InfGraph schemaGraph, ReasonerFactory factory) {
67         this.rules = rules;
68         this.schemaGraph = schemaGraph;
69         this.factory = factory;
70     }
71
72     /**
73      * Return a description of the capabilities of this reasoner encoded in
74      * RDF. These capabilities may be static or may depend on configuration
75      * information supplied at construction time. May be null if there are
76      * no useful capabilities registered.
77      */

78     public Model getReasonerCapabilities() {
79         if (factory != null) {
80             return factory.getCapabilities();
81         } else {
82             return null;
83         }
84     }
85     
86     /**
87      * Add a configuration description for this reasoner into a partial
88      * configuration specification model.
89      * @param configSpec a Model into which the configuration information should be placed
90      * @param base the Resource to which the configuration parameters should be added.
91      */

92     public void addDescription(Model configSpec, Resource base) {
93         // No configuration
94
}
95
96     /**
97      * Determine whether the given property is recognized and treated specially
98      * by this reasoner. This is a convenience packaging of a special case of getCapabilities.
99      * @param property the property which we want to ask the reasoner about, given as a Node since
100      * this is part of the SPI rather than API
101      * @return true if the given property is handled specially by the reasoner.
102      */

103     public boolean supportsProperty(Property property) {
104         if (factory == null) return false;
105         Model caps = factory.getCapabilities();
106         Resource root = caps.getResource(factory.getURI());
107         return caps.contains(root, ReasonerVocabulary.supportsP, property);
108     }
109     
110     /**
111      * Precompute the implications of a schema graph. The statements in the graph
112      * will be combined with the data when the final InfGraph is created.
113      */

114     public Reasoner bindSchema(Graph tbox) throws ReasonerException {
115         InfGraph graph = new BasicForwardRuleInfGraph(this, rules, null, tbox);
116         return new BasicForwardRuleReasoner(rules, graph, factory);
117     }
118     
119     /**
120      * Precompute the implications of a schema Model. The statements in the graph
121      * will be combined with the data when the final InfGraph is created.
122      */

123     public Reasoner bindSchema(Model tbox) throws ReasonerException {
124         InfGraph graph = new BasicForwardRuleInfGraph(this, rules, null, tbox.getGraph());
125         return new BasicForwardRuleReasoner(rules, graph, factory);
126     }
127     
128     /**
129      * Attach the reasoner to a set of RDF data to process.
130      * The reasoner may already have been bound to specific rules or ontology
131      * axioms (encoded in RDF) through earlier bindRuleset calls.
132      *
133      * @param data the RDF data to be processed, some reasoners may restrict
134      * the range of RDF which is legal here (e.g. syntactic restrictions in OWL).
135      * @return an inference graph through which the data+reasoner can be queried.
136      * @throws ReasonerException if the data is ill-formed according to the
137      * constraints imposed by this reasoner.
138      */

139     public InfGraph bind(Graph data) throws ReasonerException {
140         BasicForwardRuleInfGraph graph = new BasicForwardRuleInfGraph(this, rules, schemaGraph);
141         graph.setDerivationLogging(recordDerivations);
142         graph.setTraceOn(traceOn);
143         graph.rebind(data);
144         return graph;
145     }
146     
147     /**
148      * Return the this of Rules used by this reasoner
149      * @return a List of Rule objects
150      */

151     public List getRules() {
152         return rules;
153     }
154    
155     /**
156      * Switch on/off drivation logging.
157      * If set to true then the InfGraph created from the bind operation will start
158      * life with recording of derivations switched on. This is currently only of relevance
159      * to rule-based reasoners.
160      * <p>
161      * Default - false.
162      */

163     public void setDerivationLogging(boolean logOn) {
164         recordDerivations = logOn;
165     }
166     
167     /**
168      * Set the state of the trace flag. If set to true then rule firings
169      * are logged out to the Log at "INFO" level.
170      */

171     public void setTraceOn(boolean state) {
172         traceOn = state;
173     }
174     
175     /**
176      * Set a configuration paramter for the reasoner. The supported parameters
177      * are:
178      * <ul>
179      * <li>BasicForwaredRuleReasoner.PROPderivationLogging - set to true to enable recording all rule derivations</li>
180      * <li>BasicForwaredRuleReasoner.PROPtraceOn - set to true to enable verbose trace information to be sent to the logger INFO channel</li>
181      * </ul>
182      *
183      * @param parameter the property identifying the parameter to be changed
184      * @param value the new value for the parameter, typically this is a wrapped
185      * java object like Boolean or Integer.
186      */

187     public void setParameter(Property parameter, Object JavaDoc value) {
188         if (parameter.equals(ReasonerVocabulary.PROPderivationLogging)) {
189             recordDerivations = Util.convertBooleanPredicateArg(parameter, value);
190         } else if (parameter.equals(ReasonerVocabulary.PROPtraceOn)) {
191             traceOn = Util.convertBooleanPredicateArg(parameter, value);
192         } else {
193             throw new IllegalParameterException("Don't recognize configuration parameter " + parameter + " for rule-based reasoner");
194         }
195     }
196
197
198     /**
199      * Return the Jena Graph Capabilties that the inference graphs generated
200      * by this reasoner are expected to conform to.
201      */

202     public Capabilities getGraphCapabilities() {
203         if (capabilities == null) {
204             capabilities = new BaseInfGraph.InfCapabilities();
205         }
206         return capabilities;
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
Popular Tags