KickJava   Java API By Example, From Geeks To Geeks.

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


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

10 package com.hp.hpl.jena.reasoner.rulesys.impl;
11
12 import com.hp.hpl.jena.reasoner.*;
13 import com.hp.hpl.jena.reasoner.rulesys.*;
14 import com.hp.hpl.jena.util.iterator.ClosableIterator;
15 import com.hp.hpl.jena.graph.*;
16
17 /**
18  * An implementation of the generic RuleContext for use in the RETE implementation.
19  * The RuleContext is used to supply context information to the builtin operations.
20  *
21  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
22  * @version $Revision: 1.7 $ on $Date: 2005/02/21 12:17:58 $
23  */

24 public class RETERuleContext implements RuleContext {
25     
26     /** The binding environment which represents the state of the current rule execution. */
27     protected BindingEnvironment env;
28     
29     /** The rule current being executed. */
30     protected Rule rule;
31     
32     /** The enclosing inference graph. */
33     protected ForwardRuleInfGraphI graph;
34     
35     /** The RETE engine associated with the inference graph */
36     protected RETEEngine engine;
37     
38     /**
39      * Constructor.
40      * @param graph the inference graph which owns this context.
41      */

42     public RETERuleContext(ForwardRuleInfGraphI graph, RETEEngine engine) {
43         this.graph = graph;
44         this.engine = engine;
45     }
46     
47     /**
48      * Returns the current variable binding environment for the current rule.
49      * @return BindingEnvironment
50      */

51     public BindingEnvironment getEnv() {
52         return env;
53     }
54
55     /**
56      * Returns the graph.
57      * @return InfGraph
58      */

59     public InfGraph getGraph() {
60         return graph;
61     }
62     
63     /**
64      * Returns the RETE engine associated with this context.
65      */

66     public RETEEngine getEngine() {
67         return engine;
68     }
69
70     /**
71      * Returns the rule.
72      * @return Rule
73      */

74     public Rule getRule() {
75         return rule;
76     }
77
78     /**
79      * Sets the rule.
80      * @param rule The rule to set
81      */

82     public void setRule(Rule rule) {
83         this.rule = rule;
84     }
85
86     /**
87      * Sets the current binding environment for this context.
88      * @param env the binding environment so far
89      */

90     public void setEnv(BindingEnvironment env) {
91         this.env = env;
92     }
93     
94     /**
95      * Return true if the triple is already in either the graph or the stack.
96      * I.e. it has already been deduced.
97      */

98     public boolean contains(Triple t) {
99         // Can't use stackCache.contains because that does not do semantic equality
100
return contains(t.getSubject(), t.getPredicate(), t.getObject());
101     }
102     
103     /**
104      * Return true if the triple pattern is already in either the graph or the stack.
105      * I.e. it has already been deduced.
106      */

107     public boolean contains(Node s, Node p, Node o) {
108         ClosableIterator it = find(s, p, o);
109         boolean result = it.hasNext();
110         it.close();
111         return result;
112     }
113     
114     /**
115      * In some formulations the context includes deductions that are not yet
116      * visible to the underlying graph but need to be checked for.
117      * However, currently this calls the graph find directly.
118      */

119     public ClosableIterator find(Node s, Node p, Node o) {
120         //return graph.find(s, p, o).andThen(pendingCache.find(s, p, o));
121
return graph.findDataMatches(s, p, o);
122     }
123     
124     /**
125      * Assert a new triple in the deduction graph, bypassing any processing machinery.
126      */

127     public void silentAdd(Triple t) {
128         ((SilentAddI)graph).silentAdd(t);
129     }
130
131     /**
132      * Remove a triple from the deduction graph (and the original graph if relevant).
133      */

134     public void remove(Triple t) {
135         graph.delete(t);
136         engine.deleteTriple(t, true);
137     }
138
139     /**
140      * Assert a new triple in the deduction graph, triggering any consequent processing as appropriate.
141      */

142     public void add(Triple t) {
143         engine.addTriple(t, true);
144     }
145
146 }
147
148
149 /*
150     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
151     All rights reserved.
152
153     Redistribution and use in source and binary forms, with or without
154     modification, are permitted provided that the following conditions
155     are met:
156
157     1. Redistributions of source code must retain the above copyright
158        notice, this list of conditions and the following disclaimer.
159
160     2. Redistributions in binary form must reproduce the above copyright
161        notice, this list of conditions and the following disclaimer in the
162        documentation and/or other materials provided with the distribution.
163
164     3. The name of the author may not be used to endorse or promote products
165        derived from this software without specific prior written permission.
166
167     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
168     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
169     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
170     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
171     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
172     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
173     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
174     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
175     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
176     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
177 */
Popular Tags