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.RuleStore; 15 import com.hp.hpl.jena.reasoner.rulesys.impl.StateFlag; 16 import com.hp.hpl.jena.reasoner.*; 17 import com.hp.hpl.jena.util.PrintUtil; 18 import com.hp.hpl.jena.graph.*; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 import java.util.*; 24 25 38 public class BRuleEngine { 39 40 43 44 protected LinkedList agenda = new LinkedList(); 45 46 49 50 protected GoalTable goalTable; 51 52 53 protected BackwardRuleInfGraphI infGraph; 54 55 56 protected RuleStore ruleStore; 57 58 59 protected boolean traceOn = false; 60 61 62 protected boolean recordDerivations; 63 64 65 protected long nRulesFired = 0; 66 67 69 protected int batchSize = 100000; 70 71 static Log logger = LogFactory.getLog(BRuleEngine.class); 72 73 76 81 public BRuleEngine(BackwardRuleInfGraphI infGraph, RuleStore rules) { 82 this.infGraph = infGraph; 83 goalTable = new GoalTable(this); 84 ruleStore = rules; 85 } 86 87 91 public BRuleEngine(BackwardRuleInfGraphI infGraph) { 92 this.infGraph = infGraph; 93 goalTable = new GoalTable(this); 94 ruleStore = new RuleStore(); 95 } 96 97 100 103 public synchronized void reset() { 104 agenda.clear(); 105 goalTable.reset(); 106 } 107 108 113 public void addRule(Rule rule) { 114 ruleStore.addRule(rule); 116 } 117 118 123 public void deleteRule(Rule rule) { 124 ruleStore.deleteRule(rule); 125 } 126 127 130 public List getAllRules() { 131 return ruleStore.getAllRules(); 132 } 133 134 137 public void deleteAllRules() { 138 ruleStore.deleteAllRules(); 139 } 140 141 146 public synchronized void halt() { 147 for (Iterator i = agenda.iterator(); i.hasNext(); ) { 149 RuleState item = (RuleState)i.next(); 150 if (item.goalState != null) item.goalState.close(); 152 } 153 agenda.clear(); 154 goalTable.removePartialGoals(); 156 } 157 158 165 public synchronized GoalState findGoal(TriplePattern goal) { 166 return goalTable.findGoal(goal); 167 } 168 169 173 public void setTraceOn(boolean state) { 174 traceOn = state; 175 } 176 177 180 public boolean isTraceOn() { 181 return traceOn; 182 } 183 184 188 public long getNRulesFired() { 189 return nRulesFired; 190 } 191 192 195 public void setDerivationLogging(boolean recordDerivations) { 196 this.recordDerivations = recordDerivations; 197 } 198 199 203 public void dump() { 204 goalTable.dump(); 205 } 206 207 210 213 public synchronized void appendToAgenda(RuleState rs) { 214 if (!rs.isScheduled) { 215 if (traceOn) { 216 } 218 agenda.add(rs); 219 rs.isScheduled = true; 220 } 221 } 222 223 226 public synchronized void prependToAgenda(RuleState rs) { 227 if (!rs.isScheduled) { 228 if (traceOn) { 229 } 231 agenda.add(0, rs); 232 rs.isScheduled = true; 233 } 234 } 235 236 239 public RuleState nextAgendaItem() { 240 RuleState next = (RuleState)agenda.removeFirst(); 241 next.isScheduled = false; 242 return next; 243 244 } 283 284 287 public List rulesFor(TriplePattern goal) { 288 return ruleStore.rulesFor(goal); 289 } 290 291 294 public BackwardRuleInfGraphI getInfGraph() { 295 return infGraph; 296 } 297 298 305 public boolean processBuiltin(ClauseEntry clause, Rule rule, BindingEnvironment env) { 306 return infGraph.processBuiltin(clause, rule, env); 307 } 308 309 318 public synchronized Triple next(GoalState topGoalState) { 319 GoalResults topGoal = topGoalState.getGoalResultsEntry(); 320 int numResults = 0; 321 BindingVector env = null; 322 RuleState current = null; 323 RuleState continuation = null; 324 try { 325 while(true) { 326 boolean foundResult = false; 328 RuleState delayedRSClose = null; 329 if (current == null) { 330 current = nextAgendaItem(); 333 numResults = 0; 334 if (traceOn) { 335 logger.debug("Waken: " + current); 336 } 337 } 338 Object result = current.next(); 339 if (result == StateFlag.FAIL) { 340 if (traceOn) { 344 logger.debug("Failed"); 345 } 346 delayedRSClose = current; 347 current = current.prev; 348 } else if (result == StateFlag.SUSPEND) { 349 if (traceOn) { 351 logger.debug("Suspend: " + current); 352 } 353 GoalResults waitingFor = current.goalState.results; 354 waitingFor.addDependent(current); 355 current = current.prev; 356 } else if (result == StateFlag.SATISFIED) { 357 foundResult = true; 359 env = current.env; 360 delayedRSClose = current; 361 continuation = current.prev; 362 } else { env = current.newEnvironment((Triple)result); 364 if (env == null) { 365 continue; 368 } 369 Rule rule = current.ruleInstance.rule; 370 boolean foundGoal = false; 371 int maxClause = rule.bodyLength(); 372 int clauseIndex = current.nextClauseIndex(); 373 while (clauseIndex < maxClause && !foundGoal) { 374 ClauseEntry clause = rule.getBodyElement(clauseIndex++); 375 if (clause instanceof TriplePattern) { 376 TriplePattern subgoal = env.partInstantiate((TriplePattern)clause); 379 if (!subgoal.isLegal()) { 380 delayedRSClose = current; 382 current = current.prev; 383 } else { 384 current = new RuleState(current, subgoal, clauseIndex, env); 385 } 386 foundGoal = true; 387 } else { 388 if (!infGraph.processBuiltin(clause, rule, env)) { 389 delayedRSClose = current; 391 current = current.prev; 392 foundGoal = true; 393 } 394 } 395 } 396 if (!foundGoal) { 397 foundResult = true; 399 continuation = current; 400 } 401 } 402 if (foundResult) { 403 GoalResults resultDest = current.ruleInstance.generator; 405 Triple finalResult = current.getResult(env); 406 if (traceOn) { 407 logger.debug("Result: " + PrintUtil.print(finalResult) + " <- " + current +", newenv=" + env); 408 } 409 boolean newresult = resultDest.addResult(finalResult); 410 if (delayedRSClose != null) { 411 delayedRSClose.close(); 412 } 413 numResults++; 414 current = continuation; 415 nRulesFired++; 416 if (newresult && recordDerivations) { 417 Rule rule = current.ruleInstance.rule; 418 List matchList = new ArrayList(rule.bodyLength()); 419 for (int i = 0; i < rule.bodyLength(); i++) { 420 Object clause = rule.getBodyElement(i); 421 if (clause instanceof TriplePattern) { 422 matchList.add(env.instantiate((TriplePattern)clause)); 423 } 424 } 425 426 RuleDerivation derivation = new RuleDerivation(rule, finalResult, matchList, infGraph); 427 infGraph.logDerivation(finalResult, derivation); 428 } 429 if (newresult && resultDest == topGoal) { 430 if (current != null) prependToAgenda(current); 432 return finalResult; 433 } else if (numResults > batchSize) { 434 if (current != null) appendToAgenda(current); 436 current = null; 437 } 438 } else { 439 if (delayedRSClose != null) { 440 delayedRSClose.close(); 441 } 442 } 443 } 444 } catch (NoSuchElementException e) { 445 if (traceOn) { 447 logger.debug("Completed all"); 448 } 449 goalTable.setAllComplete(); 450 return null; 451 } 452 } 453 } 454 455 456 457 | Popular Tags |