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;
}
}
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;
}
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;
}
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) });
}
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[] {});
}
Aggregations