1 10 package com.hp.hpl.jena.reasoner.rulesys.impl; 11 12 import com.hp.hpl.jena.graph.*; 13 import com.hp.hpl.jena.reasoner.*; 14 import com.hp.hpl.jena.reasoner.rulesys.*; 15 import com.hp.hpl.jena.util.PrintUtil; 16 17 import java.util.*; 18 19 29 public class BindingVector implements BindingEnvironment { 30 31 32 protected Node[] environment; 33 34 37 public BindingVector() { 38 environment = new Node[BindingStack.MAX_VAR]; 39 } 40 41 44 public BindingVector(Node [] env) { 45 environment = env; 46 } 47 48 52 public BindingVector(BindingVector clone) { 53 Node[] orig = clone.environment; 54 environment = new Node[orig.length]; 55 System.arraycopy(orig, 0, environment, 0, orig.length); 56 } 57 58 62 public Node[] getEnvironment() { 63 return environment; 64 } 65 66 70 public Node getBinding(Node node) { 71 if (node instanceof Node_RuleVariable) { 72 Node val = environment[((Node_RuleVariable)node).getIndex()]; 73 if (val instanceof Node_RuleVariable) { 74 return getBinding(val); 75 } else { 76 return val; 77 } 78 } else if (node instanceof Node_ANY) { 79 return null; 80 } else if (Functor.isFunctor(node)) { 81 Functor functor = (Functor)node.getLiteral().getValue(); 82 if (functor.isGround()) return node; 83 Node[] args = functor.getArgs(); 84 ArrayList boundargs = new ArrayList(args.length); 85 for (int i = 0; i < args.length; i++) { 86 Object binding = getBinding(args[i]); 87 if (binding == null) { 88 return null; 90 } 91 boundargs.add(binding); 92 } 93 Functor newf = new Functor( functor.getName(), boundargs ); 94 return Functor.makeFunctorNode( newf ); 95 } else { 96 return node; 97 } 98 } 99 100 105 public Node getGroundVersion(Node node) { 106 Node bind = getBinding(node); 107 if (bind == null) { 108 return node; 109 } else { 110 return bind; 111 } 112 } 113 114 120 public boolean bind(int i, Node value) { 121 Node node = environment[i]; 122 if (node == null) { 123 environment[i] = value; 124 return true; 125 } else if (node instanceof Node_RuleVariable) { 126 environment[i] = value; 127 return bind(((Node_RuleVariable)node).getIndex(), value); 128 } else { 129 return node.sameValueAs(value); 130 } 131 } 132 133 140 public boolean bind(Node var, Node value) { 141 if (var instanceof Node_RuleVariable) { 142 return bind(((Node_RuleVariable)var).getIndex(), value); 143 } else { 144 return var.sameValueAs(value); 145 } 146 } 147 148 154 public TriplePattern partInstantiate(TriplePattern goal) { 155 return new TriplePattern( 156 getGroundVersion(goal.getSubject()), 157 getGroundVersion(goal.getPredicate()), 158 getGroundVersion(goal.getObject()) 159 ); 160 } 161 162 176 183 public Triple instantiate(TriplePattern pattern) { 184 Node s = getGroundVersion(pattern.getSubject()); 185 if (s.isVariable()) s = Node.createAnon(); 186 Node p = getGroundVersion(pattern.getPredicate()); 187 if (p.isVariable()) p = Node.createAnon(); 188 Node o = getGroundVersion(pattern.getObject()); 189 if (o.isVariable()) o = Node.createAnon(); 190 return new Triple(s, p, o); 191 } 192 193 196 public String toString() { 197 StringBuffer buffer = new StringBuffer (); 198 for (int i = 0; i < environment.length; i++) { 199 if (environment[i] == null) { 200 buffer.append("-"); 201 } else { 202 buffer.append(PrintUtil.print(environment[i])); 203 } 204 buffer.append(" "); 205 } 206 return buffer.toString(); 207 } 208 209 218 public static BindingVector unify(TriplePattern goal, TriplePattern head) { 219 return unify(goal, head, BindingStack.MAX_VAR); 220 } 221 222 234 public static BindingVector unify(TriplePattern goal, TriplePattern head, int numRuleVars) { 235 Node[] gEnv = new Node[BindingStack.MAX_VAR]; 236 Node[] hEnv = new Node[numRuleVars]; 237 238 if (!unify(goal.getSubject(), head.getSubject(), gEnv, hEnv)) { 239 return null; 240 } 241 if (!unify(goal.getPredicate(), head.getPredicate(), gEnv, hEnv)) { 242 return null; 243 } 244 245 Node gObj = goal.getObject(); 246 Node hObj = head.getObject(); 247 if (Functor.isFunctor(gObj)) { 248 Functor gFunctor = (Functor)gObj.getLiteral().getValue(); 249 if (Functor.isFunctor(hObj)) { 250 Functor hFunctor = (Functor)hObj.getLiteral().getValue(); 251 if ( ! gFunctor.getName().equals(hFunctor.getName()) ) { 252 return null; 253 } 254 Node[] gArgs = gFunctor.getArgs(); 255 Node[] hArgs = hFunctor.getArgs(); 256 if ( gArgs.length != hArgs.length ) return null; 257 for (int i = 0; i < gArgs.length; i++) { 258 if (! unify(gArgs[i], hArgs[i], gEnv, hEnv) ) { 259 return null; 260 } 261 } 262 } else if (hObj instanceof Node_RuleVariable) { 263 if (gFunctor.isGround(new BindingVector(gEnv))) { 266 if (!unify(gObj, hObj, gEnv, hEnv)) return null; 267 } 268 } else { 270 return null; 272 } 273 } else { 274 if (!unify(gObj, hObj, gEnv, hEnv)) return null; 275 } 276 return new BindingVector(hEnv); 278 } 279 280 286 private static boolean unify(Node gNode, Node hNode, Node[] gEnv, Node[] hEnv) { 287 if (hNode instanceof Node_RuleVariable) { 288 int hIndex = ((Node_RuleVariable)hNode).getIndex(); 289 if (gNode instanceof Node_RuleVariable) { 290 int gIndex = ((Node_RuleVariable)gNode).getIndex(); 292 if (gIndex < 0) return true; 293 if (gEnv[gIndex] == null) { 294 gEnv[gIndex] = hNode; 296 } else { 297 Node gVal = gEnv[gIndex]; 300 if (hIndex != gIndex || ! (gVal instanceof Node_RuleVariable)) { 301 hEnv[hIndex] = gVal; 302 } 303 } 304 } else { 305 Node hVal = hEnv[hIndex]; 306 if (hVal == null) { 307 hEnv[hIndex] = gNode; 308 } else { 309 if (hVal instanceof Node_RuleVariable) { 311 hEnv[((Node_RuleVariable)hVal).getIndex()] = gNode; 313 hEnv[hIndex] = gNode; 314 } else { 315 return hVal.sameValueAs(gNode); 317 } 318 } 319 } 320 return true; 321 } else { 322 if (gNode instanceof Node_RuleVariable) { 323 int gIndex = ((Node_RuleVariable)gNode).getIndex(); 324 if (gIndex < 0) return true; 325 Node gVal = gEnv[gIndex]; 326 if (gVal == null) { 327 gEnv[gIndex] = hNode; 329 } else if (gVal instanceof Node_RuleVariable) { 330 hEnv[((Node_RuleVariable)gVal).getIndex()] = hNode; 332 gEnv[gIndex] = hNode; 333 } else { 334 return gVal.sameValueAs(hNode); 335 } 336 return true; 337 } else { 338 return hNode.sameValueAs(gNode); 339 } 340 } 341 } 342 343 344 public boolean equals(Object o) { 345 if (! (o instanceof BindingVector) ) return false; 347 Node[] other = ((BindingVector)o).environment; 348 if (environment.length != other.length) return false; 349 for (int i = 0; i < environment.length; i++) { 350 Node n = environment[i]; 351 Node no = other[i]; 352 if (n == null) { 353 if (no != null) return false; 354 } else { 355 if (! n.sameValueAs(no)) return false; 356 } 357 } 358 return true; 359 } 360 361 362 public int hashCode() { 363 int hash = 0; 364 for (int i = 0; i < environment.length; i++) { 365 Node n = environment[i]; 366 hash = (hash << 1) ^ (n == null ? 0x537c: n.hashCode()); 367 } 368 return hash; 369 } 370 371 372 } 373 374 375 376 405 | Popular Tags |