1 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 25 public class Trail implements BindingEnvironment { 26 27 28 protected ArrayList trail = new ArrayList(); 29 30 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 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 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 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 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 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 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 if (Functor.isFunctor(dn1)) { 131 if (Functor.isFunctor(dn2)) { 132 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 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 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 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 205 public boolean bind(Node var, Node value) { 206 if (var == Node_RuleVariable.WILD || value == Node_RuleVariable.WILD) return true; 207 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 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 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 248 static class TrailEntry { 249 250 protected Node_RuleVariable var; 251 252 253 protected Node value; 254 255 256 TrailEntry(Node_RuleVariable var, Node value) { 257 this.var = var; 258 this.value = value; 259 } 260 } 261 262 } 263 264 265 | Popular Tags |