1 10 package com.hp.hpl.jena.reasoner.rulesys.impl.oldCode; 11 12 import com.hp.hpl.jena.mem.GraphMem; 13 import com.hp.hpl.jena.reasoner.rulesys.impl.*; 14 import com.hp.hpl.jena.reasoner.rulesys.*; 15 import com.hp.hpl.jena.reasoner.transitiveReasoner.*; 16 import com.hp.hpl.jena.reasoner.*; 17 import com.hp.hpl.jena.graph.*; 18 19 import java.util.*; 20 21 import com.hp.hpl.jena.util.OneToManyMap; 23 import com.hp.hpl.jena.util.iterator.*; 24 import com.hp.hpl.jena.vocabulary.RDFS; 25 import com.hp.hpl.jena.vocabulary.ReasonerVocabulary; 26 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 43 public class OrigFBRuleInfGraph extends BasicForwardRuleInfGraph implements BackwardRuleInfGraphI { 44 45 46 protected BBRuleContext context; 47 48 49 protected Finder dataFind; 50 51 52 protected BRuleEngine bEngine; 53 54 55 protected List rawRules; 56 57 58 protected List rules; 59 60 61 public static boolean useRETE = true; 62 63 64 protected boolean useTGCCaching = false; 65 66 67 protected TransitiveEngine transitiveEngine; 68 69 70 protected List preprocessorHooks; 71 72 73 protected TempNodeCache tempNodecache; 74 75 static Log logger = LogFactory.getLog(FBRuleInfGraph.class); 76 77 80 85 public OrigFBRuleInfGraph(Reasoner reasoner, Graph schema) { 86 super(reasoner, schema); 87 bEngine = new BRuleEngine(this); 88 tempNodecache = new TempNodeCache(this); 89 } 90 91 97 public OrigFBRuleInfGraph(Reasoner reasoner, List rules, Graph schema) { 98 super(reasoner, rules, schema); 99 this.rawRules = rules; 100 bEngine = new BRuleEngine(this); 101 tempNodecache = new TempNodeCache(this); 102 } 103 104 111 public OrigFBRuleInfGraph(Reasoner reasoner, List rules, Graph schema, Graph data) { 112 super(reasoner, rules, schema, data); 113 this.rawRules = rules; 114 bEngine = new BRuleEngine(this); 115 tempNodecache = new TempNodeCache(this); 116 } 117 118 123 protected void instantiateRuleEngine(List rules) { 124 if (rules != null) { 125 if (useRETE) { 126 engine = new RETEEngine(this, rules); 127 } else { 128 engine = new FRuleEngine(this, rules); 129 } 130 } else { 131 if (useRETE) { 132 engine = new RETEEngine(this); 133 } else { 134 engine = new FRuleEngine(this); 135 } 136 } 137 } 138 139 143 public void setUseTGCCache() { 144 useTGCCaching = true; 145 if (schemaGraph != null) { 146 transitiveEngine = new TransitiveEngine(((OrigFBRuleInfGraph)schemaGraph).transitiveEngine); 147 } else { 148 transitiveEngine = new TransitiveEngine( 149 new TransitiveGraphCache(ReasonerVocabulary.directSubClassOf.asNode(), RDFS.subClassOf.asNode()), 150 new TransitiveGraphCache(ReasonerVocabulary.directSubPropertyOf.asNode(), RDFS.subPropertyOf.asNode())); 151 } 152 } 153 154 157 158 163 public ExtendedIterator findDataMatches(Node subject, Node predicate, Node object) { 164 return dataFind.find(new TriplePattern(subject, predicate, object)); 165 } 166 167 172 public ExtendedIterator findDataMatches(TriplePattern pattern) { 173 return dataFind.find(pattern); 174 } 175 176 183 public boolean processBuiltin(ClauseEntry clause, Rule rule, BindingEnvironment env) { 184 if (clause instanceof Functor) { 185 context.setEnv(env); 186 context.setRule(rule); 187 return((Functor)clause).evalAsBodyClause(context); 188 } else { 189 throw new ReasonerException("Illegal builtin predicate: " + clause + " in rule " + rule); 190 } 191 } 192 193 197 public void addBRule(Rule brule) { 198 bEngine.addRule(brule); 200 bEngine.reset(); 201 } 202 203 207 public void deleteBRule(Rule brule) { 208 bEngine.deleteRule(brule); 210 bEngine.reset(); 211 } 212 213 216 public void addBRules(List rules) { 217 for (Iterator i = rules.iterator(); i.hasNext(); ) { 218 Rule rule = (Rule)i.next(); 219 bEngine.addRule(rule); 221 } 222 bEngine.reset(); 223 } 224 225 229 public List getBRules() { 230 return bEngine.getAllRules(); 231 } 232 233 237 public List getRules() { 238 return rules; 239 } 240 241 245 private Object getForwardRuleStore() { 246 return engine.getRuleStore(); 247 } 248 249 252 public void addDeduction(Triple t) { 253 getDeductionsGraph().add(t); 254 if (useTGCCaching) { 255 transitiveEngine.add(t); 256 } 257 } 258 259 266 public Node getTemp(Node instance, Node prop, Node pclass) { 267 return tempNodecache.getTemp(instance, prop, pclass); 268 } 269 270 273 279 public void addRuleDuringPrepare(Rule rule) { 280 if (rules == rawRules) { 281 if (rawRules instanceof ArrayList) { 283 rules = (ArrayList) ((ArrayList)rawRules).clone(); 284 } else { 285 rules = new ArrayList(rawRules); 286 } 287 instantiateRuleEngine(rules); 289 } 290 rules.add(rule); 291 } 292 293 297 public void addPreprocessingHook(RulePreprocessHook hook) { 298 if (preprocessorHooks == null) { 299 preprocessorHooks = new ArrayList(); 300 } 301 preprocessorHooks.add(hook); 302 } 303 304 312 public void prepare() { 313 if (!isPrepared) { 314 isPrepared = true; 315 316 rules = rawRules; 318 319 Graph data = null; 321 if (fdata != null) data = fdata.getGraph(); 322 323 fdeductions = new FGraph( new GraphMem() ); 325 dataFind = (data == null) ? fdeductions : FinderUtil.cascade(fdeductions, fdata); 326 327 if (useTGCCaching) { 329 if (schemaGraph != null) { 330 if ( 332 (transitiveEngine.checkOccurance(TransitiveReasoner.subPropertyOf, data) || 333 transitiveEngine.checkOccurance(TransitiveReasoner.subClassOf, data) || 334 transitiveEngine.checkOccurance(RDFS.domain.asNode(), data) || 335 transitiveEngine.checkOccurance(RDFS.range.asNode(), data) )) { 336 337 transitiveEngine.insert(((OrigFBRuleInfGraph)schemaGraph).fdata, fdata); 340 } 341 } else { 342 if (data != null) { 343 transitiveEngine.insert(null, fdata); 344 } 345 } 346 for (Iterator i = rules.iterator(); i.hasNext(); ) { 348 Rule r = (Rule)i.next(); 349 if (r.bodyLength() == 0) { 350 for (int j = 0; j < r.headLength(); j++) { 352 Object head = r.getHeadElement(j); 353 if (head instanceof TriplePattern) { 354 TriplePattern h = (TriplePattern) head; 355 transitiveEngine.add(h.asTriple()); 356 } 357 } 358 } 359 } 360 361 transitiveEngine.setCaching(true, true); 362 dataFind = FinderUtil.cascade(dataFind, transitiveEngine.getSubClassCache(), transitiveEngine.getSubPropertyCache()); 364 } 365 366 Finder dataSource = fdata; 368 if (preprocessorHooks != null && preprocessorHooks.size() > 0) { 369 Graph inserts = new GraphMem(); 370 for (Iterator i = preprocessorHooks.iterator(); i.hasNext(); ) { 371 RulePreprocessHook hook = (RulePreprocessHook)i.next(); 372 throw new ReasonerException("Internal error: attempted to invoke obsoleted reasoner with preprocessing hook"); 376 } 377 if (inserts.size() > 0) { 378 FGraph finserts = new FGraph(inserts); 379 dataSource = FinderUtil.cascade(fdata, finserts); 380 dataFind = FinderUtil.cascade(dataFind, finserts); 381 } 382 } 383 384 boolean rulesLoaded = false; 385 if (schemaGraph != null) { 386 Graph rawPreload = ((InfGraph)schemaGraph).getRawGraph(); 387 if (rawPreload != null) { 388 dataFind = FinderUtil.cascade(dataFind, new FGraph(rawPreload)); 389 } 390 rulesLoaded = preloadDeductions(schemaGraph); 391 } 392 if (rulesLoaded) { 393 engine.fastInit(dataSource); 394 } else { 395 addBRules(extractPureBackwardRules(rules)); 397 engine.init(true, dataSource); 398 } 399 context = new BBRuleContext(this); 401 402 } 403 } 404 405 412 public void rebind() { 413 if (bEngine != null) bEngine.reset(); 414 isPrepared = false; 415 } 416 417 419 441 445 public void setTraceOn(boolean state) { 446 super.setTraceOn(state); 447 bEngine.setTraceOn(state); 448 } 449 450 453 public void setDerivationLogging(boolean recordDerivations) { 454 this.recordDerivations = recordDerivations; 455 engine.setDerivationLogging(recordDerivations); 456 bEngine.setDerivationLogging(recordDerivations); 457 if (recordDerivations) { 458 derivations = new OneToManyMap(); 459 } else { 460 derivations = null; 461 } 462 } 463 464 468 public long getNRulesFired() { 469 return engine.getNRulesFired() + bEngine.getNRulesFired(); 470 } 471 472 483 public ExtendedIterator findWithContinuation(TriplePattern pattern, Finder continuation) { 484 checkOpen(); 485 if (!isPrepared) prepare(); 486 487 ExtendedIterator result = null; 488 if (continuation == null) { 489 result = UniqueExtendedIterator.create( new TopGoalIterator(bEngine, pattern) ); 490 } else { 491 result = UniqueExtendedIterator.create( new TopGoalIterator(bEngine, pattern) ) 492 .andThen(continuation.find(pattern)); 493 } 494 return result.filterDrop(Functor.acceptFilter); 495 } 496 497 502 public ExtendedIterator graphBaseFind(Node subject, Node property, Node object) { 503 return findWithContinuation(new TriplePattern(subject, property, object), null); 504 } 505 506 514 public ExtendedIterator find(TriplePattern pattern) { 515 return findWithContinuation(pattern, null); 516 } 517 518 521 public void reset() { 522 bEngine.reset(); 523 isPrepared = false; 524 } 525 526 530 public synchronized void performAdd(Triple t) { 531 fdata.getGraph().add(t); 532 if (useTGCCaching) { 533 if (transitiveEngine.add(t)) isPrepared = false; 534 } 535 if (isPrepared) { 536 engine.add(t); 537 } 538 bEngine.reset(); 539 } 540 541 544 public void performDelete(Triple t) { 545 fdata.getGraph().delete(t); 546 if (useTGCCaching) { 547 if (transitiveEngine.delete(t)) { 548 if (isPrepared) { 549 bEngine.deleteAllRules(); 550 } 551 isPrepared = false; 552 } 553 } 554 if (isPrepared) { 555 getDeductionsGraph().delete(t); 556 engine.delete(t); 557 } 558 bEngine.reset(); 559 } 560 561 567 public InfGraph cloneWithPremises(Graph premises) { 568 prepare(); 569 FBRuleInfGraph graph = new FBRuleInfGraph(getReasoner(), rawRules, this); 570 if (useTGCCaching) graph.setUseTGCCache(); 571 graph.setDerivationLogging(recordDerivations); 572 graph.setTraceOn(traceOn); 573 graph.rebind(premises); 579 return graph; 580 } 581 582 585 589 private static List extractPureBackwardRules(List rules) { 590 List bRules = new ArrayList(); 591 for (Iterator i = rules.iterator(); i.hasNext(); ) { 592 Rule r = (Rule)i.next(); 593 if (r.isBackward() && r.bodyLength() > 0) { 594 bRules.add(r); 595 } 596 } 597 return bRules; 598 } 599 600 607 protected boolean preloadDeductions(Graph preloadIn) { 608 Graph d = fdeductions.getGraph(); 609 OrigFBRuleInfGraph preload = (OrigFBRuleInfGraph)preloadIn; 610 if (preload.rules == rules) { 612 for (Iterator i = preload.getDeductionsGraph().find(null, null, null); i.hasNext(); ) { 614 d.add((Triple)i.next()); 615 } 616 addBRules(preload.getBRules()); 618 engine.setRuleStore(preload.getForwardRuleStore()); 620 return true; 622 } else { 623 return false; 624 } 625 } 626 627 640 643 646 public static class RuleStore { 647 648 649 protected List rawRules; 650 651 652 protected Object fRuleStore; 653 654 655 protected List bRules; 656 657 660 public RuleStore(List rawRules, Object fRuleStore, List bRules) { 661 this.rawRules = rawRules; 662 this.fRuleStore = fRuleStore; 663 this.bRules = bRules; 664 } 665 666 } 667 } 668 669 670 | Popular Tags |