Search in sources :

Example 1 with InfGraph

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

the class DatasetGraphOne method unwrap.

private static Graph unwrap(Graph graph) {
    for (; ; ) {
        if (graph instanceof InfGraph) {
            graph = ((InfGraph) graph).getRawGraph();
            continue;
        }
        Graph graph2 = GraphOps.unwrapOne(graph);
        if (graph2 == graph)
            return graph;
        graph = graph2;
    }
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) Graph(org.apache.jena.graph.Graph)

Example 2 with InfGraph

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

the class GenericRuleReasoner method bind.

/**
 * Attach the reasoner to a set of RDF data to process.
 * The reasoner may already have been bound to specific rules or ontology
 * axioms (encoded in RDF) through earlier bindRuleset calls.
 *
 * @param data the RDF data to be processed, some reasoners may restrict
 * the range of RDF which is legal here (e.g. syntactic restrictions in OWL).
 * @return an inference graph through which the data+reasoner can be queried.
 * @throws ReasonerException if the data is ill-formed according to the
 * constraints imposed by this reasoner.
 */
@Override
public InfGraph bind(Graph data) throws ReasonerException {
    Graph schemaArg = schemaGraph == null ? getPreload() : schemaGraph;
    InfGraph graph = null;
    if (mode == FORWARD) {
        graph = new BasicForwardRuleInfGraph(this, rules, schemaArg);
        ((BasicForwardRuleInfGraph) graph).setTraceOn(traceOn);
    } else if (mode == FORWARD_RETE) {
        graph = new RETERuleInfGraph(this, rules, schemaArg);
        ((BasicForwardRuleInfGraph) graph).setTraceOn(traceOn);
        ((BasicForwardRuleInfGraph) graph).setFunctorFiltering(filterFunctors);
    } else if (mode == BACKWARD) {
        graph = new LPBackwardRuleInfGraph(this, getBruleStore(), data, schemaArg);
        ((LPBackwardRuleInfGraph) graph).setTraceOn(traceOn);
    } else {
        List<Rule> ruleSet = ((FBRuleInfGraph) schemaArg).getRules();
        FBRuleInfGraph fbgraph = new FBRuleInfGraph(this, ruleSet, schemaArg);
        graph = fbgraph;
        if (enableTGCCaching)
            fbgraph.setUseTGCCache();
        fbgraph.setTraceOn(traceOn);
        fbgraph.setFunctorFiltering(filterFunctors);
        if (preprocessorHooks != null) {
            for (RulePreprocessHook preprocessorHook : preprocessorHooks) {
                fbgraph.addPreprocessingHook(preprocessorHook);
            }
        }
    }
    graph.setDerivationLogging(recordDerivations);
    graph.rebind(data);
    return graph;
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) Graph(org.apache.jena.graph.Graph)

Example 3 with InfGraph

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

the class GenericRuleReasoner method bindSchema.

// =======================================================================
// Implementation methods
/**
 * Precompute the implications of a schema graph. The statements in the graph
 * will be combined with the data when the final InfGraph is created.
 */
@Override
public Reasoner bindSchema(Graph tbox) throws ReasonerException {
    if (schemaGraph != null) {
        throw new ReasonerException("Can only bind one schema at a time to a GenericRuleReasoner");
    }
    Graph graph = null;
    if (mode == FORWARD) {
        graph = new BasicForwardRuleInfGraph(this, rules, null, tbox);
        ((InfGraph) graph).prepare();
    } else if (mode == FORWARD_RETE) {
        graph = new RETERuleInfGraph(this, rules, null, tbox);
        ((InfGraph) graph).prepare();
    } else if (mode == BACKWARD) {
        graph = tbox;
    } else {
        List<Rule> ruleSet = rules;
        graph = new FBRuleInfGraph(this, ruleSet, getPreload(), tbox);
        if (enableTGCCaching)
            ((FBRuleInfGraph) graph).setUseTGCCache();
        ((FBRuleInfGraph) graph).prepare();
    }
    GenericRuleReasoner grr = new GenericRuleReasoner(rules, graph, factory, mode);
    grr.setDerivationLogging(recordDerivations);
    grr.setTraceOn(traceOn);
    grr.setTransitiveClosureCaching(enableTGCCaching);
    grr.setFunctorFiltering(filterFunctors);
    if (preprocessorHooks != null) {
        for (RulePreprocessHook preprocessorHook : preprocessorHooks) {
            grr.addPreprocessingHook(preprocessorHook);
        }
    }
    return grr;
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) Graph(org.apache.jena.graph.Graph) ReasonerException(org.apache.jena.reasoner.ReasonerException)

Example 4 with InfGraph

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

the class TestReasoners method testTransitiveRebind.

/**
 * Test rebind operation for the transitive reasoner
 */
public void testTransitiveRebind() {
    Graph data = Factory.createGraphMem();
    Node C1 = NodeFactory.createURI("C1");
    Node C2 = NodeFactory.createURI("C2");
    Node C3 = NodeFactory.createURI("C3");
    Node C4 = NodeFactory.createURI("C4");
    data.add(new Triple(C1, RDFS.subClassOf.asNode(), C2));
    data.add(new Triple(C2, RDFS.subClassOf.asNode(), C3));
    Reasoner reasoner = TransitiveReasonerFactory.theInstance().create(null);
    assertTrue(reasoner.supportsProperty(RDFS.subClassOf));
    assertTrue(!reasoner.supportsProperty(RDFS.domain));
    InfGraph infgraph = reasoner.bind(data);
    TestUtil.assertIteratorValues(this, infgraph.find(C1, null, null), new Object[] { new Triple(C1, RDFS.subClassOf.asNode(), C1), new Triple(C1, RDFS.subClassOf.asNode(), C2), new Triple(C1, RDFS.subClassOf.asNode(), C3) });
    Graph data2 = Factory.createGraphMem();
    data2.add(new Triple(C1, RDFS.subClassOf.asNode(), C2));
    data2.add(new Triple(C2, RDFS.subClassOf.asNode(), C4));
    infgraph.rebind(data2);
    // Incremental additions
    Node a = NodeFactory.createURI("a");
    Node b = NodeFactory.createURI("b");
    Node c = NodeFactory.createURI("c");
    infgraph.add(new Triple(a, RDFS.subClassOf.asNode(), b));
    infgraph.add(new Triple(b, RDFS.subClassOf.asNode(), c));
    TestUtil.assertIteratorValues(this, infgraph.find(b, RDFS.subClassOf.asNode(), null), new Object[] { new Triple(b, RDFS.subClassOf.asNode(), c), new Triple(b, RDFS.subClassOf.asNode(), b) });
    TestUtil.assertIteratorValues(this, infgraph.find(a, RDFS.subClassOf.asNode(), null), new Object[] { new Triple(a, RDFS.subClassOf.asNode(), a), new Triple(a, RDFS.subClassOf.asNode(), b), new Triple(a, RDFS.subClassOf.asNode(), c) });
    Node p = NodeFactory.createURI("p");
    Node q = NodeFactory.createURI("q");
    Node r = NodeFactory.createURI("r");
    infgraph.add(new Triple(p, RDFS.subPropertyOf.asNode(), q));
    infgraph.add(new Triple(q, RDFS.subPropertyOf.asNode(), r));
    TestUtil.assertIteratorValues(this, infgraph.find(q, RDFS.subPropertyOf.asNode(), null), new Object[] { new Triple(q, RDFS.subPropertyOf.asNode(), q), new Triple(q, RDFS.subPropertyOf.asNode(), r) });
    TestUtil.assertIteratorValues(this, infgraph.find(p, RDFS.subPropertyOf.asNode(), null), new Object[] { new Triple(p, RDFS.subPropertyOf.asNode(), p), new Triple(p, RDFS.subPropertyOf.asNode(), q), new Triple(p, RDFS.subPropertyOf.asNode(), r) });
}
Also used : Triple(org.apache.jena.graph.Triple) InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) Graph(org.apache.jena.graph.Graph) TransitiveReasoner(org.apache.jena.reasoner.transitiveReasoner.TransitiveReasoner) Reasoner(org.apache.jena.reasoner.Reasoner) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node)

Example 5 with InfGraph

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

the class TestReasoners method doTestMetaLevel.

/**
 * Test metalevel add/remove subproperty operations for a reasoner.
 */
public void doTestMetaLevel(ReasonerFactory rf) {
    Graph data = Factory.createGraphMem();
    Node c1 = NodeFactory.createURI("C1");
    Node c2 = NodeFactory.createURI("C2");
    Node c3 = NodeFactory.createURI("C3");
    Node p = NodeFactory.createURI("p");
    Node q = NodeFactory.createURI("q");
    Node sC = RDFS.subClassOf.asNode();
    Node sP = RDFS.subPropertyOf.asNode();
    data.add(new Triple(c2, sC, c3));
    data.add(new Triple(c1, p, c2));
    Reasoner reasoner = rf.create(null);
    InfGraph infgraph = reasoner.bind(data);
    TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null), new Object[] {});
    infgraph.add(new Triple(p, q, sC));
    TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null), new Object[] {});
    infgraph.add(new Triple(q, sP, sP));
    TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null), new Object[] { new Triple(c1, sC, c1), new Triple(c1, sC, c2), new Triple(c1, sC, c3) });
    infgraph.delete(new Triple(p, q, sC));
    TestUtil.assertIteratorValues(this, infgraph.find(c1, sC, null), new Object[] {});
}
Also used : Triple(org.apache.jena.graph.Triple) InfGraph(org.apache.jena.reasoner.InfGraph) InfGraph(org.apache.jena.reasoner.InfGraph) Graph(org.apache.jena.graph.Graph) TransitiveReasoner(org.apache.jena.reasoner.transitiveReasoner.TransitiveReasoner) Reasoner(org.apache.jena.reasoner.Reasoner) RDFNode(org.apache.jena.rdf.model.RDFNode) Node(org.apache.jena.graph.Node)

Aggregations

InfGraph (org.apache.jena.reasoner.InfGraph)71 Reasoner (org.apache.jena.reasoner.Reasoner)36 Graph (org.apache.jena.graph.Graph)12 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