KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > Node_RuleVariable


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

10 package com.hp.hpl.jena.reasoner.rulesys;
11
12 import com.hp.hpl.jena.graph.Node;
13 import com.hp.hpl.jena.graph.Node_Variable;
14
15 /**
16  * A variation on the normal Node_Variable which support for value bindings.
17  * Currently the forward rule system stores the values externally but requires
18  * variables to have an offset index in the rule environment vector. The
19  * variables can also suport prolog-like reference chains and trails but these
20  * are not yet used.
21  * <p>
22  * Note that this should not be used in a real Triple, in particular
23  * it should not end up in a Graph. It is only needed for the rule systems. </p>
24  *
25  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
26  * @version $Revision: 1.17 $ on $Date: 2005/02/21 12:17:00 $
27  */

28 public class Node_RuleVariable extends Node_Variable {
29     /** The offset of this variable in the Frule's binding table */
30     protected int index;
31
32     /** The value to which this variable is bound, can be another variable,
33      * itself (meaning unbound) or an actual value */

34     protected Node value;
35     
36     /** A flag to indicate that the value is reference (pointer to a var) */
37     protected boolean isRef = true;
38     
39     /** A static wildcard - like Node.ANY but tests equl to other Node_RuleVariables */
40     public static final Node_RuleVariable WILD = new Node_RuleVariable("*", -1);
41          
42     /**
43      * Constructor
44      * @param label the text label for the variable
45      * @param index the calculated index of this variable in the rule
46      */

47     public Node_RuleVariable(String JavaDoc label, int index) {
48         super(new VarLabel(label));
49         this.index = index;
50         this.value = this;
51     }
52          
53     /**
54      * Constructor
55      * @param label the text label for the variable
56      * @param index the calculated index of this variable in the rule
57      */

58     private Node_RuleVariable(VarLabel label, int index) {
59         super(label);
60         this.index = index;
61         this.value = this;
62     }
63     
64     /**
65      * Returns the variable's index in a binding vector.
66      * @return int
67      */

68     public int getIndex() {
69         return index;
70     }
71     
72     /**
73      * Changes the variable's index. This is used in LP rules which classify the
74      * variables into different sequences.
75      */

76     public void setIndex(int index) {
77         this.index = index;
78     }
79     
80     /**
81      * Return an indexable object for this Node. This is actually the
82      * rule label. This is weird but needed because equals is (deliberately)
83      * perverse on Node_RuleVariable so if we want to put then in a Set or Map
84      * we need something with a better equals function.
85      */

86 // public Object getRepresentative() {
87
// return label;
88
// }
89

90     /**
91      * Binds a value to the brule version of the variable. Does not follow
92      * any reference trail, assues we have already been derefenced.
93      * @param node a concrete Node value or another Node_RuleVariable
94      * to alias to
95      */

96     public void simpleBind(Node node) {
97         value = node;
98         isRef = node instanceof Node_RuleVariable;
99     }
100     
101     /**
102      * Dereference a variable by following the reference chain.
103      * @return either a concrete node value or the last variable
104      * in the reference chain.
105      */

106     public Node deref() {
107         Node_RuleVariable var = this;
108         while (var.isRef) {
109             if (var.value == var) {
110                 return var;
111             }
112             var = (Node_RuleVariable)var.value;
113         }
114         return var.value;
115     }
116     
117     /**
118      * Return the raw value to which this variable is bound (via LP binding) with
119      * no dereferencing.
120      */

121     public Node getRawBoundValue() {
122         return value;
123     }
124     
125     /**
126      * Set the variable to be unbound (in the brule sense)
127      */

128     public void unbind() {
129         isRef = true;
130         value = this;
131     }
132     
133     /**
134      * Test if the variable is unbound (in the brule sense).
135      */

136     public boolean isUnbound() {
137         return (isRef && (value == this));
138     }
139     
140     /**
141      * Clone the rule variable to allow multiple rule instaces to be active at the same time.
142      */

143     public Node_RuleVariable cloneNode() {
144         return new Node_RuleVariable((VarLabel)label, index);
145     }
146     
147     /** printable form */
148     public String JavaDoc toString() {
149         String JavaDoc l = ((VarLabel)label).getLabel();
150         return (l == null) ? "*" : l;
151     }
152     
153 // Obsolete equality override this functionality has been moved into TriplePattern
154

155 // /** Equality override - all rule variables are treated as equal
156
// * to support easy variant matching. */
157
// public boolean equals(Object o) {
158
// return o instanceof Node_RuleVariable;
159
// }
160
//
161
// /** hash function override - all vars have same hash code to support fast
162
// * search of variant tables */
163
// public int hashCode() {
164
// return 0xc3a7;
165
// }
166

167     /**
168      * Test that two nodes are semantically equivalent.
169      */

170     public boolean sameValueAs(Object JavaDoc o) {
171         return o instanceof Node_RuleVariable;
172     }
173
174     /**
175      * Compare two nodes, taking into account variable indices.
176      */

177     public static boolean sameNodeAs(Node n, Node m) {
178         if (n instanceof Node_RuleVariable) {
179             if (m instanceof Node_RuleVariable) {
180                 return ((Node_RuleVariable)n).getIndex() == ((Node_RuleVariable)m).getIndex();
181             } else {
182                 return false;
183             }
184         } else {
185             return n.sameValueAs(m);
186         }
187     }
188     
189     /** Inner class to wrap the label to ensure it is distinct from other usages */
190     static class VarLabel {
191         
192         /** The label being wrapped */
193         String JavaDoc label;
194         
195         VarLabel(String JavaDoc label ) {
196             this.label = label;
197         }
198         
199         public String JavaDoc getLabel() {
200             return label;
201         }
202     }
203
204 }
205
206 /*
207  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
208  * All rights reserved.
209  *
210  * Redistribution and use in source and binary forms, with or without
211  * modification, are permitted provided that the following conditions
212  * are met:
213  * 1. Redistributions of source code must retain the above copyright
214  * notice, this list of conditions and the following disclaimer.
215  * 2. Redistributions in binary form must reproduce the above copyright
216  * notice, this list of conditions and the following disclaimer in the
217  * documentation and/or other materials provided with the distribution.
218  * 3. The name of the author may not be used to endorse or promote products
219  * derived from this software without specific prior written permission.
220  *
221  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
222  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
223  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
224  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
225  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
226  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
227  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
228  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
229  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
230  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
231  */

232
Popular Tags