001: /******************************************************************
002: * File: RETERuleContext.java
003: * Created by: Dave Reynolds
004: * Created on: 09-Jun-2003
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: RETERuleContext.java,v 1.11 2008/01/02 12:06:15 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.rulesys.impl;
010:
011: import com.hp.hpl.jena.reasoner.*;
012: import com.hp.hpl.jena.reasoner.rulesys.*;
013: import com.hp.hpl.jena.util.iterator.ClosableIterator;
014: import com.hp.hpl.jena.graph.*;
015:
016: /**
017: * An implementation of the generic RuleContext for use in the RETE implementation.
018: * The RuleContext is used to supply context information to the builtin operations.
019: *
020: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
021: * @version $Revision: 1.11 $ on $Date: 2008/01/02 12:06:15 $
022: */
023: public class RETERuleContext implements RuleContext {
024:
025: /** The binding environment which represents the state of the current rule execution. */
026: protected BindingEnvironment env;
027:
028: /** The rule current being executed. */
029: protected Rule rule;
030:
031: /** The enclosing inference graph. */
032: protected ForwardRuleInfGraphI graph;
033:
034: /** The RETE engine associated with the inference graph */
035: protected RETEEngine engine;
036:
037: /**
038: * Constructor.
039: * @param graph the inference graph which owns this context.
040: */
041: public RETERuleContext(ForwardRuleInfGraphI graph, RETEEngine engine) {
042: this .graph = graph;
043: this .engine = engine;
044: }
045:
046: /**
047: * Returns the current variable binding environment for the current rule.
048: * @return BindingEnvironment
049: */
050: public BindingEnvironment getEnv() {
051: return env;
052: }
053:
054: /**
055: * Returns the graph.
056: * @return InfGraph
057: */
058: public InfGraph getGraph() {
059: return graph;
060: }
061:
062: /**
063: * Returns the RETE engine associated with this context.
064: */
065: public RETEEngine getEngine() {
066: return engine;
067: }
068:
069: /**
070: * Returns the rule.
071: * @return Rule
072: */
073: public Rule getRule() {
074: return rule;
075: }
076:
077: /**
078: * Sets the rule.
079: * @param rule The rule to set
080: */
081: public void setRule(Rule rule) {
082: this .rule = rule;
083: }
084:
085: /**
086: * Sets the current binding environment for this context.
087: * @param env the binding environment so far
088: */
089: public void setEnv(BindingEnvironment env) {
090: this .env = env;
091: }
092:
093: /**
094: * Return true if the triple is already in either the graph or the stack.
095: * I.e. it has already been deduced.
096: */
097: public boolean contains(Triple t) {
098: // Can't use stackCache.contains because that does not do semantic equality
099: return contains(t.getSubject(), t.getPredicate(), t.getObject());
100: }
101:
102: /**
103: * Return true if the triple pattern is already in either the graph or the stack.
104: * I.e. it has already been deduced.
105: */
106: public boolean contains(Node s, Node p, Node o) {
107: ClosableIterator it = find(s, p, o);
108: boolean result = it.hasNext();
109: it.close();
110: return result;
111: }
112:
113: /**
114: * In some formulations the context includes deductions that are not yet
115: * visible to the underlying graph but need to be checked for.
116: * However, currently this calls the graph find directly.
117: */
118: public ClosableIterator find(Node s, Node p, Node o) {
119: //return graph.find(s, p, o).andThen(pendingCache.find(s, p, o));
120: return graph.findDataMatches(s, p, o);
121: }
122:
123: /**
124: * Assert a new triple in the deduction graph, bypassing any processing machinery.
125: */
126: public void silentAdd(Triple t) {
127: ((SilentAddI) graph).silentAdd(t);
128: }
129:
130: /**
131: * Remove a triple from the deduction graph (and the original graph if relevant).
132: */
133: public void remove(Triple t) {
134: graph.getRawGraph().delete(t);
135: engine.deleteTriple(t, true);
136: }
137:
138: /**
139: * Assert a new triple in the deduction graph, triggering any consequent processing as appropriate.
140: */
141: public void add(Triple t) {
142: engine.addTriple(t, true);
143: }
144:
145: /**
146: * Check whether the rule should fire in this context.
147: */
148: public boolean shouldFire(boolean allowUnsafe) {
149: // Check any non-pattern clauses
150: for (int i = 0; i < rule.bodyLength(); i++) {
151: Object clause = rule.getBodyElement(i);
152: if (clause instanceof Functor) {
153: // Fire a built in
154: if (allowUnsafe) {
155: if (!((Functor) clause).evalAsBodyClause(this )) {
156: // Failed guard so just discard and return
157: return false;
158: }
159: } else {
160: // Don't re-run side-effectful clause on a re-run
161: if (!((Functor) clause).safeEvalAsBodyClause(this )) {
162: // Failed guard so just discard and return
163: return false;
164: }
165: }
166: }
167: }
168: return true;
169: }
170:
171: /**
172: * Check if a rule from the conflict set is still OK to fire.
173: * Just checks the non-monotonic guards such as noValue.
174: */
175: public boolean shouldStillFire() {
176: // Check any non-pattern clauses
177: for (int i = 0; i < rule.bodyLength(); i++) {
178: Object clause = rule.getBodyElement(i);
179: if (clause instanceof Functor) {
180: Builtin builtin = ((Functor) clause).getImplementor();
181: if (builtin != null && !builtin.isMonotonic()) {
182: if (!((Functor) clause).evalAsBodyClause(this )) {
183: return false;
184: }
185: }
186: }
187: }
188: return true;
189: }
190:
191: }
192:
193: /*
194: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
195: All rights reserved.
196:
197: Redistribution and use in source and binary forms, with or without
198: modification, are permitted provided that the following conditions
199: are met:
200:
201: 1. Redistributions of source code must retain the above copyright
202: notice, this list of conditions and the following disclaimer.
203:
204: 2. Redistributions in binary form must reproduce the above copyright
205: notice, this list of conditions and the following disclaimer in the
206: documentation and/or other materials provided with the distribution.
207:
208: 3. The name of the author may not be used to endorse or promote products
209: derived from this software without specific prior written permission.
210:
211: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
212: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
213: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
214: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
215: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
216: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
217: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
218: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
219: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
220: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
221: */
|