LPBRuleEngine { public synchronized ExtendedIterator<Triple> find(TriplePattern goal) { LPInterpreter interpreter = new LPInterpreter(this, goal); activeInterpreters.add(interpreter); return WrappedIterator.create( new LPTopGoalIterator(interpreter)); } LPBRuleEngine(BackwardRuleInfGraphI infGraph, LPRuleStore rules);  LPBRuleEngine(BackwardRuleInfGraphI infGraph); synchronized ExtendedIterator<Triple> find(TriplePattern goal); synchronized void reset(); synchronized void addRule(Rule rule); synchronized void deleteRule(Rule rule); synchronized List<Rule> getAllRules(); synchronized void deleteAllRules(); synchronized void halt(); void setTraceOn(boolean state); boolean isTraceOn(); void setDerivationLogging(boolean recordDerivations); boolean getDerivationLogging(); LPRuleStore getRuleStore(); BackwardRuleInfGraphI getInfGraph(); synchronized void detach(LPInterpreter engine); void checkSafeToUpdate(); synchronized void tablePredicate(Node predicate); synchronized Generator generatorFor(final TriplePattern goal, final List<RuleClauseCode> clauses); synchronized Generator generatorFor(final TriplePattern goal); void schedule(LPAgendaEntry state); void pump(LPInterpreterContext gen); void checkForCompletions(); void incrementProfile(RuleClauseCode clause); void resetProfile(boolean enable); void printProfile(); static final int CYCLES_BETWEEN_COMPLETION_CHECK; }
@Test public void testTabledGoalsCacheHits() throws Exception { Graph data = Factory.createGraphMem(); data.add(new Triple(a, ty, C1)); List<Rule> rules = Rule .parseRules("[r1: (?x p ?t) <- (?x rdf:type C1), makeInstance(?x, p, C2, ?t)]" + "[r2: (?t rdf:type C2) <- (?x rdf:type C1), makeInstance(?x, p, C2, ?t)]"); FBRuleInfGraph infgraph = (FBRuleInfGraph) createReasoner(rules).bind( data); LPBRuleEngine engine = getEngineForGraph(infgraph); assertEquals(0, engine.activeInterpreters.size()); assertEquals(0, engine.tabledGoals.size()); ExtendedIterator<Triple> it = infgraph.find(a, ty, C1); while (it.hasNext()) { it.next(); } it.close(); assertEquals(1, engine.tabledGoals.size()); assertEquals(0, engine.activeInterpreters.size()); it = infgraph.find(a, ty, C1); while (it.hasNext()) { it.next(); } it.close(); assertEquals(1, engine.tabledGoals.size()); assertEquals(0, engine.activeInterpreters.size()); }
@Test public void testTabledGoalsLeak() throws Exception { Graph data = Factory.createGraphMem(); data.add(new Triple(a, ty, C1)); List<Rule> rules = Rule .parseRules("[r1: (?x p ?t) <- (?x rdf:type C1), makeInstance(?x, p, C2, ?t)]" + "[r2: (?t rdf:type C2) <- (?x rdf:type C1), makeInstance(?x, p, C2, ?t)]"); FBRuleInfGraph infgraph = (FBRuleInfGraph) createReasoner(rules).bind( data); LPBRuleEngine engine = getEngineForGraph(infgraph); assertEquals(0, engine.activeInterpreters.size()); assertEquals(0, engine.tabledGoals.size()); ExtendedIterator<Triple> it = infgraph.find(a, ty, C1); it.close(); assertEquals(1, engine.tabledGoals.size()); assertEquals(0, engine.activeInterpreters.size()); it = infgraph.find(a, ty, C1); it.close(); assertEquals(1, engine.tabledGoals.size()); assertEquals(0, engine.activeInterpreters.size()); for(Generator generator : engine.tabledGoals.asMap().values()){ assertEquals(0, generator.consumingCPs.size()); } }
@Test public void testSaturateTabledGoals() throws Exception { final int MAX = 1024; System.setProperty("jena.rulesys.lp.max_cached_tabled_goals", "" + MAX); try { Graph data = Factory.createGraphMem(); data.add(new Triple(a, ty, C1)); List<Rule> rules = Rule .parseRules("[r1: (?x p ?t) <- (?x rdf:type C1), makeInstance(?x, p, C2, ?t)]" + "[r2: (?t rdf:type C2) <- (?x rdf:type C1), makeInstance(?x, p, C2, ?t)]"); FBRuleInfGraph infgraph = (FBRuleInfGraph) createReasoner(rules) .bind(data); LPBRuleEngine engine = getEngineForGraph(infgraph); assertEquals(0, engine.activeInterpreters.size()); assertEquals(0, engine.tabledGoals.size()); for (int i = 0; i < MAX * 128; i++) { Node test = NodeFactory.createURI("test" + i); ExtendedIterator<Triple> it = infgraph.find(test, ty, C2); assertFalse(it.hasNext()); it.close(); } assertTrue(engine.tabledGoals.size() <= MAX); assertEquals(0, engine.activeInterpreters.size()); } finally { System.clearProperty("jena.rulesys.lp.max_cached_tabled_goals"); } }