Search in sources :

Example 1 with GenericRuleReasoner

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;
    }
}
Also used : GenericRuleReasoner(org.apache.jena.reasoner.rulesys.GenericRuleReasoner) Reasoner(org.apache.jena.reasoner.Reasoner) GenericRuleReasoner(org.apache.jena.reasoner.rulesys.GenericRuleReasoner) IOException(java.io.IOException)

Example 2 with GenericRuleReasoner

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);
}
Also used : Triple(org.apache.jena.graph.Triple) Graph(org.apache.jena.graph.Graph) ByteArrayInputStream(java.io.ByteArrayInputStream) GenericRuleReasoner(org.apache.jena.reasoner.rulesys.GenericRuleReasoner) Reasoner(org.apache.jena.reasoner.Reasoner) Node(org.apache.jena.graph.Node) InfModel(org.apache.jena.rdf.model.InfModel) Model(org.apache.jena.rdf.model.Model) GenericRuleReasoner(org.apache.jena.reasoner.rulesys.GenericRuleReasoner) InfModel(org.apache.jena.rdf.model.InfModel) Test(org.junit.Test)

Example 3 with GenericRuleReasoner

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));
}
Also used : GenericRuleReasoner(org.apache.jena.reasoner.rulesys.GenericRuleReasoner) Test(org.junit.Test)

Example 4 with GenericRuleReasoner

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());
}
Also used : InfGraph(org.apache.jena.reasoner.InfGraph) GenericRuleReasoner(org.apache.jena.reasoner.rulesys.GenericRuleReasoner) Reasoner(org.apache.jena.reasoner.Reasoner) GenericRuleReasoner(org.apache.jena.reasoner.rulesys.GenericRuleReasoner) InfModel(org.apache.jena.rdf.model.InfModel)

Example 5 with GenericRuleReasoner

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 });
}
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

GenericRuleReasoner (org.apache.jena.reasoner.rulesys.GenericRuleReasoner)5 Reasoner (org.apache.jena.reasoner.Reasoner)3 Graph (org.apache.jena.graph.Graph)2 Triple (org.apache.jena.graph.Triple)2 InfModel (org.apache.jena.rdf.model.InfModel)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Node (org.apache.jena.graph.Node)1 Model (org.apache.jena.rdf.model.Model)1 InfGraph (org.apache.jena.reasoner.InfGraph)1 Rule (org.apache.jena.reasoner.rulesys.Rule)1 SafeGraph (org.apache.jena.reasoner.rulesys.impl.SafeGraph)1