KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************
2  * File: RuleContext.java
3  * Created by: Dave Reynolds
4  * Created on: 15-Apr-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: BFRuleContext.java,v 1.13 2005/02/21 12:17:38 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys.impl;
11
12 import com.hp.hpl.jena.mem.GraphMem;
13 import com.hp.hpl.jena.reasoner.*;
14 import com.hp.hpl.jena.reasoner.rulesys.*;
15 import com.hp.hpl.jena.util.PrintUtil;
16 import com.hp.hpl.jena.util.iterator.ClosableIterator;
17 import com.hp.hpl.jena.graph.*;
18 import java.util.*;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 /**
23  * An implementation of the generic RuleContext interface used by
24  * the basic forward (BF) rule engine. This provides additional
25  * methods specific to the functioning of that engine.
26  *
27  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
28  * @version $Revision: 1.13 $ on $Date: 2005/02/21 12:17:38 $
29  */

30 public class BFRuleContext implements RuleContext {
31     /** The binding environment which represents the state of the current rule execution. */
32     protected BindingStack env;
33     
34     /** The rule current being executed. */
35     protected Rule rule;
36     
37     /** The enclosing inference graph. */
38     protected ForwardRuleInfGraphI graph;
39     
40     /** A stack of triples which have been added to the graph but haven't yet been processed. */
41     protected List stack;
42     
43     /** A temporary list of Triples which will be added to the stack and triples at the end of a rule scan */
44     protected List pending;
45
46     /** A temporary list of Triples which will be removed from the graph at the end of a rule scan */
47     protected List deletesPending = new ArrayList();
48
49     /** A searchable index into the pending triples */
50     protected Graph pendingCache;
51     
52     protected static Log logger = LogFactory.getLog(BFRuleContext.class);
53     
54     /**
55      * Constructor.
56      * @param graph the inference graph which owns this context.
57      */

58     public BFRuleContext(ForwardRuleInfGraphI graph) {
59         this.graph = graph;
60         env = new BindingStack();
61         stack = new ArrayList();
62         pending = new ArrayList();
63         pendingCache = new GraphMem();
64     }
65     
66     /**
67      * Returns the current variable binding environment for the current rule.
68      * @return BindingEnvironment
69      */

70     public BindingEnvironment getEnv() {
71         return env;
72     }
73     
74     /**
75      * Variant of the generic getEnv interface specific to the basic
76      * forward rule system.
77      * Returns the current variable binding environment for the current rule.
78      * @return BindingStack
79      */

80     public BindingStack getEnvStack() {
81         return env;
82     }
83
84     /**
85      * Returns the graph.
86      * @return InfGraph
87      */

88     public InfGraph getGraph() {
89         return graph;
90     }
91
92     /**
93      * Returns the rule.
94      * @return Rule
95      */

96     public Rule getRule() {
97         return rule;
98     }
99
100     /**
101      * Sets the rule.
102      * @param rule The rule to set
103      */

104     public void setRule(Rule rule) {
105         this.rule = rule;
106     }
107
108     /**
109      * Add a triple to the stack of triples to waiting to be processed by the rule engine.
110      */

111     public void addTriple(Triple t) {
112         if (graph.shouldTrace()) {
113             if (rule != null) {
114                 logger.debug("Adding to stack (" + rule.toShortString() + "): " + PrintUtil.print(t));
115             } else {
116                 logger.debug("Adding to stack : " + PrintUtil.print(t));
117             }
118         }
119         stack.add(t);
120     }
121     
122     /**
123      * Add a triple to a temporary "pending" store, ready to be added to added to the
124      * deductions graph and the processing stack later.
125      * <p>This is needed to prevent concurrrent modification exceptions which searching
126      * the deductions for matches to a given rule.
127      */

128     public void add(Triple t) {
129         if (graph.shouldTrace()) {
130             if (rule != null) {
131                 logger.debug("Adding to pending (" + rule.toShortString() + "): " + PrintUtil.print(t));
132             } else {
133                 logger.debug("Adding to pending : " + PrintUtil.print(t));
134             }
135         }
136         pending.add(t);
137         //pendingCache.add(t);
138
}
139             
140     /**
141      * Take all the pending triples and add them to both the given graph and
142      * to the processing stack.
143      */

144     public void flushPending() {
145         for (Iterator i = pending.iterator(); i.hasNext(); ) {
146             Triple t = (Triple)i.next();
147             stack.add(t);
148             graph.addDeduction(t);
149             i.remove();
150             // pendingCache.delete(t);
151
}
152         pending.clear();
153         // Flush out pending removes as well
154
for (Iterator i = deletesPending.iterator(); i.hasNext(); ) {
155             Triple t = (Triple)i.next();
156             graph.delete(t);
157         }
158         deletesPending.clear();
159     }
160     
161     /**
162      * Return true if the triple is already in either the graph or the stack.
163      * I.e. it has already been deduced.
164      */

165     public boolean contains(Triple t) {
166         // Can't use stackCache.contains because that does not do semantic equality
167
return contains(t.getSubject(), t.getPredicate(), t.getObject());
168     }
169     
170     /**
171      * Return true if the triple pattern is already in either the graph or the stack.
172      * I.e. it has already been deduced.
173      */

174     public boolean contains(Node s, Node p, Node o) {
175         // Can't use stackCache.contains because that does not do semantic equality
176
ClosableIterator it = find(s, p, o);
177         boolean result = it.hasNext();
178         it.close();
179         return result;
180     }
181     
182     /**
183      * In some formulations the context includes deductions that are not yet
184      * visible to the underlying graph but need to be checked for.
185      * However, currently this calls the graph find directly.
186      */

187     public ClosableIterator find(Node s, Node p, Node o) {
188         //return graph.find(s, p, o).andThen(pendingCache.find(s, p, o));
189
return graph.findDataMatches(s, p, o);
190     }
191     
192     /**
193      * Return the next triple to be added to the graph, removing it from
194      * the stack.
195      * @return the Triple or null if there are no more
196      */

197     public Triple getNextTriple() {
198         if (stack.size() > 0) {
199             Triple t = (Triple)stack.remove(stack.size() - 1);
200             return t;
201         } else {
202             return null;
203         }
204     }
205     
206     /**
207      * Reset the binding environemnt back to empty
208      */

209     public void resetEnv() {
210         env.reset();
211     }
212     
213     /**
214      * Assert a new triple in the deduction graph, bypassing any processing machinery.
215      */

216     public void silentAdd(Triple t) {
217         ((SilentAddI)graph).silentAdd(t);
218     }
219
220     /**
221      * Remove a triple from the deduction graph (and the original graph if relevant).
222      */

223     public void remove(Triple t) {
224         deletesPending.add(t);
225 // graph.delete(t);
226
}
227
228 }
229
230 /*
231     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
232     All rights reserved.
233
234     Redistribution and use in source and binary forms, with or without
235     modification, are permitted provided that the following conditions
236     are met:
237
238     1. Redistributions of source code must retain the above copyright
239        notice, this list of conditions and the following disclaimer.
240
241     2. Redistributions in binary form must reproduce the above copyright
242        notice, this list of conditions and the following disclaimer in the
243        documentation and/or other materials provided with the distribution.
244
245     3. The name of the author may not be used to endorse or promote products
246        derived from this software without specific prior written permission.
247
248     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
249     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
250     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
251     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
252     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
253     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
254     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
255     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
257     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
258 */

259
Popular Tags