use of org.apache.jena.reasoner.rulesys.GenericRuleReasoner in project jena by apache.
the class LibTestRDFS method createRulesGraph.
/**
* Create a Jena-rules backed graph
*/
static Graph createRulesGraph(Graph data, Graph vocab, String rulesFile) {
try {
String rules = FileUtils.readWholeFileAsUTF8(rulesFile);
rules = rules.replaceAll("#[^\\n]*", "");
Reasoner reasoner = new GenericRuleReasoner(Rule.parseRules(rules));
return reasoner.bindSchema(vocab).bind(data);
} catch (IOException ex) {
IO.exception(ex);
return null;
}
}
use of org.apache.jena.reasoner.rulesys.GenericRuleReasoner in project jena by apache.
the class TestLPBRuleCloseBug method testCloseOfTabledIterator.
/**
* Test case for JENA-2184.
*/
@Test
public void testCloseOfTabledIterator() {
Model m = ModelFactory.createDefaultModel();
String data = StrUtils.strjoinNL("@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .", "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .", "", "<urn:ic:x1> rdf:type <urn:ic:SUB> .", "<urn:ic:SUB> rdfs:subClassOf <urn:ic:CLASS> .");
m.read(new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8)), "", "Turtle");
String rules = "-> table(rdf:type). (?a rdf:type <urn:ic:CLASS>) <- (?a rdf:type <urn:ic:SUB>) .";
Reasoner reasoner = new GenericRuleReasoner(Rule.parseRules(rules));
InfModel infModel = ModelFactory.createInfModel(reasoner.bind(m.getGraph()));
Graph infGraph = infModel.getGraph();
Node x1 = NodeFactory.createURI("urn:ic:x1");
Node clsSUB = NodeFactory.createURI("urn:ic:SUB");
Node clsCLASS = NodeFactory.createURI("urn:ic:CLASS");
ExtendedIterator<Triple> sInfIter = infGraph.find(x1, RDF.Nodes.type, clsSUB);
assertTrue(sInfIter.hasNext());
// Closing without having read from the iterator
// Forces a close of LPInterpreter instances including on behind the tabled goal for the find
sInfIter.close();
// This query depends on the above tabled goal which was not complete before the close()
ExtendedIterator<Triple> cInfIter = infGraph.find(x1, RDF.Nodes.type, clsCLASS);
boolean foundClass = cInfIter.hasNext();
assertTrue(foundClass);
}
use of org.apache.jena.reasoner.rulesys.GenericRuleReasoner in project jena by apache.
the class TestRestartableLBRule method testCrossTransactionQueryBug.
@Test
public void testCrossTransactionQueryBug() {
DummyTxnGraph graph = new DummyTxnGraph();
Model data = ModelFactory.createModelForGraph(graph);
for (int i = 0; i < 1000; i++) {
data.add(data.createResource("http://example.com/ns/i-" + i), RDF.type, Senator);
}
String rules = "-> tableAll(). " + "[r1: (?x rdf:type <http://example.com/ns/Person>) <- (?x rdf:type <http://example.com/ns/Politician>)] " + "[r2: (?x rdf:type <http://example.com/ns/Politician>) <- (?x rdf:type <http://example.com/ns/Senator>)] " + "[r3: (?x rdf:type <http://example.com/ns/Person>) <- (?x rdf:type <http://example.com/ns/Senator>)] ";
GenericRuleReasoner reasoner = new GenericRuleReasoner(Rule.parseRules(rules));
InfModel infmodel = ModelFactory.createInfModel(reasoner, data);
assertTrue(queryN(infmodel, Person, 10));
assertTrue(queryN(infmodel, Politician, 1000));
assertTrue(queryN(infmodel, Person, 1000));
}
use of org.apache.jena.reasoner.rulesys.GenericRuleReasoner in project jena by apache.
the class TestModelFactory method testCreateInfModel.
public void testCreateInfModel() {
final String rule = "-> (eg:r eg:p eg:v).";
final Reasoner r = new GenericRuleReasoner(Rule.parseRules(rule));
final InfGraph ig = r.bind(ModelFactory.createDefaultModel().getGraph());
final InfModel im = ModelFactory.createInfModel(ig);
JenaTestBase.assertInstanceOf(InfModel.class, im);
Assert.assertEquals(1, im.size());
}
use of org.apache.jena.reasoner.rulesys.GenericRuleReasoner 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 });
}
Aggregations