001: /******************************************************************
002: * File: FBLPRuleInfGraph.java
003: * Created by: Dave Reynolds
004: * Created on: 26-Jul-2003
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: FBLPRuleInfGraph.java,v 1.8 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.reasoner.rulesys.*;
012: import com.hp.hpl.jena.reasoner.rulesys.impl.*;
013: import com.hp.hpl.jena.reasoner.*;
014: import com.hp.hpl.jena.graph.*;
015:
016: import java.util.*;
017:
018: import com.hp.hpl.jena.util.OneToManyMap;
019: import com.hp.hpl.jena.util.iterator.*;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023:
024: /**
025: * An inference graph that uses a mixture of forward and backward
026: * chaining rules. This is a temporary harness for testing the LP engine.
027: * When that works the content of this class will be folded into FBRuleInfGraph
028: * and this one will disappear
029: *
030: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
031: * @version $Revision: 1.8 $ on $Date: 2008/01/02 12:09:44 $
032: */
033: public class FBLPRuleInfGraph extends FBRuleInfGraph {
034:
035: /** The core backward rule engine which includes all the memoized results */
036: protected LPBRuleEngine lpbEngine;
037:
038: static Log logger = LogFactory.getLog(FBLPRuleInfGraph.class);
039:
040: // =======================================================================
041: // Constructors
042:
043: /**
044: * Constructor.
045: * @param reasoner the reasoner which created this inf graph instance
046: * @param schema the (optional) schema graph to be included
047: */
048: public FBLPRuleInfGraph(Reasoner reasoner, Graph schema) {
049: super (reasoner, schema);
050: initLP(schema);
051: }
052:
053: /**
054: * Constructor.
055: * @param reasoner the reasoner which created this inf graph instance
056: * @param rules the rules to process
057: * @param schema the (optional) schema graph to be included
058: */
059: public FBLPRuleInfGraph(Reasoner reasoner, List rules, Graph schema) {
060: super (reasoner, rules, schema);
061: initLP(schema);
062: }
063:
064: /**
065: * Constructor.
066: * @param reasoner the reasoner which created this inf graph instance
067: * @param rules the rules to process
068: * @param schema the (optional) schema graph to be included
069: * @param data the data graph to be processed
070: */
071: public FBLPRuleInfGraph(Reasoner reasoner, List rules,
072: Graph schema, Graph data) {
073: super (reasoner, rules, schema, data);
074: initLP(schema);
075: }
076:
077: /**
078: * Initialize the LP engine, based on an optional schema graph.
079: */
080: private void initLP(Graph schema) {
081: if (schema != null && schema instanceof FBLPRuleInfGraph) {
082: LPRuleStore newStore = new LPRuleStore();
083: newStore.addAll(((FBLPRuleInfGraph) schema).lpbEngine
084: .getRuleStore());
085: lpbEngine = new LPBRuleEngine(this , newStore);
086: } else {
087: lpbEngine = new LPBRuleEngine(this );
088: }
089: }
090:
091: // =======================================================================
092: // Interface between infGraph and the goal processing machinery
093:
094: /**
095: * Process a call to a builtin predicate
096: * @param clause the Functor representing the call
097: * @param env the BindingEnvironment for this call
098: * @param rule the rule which is invoking this call
099: * @return true if the predicate succeeds
100: */
101: public boolean processBuiltin(Object clause, Rule rule,
102: BindingEnvironment env) {
103: throw new ReasonerException(
104: "Internal error in FBLP rule engine, incorrect invocation of building in rule "
105: + rule);
106: }
107:
108: /**
109: * Adds a new Backward rule as a rusult of a forward rule process. Only some
110: * infgraphs support this.
111: */
112: public void addBRule(Rule brule) {
113: // logger.debug("Adding rule " + brule);
114: lpbEngine.addRule(brule);
115: lpbEngine.reset();
116: }
117:
118: /**
119: * Deletes a new Backward rule as a rules of a forward rule process. Only some
120: * infgraphs support this.
121: */
122: public void deleteBRule(Rule brule) {
123: // logger.debug("Deleting rule " + brule);
124: lpbEngine.deleteRule(brule);
125: lpbEngine.reset();
126: }
127:
128: /**
129: * Adds a set of new Backward rules
130: */
131: public void addBRules(List rules) {
132: for (Iterator i = rules.iterator(); i.hasNext();) {
133: Rule rule = (Rule) i.next();
134: // logger.debug("Adding rule " + rule);
135: lpbEngine.addRule(rule);
136: }
137: lpbEngine.reset();
138: }
139:
140: /**
141: * Return an ordered list of all registered backward rules. Includes those
142: * generated by forward productions.
143: */
144: public List getBRules() {
145: return lpbEngine.getAllRules();
146: }
147:
148: /**
149: * Set a predicate to be tabled/memoized by the LP engine.
150: */
151: public void setTabled(Node predicate) {
152: lpbEngine.tablePredicate(predicate);
153: if (traceOn) {
154: logger.info("LP TABLE " + predicate);
155: }
156: }
157:
158: // =======================================================================
159: // Core inf graph methods
160:
161: /**
162: * Cause the inference graph to reconsult the underlying graph to take
163: * into account changes. Normally changes are made through the InfGraph's add and
164: * remove calls are will be handled appropriately. However, in some cases changes
165: * are made "behind the InfGraph's back" and this forces a full reconsult of
166: * the changed data.
167: */
168: public void rebind() {
169: if (lpbEngine != null)
170: lpbEngine.reset();
171: isPrepared = false;
172: }
173:
174: /**
175: * Set the state of the trace flag. If set to true then rule firings
176: * are logged out to the Log at "INFO" level.
177: */
178: public void setTraceOn(boolean state) {
179: super .setTraceOn(state);
180: lpbEngine.setTraceOn(state);
181: }
182:
183: /**
184: * Set to true to enable derivation caching
185: */
186: public void setDerivationLogging(boolean recordDerivations) {
187: this .recordDerivations = recordDerivations;
188: engine.setDerivationLogging(recordDerivations);
189: lpbEngine.setDerivationLogging(recordDerivations);
190: if (recordDerivations) {
191: derivations = new OneToManyMap();
192: } else {
193: derivations = null;
194: }
195: }
196:
197: /**
198: * Return the number of rules fired since this rule engine instance
199: * was created and initialized
200: */
201: public long getNRulesFired() {
202: return engine.getNRulesFired();
203: }
204:
205: /**
206: * Extended find interface used in situations where the implementator
207: * may or may not be able to answer the complete query. It will
208: * attempt to answer the pattern but if its answers are not known
209: * to be complete then it will also pass the request on to the nested
210: * Finder to append more results.
211: * @param pattern a TriplePattern to be matched against the data
212: * @param continuation either a Finder or a normal Graph which
213: * will be asked for additional match results if the implementor
214: * may not have completely satisfied the query.
215: */
216: public ExtendedIterator findWithContinuation(TriplePattern pattern,
217: Finder continuation) {
218: checkOpen();
219: // System.out.println("FBLP find called on: " + pattern);
220: if (!isPrepared)
221: prepare();
222: ExtendedIterator result = new UniqueExtendedIterator(lpbEngine
223: .find(pattern));
224: if (continuation != null) {
225: result = result.andThen(continuation.find(pattern));
226: }
227: return result.filterDrop(Functor.acceptFilter);
228: }
229:
230: /**
231: * Flush out all cached results. Future queries have to start from scratch.
232: */
233: public void reset() {
234: lpbEngine.reset();
235: isPrepared = false;
236: }
237:
238: /**
239: * Add one triple to the data graph, run any rules triggered by
240: * the new data item, recursively adding any generated triples.
241: */
242: public synchronized void performAdd(Triple t) {
243: fdata.getGraph().add(t);
244: if (useTGCCaching) {
245: if (transitiveEngine.add(t))
246: isPrepared = false;
247: }
248: if (isPrepared) {
249: engine.add(t);
250: }
251: lpbEngine.reset();
252: }
253:
254: /**
255: * Removes the triple t (if possible) from the set belonging to this graph.
256: */
257: public void performDelete(Triple t) {
258: fdata.getGraph().delete(t);
259: if (useTGCCaching) {
260: if (transitiveEngine.delete(t)) {
261: if (isPrepared) {
262: bEngine.deleteAllRules();
263: }
264: isPrepared = false;
265: }
266: }
267: if (isPrepared) {
268: getDeductionsGraph().delete(t);
269: engine.delete(t);
270: }
271: lpbEngine.reset();
272: }
273:
274: // =======================================================================
275: // Support for LP engine profiling
276:
277: /**
278: * Reset the LP engine profile.
279: * @param enable it true then profiling will continue with a new empty profile table,
280: * if false profiling will stop all current data lost.
281: */
282: public void resetLPProfile(boolean enable) {
283: lpbEngine.resetProfile(enable);
284: }
285:
286: /**
287: * Print a profile of LP rules used since the last reset.
288: */
289: public void printLPProfile() {
290: lpbEngine.printProfile();
291: }
292:
293: }
294:
295: /*
296: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
297: All rights reserved.
298:
299: Redistribution and use in source and binary forms, with or without
300: modification, are permitted provided that the following conditions
301: are met:
302:
303: 1. Redistributions of source code must retain the above copyright
304: notice, this list of conditions and the following disclaimer.
305:
306: 2. Redistributions in binary form must reproduce the above copyright
307: notice, this list of conditions and the following disclaimer in the
308: documentation and/or other materials provided with the distribution.
309:
310: 3. The name of the author may not be used to endorse or promote products
311: derived from this software without specific prior written permission.
312:
313: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
314: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
315: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
316: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
317: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
318: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
319: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
320: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
321: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
322: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
323: */
|