use of org.apache.jena.reasoner.InfGraph in project jena by apache.
the class TestFBRules method testHybrid2.
/**
* Test example hybrid rule.
*/
public void testHybrid2() {
Graph data = Factory.createGraphMem();
data.add(new Triple(a, r, b));
data.add(new Triple(p, ty, s));
String rules = "[a1: -> (a rdf:type t)]" + "[r0: (?x r ?y) -> (?x p ?y)]" + "[r1: (?p rdf:type s) -> [r1b: (?x ?p ?y) <- (?y ?p ?x)]]" + "[r2: (?p rdf:type s) -> [r2b: (?x ?p ?x) <- (?x rdf:type t)]]";
FBRuleInfGraph infgraph = (FBRuleInfGraph) createInfGraph(rules, data);
infgraph.setDerivationLogging(true);
infgraph.prepare();
assertTrue("Forward rule count", infgraph.getNRulesFired() == 3);
TestUtil.assertIteratorValues(this, infgraph.find(null, p, null), new Object[] { new Triple(a, p, a), new Triple(a, p, b), new Triple(b, p, a) });
// Suppressed until LP engine implements rule counting, if ever
// assertTrue("Backward rule count", infgraph.getNRulesFired() == 8);
// Check derivation tracing as well
// Suppressed until LP engine implements derivation tracing
Iterator<Derivation> di = infgraph.getDerivation(new Triple(b, p, a));
assertTrue(di.hasNext());
RuleDerivation d = (RuleDerivation) di.next();
assertTrue(d.getRule().getName().equals("r1b"));
TestUtil.assertIteratorValues(this, d.getMatches().iterator(), new Object[] { new Triple(a, p, b) });
assertTrue(!di.hasNext());
}
use of org.apache.jena.reasoner.InfGraph in project jena by apache.
the class TestBackchainer method testRebind.
// /**
// * Test multiheaded rule.
// */
// public void testMutliHead() {
// Graph data = new GraphMem();
// data.add(new Triple(a, p, b));
// data.add(new Triple(b, r, c));
// List<Rule> rules = Rule.parseRules(
// "[r1: (?x s ?z), (?z s ?x) <- (?x p ?y) (?y r ?z) ]"
// );
// Reasoner reasoner = createReasoner(rules);
// InfGraph infgraph = reasoner.bind(data);
// TestUtil.assertIteratorValues(this,
// infgraph.find(null, s, null),
// new Object[] {
// new Triple(a, s, c),
// new Triple(c, s, a)
// } );
// }
/**
* Test rebind operation
*/
public void testRebind() {
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) });
Graph ndata = Factory.createGraphMem();
ndata.add(new Triple(a, p, d));
ndata.add(new Triple(d, p, b));
infgraph.rebind(ndata);
TestUtil.assertIteratorValues(this, infgraph.find(null, r, null), new Object[] { new Triple(a, r, b) });
}
use of org.apache.jena.reasoner.InfGraph in project jena by apache.
the class TestBackchainer method testBaseRules2b.
/**
* Test basic rule operations - simple OR rule with chaining
*/
public void testBaseRules2b() {
List<Rule> rules = Rule.parseRules("[r1: (?a r ?b) <- (?a p ?b)]" + "[r2: (?a r ?b) <- (?a q ?b)]" + "[r3: (?a r ?b) <- (?a t ?c), (?c t ?b)]" + "[r4: (?a t ?b) <- (?a s ?b)]");
Graph data = Factory.createGraphMem();
data.add(new Triple(a, p, b));
data.add(new Triple(b, q, c));
data.add(new Triple(a, s, b));
data.add(new Triple(b, s, d));
Reasoner reasoner = createReasoner(rules);
InfGraph infgraph = reasoner.bind(data);
TestUtil.assertIteratorValues(this, infgraph.find(null, r, null), new Object[] { new Triple(a, r, b), new Triple(b, r, c), new Triple(a, r, d) });
}
use of org.apache.jena.reasoner.InfGraph in project jena by apache.
the class TestBackchainer method testListData.
/**
* Check that a reasoner over an empty rule set accesses
* the raw data successfully.
*/
public void testListData() {
Graph data = Factory.createGraphMem();
for (Triple dataElt : dataElts) {
data.add(dataElt);
}
Graph schema = Factory.createGraphMem();
schema.add(new Triple(c, p, c));
// Case of schema and data but no rule axioms
Reasoner reasoner = createReasoner(new ArrayList<Rule>());
InfGraph infgraph = reasoner.bindSchema(schema).bind(data);
TestUtil.assertIteratorValues(this, infgraph.find(null, null, null), new Object[] { new Triple(p, sP, q), new Triple(q, sP, r), new Triple(a, p, b), new Triple(c, p, c) });
// Case of data and rule axioms but no schema
List<Rule> rules = Rule.parseRules("-> (d p d).");
reasoner = createReasoner(rules);
infgraph = reasoner.bind(data);
TestUtil.assertIteratorValues(this, infgraph.find(null, null, null), new Object[] { new Triple(p, sP, q), new Triple(q, sP, r), new Triple(a, p, b), new Triple(d, p, d) });
// Case of data and rule axioms and schema
infgraph = reasoner.bindSchema(schema).bind(data);
TestUtil.assertIteratorValues(this, infgraph.find(null, null, null), new Object[] { new Triple(p, sP, q), new Triple(q, sP, r), new Triple(a, p, b), new Triple(c, p, c), new Triple(d, p, d) });
}
use of org.apache.jena.reasoner.InfGraph in project jena by apache.
the class TestModelFactory method testCreateInfModel.
public void testCreateInfModel() {
final String rule = "-> (eg:r eg:p eg:v).";
final Reasoner r = new GenericRuleReasoner(Rule.parseRules(rule));
final InfGraph ig = r.bind(ModelFactory.createDefaultModel().getGraph());
final InfModel im = ModelFactory.createInfModel(ig);
JenaTestBase.assertInstanceOf(InfModel.class, im);
Assert.assertEquals(1, im.size());
}
Aggregations