Search in sources :

Example 1 with InfModel

use of org.apache.jena.rdf.model.InfModel in project jena by apache.

the class TestGenericRuleReasonerConfig method testRuleLoadingWithOverridenBuiltins.

public void testRuleLoadingWithOverridenBuiltins() {
    List<Node> savedNode = new ArrayList<>();
    Builtin b = new BaseBuiltin() {

        @Override
        public String getName() {
            return "groo";
        }

        @Override
        public int getArgLength() {
            return 1;
        }

        @Override
        public void headAction(Node[] args, int length, RuleContext context) {
            savedNode.add(getArg(0, args, context));
        }
    };
    BuiltinRegistry r = new OverrideBuiltinRegistry(BuiltinRegistry.theRegistry);
    r.register(b);
    assertEquals(b, r.getImplementation("groo"));
    List<Rule> rules = new ArrayList<>();
    //
    // note that the head action does not appear to fire unless we put a triple in the head as well..  is
    // this expected?
    //
    rules.add(parseRule("[ (?instance rdf:type ?type) -> groo(?type) ]", r));
    GenericRuleReasoner article = new GenericRuleReasoner(rules);
    article.setMode(GenericRuleReasoner.FORWARD_RETE);
    Model input = ModelFactory.createDefaultModel();
    input.add(input.createResource(), RDF.type, input.createResource("http://example.com/Renegade"));
    InfModel output = ModelFactory.createInfModel(article, input);
    // not optional, inferences are not run if we don't trigger them
    output.size();
    assertEquals(1, savedNode.size());
    assertEquals("http://example.com/Renegade", savedNode.get(0).getURI());
}
Also used : Node(org.apache.jena.graph.Node) ArrayList(java.util.ArrayList) BaseBuiltin(org.apache.jena.reasoner.rulesys.builtins.BaseBuiltin) InfModel(org.apache.jena.rdf.model.InfModel) InfModel(org.apache.jena.rdf.model.InfModel) Model(org.apache.jena.rdf.model.Model) Rule.parseRule(org.apache.jena.reasoner.rulesys.Rule.parseRule) BaseBuiltin(org.apache.jena.reasoner.rulesys.builtins.BaseBuiltin)

Example 2 with InfModel

use of org.apache.jena.rdf.model.InfModel in project jena by apache.

the class TestReasoners method testTransitiveBindReuse.

/**
     * The reasoner contract for bind(data) is not quite precise. It allows for
     * reasoners which have state so that reusing the same reasoner on a second data
     * model might lead to interference. This in fact used to happen with the transitive
     * reasoner. This is a test to check that the transitive reasoner state reuse has been fixed at source.
     */
public void testTransitiveBindReuse() {
    Reasoner r = ReasonerRegistry.getTransitiveReasoner();
    InfModel om1 = ModelFactory.createInfModel(r, ModelFactory.createDefaultModel());
    Resource c1 = om1.createResource(PrintUtil.egNS + "Class1");
    Resource c2 = om1.createResource(PrintUtil.egNS + "Class2");
    Resource c3 = om1.createResource(PrintUtil.egNS + "Class3");
    om1.add(c1, RDFS.subClassOf, c2);
    om1.add(c2, RDFS.subClassOf, c3);
    om1.prepare();
    assertFalse(om1.isEmpty());
    InfModel om2 = ModelFactory.createInfModel(r, ModelFactory.createDefaultModel());
    StmtIterator si = om2.listStatements();
    boolean ok = !si.hasNext();
    si.close();
    assertTrue("Transitive reasoner state leak", ok);
}
Also used : StmtIterator(org.apache.jena.rdf.model.StmtIterator) TransitiveReasoner(org.apache.jena.reasoner.transitiveReasoner.TransitiveReasoner) Reasoner(org.apache.jena.reasoner.Reasoner) Resource(org.apache.jena.rdf.model.Resource) InfModel(org.apache.jena.rdf.model.InfModel)

Example 3 with InfModel

use of org.apache.jena.rdf.model.InfModel in project jena by apache.

the class TestReasoners method testTransitiveEngineSeparation.

/**
     * Test that two transitive engines are independent.
     * See JENA-1260
     */
public void testTransitiveEngineSeparation() throws InterruptedException {
    String NS = "http://example.com/test#";
    Property sp = ResourceFactory.createProperty(NS, "sp");
    Property p = ResourceFactory.createProperty(NS, "p");
    Property s = ResourceFactory.createProperty(NS, "s");
    Resource q = ResourceFactory.createProperty(NS, "q");
    Reasoner reasoner = ReasonerRegistry.getTransitiveReasoner();
    InfModel simple = ModelFactory.createInfModel(reasoner, ModelFactory.createDefaultModel());
    simple.add(s, sp, p);
    assertFalse(simple.contains(s, RDFS.subPropertyOf, p));
    InfModel withSP = ModelFactory.createInfModel(reasoner, ModelFactory.createDefaultModel());
    withSP.add(sp, RDFS.subPropertyOf, RDFS.subPropertyOf);
    withSP.add(s, sp, p);
    assertTrue(withSP.contains(s, RDFS.subPropertyOf, p));
    simple.add(q, sp, p);
    assertFalse(simple.contains(q, RDFS.subPropertyOf, p));
}
Also used : TransitiveReasoner(org.apache.jena.reasoner.transitiveReasoner.TransitiveReasoner) Reasoner(org.apache.jena.reasoner.Reasoner) Resource(org.apache.jena.rdf.model.Resource) InfModel(org.apache.jena.rdf.model.InfModel) Property(org.apache.jena.rdf.model.Property)

Example 4 with InfModel

use of org.apache.jena.rdf.model.InfModel in project jena by apache.

the class TestQueryEngineMultiThreaded method createForwardChainingModel.

private Model createForwardChainingModel() {
    Model baseModel = ModelFactory.createDefaultModel();
    Model schemaModel = ModelFactory.createDefaultModel();
    Model configurationRuleReasoner = ModelFactory.createDefaultModel();
    org.apache.jena.rdf.model.Resource configuration = configurationRuleReasoner.createResource();
    configuration.addProperty(ReasonerVocabulary.PROPruleMode, "forward");
    configuration.addProperty(ReasonerVocabulary.PROPsetRDFSLevel, ReasonerVocabulary.RDFS_SIMPLE);
    Reasoner ruleReasoner = RDFSRuleReasonerFactory.theInstance().create(configuration);
    InfModel inf = ModelFactory.createInfModel(ruleReasoner, schemaModel, baseModel);
    inf.read(new ByteArrayInputStream(TURTLE_RDF.getBytes()), "", "TURTLE");
    return inf;
}
Also used : Reasoner(org.apache.jena.reasoner.Reasoner) ByteArrayInputStream(java.io.ByteArrayInputStream) InfModel(org.apache.jena.rdf.model.InfModel) Model(org.apache.jena.rdf.model.Model) InfModel(org.apache.jena.rdf.model.InfModel)

Example 5 with InfModel

use of org.apache.jena.rdf.model.InfModel in project jena by apache.

the class TestReasoners method testRDFSRemove.

/**
     * Test remove operations on an RDFS reasoner instance.
     * This is an example to test that rebing is invoked correctly rather
     * than an RDFS-specific test.
     */
public void testRDFSRemove() {
    InfModel m = ModelFactory.createRDFSModel(ModelFactory.createDefaultModel());
    String NS = PrintUtil.egNS;
    Property p = m.createProperty(NS, "p");
    Resource D = m.createResource(NS + "D");
    Resource i = m.createResource(NS + "i");
    Resource c = m.createResource(NS + "c");
    Resource d = m.createResource(NS + "d");
    p.addProperty(RDFS.domain, D);
    i.addProperty(p, c);
    i.addProperty(p, d);
    TestUtil.assertIteratorValues(this, i.listProperties(), new Object[] { m.createStatement(i, p, c), m.createStatement(i, p, d), m.createStatement(i, RDF.type, D), m.createStatement(i, RDF.type, RDFS.Resource) });
    i.removeAll(p);
    TestUtil.assertIteratorValues(this, i.listProperties(), new Object[] {});
}
Also used : Resource(org.apache.jena.rdf.model.Resource) InfModel(org.apache.jena.rdf.model.InfModel) Property(org.apache.jena.rdf.model.Property)

Aggregations

InfModel (org.apache.jena.rdf.model.InfModel)10 Resource (org.apache.jena.rdf.model.Resource)5 Reasoner (org.apache.jena.reasoner.Reasoner)5 Model (org.apache.jena.rdf.model.Model)4 ArrayList (java.util.ArrayList)2 Property (org.apache.jena.rdf.model.Property)2 TransitiveReasoner (org.apache.jena.reasoner.transitiveReasoner.TransitiveReasoner)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 AssemblerGroup (org.apache.jena.assembler.assemblers.AssemblerGroup)1 ExpandingAssemblerGroup (org.apache.jena.assembler.assemblers.AssemblerGroup.ExpandingAssemblerGroup)1 Node (org.apache.jena.graph.Node)1 NodeIterator (org.apache.jena.rdf.model.NodeIterator)1 RDFNode (org.apache.jena.rdf.model.RDFNode)1 ResIterator (org.apache.jena.rdf.model.ResIterator)1 StmtIterator (org.apache.jena.rdf.model.StmtIterator)1 InfGraph (org.apache.jena.reasoner.InfGraph)1 GenericRuleReasoner (org.apache.jena.reasoner.rulesys.GenericRuleReasoner)1 Rule.parseRule (org.apache.jena.reasoner.rulesys.Rule.parseRule)1 BaseBuiltin (org.apache.jena.reasoner.rulesys.builtins.BaseBuiltin)1