001: /******************************************************************
002: * File: LPLBackwardRuleReasoner.java
003: * Created by: Dave Reynolds
004: * Created on: 25-Jul-2003
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: LPBackwardRuleReasoner.java,v 1.11 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.reasoner.rulesys.impl.*;
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 LP 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:07:47 $
027: */
028: public class LPBackwardRuleReasoner 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 LPRuleStore 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 LPBackwardRuleReasoner(List rules) {
057: this .rules = rules;
058: ruleStore = new LPRuleStore(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 LPBackwardRuleReasoner(List rules, ReasonerFactory factory) {
067: this .rules = rules;
068: this .factory = factory;
069: ruleStore = new LPRuleStore(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 LPBackwardRuleReasoner(LPBackwardRuleReasoner parent,
077: 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: * Register an RDF predicate as one whose presence in a goal should force
110: * the goal to be tabled.
111: */
112: public synchronized void tablePredicate(Node predicate) {
113: ruleStore.tablePredicate(predicate);
114: }
115:
116: /**
117: * Determine whether the given property is recognized and treated specially
118: * by this reasoner. This is a convenience packaging of a special case of getCapabilities.
119: * @param property the property which we want to ask the reasoner about, given as a Node since
120: * this is part of the SPI rather than API
121: * @return true if the given property is handled specially by the reasoner.
122: */
123: public boolean supportsProperty(Property property) {
124: if (factory == null)
125: return false;
126: Model caps = factory.getCapabilities();
127: Resource root = caps.getResource(factory.getURI());
128: return caps.contains(root, ReasonerVocabulary.supportsP,
129: 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 ,
161: ruleStore, data, schemaGraph);
162: graph.setDerivationLogging(recordDerivations);
163: return graph;
164: }
165:
166: /**
167: * Return the this of Rules used by this reasoner
168: * @return a List of Rule objects
169: */
170: public List getRules() {
171: return rules;
172: }
173:
174: /**
175: * Switch on/off drivation logging.
176: * If set to true then the InfGraph created from the bind operation will start
177: * life with recording of derivations switched on. This is currently only of relevance
178: * to rule-based reasoners.
179: * <p>
180: * Default - false.
181: */
182: public void setDerivationLogging(boolean logOn) {
183: recordDerivations = logOn;
184: }
185:
186: /**
187: * Set the state of the trace flag. If set to true then rule firings
188: * are logged out to the Log at "INFO" level.
189: */
190: public void setTraceOn(boolean state) {
191: traceOn = state;
192: }
193:
194: /**
195: * Set a configuration paramter for the reasoner. In the case of the this
196: * reasoner there are no configuration parameters and this method is simply
197: * here to meet the interfaces specification
198: *
199: * @param parameter the property identifying the parameter to be changed
200: * @param value the new value for the parameter, typically this is a wrapped
201: * java object like Boolean or Integer.
202: */
203: public void setParameter(Property parameter, Object value) {
204: throw new IllegalParameterException(parameter.toString());
205: }
206:
207: /**
208: * Return the Jena Graph Capabilties that the inference graphs generated
209: * by this reasoner are expected to conform to.
210: */
211: public Capabilities getGraphCapabilities() {
212: if (capabilities == null) {
213: capabilities = new BaseInfGraph.InfCapabilities();
214: }
215: return capabilities;
216: }
217:
218: }
219:
220: /*
221: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
222: All rights reserved.
223:
224: Redistribution and use in source and binary forms, with or without
225: modification, are permitted provided that the following conditions
226: are met:
227:
228: 1. Redistributions of source code must retain the above copyright
229: notice, this list of conditions and the following disclaimer.
230:
231: 2. Redistributions in binary form must reproduce the above copyright
232: notice, this list of conditions and the following disclaimer in the
233: documentation and/or other materials provided with the distribution.
234:
235: 3. The name of the author may not be used to endorse or promote products
236: derived from this software without specific prior written permission.
237:
238: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
239: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
240: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
241: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
242: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
243: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
244: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
246: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
247: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
248: */
|