KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************
2  * File: Trail.java
3  * Created by: Dave Reynolds
4  * Created on: 20-May-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: Trail.java,v 1.4 2005/02/21 12:18:06 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.*;
14 import com.hp.hpl.jena.graph.*;
15
16 import java.util.*;
17
18 /**
19  * Representation of a trail of variable bindings. Each rule state has its
20  * own trail segment which is an instance of this class.
21  *
22  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
23  * @version $Revision: 1.4 $ on $Date: 2005/02/21 12:18:06 $
24  */

25 public class Trail implements BindingEnvironment {
26     
27     /** A trail of variable bindings made during the processing of this state */
28     protected ArrayList trail = new ArrayList();
29     
30     /**
31      * Unwind all the bindings on the trail.
32      */

33     public void unwindBindings() {
34         for (int i = trail.size() - 1; i >= 0; i--) {
35             TrailEntry entry = (TrailEntry)trail.get(i);
36             entry.var.unbind();
37         }
38     }
39     
40     /**
41      * Unwind all the bindings on the trail the forget them all.
42      */

43     public void unwindAndClear() {
44         for (int i = trail.size() - 1; i >= 0; i--) {
45             TrailEntry entry = (TrailEntry)trail.get(i);
46             entry.var.unbind();
47         }
48         trail.clear();
49     }
50     
51     /**
52      * Restore the set of trail bindings.
53      */

54     public void activate() {
55         for (Iterator i = trail.iterator(); i.hasNext(); ) {
56             TrailEntry entry = (TrailEntry)i.next();
57             entry.var.simpleBind(entry.value);
58         }
59     }
60     
61     /**
62      * Unify two triple patterns recording all of the bindings on the trail.
63      * If the unification fails returns false and the trail is left unchanged.
64      */

65     public boolean unify(TriplePattern t, TriplePattern tp) {
66         int watermark = trail.size();
67         boolean ok = unify(t.getSubject(), tp.getSubject())
68                     && unify(t.getPredicate(), tp.getPredicate())
69                     && unifyObj(t.getObject(), tp.getObject());
70         if (!ok) {
71             for (int i = trail.size() - 1; i >= watermark; i--) {
72                 TrailEntry entry = (TrailEntry)trail.get(i);
73                 entry.var.unbind();
74                 trail.remove(i);
75             }
76         }
77         return ok;
78     }
79
80     /**
81      * Unify a triple against a triple pattern recording all of the bindings on the trail.
82      * If the unification fails returns false and the trail is left unchanged.
83      */

84     public boolean unify(Triple t, TriplePattern tp) {
85         int watermark = trail.size();
86         boolean ok = unify(t.getSubject(), tp.getSubject())
87                     && unify(t.getPredicate(), tp.getPredicate())
88                     && unifyObj(t.getObject(), tp.getObject());
89         if (!ok) {
90             for (int i = trail.size() - 1; i >= watermark; i--) {
91                 TrailEntry entry = (TrailEntry)trail.get(i);
92                 entry.var.unbind();
93                 trail.remove(i);
94             }
95         }
96         return ok;
97     }
98         
99     /**
100      * Unify two nodes, neither can be a literal.
101      */

102     public boolean unify(Node n1, Node n2) {
103         Node dn1 = getGroundVersion(n1);
104         Node dn2 = getGroundVersion(n2);
105         if (dn1 instanceof Node_RuleVariable) {
106             bind(dn1, dn2);
107             return true;
108         } else if (dn2 instanceof Node_RuleVariable) {
109             bind(dn2, dn1);
110             return true;
111         } else {
112             return dn1.sameValueAs(dn2);
113         }
114     }
115         
116     /**
117      * Unify two nodes, can be a literals.
118      */

119     public boolean unifyObj(Node n1, Node n2) {
120         Node dn1 = getGroundVersion(n1);
121         Node dn2 = getGroundVersion(n2);
122         if (dn1 instanceof Node_RuleVariable) {
123             bind(dn1, dn2);
124             return true;
125         } else if (dn2 instanceof Node_RuleVariable) {
126             bind(dn2, dn1);
127             return true;
128         } else {
129             // Both are ground, either functors or literals
130
if (Functor.isFunctor(dn1)) {
131                 if (Functor.isFunctor(dn2)) {
132                     // Unify functors
133
Functor f1 = (Functor)dn1.getLiteral().getValue();
134                     Functor f2 = (Functor)dn2.getLiteral().getValue();
135                     if ( ! f1.getName().equals(f2.getName()) ) return false;
136                     Node[] args1 = f1.getArgs();
137                     Node[] args2 = f2.getArgs();
138                     if (args1.length != args2.length) return false;
139                     for (int i = 0; i < args1.length; i++) {
140                         if (! unify(args1[i], args2[i]) ) return false;
141                     }
142                     return true;
143                 } else {
144                     return false;
145                 }
146             } else {
147                 return dn1.sameValueAs(dn2);
148             }
149         }
150     }
151    
152     /**
153      * Return the most ground version of the node. If the node is not a variable
154      * just return it, if it is a varible bound in this environment return the binding,
155      * if it is an unbound variable return the variable.
156      */

157     public Node getGroundVersion(Node node) {
158         if (node instanceof Node_RuleVariable) {
159             return ((Node_RuleVariable)node).deref();
160         } else {
161             return node;
162         }
163     }
164    
165     /**
166      * Return the most ground version of the node. This extends getGroundVersion by
167      * also grounding any embedded functors.
168      */

169     public Node getMostGroundVersion(Node node) {
170         if (node instanceof Node_RuleVariable) {
171             node = ((Node_RuleVariable)node).deref();
172         }
173         if (Functor.isFunctor(node)) {
174             Functor f = (Functor) node.getLiteral().getValue();
175             Node[] args = f.getArgs();
176             Node[] cargs = new Node[args.length];
177             for (int i = 0; i < args.length; i++) {
178                 cargs[i] = getGroundVersion(args[i]);
179             }
180             return Functor.makeFunctorNode(f.getName(), cargs);
181         } else {
182             return node;
183         }
184     }
185    
186     /**
187      * Return the most ground version of the functor. Only used for pretty printing.
188      */

189     public Functor getMostGroundVersion(Functor f) {
190             Node[] args = f.getArgs();
191             Node[] cargs = new Node[args.length];
192             for (int i = 0; i < args.length; i++) {
193                 cargs[i] = getGroundVersion(args[i]);
194             }
195             return new Functor(f.getName(), cargs);
196     }
197     
198     /**
199      * Bind a variable in the current envionment to the given value.
200      * Checks that the new binding is compatible with any current binding.
201      * @param var a Node_RuleVariable defining the variable to bind
202      * @param value the value to bind
203      * @return false if the binding fails
204      */

205     public boolean bind(Node var, Node value) {
206         if (var == Node_RuleVariable.WILD || value == Node_RuleVariable.WILD) return true;
207 // if (var == Node.ANY || value == Node.ANY) return true;
208
Node dvar = getGroundVersion(var);
209         if (dvar instanceof Node_RuleVariable) {
210             trail.add(new TrailEntry((Node_RuleVariable)dvar, value));
211             ((Node_RuleVariable)dvar).simpleBind(value);
212             return true;
213         } else {
214             return dvar.sameValueAs(value);
215         }
216     }
217    
218     /**
219      * Bind the variables in a goal pattern using the binding environment, to
220      * generate a more specialized goal
221      * @param goal the TriplePattern to be instantiated
222      * @return a TriplePattern obtained from the goal by substituting current bindinds
223      */

224     public TriplePattern partInstantiate(TriplePattern goal) {
225         return new TriplePattern(
226             getMostGroundVersion(goal.getSubject()),
227             getMostGroundVersion(goal.getPredicate()),
228             getMostGroundVersion(goal.getObject())
229         );
230     }
231     
232     /**
233      * Instatiate a goal pattern using the binding environment
234      * @param goal the TriplePattern to be instantiated
235      * @return an instantiated Triple
236      */

237     public Triple instantiate(TriplePattern goal) {
238         return new Triple(
239                 getMostGroundVersion(goal.getSubject()),
240                 getMostGroundVersion(goal.getPredicate()),
241                 getMostGroundVersion(goal.getObject())
242         );
243     }
244     
245     /**
246      * Inner class used to represent an entry on the binding trail.
247      */

248     static class TrailEntry {
249         /** The deferenced var which was bound */
250         protected Node_RuleVariable var;
251         
252         /** The value to which it was bound */
253         protected Node value;
254         
255         /** constructor */
256         TrailEntry(Node_RuleVariable var, Node value) {
257             this.var = var;
258             this.value = value;
259         }
260     }
261
262 }
263
264
265 /*
266     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
267     All rights reserved.
268
269     Redistribution and use in source and binary forms, with or without
270     modification, are permitted provided that the following conditions
271     are met:
272
273     1. Redistributions of source code must retain the above copyright
274        notice, this list of conditions and the following disclaimer.
275
276     2. Redistributions in binary form must reproduce the above copyright
277        notice, this list of conditions and the following disclaimer in the
278        documentation and/or other materials provided with the distribution.
279
280     3. The name of the author may not be used to endorse or promote products
281        derived from this software without specific prior written permission.
282
283     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
284     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
285     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
286     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
287     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
288     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
289     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
290     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
291     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
292     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
293 */
Popular Tags