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.TriplePattern; 14 import com.hp.hpl.jena.reasoner.rulesys.BindingEnvironment; 15 import com.hp.hpl.jena.reasoner.rulesys.Functor; 16 import com.hp.hpl.jena.reasoner.rulesys.Node_RuleVariable; 17 18 import java.util.*; 19 20 26 public class BindingStack implements BindingEnvironment { 27 28 32 33 protected Node[] environment; 34 35 36 protected ArrayList trail = new ArrayList(); 37 38 39 protected int index = 0; 40 41 42 protected int highWater = 0; 43 44 45 protected static final int MAX_VAR = 10; 46 47 50 public BindingStack() { 51 trail.add(new Node[MAX_VAR]); 52 environment = (Node[])trail.get(0); 53 index = highWater = 0; 54 } 55 56 59 public void push() { 60 if (index == highWater) { 61 trail.add(new Node[MAX_VAR]); 62 highWater++; 63 } 64 Node[] newenv = (Node[]) trail.get(++index); 65 System.arraycopy(environment, 0, newenv, 0, MAX_VAR); 66 environment = newenv; 67 } 68 69 74 public void unwind() throws IndexOutOfBoundsException { 75 if (index > 0) { 76 environment = (Node[]) trail.get(--index); 78 } else { 79 throw new IndexOutOfBoundsException ("Underflow of BindingEnvironment"); 80 } 81 } 82 83 87 public void commit() throws IndexOutOfBoundsException { 88 if (index > 0) { 89 Node[] newenv = (Node[]) trail.get(index-1); 91 trail.set(index-1, environment); 92 trail.set(index, newenv); 93 --index; 94 } else { 95 throw new IndexOutOfBoundsException ("Underflow of BindingEnvironment"); 96 } 97 } 98 99 102 public void reset() { 103 index = 0; 104 environment = (Node[]) trail.get(0); 105 Arrays.fill(environment, null); 106 } 107 108 111 public Node[] getEnvironment() { 112 return environment; 113 } 114 115 119 public Node getBinding(Node node) { 120 if (node instanceof Node_RuleVariable) { 121 return environment[((Node_RuleVariable)node).getIndex()]; 122 } else if (node instanceof Node_ANY) { 123 return null; 124 } else if (Functor.isFunctor(node)) { 125 Functor functor = (Functor)node.getLiteral().getValue(); 126 if (functor.isGround()) return node; 127 Node[] args = functor.getArgs(); 128 ArrayList boundargs = new ArrayList(args.length); 129 for (int i = 0; i < args.length; i++) { 130 Object binding = getBinding(args[i]); 131 if (binding == null) { 132 return null; 134 } 135 boundargs.add(binding); 136 } 137 Functor newf = new Functor(functor.getName(), boundargs); 138 return Functor.makeFunctorNode( newf ); 139 } else { 140 return node; 141 } 142 } 143 144 149 public Node getGroundVersion(Node node) { 150 Node bind = getBinding(node); 151 if (bind == null) { 152 return node; 153 } else { 154 return bind; 155 } 156 } 157 158 163 public boolean bind(int i, Node value) { 164 Node node = environment[i]; 165 if (node == null) { 166 environment[i] = value; 167 return true; 168 } else { 169 return node.sameValueAs(value); 170 } 171 } 172 173 180 public boolean bind(Node var, Node value) { 181 if (var instanceof Node_RuleVariable) { 182 return bind(((Node_RuleVariable)var).getIndex(), value); 183 } else { 184 return var.sameValueAs(value); 185 } 186 } 187 188 194 public void bindNoCheck(Node_RuleVariable var, Node value) { 195 environment[var.getIndex()] = value; 196 } 197 198 205 public Triple instantiate(TriplePattern pattern) { 206 Node s = getGroundVersion(pattern.getSubject()); 207 if (s.isVariable()) s = Node.createAnon(); 208 Node p = getGroundVersion(pattern.getPredicate()); 209 if (p.isVariable()) p = Node.createAnon(); 210 Node o = getGroundVersion(pattern.getObject()); 211 if (o.isVariable()) o = Node.createAnon(); 212 return new Triple(s, p, o); 213 } 214 215 } 216 217 246 247 | Popular Tags |