KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************
2  * File: BBRuleContext.java
3  * Created by: Dave Reynolds
4  * Created on: 05-May-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: BBRuleContext.java,v 1.10 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.graph.Node;
13 import com.hp.hpl.jena.graph.Triple;
14 import com.hp.hpl.jena.reasoner.*;
15 import com.hp.hpl.jena.reasoner.rulesys.*;
16 import com.hp.hpl.jena.util.iterator.ClosableIterator;
17
18 /**
19  * Implementation of RuleContext for use in the backward chaining
20  * interpreter. The RuleContext allows builtin predicates to
21  * interpret variable bindings to access the static triple data.
22  *
23  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
24  * @version $Revision: 1.10 $ on $Date: 2005/02/21 12:17:38 $
25  */

26 public class BBRuleContext implements RuleContext {
27     
28     /** The binding environment which represents the state of the current rule execution. */
29     protected BindingEnvironment env;
30     
31     /** The rule current being executed. */
32     protected Rule rule;
33     
34     /** The enclosing inference graph. */
35     protected BackwardRuleInfGraphI graph;
36     
37     /**
38      * Construct an empty context. It can't be used until
39      * the rule and environment have been set.
40      */

41     public BBRuleContext(BackwardRuleInfGraphI graph) {
42         this.graph = graph;
43     }
44
45     /**
46      * @see com.hp.hpl.jena.reasoner.rulesys.RuleContext#contains(com.hp.hpl.jena.graph.Node, com.hp.hpl.jena.graph.Node, com.hp.hpl.jena.graph.Node)
47      */

48     public boolean contains(Node s, Node p, Node o) {
49         ClosableIterator i = find(s, p, o);
50         boolean result = i.hasNext();
51         i.close();
52         return result;
53     }
54
55     /**
56      * @see com.hp.hpl.jena.reasoner.rulesys.RuleContext#contains(com.hp.hpl.jena.graph.Triple)
57      */

58     public boolean contains(Triple t) {
59         return contains(t.getSubject(), t.getPredicate(), t.getObject());
60     }
61
62     /**
63      * @see com.hp.hpl.jena.reasoner.rulesys.RuleContext#find(com.hp.hpl.jena.graph.Node, com.hp.hpl.jena.graph.Node, com.hp.hpl.jena.graph.Node)
64      */

65     public ClosableIterator find(Node s, Node p, Node o) {
66         return graph.findDataMatches(new TriplePattern(s, p, o));
67 // return searchpath.find(new TriplePattern(s, p, o));
68
}
69
70     /**
71      * @see com.hp.hpl.jena.reasoner.rulesys.RuleContext#getEnv()
72      */

73     public BindingEnvironment getEnv() {
74         return env;
75     }
76
77     /**
78      * Set the binding environment for the this context
79      */

80     public void setEnv(BindingEnvironment env) {
81         this.env = env;
82     }
83     
84     /**
85      * @see com.hp.hpl.jena.reasoner.rulesys.RuleContext#getGraph()
86      */

87     public InfGraph getGraph() {
88         return graph;
89     }
90
91     /**
92      * @see com.hp.hpl.jena.reasoner.rulesys.RuleContext#getRule()
93      */

94     public Rule getRule() {
95         return rule;
96     }
97
98     /**
99      * @see com.hp.hpl.jena.reasoner.rulesys.RuleContext#setRule(com.hp.hpl.jena.reasoner.rulesys.Rule)
100      */

101     public void setRule(Rule rule) {
102         this.rule = rule;
103     }
104     
105     /**
106      * Assert a new triple in the deduction graph, bypassing any processing machinery.
107      */

108     public void silentAdd(Triple t) {
109         ((SilentAddI)graph).silentAdd(t);
110     }
111
112     /**
113      * Assert a new triple in the deduction graph, triggering any consequent processing as appropriate.
114      * In the backward case there no immediate consequences so this is equivalent to a silentAdd.
115      */

116     public void add(Triple t) {
117         ((SilentAddI)graph).silentAdd(t);
118     }
119
120     /**
121      * Remove a triple from the deduction graph (and the original graph if relevant).
122      */

123     public void remove(Triple t) {
124         graph.delete(t);
125     }
126
127     /**
128      * Retrieve or create a bNode representing an inferred property value.
129      * This is currently only available on backward contexts and not part of the
130      * normal RuleContext interface.
131      * @param instance the base instance node to which the property applies
132      * @param prop the property node whose value is being inferred
133      * @param pclass the (optional, can be null) class for the inferred value.
134      * @return the bNode representing the property value
135      */

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