KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > impl > oldCode > FBLPRuleInfGraph


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

10 package com.hp.hpl.jena.reasoner.rulesys.impl.oldCode;
11
12 import com.hp.hpl.jena.reasoner.rulesys.*;
13 import com.hp.hpl.jena.reasoner.rulesys.impl.*;
14 import com.hp.hpl.jena.reasoner.*;
15 import com.hp.hpl.jena.graph.*;
16
17 import java.util.*;
18
19 import com.hp.hpl.jena.util.OneToManyMap;
20 import com.hp.hpl.jena.util.iterator.*;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 /**
26  * An inference graph that uses a mixture of forward and backward
27  * chaining rules. This is a temporary harness for testing the LP engine.
28  * When that works the content of this class will be folded into FBRuleInfGraph
29  * and this one will disappear
30  *
31  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
32  * @version $Revision: 1.5 $ on $Date: 2005/02/21 12:18:03 $
33  */

34 public class FBLPRuleInfGraph extends FBRuleInfGraph {
35     
36     /** The core backward rule engine which includes all the memoized results */
37     protected LPBRuleEngine lpbEngine;
38     
39     static Log logger = LogFactory.getLog(FBLPRuleInfGraph.class);
40
41 // =======================================================================
42
// Constructors
43

44     /**
45      * Constructor.
46      * @param reasoner the reasoner which created this inf graph instance
47      * @param schema the (optional) schema graph to be included
48      */

49     public FBLPRuleInfGraph(Reasoner reasoner, Graph schema) {
50         super(reasoner, schema);
51         initLP(schema);
52     }
53
54     /**
55      * Constructor.
56      * @param reasoner the reasoner which created this inf graph instance
57      * @param rules the rules to process
58      * @param schema the (optional) schema graph to be included
59      */

60     public FBLPRuleInfGraph(Reasoner reasoner, List rules, Graph schema) {
61         super(reasoner, rules, schema);
62         initLP(schema);
63     }
64
65     /**
66      * Constructor.
67      * @param reasoner the reasoner which created this inf graph instance
68      * @param rules the rules to process
69      * @param schema the (optional) schema graph to be included
70      * @param data the data graph to be processed
71      */

72     public FBLPRuleInfGraph(Reasoner reasoner, List rules, Graph schema, Graph data) {
73         super(reasoner, rules, schema, data);
74         initLP(schema);
75     }
76
77     /**
78      * Initialize the LP engine, based on an optional schema graph.
79      */

80     private void initLP(Graph schema) {
81         if (schema != null && schema instanceof FBLPRuleInfGraph) {
82             LPRuleStore newStore = new LPRuleStore();
83             newStore.addAll(((FBLPRuleInfGraph)schema).lpbEngine.getRuleStore());
84             lpbEngine = new LPBRuleEngine(this, newStore);
85         } else {
86             lpbEngine = new LPBRuleEngine(this);
87         }
88     }
89     
90 // =======================================================================
91
// Interface between infGraph and the goal processing machinery
92

93                
94     /**
95      * Process a call to a builtin predicate
96      * @param clause the Functor representing the call
97      * @param env the BindingEnvironment for this call
98      * @param rule the rule which is invoking this call
99      * @return true if the predicate succeeds
100      */

101     public boolean processBuiltin(Object JavaDoc clause, Rule rule, BindingEnvironment env) {
102         throw new ReasonerException("Internal error in FBLP rule engine, incorrect invocation of building in rule " + rule);
103     }
104     
105     /**
106      * Adds a new Backward rule as a rusult of a forward rule process. Only some
107      * infgraphs support this.
108      */

109     public void addBRule(Rule brule) {
110 // logger.debug("Adding rule " + brule);
111
lpbEngine.addRule(brule);
112         lpbEngine.reset();
113     }
114        
115     /**
116      * Deletes a new Backward rule as a rules of a forward rule process. Only some
117      * infgraphs support this.
118      */

119     public void deleteBRule(Rule brule) {
120 // logger.debug("Deleting rule " + brule);
121
lpbEngine.deleteRule(brule);
122         lpbEngine.reset();
123     }
124     
125     /**
126      * Adds a set of new Backward rules
127      */

128     public void addBRules(List rules) {
129         for (Iterator i = rules.iterator(); i.hasNext(); ) {
130             Rule rule = (Rule)i.next();
131 // logger.debug("Adding rule " + rule);
132
lpbEngine.addRule(rule);
133         }
134         lpbEngine.reset();
135     }
136     
137     /**
138      * Return an ordered list of all registered backward rules. Includes those
139      * generated by forward productions.
140      */

141     public List getBRules() {
142         return lpbEngine.getAllRules();
143     }
144        
145     /**
146      * Set a predicate to be tabled/memoized by the LP engine.
147      */

148     public void setTabled(Node predicate) {
149         lpbEngine.tablePredicate(predicate);
150         if (traceOn) {
151             logger.info("LP TABLE " + predicate);
152         }
153     }
154     
155 // =======================================================================
156
// Core inf graph methods
157

158     /**
159      * Cause the inference graph to reconsult the underlying graph to take
160      * into account changes. Normally changes are made through the InfGraph's add and
161      * remove calls are will be handled appropriately. However, in some cases changes
162      * are made "behind the InfGraph's back" and this forces a full reconsult of
163      * the changed data.
164      */

165     public void rebind() {
166         if (lpbEngine != null) lpbEngine.reset();
167         isPrepared = false;
168     }
169     
170     /**
171      * Set the state of the trace flag. If set to true then rule firings
172      * are logged out to the Log at "INFO" level.
173      */

174     public void setTraceOn(boolean state) {
175         super.setTraceOn(state);
176         lpbEngine.setTraceOn(state);
177     }
178
179     /**
180      * Set to true to enable derivation caching
181      */

182     public void setDerivationLogging(boolean recordDerivations) {
183         this.recordDerivations = recordDerivations;
184         engine.setDerivationLogging(recordDerivations);
185         lpbEngine.setDerivationLogging(recordDerivations);
186         if (recordDerivations) {
187             derivations = new OneToManyMap();
188         } else {
189             derivations = null;
190         }
191     }
192    
193     /**
194      * Return the number of rules fired since this rule engine instance
195      * was created and initialized
196      */

197     public long getNRulesFired() {
198         return engine.getNRulesFired();
199     }
200     
201     /**
202      * Extended find interface used in situations where the implementator
203      * may or may not be able to answer the complete query. It will
204      * attempt to answer the pattern but if its answers are not known
205      * to be complete then it will also pass the request on to the nested
206      * Finder to append more results.
207      * @param pattern a TriplePattern to be matched against the data
208      * @param continuation either a Finder or a normal Graph which
209      * will be asked for additional match results if the implementor
210      * may not have completely satisfied the query.
211      */

212     public ExtendedIterator findWithContinuation(TriplePattern pattern, Finder continuation) {
213         checkOpen();
214 // System.out.println("FBLP find called on: " + pattern);
215
if (!isPrepared) prepare();
216         ExtendedIterator result = new UniqueExtendedIterator(lpbEngine.find(pattern));
217         if (continuation != null) {
218             result = result.andThen(continuation.find(pattern));
219         }
220         return result.filterDrop(Functor.acceptFilter);
221     }
222    
223     /**
224      * Flush out all cached results. Future queries have to start from scratch.
225      */

226     public void reset() {
227         lpbEngine.reset();
228         isPrepared = false;
229     }
230
231     /**
232      * Add one triple to the data graph, run any rules triggered by
233      * the new data item, recursively adding any generated triples.
234      */

235     public synchronized void performAdd(Triple t) {
236         fdata.getGraph().add(t);
237         if (useTGCCaching) {
238             if (transitiveEngine.add(t)) isPrepared = false;
239         }
240         if (isPrepared) {
241             engine.add(t);
242         }
243         lpbEngine.reset();
244     }
245
246     /**
247      * Removes the triple t (if possible) from the set belonging to this graph.
248      */

249     public void performDelete(Triple t) {
250         fdata.getGraph().delete(t);
251         if (useTGCCaching) {
252             if (transitiveEngine.delete(t)) {
253                 if (isPrepared) {
254                     bEngine.deleteAllRules();
255                 }
256                 isPrepared = false;
257             }
258         }
259         if (isPrepared) {
260             getDeductionsGraph().delete(t);
261             engine.delete(t);
262         }
263         lpbEngine.reset();
264     }
265     
266 // =======================================================================
267
// Support for LP engine profiling
268

269     /**
270      * Reset the LP engine profile.
271      * @param enable it true then profiling will continue with a new empty profile table,
272      * if false profiling will stop all current data lost.
273      */

274     public void resetLPProfile(boolean enable) {
275         lpbEngine.resetProfile(enable);
276     }
277     
278     /**
279      * Print a profile of LP rules used since the last reset.
280      */

281     public void printLPProfile() {
282         lpbEngine.printProfile();
283     }
284         
285 }
286
287
288
289 /*
290     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
291     All rights reserved.
292
293     Redistribution and use in source and binary forms, with or without
294     modification, are permitted provided that the following conditions
295     are met:
296
297     1. Redistributions of source code must retain the above copyright
298        notice, this list of conditions and the following disclaimer.
299
300     2. Redistributions in binary form must reproduce the above copyright
301        notice, this list of conditions and the following disclaimer in the
302        documentation and/or other materials provided with the distribution.
303
304     3. The name of the author may not be used to endorse or promote products
305        derived from this software without specific prior written permission.
306
307     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
308     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
309     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
310     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
311     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
312     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
313     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
314     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
315     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
316     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
317 */
Popular Tags