use of org.apache.jena.reasoner.rulesys.Rule in project jena by apache.
the class TestLPBRuleEngine method testTabledGoalsLeak.
@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();
// how many were cached
assertEquals(1, engine.tabledGoals.size());
// and no leaks of activeInterpreters
assertEquals(0, engine.activeInterpreters.size());
// Now ask again:
it = infgraph.find(a, ty, C1);
it.close();
// if it was a cache hit, no change here:
assertEquals(1, engine.tabledGoals.size());
assertEquals(0, engine.activeInterpreters.size());
//the cached generator should not have any consumingCP left
for (Generator generator : engine.tabledGoals.asMap().values()) {
assertEquals(0, generator.consumingCPs.size());
}
}
use of org.apache.jena.reasoner.rulesys.Rule in project jena by apache.
the class TestLPBRuleEngine method testSaturateTabledGoals.
@Test
public void testSaturateTabledGoals() throws Exception {
final int MAX = 1024;
// Set the cache size very small just for this test
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());
// Let's ask about lots of unknown subjects
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();
}
// Let's see how many were cached
assertEquals(MAX, engine.tabledGoals.size());
// and no leaks of activeInterpreters (this will happen if we forget
// to call hasNext above)
assertEquals(0, engine.activeInterpreters.size());
} finally {
System.clearProperty("jena.rulesys.lp.max_cached_tabled_goals");
}
}
use of org.apache.jena.reasoner.rulesys.Rule in project jena by apache.
the class TestRuleSetAssembler method testMultipleRuleStrings.
public void testMultipleRuleStrings() {
Assembler a = new RuleSetAssembler();
String ruleStringA = "[(?a P ?b) -> (?a Q ?b)]";
String ruleStringB = "[(?a R ?b) -> (?a S ?b)]";
Resource root = resourceInModel("x rdf:type ja:RuleSet" + "; x ja:rule '" + ruleStringA.replaceAll(" ", "\\\\s") + "'" + "; x ja:rule '" + ruleStringB.replaceAll(" ", "\\\\s") + "'");
RuleSet rules = (RuleSet) a.open(root);
Set<Rule> expected = new HashSet<>(Rule.parseRules(ruleStringA));
expected.addAll(Rule.parseRules(ruleStringB));
assertEquals(expected, new HashSet<>(rules.getRules()));
}
use of org.apache.jena.reasoner.rulesys.Rule in project jena by apache.
the class TestRuleSet method testSingleRuleSet.
public void testSingleRuleSet() {
Rule rule = Rule.parseRule("[(?a P b) -> (?a rdf:type T)]");
List<Rule> list = listOfOne(rule);
RuleSet s = RuleSet.create(list);
assertEquals(list, s.getRules());
assertNotSame(list, s.getRules());
}
use of org.apache.jena.reasoner.rulesys.Rule in project jena by apache.
the class TestRuleSetAssembler method testSubRules.
public void testSubRules() {
Assembler a = new RuleSetAssembler();
String ruleStringA = "[(?a P ?b) -> (?a Q ?b)]";
Resource root = resourceInModel("x rdf:type ja:RuleSet; x ja:rules y" + "; y rdf:type ja:RuleSet; y ja:rule '" + ruleStringA.replaceAll(" ", "\\\\s") + "'");
Set<Rule> expected = new HashSet<>(Rule.parseRules(ruleStringA));
RuleSet rules = (RuleSet) a.open(root);
assertEquals(expected, new HashSet<>(rules.getRules()));
}
Aggregations