Once I have written a built-in application, what do I need to do to make it understand this?

I wrote a custom built-in for use in my project, but I really don't know how I can use it. I wrote two classes. In one of them there is an inline I made (using BaseBuiltin

) and in the other I registered a new inline (using BuiltinRegistry

).

I've already tried using the default built-in functions by writing the rules that use them in a text file readable from Eclipse using Java. In this case, I have no problem. How can I use the built-in I have built? Should I import (or include) something in some files?

+2


source to share


1 answer


You define first Builtin

, usually by expanding BaseBuiltin

, and then use BuiltinRegistry.theRegistry.register(Builtin)

to make it available for Yen-based inference.

Once you've done that, you need to use a rule that will reference yours Builtin

to invoke it.

BuiltinRegistry.theRegistry.register( new BaseBuiltin() {
    @Override
    public String getName() {
        return "example";
    }
    @Override
    public void headAction( final Node[] args, final int length, final RuleContext context ) {
        System.out.println("Head Action: "+Arrays.toString(args));
    }
} );

final String exampleRuleString =
    "[mat1: (?s ?p ?o)\n\t-> print(?s ?p ?o),\n\t   example(?s ?p ?o)\n]"+
    "";
System.out.println(exampleRuleString);

/* I tend to use a fairly verbose syntax for parsing out my rules when I construct them
 * from a string. You can read them from whatever other sources.
 */
final List<Rule> rules;
try( final BufferedReader src = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(exampleRuleString.getBytes()))) ) {
    rules = Rule.parseRules(Rule.rulesParserFromReader(src));
}

/* Construct a reasoner and associate the rules with it  */
final GenericRuleReasoner reasoner = (GenericRuleReasoner) GenericRuleReasonerFactory.theInstance().create(null);
reasoner.setRules(rules);

/* Create & Prepare the InfModel. If you don't call prepare, then
 * rule firings and inference may be deferred until you query the
 * model rather than happening at insertion. This can make you think
 * that your Builtin is not working, when it is.
 */
final InfModel infModel = ModelFactory.createInfModel(reasoner, ModelFactory.createDefaultModel());
infModel.prepare();

/* Add a triple to the graph: 
* [] rdf:type rdfs:Class
*/
infModel.createResource(RDFS.Class);

      

The output of this code will be:



  • Straight chain rule string
  • Print call result Builtin

  • Example call result Builtin

... this is exactly what we see:

[mat1: (?s ?p ?o)
    -> print(?s ?p ?o),
       example(?s ?p ?o)
]
-2b47400d:14593fc1564:-7fff rdf:type rdfs:Class 
Head Action: [-2b47400d:14593fc1564:-7fff, http://www.w3.org/1999/02/22-rdf-syntax-ns#type, http://www.w3.org/2000/01/rdf-schema#Class]

      

+3


source







All Articles