Search in sources :

Example 1 with Derivation

use of org.apache.jena.reasoner.Derivation in project jena by apache.

the class TestLPDerivation method doTest.

/**
     * Run a single derivation test. Only tests the first derivation found.
     * @param ruleSrc source for a set of rules
     * @param tabled  array of predicate nodes which should be tabled by the rules
     * @param triples inital array of triple data
     * @param query   the query to be tested the first result will be checked
     * @param matches the set of triple matches which should occur in the derivation
     * @param rulenumber the index of the rule in the rule list which should occur in the derivation
     */
private void doTest(String ruleSrc, Node[] tabled, Triple[] triples, Triple query, Triple[] matches, int rulenumber) {
    List<Rule> rules = Rule.parseRules(ruleSrc);
    Graph data = Factory.createGraphMem();
    for (Triple triple : triples) {
        data.add(triple);
    }
    InfGraph infgraph = makeInfGraph(rules, data, tabled);
    ExtendedIterator<Triple> results = infgraph.find(query);
    assertTrue(results.hasNext());
    Triple result = results.next();
    results.close();
    Rule rule = rules.get(rulenumber);
    List<Triple> matchList = Arrays.asList(matches);
    Iterator<Derivation> derivations = infgraph.getDerivation(result);
    assertTrue(derivations.hasNext());
    RuleDerivation derivation = (RuleDerivation) derivations.next();
    //        PrintWriter pw = new PrintWriter(System.out);
    //        derivation.printTrace(pw, true);
    //        pw.close();
    assertEquals(result, derivation.getConclusion());
    assertEquals(matchList, derivation.getMatches());
    assertEquals(rule, derivation.getRule());
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) FBRuleInfGraph(org.apache.jena.reasoner.rulesys.FBRuleInfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) FBRuleInfGraph(org.apache.jena.reasoner.rulesys.FBRuleInfGraph) Derivation(org.apache.jena.reasoner.Derivation) RuleDerivation(org.apache.jena.reasoner.rulesys.RuleDerivation) Rule(org.apache.jena.reasoner.rulesys.Rule) RuleDerivation(org.apache.jena.reasoner.rulesys.RuleDerivation)

Example 2 with Derivation

use of org.apache.jena.reasoner.Derivation 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)

Aggregations

Derivation (org.apache.jena.reasoner.Derivation)2 InfGraph (org.apache.jena.reasoner.InfGraph)2 FBRuleInfGraph (org.apache.jena.reasoner.rulesys.FBRuleInfGraph)1 Rule (org.apache.jena.reasoner.rulesys.Rule)1 RuleDerivation (org.apache.jena.reasoner.rulesys.RuleDerivation)1