Search in sources :

Example 1 with SafeGraph

use of org.apache.jena.reasoner.rulesys.impl.SafeGraph in project jena by apache.

the class BasicForwardRuleInfGraph method createDeductionsGraph.

/** 
     * Create the graph used to hold the deductions. Can be overridden
     * by subclasses that need special purpose graph implementations here.
     * Assumes the graph underlying fdeductions and associated SafeGraph
     * wrapper can be reused if present thus enabling preservation of
     * listeners. 
     */
protected Graph createDeductionsGraph() {
    if (fdeductions != null) {
        Graph dg = fdeductions.getGraph();
        if (dg != null) {
            // Reuse the old graph in order to preserve any listeners
            safeDeductions.clear();
            return dg;
        }
    }
    Graph dg = Factory.createGraphMem();
    safeDeductions = new SafeGraph(dg);
    return dg;
}
Also used : Graph(org.apache.jena.graph.Graph) SafeGraph(org.apache.jena.reasoner.rulesys.impl.SafeGraph) SafeGraph(org.apache.jena.reasoner.rulesys.impl.SafeGraph)

Example 2 with SafeGraph

use of org.apache.jena.reasoner.rulesys.impl.SafeGraph in project jena by apache.

the class TestSafeModel method testBasics.

/**
     * Create a generalized model via inference and check it is
     * safe but unwrappable
     */
public void testBasics() {
    Model base = ModelFactory.createDefaultModel();
    Resource r = base.createResource(egNS + "r");
    Property p = base.createProperty(egNS + "p");
    Property q = base.createProperty(egNS + "q");
    Literal l = base.createLiteral("foo");
    Statement asserted = base.createStatement(r, p, l);
    r.addProperty(p, l);
    List<Rule> rules = Rule.parseRules("(?r eg:p ?v) -> (?v eg:q ?r).");
    GenericRuleReasoner reasoner = new GenericRuleReasoner(rules);
    InfModel inf = ModelFactory.createInfModel(reasoner, base);
    TestUtil.assertIteratorValues(this, inf.listStatements(), new Statement[] { asserted });
    Model deductions = inf.getDeductionsModel();
    TestUtil.assertIteratorValues(this, deductions.listStatements(), new Statement[] {});
    Graph safeGraph = deductions.getGraph();
    assertTrue(safeGraph instanceof SafeGraph);
    Graph rawGraph = ((SafeGraph) safeGraph).getRawGraph();
    Triple deduction = new Triple(l.asNode(), q.asNode(), r.asNode());
    TestUtil.assertIteratorValues(this, rawGraph.find(Node.ANY, Node.ANY, Node.ANY), new Triple[] { deduction });
}
Also used : Triple(org.apache.jena.graph.Triple) SafeGraph(org.apache.jena.reasoner.rulesys.impl.SafeGraph) Graph(org.apache.jena.graph.Graph) SafeGraph(org.apache.jena.reasoner.rulesys.impl.SafeGraph) GenericRuleReasoner(org.apache.jena.reasoner.rulesys.GenericRuleReasoner) Rule(org.apache.jena.reasoner.rulesys.Rule)

Aggregations

Graph (org.apache.jena.graph.Graph)2 SafeGraph (org.apache.jena.reasoner.rulesys.impl.SafeGraph)2 Triple (org.apache.jena.graph.Triple)1 GenericRuleReasoner (org.apache.jena.reasoner.rulesys.GenericRuleReasoner)1 Rule (org.apache.jena.reasoner.rulesys.Rule)1