Refine search
/** * Assemble a test infGraph from a rule source and empty data */ public InfGraph createInfGraph(String rules) { return createReasoner( Rule.parseRules(rules) ).bind(Factory.createGraphMem()); }
/** * Test basic rule operations - simple AND rule */ public void testBaseRules1() { List<Rule> rules = Rule.parseRules("[r1: (?a r ?c) <- (?a p ?b),(?b p ?c)]"); Graph data = Factory.createGraphMem(); data.add(new Triple(a, p, b)); data.add(new Triple(b, p, c)); data.add(new Triple(b, p, d)); Reasoner reasoner = createReasoner(rules); InfGraph infgraph = reasoner.bind(data); TestUtil.assertIteratorValues(this, infgraph.find(null, r, null), new Object[] { new Triple(a, r, c), new Triple(a, r, d) } ); }
/** * Generic test operation. * @param ruleSrc the source of the rules * @param triples a set of triples to insert in the graph before the query * @param query the Triple to search for * @param results the array of expected results */ private void doTest(String ruleSrc, Triple[] triples, Triple query, Object[] results) { List<Rule> rules = Rule.parseRules(ruleSrc); Graph data = Factory.createGraphMem(); for ( Triple triple : triples ) { data.add( triple ); } InfGraph infgraph = makeInfGraph(rules, data); TestUtil.assertIteratorValues(this, infgraph.find(query), results); }
/** * Test basic builtin usage. */ public void testBuiltin3() { Graph data = Factory.createGraphMem(); List<Rule> rules = Rule.parseRules( "[r1: (a p b ) <- unbound(?x) ]" ); Reasoner reasoner = createReasoner(rules); InfGraph infgraph = reasoner.bind(data); TestUtil.assertIteratorValues(this, infgraph.find(a, null, null), new Object[] { new Triple(a, p, b) } ); }
/** * Test basic rule operations - simple AND rule */ public void testBaseRules1() { List<Rule> rules = Rule.parseRules("[r1: (?a r ?c) <- (?a p ?b),(?b p ?c)]"); Graph data = Factory.createGraphMem(); data.add(new Triple(a, p, b)); data.add(new Triple(b, p, c)); data.add(new Triple(b, p, d)); Reasoner reasoner = createReasoner(rules); InfGraph infgraph = reasoner.bind(data); TestUtil.assertIteratorValues(this, infgraph.find(null, r, null), new Object[] { new Triple(a, r, c), new Triple(a, r, d) } ); }
/** * Test basic builtin usage. */ public void testBuiltin3() { Graph data = Factory.createGraphMem(); List<Rule> rules = Rule.parseRules( "[r1: (a p b ) <- unbound(?x) ]" ); Reasoner reasoner = createReasoner(rules); InfGraph infgraph = reasoner.bind(data); TestUtil.assertIteratorValues(this, infgraph.find(a, null, null), new Object[] { new Triple(a, p, b) } ); }
/** * Generic test operation. * @param ruleSrc the source of the rules * @param triples a set of triples to insert in the graph before the query * @param query the Triple to search for * @param results the array of expected results */ private void doTest(String ruleSrc, Triple[] triples, Triple query, Object[] results) { List<Rule> rules = Rule.parseRules(ruleSrc); Graph data = Factory.createGraphMem(); for ( Triple triple : triples ) { data.add( triple ); } InfGraph infgraph = makeInfGraph(rules, data); TestUtil.assertIteratorValues(this, infgraph.find(query), results); }
/** * Assemble a test infGraph from a rule source and empty data */ public InfGraph createInfGraph(String rules) { return createReasoner( Rule.parseRules(rules) ).bind(Factory.createGraphMem()); }
/** * Test size bug, used to blow up if size was called before any queries. */ public void testSize() { String rules = "[rule1: (?x p ?y) -> (?x q ?y)]"; List<Rule> ruleList = Rule.parseRules(rules); Graph data = Factory.createGraphMem(); data.add(new Triple(n1, p, n2)); InfGraph infgraph = new BasicForwardRuleReasoner(ruleList).bind(data); assertEquals(infgraph.size(), 2); }
/** * Minimal rule tester to check basic pattern match */ public void testRuleMatcher() { String rules = "[r1: (?a p ?b), (?b q ?c) -> (?a, q, ?c)]" + "[r2: (?a p ?b), (?b p ?c) -> (?a, p, ?c)]" + "[r3: (?a p ?a), (n1 p ?c), (n1, p, ?a) -> (?a, p, ?c)]" + "[r4: (n4 ?p ?a) -> (n4, ?a, ?p)]"; List<Rule> ruleList = Rule.parseRules(rules); InfGraph infgraph = new BasicForwardRuleReasoner(ruleList).bind(Factory.createGraphMem()); infgraph.add(new Triple(n1, p, n2)); infgraph.add(new Triple(n2, p, n3)); infgraph.add(new Triple(n2, q, n3)); infgraph.add(new Triple(n4, p, n4)); TestUtil.assertIteratorValues(this, infgraph.find(null, null, null), new Triple[] { new Triple(n1, p, n2), new Triple(n2, p, n3), new Triple(n2, q, n3), new Triple(n4, p, n4), new Triple(n1, p, n3), new Triple(n1, q, n3), new Triple(n4, n4, p), }); }
/** * Generic test operation. * @param ruleSrc the source of the rules * @param tabled the predicates that should be tabled * @param triples a set of triples to insert in the graph before the query * @param query the Triple to search for * @param results the array of expected results */ private void doTest(String ruleSrc, Node[] tabled, Triple[] triples, Triple query, Object[] results) { List<Rule> rules = Rule.parseRules(ruleSrc); Graph data = Factory.createGraphMem(); for ( Triple triple : triples ) { data.add( triple ); } InfGraph infgraph = makeInfGraph(rules, data, tabled); TestUtil.assertIteratorValues(this, infgraph.find(query), results); }
/** * Perform a rule test on the raw RETE engine. This requires some fiddling * with dummy parent graphs. */ private void doRuleTest(String rules, Triple[] adds, Triple[] expected) { List<Rule> ruleList = Rule.parseRules(rules); BasicForwardRuleInfGraph infgraph = new BasicForwardRuleInfGraph(null, new ArrayList<Rule>(), null, Factory.createGraphMem()); // infgraph.setTraceOn(true); RETEEngine engine = new RETEEngine(infgraph, ruleList); infgraph.prepare(); engine.init(true, new FGraph(Factory.createGraphMem())); for ( Triple add : adds ) { engine.addTriple( add, true ); } engine.runAll(); TestUtil.assertIteratorValues(this, infgraph.find(null, null, null), expected); }
/** * Test basic rule operations - simple AND/OR with tabling. */ public void testBaseRules4() { Graph data = Factory.createGraphMem(); data.add(new Triple(a, r, b)); data.add(new Triple(b, r, c)); data.add(new Triple(b, r, b)); data.add(new Triple(b, r, d)); List<Rule> rules = Rule.parseRules( "[r1: (?x p ?y) <- (?x r ?y)]" + "[r2: (?x p ?z) <- (?x p ?y), (?y r ?z)]" ); Reasoner reasoner = createReasoner(rules); InfGraph infgraph = reasoner.bind(data); TestUtil.assertIteratorValues(this, infgraph.find(a, p, null), new Object[] { new Triple(a, p, b), new Triple(a, p, d), new Triple(a, p, c) } ); }
"[testRule3: (n2 p ?a), (n2 q ?a) -> (res p ?a)]" + "[axiom1: -> (n1 p n3)]"; List<Rule> ruleList = Rule.parseRules(rules); InfGraph infgraph = new BasicForwardRuleReasoner(ruleList).bind(Factory.createGraphMem()); TestUtil.assertIteratorValues(this, infgraph.find(null, null, null), new Triple[] { new Triple(n1, p, n3), new Triple(n2, p, n3), }); infgraph.add(new Triple(n1, q, n4)); infgraph.add(new Triple(n1, q, n3));
/** * Generic test operation. * @param ruleSrc the source of the rules * @param tabled the predicates that should be tabled * @param triples a set of triples to insert in the graph before the query * @param query the Triple to search for * @param results the array of expected results */ private void doTest(String ruleSrc, Node[] tabled, Triple[] triples, Triple query, Object[] results) { List<Rule> rules = Rule.parseRules(ruleSrc); Graph data = Factory.createGraphMem(); for ( Triple triple : triples ) { data.add( triple ); } InfGraph infgraph = makeInfGraph(rules, data, tabled); TestUtil.assertIteratorValues(this, infgraph.find(query), results); }
/** * Perform a rule test on the raw RETE engine. This requires some fiddling * with dummy parent graphs. */ private void doRuleTest(String rules, Triple[] adds, Triple[] expected) { List<Rule> ruleList = Rule.parseRules(rules); BasicForwardRuleInfGraph infgraph = new BasicForwardRuleInfGraph(null, new ArrayList<Rule>(), null, Factory.createGraphMem()); // infgraph.setTraceOn(true); RETEEngine engine = new RETEEngine(infgraph, ruleList); infgraph.prepare(); engine.init(true, new FGraph(Factory.createGraphMem())); for ( Triple add : adds ) { engine.addTriple( add, true ); } engine.runAll(); TestUtil.assertIteratorValues(this, infgraph.find(null, null, null), expected); }
/** * Test basic rule operations - simple AND/OR with tabling. */ public void testBaseRulesXSB1() { Graph data = Factory.createGraphMem(); data.add(new Triple(p, c, q)); data.add(new Triple(q, c, r)); data.add(new Triple(p, d, q)); data.add(new Triple(q, d, r)); List<Rule> rules = Rule.parseRules( "[r1: (?x a ?y) <- (?x c ?y)]" + "[r2: (?x a ?y) <- (?x b ?z), (?z c ?y)]" + "[r3: (?x b ?y) <- (?x d ?y)]" + "[r4: (?x b ?y) <- (?x a ?z), (?z d ?y)]" ); Reasoner reasoner = createReasoner(rules); InfGraph infgraph = reasoner.bind(data); TestUtil.assertIteratorValues(this, infgraph.find(p, a, null), new Object[] { new Triple(p, a, q), new Triple(p, a, r) } ); }
/** * Minimal rule tester to check basic pattern match */ public void testRuleMatcher() { String rules = "[r1: (?a p ?b), (?b q ?c) -> (?a, q, ?c)]" + "[r2: (?a p ?b), (?b p ?c) -> (?a, p, ?c)]" + "[r3: (?a p ?a), (n1 p ?c), (n1, p, ?a) -> (?a, p, ?c)]" + "[r4: (n4 ?p ?a) -> (n4, ?a, ?p)]"; List<Rule> ruleList = Rule.parseRules(rules); InfGraph infgraph = new BasicForwardRuleReasoner(ruleList).bind(Factory.createGraphMem()); infgraph.add(new Triple(n1, p, n2)); infgraph.add(new Triple(n2, p, n3)); infgraph.add(new Triple(n2, q, n3)); infgraph.add(new Triple(n4, p, n4)); TestUtil.assertIteratorValues(this, infgraph.find(null, null, null), new Triple[] { new Triple(n1, p, n2), new Triple(n2, p, n3), new Triple(n2, q, n3), new Triple(n4, p, n4), new Triple(n1, p, n3), new Triple(n1, q, n3), new Triple(n4, n4, p), }); }
/** * Test early detection of illegal backward rules. */ public void testBRuleErrorHandling() { Graph data = Factory.createGraphMem(); List<Rule> rules = Rule.parseRules( "[a1: -> [(?x eg:p ?y) (?x eg:q ?y) <- (?x eg:r ?y)]]" ); boolean foundException = false; try { GenericRuleReasoner reasoner = (GenericRuleReasoner)GenericRuleReasonerFactory.theInstance().create(null); reasoner.setRules(rules); reasoner.setMode(GenericRuleReasoner.HYBRID); InfGraph infgraph = reasoner.bind(data); infgraph.prepare(); } catch (ReasonerException e) { foundException = true; } assertTrue("Catching use of multi-headed brules", foundException); }
/** * Test basic rule operations - simple AND rule check with tabling. */ public void testBaseRules3() { List<Rule> rules = Rule.parseRules("[rule: (?a rdfs:subPropertyOf ?c) <- (?a rdfs:subPropertyOf ?b),(?b rdfs:subPropertyOf ?c)]"); Reasoner reasoner = createReasoner(rules); Graph data = Factory.createGraphMem(); data.add(new Triple(p, sP, q) ); data.add(new Triple(q, sP, r) ); data.add(new Triple(p, sP, s) ); data.add(new Triple(s, sP, t) ); data.add(new Triple(a, p, b) ); InfGraph infgraph = reasoner.bind(data); TestUtil.assertIteratorValues(this, infgraph.find(null, RDFS.subPropertyOf.asNode(), null), new Object[] { new Triple(p, sP, q), new Triple(q, sP, r), new Triple(p, sP, s), new Triple(s, sP, t), new Triple(p, sP, t), new Triple(p, sP, r) } ); }