Search in sources :

Example 66 with InfGraph

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());
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) Derivation(org.apache.jena.reasoner.Derivation)

Example 67 with InfGraph

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) });
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) Reasoner(org.apache.jena.reasoner.Reasoner)

Example 68 with InfGraph

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) });
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) Reasoner(org.apache.jena.reasoner.Reasoner)

Example 69 with InfGraph

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) });
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) Reasoner(org.apache.jena.reasoner.Reasoner)

Example 70 with InfGraph

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());
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) GenericRuleReasoner(org.apache.jena.reasoner.rulesys.GenericRuleReasoner) Reasoner(org.apache.jena.reasoner.Reasoner) GenericRuleReasoner(org.apache.jena.reasoner.rulesys.GenericRuleReasoner) InfModel(org.apache.jena.rdf.model.InfModel)

Aggregations

InfGraph (org.apache.jena.reasoner.InfGraph)70 Reasoner (org.apache.jena.reasoner.Reasoner)36 Graph (org.apache.jena.graph.Graph)11 Triple (org.apache.jena.graph.Triple)10 Node (org.apache.jena.graph.Node)7 RDFNode (org.apache.jena.rdf.model.RDFNode)5 TransitiveReasoner (org.apache.jena.reasoner.transitiveReasoner.TransitiveReasoner)5 FBRuleInfGraph (org.apache.jena.reasoner.rulesys.FBRuleInfGraph)3 JenaException (org.apache.jena.shared.JenaException)3 Derivation (org.apache.jena.reasoner.Derivation)2 ReasonerException (org.apache.jena.reasoner.ReasonerException)2 TriplePattern (org.apache.jena.reasoner.TriplePattern)2 XSDDatatype (org.apache.jena.datatypes.xsd.XSDDatatype)1 XSDDateTime (org.apache.jena.datatypes.xsd.XSDDateTime)1 InfModel (org.apache.jena.rdf.model.InfModel)1 InfModelImpl (org.apache.jena.rdf.model.impl.InfModelImpl)1 BasicForwardRuleReasoner (org.apache.jena.reasoner.rulesys.BasicForwardRuleReasoner)1 BuiltinException (org.apache.jena.reasoner.rulesys.BuiltinException)1 GenericRuleReasoner (org.apache.jena.reasoner.rulesys.GenericRuleReasoner)1 Rule (org.apache.jena.reasoner.rulesys.Rule)1