一旦我写了一个内置函数,我需要做些什么来使这个推理器知道呢? [英] Once I've written a builtin, what do I need to do to make the reasoners aware of it?
问题描述
我已经写了两个类。其中一个是内建的(使用
BaseBuiltin
),另一个我已经注册了新的内置函数(使用 BuiltinRegistry
) 我已经尝试使用默认内置函数,使用Java编写的可以从Eclipse读取的文本文件中使用它们的规则。在这种情况下,我没有任何问题。我如何使用我已经建成的内置?我应该在某些文件中导入(或包含)某些东西吗?
首先定义一个 Builtin
,通常通过扩展
BaseBuiltin
,然后使用 BuiltinRegistry.theRegistry.register(Builtin)
使它可用于耶拿基于规则的推理。
一旦你这样做,你需要实际使用一个规则,将引用你的内置
为了触发它。
BuiltinRegistry.theRegistry.register (new BaseBuiltin(){
@Override
public String getName(){
returnexample;
}
@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)\\\
\t-> print(?s?p?o),\\\
\t example(?s?p?o)\\\
]+
;
System.out.println(exampleRuleString);
/ *当我从字符串中构造
*时,我倾向于使用相当详细的语法来解析我的规则。您可以从任何其他来源阅读它们。
* /
final列表< Rule>规则;
try(final BufferedReader src = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(exampleRuleString.getBytes())))){
rules = Rule.parseRules(Rule.rulesParserFromReader(src));
}
/ *构造一个推理器并将规则与它相关联* /
final GenericRuleReasoner reasoner =(GenericRuleReasoner)GenericRuleReasonerFactory.theInstance()。create(null);
reasoner.setRules(rules);
/ *创建&准备InfModel。如果不调用prepare,则
*规则启动和推断可能会被推迟,直到您查询
*模型,而不是在插入时发生。这可以让你认为
*你的内置不工作,当它是。
* /
final InfModel infModel = ModelFactory.createInfModel(reasoner,ModelFactory.createDefaultModel());
infModel.prepare();
/ *在图中添加一个三元组:
* [] rdf:type rdfs:Class
* /
infModel.createResource(RDFS.Class);
此代码的输出将是:
- 前向链接规则的字符串
- 调用打印的结果
内置
- 调用示例的结果
内置
...这正是我们看到的:
[mat1:(?s?p?o)
- >打印(?s?p?o),
示例(?s?p?o)
]
-2b47400d:14593fc1564:-7fff rdf:type rdfs:Class
头行动:[-2b47400d:14593fc1564:-7fff,http://www.w3.org/1999/02/22-rdf-syntax-ns#type,http://www.w3.org/2000/01/rdf -schema#Class]
I have written a custom builtin to use in my Project but I do not really know how I can use it.
I have written two classes. In one of them there is the builtin I have made (using BaseBuiltin
) and in the other one I have registered the new builtin (using BuiltinRegistry
).
I have already tried to use default builtins, writing rules that use them in a text file readable from Eclipse using Java. In this case I do not have any problems. How can I use the builtin I have built? Should I import (or include) something in some files?
First you define a Builtin
, usually by extending BaseBuiltin
, and then you use BuiltinRegistry.theRegistry.register(Builtin)
to make it available to Jena rule-based inference.
Once you've done that, you need to actually use a rule that will reference your Builtin
in order to trigger 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:
- The string of the forward-chaining rule
- The result of calling the print
Builtin
- The result of calling the example
Builtin
... which 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]
这篇关于一旦我写了一个内置函数,我需要做些什么来使这个推理器知道呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!