Refine search
private static Graph make(int N) { Graph graph = Factory.createGraphMem(); for ( int i = 0 ; i < N ; i++ ) { Triple t = GraphTestBase.triple("a P 'x"+i+"'"); graph.add(t); } return graph ; }
/** * 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) } ); }
/** * Test example hybrid rule. */ public void testHybrid1() { Graph data = Factory.createGraphMem(); data.add(new Triple(a, p, b)); data.add(new Triple(p, ty, s)); String rules = "[r1: (?p rdf:type s) -> [r1b: (?x ?p ?y) <- (?y ?p ?x)]]"; InfGraph infgraph = createInfGraph(rules, data); TestUtil.assertIteratorValues(this, infgraph.find(null, p, null), new Object[] { new Triple(a, p, b), new Triple(b, p, a) } ); }
/** * 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); }
/** * 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 example hybrid rule. */ public void testHybrid1() { Graph data = Factory.createGraphMem(); data.add(new Triple(a, p, b)); data.add(new Triple(p, ty, s)); String rules = "[r1: (?p rdf:type s) -> [r1b: (?x ?p ?y) <- (?y ?p ?x)]]"; InfGraph infgraph = createInfGraph(rules, data); TestUtil.assertIteratorValues(this, infgraph.find(null, p, null), new Object[] { new Triple(a, p, b), new Triple(b, p, a) } ); }
/** * 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); }
private static Graph make(int N) { Graph graph = Factory.createGraphMem(); for ( int i = 0 ; i < N ; i++ ) { Triple t = GraphTestBase.triple("a P 'x"+i+"'"); graph.add(t); } return graph ; }
/** * 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); }
private Node getSkolem(Node x, Node y) { String rules = "[r1: (?n p ?x) (?n q ?y) makeSkolem(?s ?x ?y) -> (?n s ?s)]"; Graph data = Factory.createGraphMem(); data.add(new Triple(n1, p, x)); data.add(new Triple(n1, q, y)); InfGraph infgraph = createInfGraph(rules, data); return infgraph.find(n1, s, Node.ANY).next().getObject(); }
/** * Test basic builtin usage. */ public void testBuiltin1() { Graph data = Factory.createGraphMem(); List<Rule> rules = Rule.parseRules( "[a1: -> (a p 2) ]" + "[a2: -> (a q 3) ]" + "[r1: (?x r ?s) <- (?x p ?y), (?x q ?z), sum(?y, ?z, ?s)]" ); Reasoner reasoner = createReasoner(rules); InfGraph infgraph = reasoner.bind(data); TestUtil.assertIteratorValues(this, infgraph.find(a, r, null), new Object[] { new Triple(a, r, Util.makeIntNode(5)) } ); }
/** * 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) } ); }
private Node getSkolem(Node x, Node y) { String rules = "[r1: (?n p ?x) (?n q ?y) makeSkolem(?s ?x ?y) -> (?n s ?s)]"; Graph data = Factory.createGraphMem(); data.add(new Triple(n1, p, x)); data.add(new Triple(n1, q, y)); InfGraph infgraph = createInfGraph(rules, data); return infgraph.find(n1, s, Node.ANY).next().getObject(); }
/** * Test basic builtin usage. */ public void testBuiltin1() { Graph data = Factory.createGraphMem(); List<Rule> rules = Rule.parseRules( "[a1: -> (a p 2) ]" + "[a2: -> (a q 3) ]" + "[r1: (?x r ?s) <- (?x p ?y), (?x q ?z), sum(?y, ?z, ?s)]" ); Reasoner reasoner = createReasoner(rules); InfGraph infgraph = reasoner.bind(data); TestUtil.assertIteratorValues(this, infgraph.find(a, r, null), new Object[] { new Triple(a, r, Util.makeIntNode(5)) } ); }