001: /******************************************************************
002: * File: BasicForwardRuleReasoner.java
003: * Created by: Dave Reynolds
004: * Created on: 30-Mar-03
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: BasicForwardRuleReasoner.java,v 1.19 2008/01/02 12:07:47 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.rulesys;
010:
011: import com.hp.hpl.jena.rdf.model.*;
012: import com.hp.hpl.jena.reasoner.*;
013: import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
014: import com.hp.hpl.jena.graph.*;
015:
016: import java.util.*;
017:
018: /** * Reasoner implementation which augments or transforms an RDF graph
019: * according to a set of rules. This trivial version does not support
020: * separate schema processing. The actual work is done in the inference
021: * graph implementation.
022: * * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a> * @version $Revision: 1.19 $ on $Date: 2008/01/02 12:07:47 $ */
023: public class BasicForwardRuleReasoner implements Reasoner {
024:
025: /** The parent reasoner factory which is consulted to answer capability questions */
026: protected ReasonerFactory factory;
027:
028: /** The rules to be used by this instance of the forward engine */
029: protected List rules;
030:
031: /** A precomputed set of schema deductions */
032: protected InfGraph schemaGraph;
033:
034: /** Flag to set whether the inference class should record derivations */
035: protected boolean recordDerivations = false;
036:
037: /** Flag which, if true, enables tracing of rule actions to logger.info */
038: protected boolean traceOn = false;
039:
040: /** The graph capabilities of the infgraphs generated by this reasoner */
041: protected Capabilities capabilities;
042:
043: /**
044: * Constructor. This is the raw version that does not reference a ReasonerFactory
045: * and so has no capabilities description.
046: * @param rules a list of Rule instances which defines the ruleset to process
047: */
048: public BasicForwardRuleReasoner(List rules) {
049: this .rules = rules;
050: }
051:
052: /**
053: * Constructor
054: * @param rules a list of Rule instances which defines the ruleset to process
055: * @param factory the parent reasoner factory which is consulted to answer capability questions
056: */
057: public BasicForwardRuleReasoner(List rules, ReasonerFactory factory) {
058: this .rules = rules;
059: this .factory = factory;
060: }
061:
062: /**
063: * Internal constructor, used to generated a partial binding of a schema
064: * to a rule reasoner instance.
065: */
066: private BasicForwardRuleReasoner(List rules, InfGraph schemaGraph,
067: ReasonerFactory factory) {
068: this .rules = rules;
069: this .schemaGraph = schemaGraph;
070: this .factory = factory;
071: }
072:
073: /**
074: * Return a description of the capabilities of this reasoner encoded in
075: * RDF. These capabilities may be static or may depend on configuration
076: * information supplied at construction time. May be null if there are
077: * no useful capabilities registered.
078: */
079: public Model getReasonerCapabilities() {
080: if (factory != null) {
081: return factory.getCapabilities();
082: } else {
083: return null;
084: }
085: }
086:
087: /**
088: * Add a configuration description for this reasoner into a partial
089: * configuration specification model.
090: * @param configSpec a Model into which the configuration information should be placed
091: * @param base the Resource to which the configuration parameters should be added.
092: */
093: public void addDescription(Model configSpec, Resource base) {
094: // No configuration
095: }
096:
097: /**
098: * Determine whether the given property is recognized and treated specially
099: * by this reasoner. This is a convenience packaging of a special case of getCapabilities.
100: * @param property the property which we want to ask the reasoner about, given as a Node since
101: * this is part of the SPI rather than API
102: * @return true if the given property is handled specially by the reasoner.
103: */
104: public boolean supportsProperty(Property property) {
105: if (factory == null)
106: return false;
107: Model caps = factory.getCapabilities();
108: Resource root = caps.getResource(factory.getURI());
109: return caps.contains(root, ReasonerVocabulary.supportsP,
110: property);
111: }
112:
113: /**
114: * Precompute the implications of a schema graph. The statements in the graph
115: * will be combined with the data when the final InfGraph is created.
116: */
117: public Reasoner bindSchema(Graph tbox) throws ReasonerException {
118: InfGraph graph = new BasicForwardRuleInfGraph(this , rules,
119: null, tbox);
120: return new BasicForwardRuleReasoner(rules, graph, factory);
121: }
122:
123: /**
124: * Precompute the implications of a schema Model. The statements in the graph
125: * will be combined with the data when the final InfGraph is created.
126: */
127: public Reasoner bindSchema(Model tbox) throws ReasonerException {
128: InfGraph graph = new BasicForwardRuleInfGraph(this , rules,
129: null, tbox.getGraph());
130: return new BasicForwardRuleReasoner(rules, graph, factory);
131: }
132:
133: /**
134: * Attach the reasoner to a set of RDF data to process.
135: * The reasoner may already have been bound to specific rules or ontology
136: * axioms (encoded in RDF) through earlier bindRuleset calls.
137: *
138: * @param data the RDF data to be processed, some reasoners may restrict
139: * the range of RDF which is legal here (e.g. syntactic restrictions in OWL).
140: * @return an inference graph through which the data+reasoner can be queried.
141: * @throws ReasonerException if the data is ill-formed according to the
142: * constraints imposed by this reasoner.
143: */
144: public InfGraph bind(Graph data) throws ReasonerException {
145: BasicForwardRuleInfGraph graph = new BasicForwardRuleInfGraph(
146: this , rules, schemaGraph);
147: graph.setDerivationLogging(recordDerivations);
148: graph.setTraceOn(traceOn);
149: graph.rebind(data);
150: return graph;
151: }
152:
153: /**
154: * Return the this of Rules used by this reasoner
155: * @return a List of Rule objects
156: */
157: public List getRules() {
158: return rules;
159: }
160:
161: /**
162: * Switch on/off drivation logging.
163: * If set to true then the InfGraph created from the bind operation will start
164: * life with recording of derivations switched on. This is currently only of relevance
165: * to rule-based reasoners.
166: * <p>
167: * Default - false.
168: */
169: public void setDerivationLogging(boolean logOn) {
170: recordDerivations = logOn;
171: }
172:
173: /**
174: * Set the state of the trace flag. If set to true then rule firings
175: * are logged out to the Log at "INFO" level.
176: */
177: public void setTraceOn(boolean state) {
178: traceOn = state;
179: }
180:
181: /**
182: * Set a configuration paramter for the reasoner. The supported parameters
183: * are:
184: * <ul>
185: * <li>BasicForwaredRuleReasoner.PROPderivationLogging - set to true to enable recording all rule derivations</li>
186: * <li>BasicForwaredRuleReasoner.PROPtraceOn - set to true to enable verbose trace information to be sent to the logger INFO channel</li>
187: * </ul>
188: *
189: * @param parameter the property identifying the parameter to be changed
190: * @param value the new value for the parameter, typically this is a wrapped
191: * java object like Boolean or Integer.
192: */
193: public void setParameter(Property parameter, Object value) {
194: if (parameter.equals(ReasonerVocabulary.PROPderivationLogging)) {
195: recordDerivations = Util.convertBooleanPredicateArg(
196: parameter, value);
197: } else if (parameter.equals(ReasonerVocabulary.PROPtraceOn)) {
198: traceOn = Util.convertBooleanPredicateArg(parameter, value);
199: } else {
200: throw new IllegalParameterException(
201: "Don't recognize configuration parameter "
202: + parameter + " for rule-based reasoner");
203: }
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: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
221: All rights reserved.
222:
223: Redistribution and use in source and binary forms, with or without
224: modification, are permitted provided that the following conditions
225: are met:
226:
227: 1. Redistributions of source code must retain the above copyright
228: notice, this list of conditions and the following disclaimer.
229:
230: 2. Redistributions in binary form must reproduce the above copyright
231: notice, this list of conditions and the following disclaimer in the
232: documentation and/or other materials provided with the distribution.
233:
234: 3. The name of the author may not be used to endorse or promote products
235: derived from this software without specific prior written permission.
236:
237: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
238: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
239: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
240: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
241: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
242: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
243: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
245: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
246: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
247: */
|