Search in sources :

Example 1 with Reasoner

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

the class OntModelImpl method generateGraph.

/**
     * <p>Helper method to the constructor, which interprets the spec and generates an appropriate
     * graph for this model</p>
     * @param spec The model spec to interpret
     * @param base The base model, or null
     */
private static Graph generateGraph(OntModelSpec spec, Graph base) {
    // create a empty union graph
    MultiUnion u = new MultiUnion();
    u.addGraph(base);
    u.setBaseGraph(base);
    Reasoner r = spec.getReasoner();
    // if we have a reasoner in the spec, bind to the union graph and return
    return r == null ? (Graph) u : r.bind(u);
}
Also used : Reasoner(org.apache.jena.reasoner.Reasoner) MultiUnion(org.apache.jena.graph.compose.MultiUnion)

Example 2 with Reasoner

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

the class TestBackchainer method testBaseRulesXSB1.

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

Example 3 with Reasoner

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

the class TestBackchainer method testBaseRules4.

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

Example 4 with Reasoner

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

the class TestBackchainer method testClose.

/**
     * Test close and halt operation.
     */
public void testClose() {
    Graph data = Factory.createGraphMem();
    data.add(new Triple(p, sP, q));
    data.add(new Triple(q, sP, r));
    data.add(new Triple(C1, sC, C2));
    data.add(new Triple(C2, sC, C3));
    data.add(new Triple(a, ty, C1));
    data.add(new Triple(ty, RDFS.range.asNode(), RDFS.Class.asNode()));
    List<Rule> rules = Rule.parseRules("[rdfs8:  (?a rdfs:subClassOf ?b), (?b rdfs:subClassOf ?c) -> (?a rdfs:subClassOf ?c)]" + "[rdfs9:  (?x rdfs:subClassOf ?y), (?a rdf:type ?x) -> (?a rdf:type ?y)]" + //        "[-> (rdf:type rdfs:range rdfs:Class)]" +
    "[rdfs3:  (?x ?p ?y), (?p rdfs:range ?c) -> (?y rdf:type ?c)]" + "[rdfs7:  (?a rdf:type rdfs:Class) -> (?a rdfs:subClassOf ?a)]");
    Reasoner reasoner = createReasoner(rules);
    InfGraph infgraph = reasoner.bind(data);
    // Get just one result
    ExtendedIterator<Triple> it = infgraph.find(a, ty, null);
    Triple result = it.next();
    assertEquals(result.getSubject(), a);
    assertEquals(result.getPredicate(), ty);
    it.close();
    // Make sure if we start again we get the full listing.
    TestUtil.assertIteratorValues(this, infgraph.find(a, ty, null), new Object[] { new Triple(a, ty, C1), new Triple(a, ty, C2), new Triple(a, ty, C3) });
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) Reasoner(org.apache.jena.reasoner.Reasoner)

Example 5 with Reasoner

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

the class TestBackchainer method testHeadUnify.

/**
     * Test complex rule head unification
     */
public void testHeadUnify() {
    Graph data = Factory.createGraphMem();
    data.add(new Triple(c, q, d));
    List<Rule> rules = Rule.parseRules("[r1: (c r ?x) <- (?x p f(?x b))]" + "[r2: (?y p f(a ?y)) <- (c q ?y)]");
    Reasoner reasoner = createReasoner(rules);
    InfGraph infgraph = reasoner.bind(data);
    TestUtil.assertIteratorValues(this, infgraph.find(c, r, null), new Object[] {});
    data.add(new Triple(c, q, a));
    rules = Rule.parseRules("[r1: (c r ?x) <- (?x p f(?x a))]" + "[r2: (?y p f(a ?y)) <- (c q ?y)]");
    reasoner = createReasoner(rules);
    infgraph = reasoner.bind(data);
    TestUtil.assertIteratorValues(this, infgraph.find(c, r, null), new Object[] { new Triple(c, r, a) });
    data = Factory.createGraphMem();
    data.add(new Triple(a, q, a));
    data.add(new Triple(a, q, b));
    data.add(new Triple(a, q, c));
    data.add(new Triple(b, q, d));
    data.add(new Triple(b, q, b));
    rules = Rule.parseRules("[r1: (c r ?x) <- (?x p ?x)]" + "[r2: (?x p ?y) <- (a q ?x), (b q ?y)]");
    reasoner = createReasoner(rules);
    infgraph = reasoner.bind(data);
    TestUtil.assertIteratorValues(this, infgraph.find(c, r, null), new Object[] { new Triple(c, r, b) });
    rules = Rule.parseRules("[r1: (c r ?x) <- (?x p ?x)]" + "[r2: (a p ?x) <- (a q ?x)]");
    reasoner = createReasoner(rules);
    infgraph = reasoner.bind(data);
    TestUtil.assertIteratorValues(this, infgraph.find(c, r, null), new Object[] { new Triple(c, r, a) });
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) Reasoner(org.apache.jena.reasoner.Reasoner)

Aggregations

Reasoner (org.apache.jena.reasoner.Reasoner)45 InfGraph (org.apache.jena.reasoner.InfGraph)36 TransitiveReasoner (org.apache.jena.reasoner.transitiveReasoner.TransitiveReasoner)7 Graph (org.apache.jena.graph.Graph)5 Node (org.apache.jena.graph.Node)5 Triple (org.apache.jena.graph.Triple)5 InfModel (org.apache.jena.rdf.model.InfModel)5 RDFNode (org.apache.jena.rdf.model.RDFNode)5 ByteArrayInputStream (java.io.ByteArrayInputStream)2 OntModel (org.apache.jena.ontology.OntModel)2 Model (org.apache.jena.rdf.model.Model)2 Resource (org.apache.jena.rdf.model.Resource)2 JenaException (org.apache.jena.shared.JenaException)2 CommandLine (org.apache.commons.cli.CommandLine)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1 DefaultParser (org.apache.commons.cli.DefaultParser)1 Options (org.apache.commons.cli.Options)1 MultiUnion (org.apache.jena.graph.compose.MultiUnion)1 Property (org.apache.jena.rdf.model.Property)1 StmtIterator (org.apache.jena.rdf.model.StmtIterator)1