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.rulesys.impl.BindingVector; 14 import com.hp.hpl.jena.reasoner.rulesys.impl.StateFlag; 15 import com.hp.hpl.jena.reasoner.*; 16 import com.hp.hpl.jena.graph.*; 17 18 33 public class RuleState { 34 35 36 protected RuleInstance ruleInstance; 37 38 39 protected RuleState prev; 40 41 42 protected BindingVector env; 43 44 45 protected GoalState goalState; 46 47 48 protected boolean isScheduled = false; 49 50 51 int clauseIndex; 52 53 54 int subjectBind; 55 56 57 int predicateBind; 58 59 60 int objectBind; 61 62 63 protected Functor functorMatch = null; 64 65 72 public RuleState(RuleState parent, TriplePattern clause, int index, BindingVector env) { 73 prev = parent; 74 ruleInstance = parent.ruleInstance; 75 clauseIndex = index; 76 this.env = env; 77 TriplePattern subgoal = env.partInstantiate((TriplePattern)clause); 78 goalState = ruleInstance.engine.findGoal(subgoal); 79 initMapping(subgoal); 80 ruleInstance.generator.incRefCount(); 81 } 82 83 87 private RuleState(RuleInstance ruleInstance, BindingVector env, GoalState goalState, int index) { 88 prev = null; 89 this.ruleInstance = ruleInstance; 90 this.env = env; 91 this.goalState = goalState; 92 this.clauseIndex = index; 93 ruleInstance.generator.incRefCount(); 94 } 95 96 99 Object next() { 100 if (goalState == null) { 101 return StateFlag.SATISFIED; 102 } else { 103 return goalState.next(); 104 } 105 } 106 107 111 public BindingVector newEnvironment(Triple result) { 112 BindingVector newenv = new BindingVector(env); 113 if (subjectBind != -1) newenv.bind(subjectBind, result.getSubject()); 114 if (predicateBind != -1) newenv.bind(predicateBind, result.getPredicate()); 115 if (objectBind != -1) newenv.bind(objectBind, result.getObject()); 116 if (functorMatch != null) { 118 Node obj = result.getObject(); 119 if (Functor.isFunctor(obj)) { 120 Functor objValue = (Functor)obj.getLiteral().getValue(); 121 if (objValue.getName().equals(functorMatch.getName())) { 122 Node[] margs = functorMatch.getArgs(); 123 Node[] args = objValue.getArgs(); 124 if (margs.length != args.length) return null; 125 for (int i = 0; i < margs.length; i++) { 126 Node match = margs[i]; 127 if (match instanceof Node_RuleVariable) { 128 Node val = args[i]; 129 if (Functor.isFunctor(val)) return null; 130 if (!newenv.bind(match, val)) return null; 131 } 132 } 133 } else { 134 return null; 135 } 136 } else { 137 return null; 138 } 139 } 140 return newenv; 141 } 142 143 146 public Triple getResult(BindingVector newenv) { 147 return newenv.instantiate(ruleInstance.head); 148 } 149 150 154 public boolean couldProcess() { 155 if (goalState == null) return true; 156 return goalState.couldProcess(); 157 } 158 159 162 private void initMapping(TriplePattern goal) { 163 Node n = goal.getSubject(); 164 subjectBind = (n instanceof Node_RuleVariable) ? ((Node_RuleVariable)n).getIndex() : -1 ; 165 n = goal.getPredicate(); 166 predicateBind = (n instanceof Node_RuleVariable) ? ((Node_RuleVariable)n).getIndex() : -1 ; 167 n = goal.getObject(); 168 objectBind = (n instanceof Node_RuleVariable) ? ((Node_RuleVariable)n).getIndex() : -1 ; 169 if (Functor.isFunctor(n)) functorMatch = (Functor)n.getLiteral().getValue(); 170 } 171 172 176 protected int nextClauseIndex() { 177 if (ruleInstance.clausesReordered) { 178 if (clauseIndex == (ruleInstance.secondClause + 1) ) { 179 return ruleInstance.secondClause - 1; 181 } else if (clauseIndex == ruleInstance.secondClause) { 182 return clauseIndex + 1; 183 } 184 } 185 return clauseIndex; 186 } 187 188 194 public void close() { 195 if (goalState != null) goalState.close(); 196 ruleInstance.generator.decRefCount(); 197 } 198 199 207 public static RuleState createInitialState(Rule rule, GoalResults generator) { 208 TriplePattern goal = generator.goal; 212 TriplePattern head = (TriplePattern) rule.getHeadElement(0); 213 BindingVector env = BindingVector.unify(goal, head, rule.getNumVars()); 214 if (env == null) return null; 215 216 RuleInstance ri = new RuleInstance(generator, rule, head); 218 int maxClause = rule.bodyLength(); 219 int clauseIndex = 0; 220 while (clauseIndex < maxClause) { 221 ClauseEntry clause = rule.getBodyElement(clauseIndex++); 222 if (clause instanceof TriplePattern) { 223 ClauseEntry secondClause = null; 225 boolean foundSecondClause = false; 226 if (clauseIndex < maxClause) { 227 secondClause = rule.getBodyElement(clauseIndex); 228 if (secondClause instanceof TriplePattern) { 229 foundSecondClause = true; 230 } 231 } 232 if (foundSecondClause) { 233 int score1 = scoreClauseBoundness((TriplePattern)clause, head, env); 234 int score2 = scoreClauseBoundness((TriplePattern)secondClause, head, env); 235 if (score2 > score1) { 236 ri.clausesReordered = true; 237 ri.secondClause = clauseIndex; 238 clause = secondClause; 239 clauseIndex++; 240 } 241 } 242 TriplePattern subgoal = env.partInstantiate((TriplePattern)clause); 244 if (!subgoal.isLegal()) return null; 245 GoalState gs = generator.getEngine().findGoal(subgoal); 246 RuleState rs = new RuleState(ri, env, gs, clauseIndex); 247 rs.initMapping(subgoal); 248 return rs; 249 } else { 250 if (!generator.getEngine().processBuiltin(clause, rule, env)) { 251 return null; 252 } 253 } 254 } 255 return new RuleState(ri, env, null, 0); 257 } 258 259 260 265 private static int scoreClauseBoundness(TriplePattern clause, 266 TriplePattern head, 267 BindingVector env) { 268 return 269 scoreNodeBoundness(clause.getSubject(), head, env) + 270 scoreNodeBoundness(clause.getPredicate(), head, env) + 271 scoreNodeBoundness(clause.getObject(), head, env); 272 273 } 274 275 278 private static int scoreNodeBoundness(Node n, TriplePattern head, BindingVector env) { 279 if (n.isVariable()) { 280 if (n == head.getSubject() || n == head.getPredicate() || n == head.getObject() ) { 281 Node val = env.getBinding(n); 282 if (n == null || n.isVariable()) return -5; 283 return 5; 284 } else { 285 return 0; 286 } 287 } else { 288 return 1; 289 } 290 } 291 292 295 public String toString() { 296 return "RuleState " 297 + ruleInstance.rule.toShortString() 298 + "("+ (clauseIndex-1) +")" 299 + ", gs=" + goalState; 301 } 302 303 } 304 305 | Popular Tags |