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