Search in sources :

Example 1 with BaseBuiltin

use of org.apache.jena.reasoner.rulesys.builtins.BaseBuiltin 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)

Aggregations

ArrayList (java.util.ArrayList)1 Node (org.apache.jena.graph.Node)1 InfModel (org.apache.jena.rdf.model.InfModel)1 Model (org.apache.jena.rdf.model.Model)1 Rule.parseRule (org.apache.jena.reasoner.rulesys.Rule.parseRule)1 BaseBuiltin (org.apache.jena.reasoner.rulesys.builtins.BaseBuiltin)1