我成功地使用Jena API和Eclipse创建,修改和合并本体。我还成功地在Eclipse中启动了OWL推理器和Pellet推理器。
现在我想了解如何使用Jena API在Eclipse中编写一个简单的规则,以及是否有一种或多种方法可以执行此操作。
例如,在Protégé中,我曾经在这样的SWRL中编写规则来计算三角分布的均值:
TriangularDistribution(?t), hasLowerLimit(?t,?a), hasUpperLimit(?t,?c), hasMode(?t,?b), add(?ab, ?a, ?b), add(?abc, ?ab, ?c), divide(?m, ?abc, 3) -> hasMean(?t, ?m)
如何使用Jena翻译它?
========升级=========
为了更好地了解如何在Jena中创建规则,我按照https://jena.apache.org/documentation/inference/#RULEexamples
中的示例进行了操作我创建了三个文件: 第一个是“ReasoningJena.java”
package test01;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.reasoner.*;
import com.hp.hpl.jena.reasoner.rulesys.*;
import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
import com.hp.hpl.jena.util.*;
import java.io.*;
public class ReasoningJena {
private static Model reason(Model input) {
// Register a namespace to be used in the rules
String flUri = "http://www.snee.com/ns/demo#";
PrintUtil.registerPrefix("fl", flUri);
// Create an (RDF) specification of a hybrid reasoner which loads its rules from an external file.
Model m = ModelFactory.createDefaultModel();
Resource configuration = m.createResource();
configuration.addProperty(ReasonerVocabulary.PROPruleMode, "hybrid");
configuration.addProperty(ReasonerVocabulary.PROPruleSet, "C:/Users/pecore/Desktop/file.rules");
// Create an instance of such a reasoner
Reasoner reasoner = GenericRuleReasonerFactory.theInstance().create(configuration);
// Infere new knowledge on the input model, generating a new one
InfModel infmodel = ModelFactory.createInfModel(reasoner, input);
return infmodel;
}
private static Model readModelFromFile(String filePath) throws Exception {
// Create an empty model
Model model = ModelFactory.createDefaultModel();
// Use the FileManager to find the input file
InputStream in = new FileInputStream(new File(filePath));
// Read the RDF/XML file
model.read(in, "");
return model;
}
public static void main(String[] args){
try {
Model model = readModelFromFile("C:/Users/pecore/Desktop/demoData.rdf");
System.out.println("The model has " + model.size() + " statements");
// Do the reasoning
model = reason(model);
System.out.println("After inferencing the model has " + model.size() + " statements");
}
catch(Exception e) {
e.printStackTrace();
}
}
}
rdf文件是:“demoData.rdf”
<?xml version="1.0"?>
<!DOCTYPE rdf:RDF [
<!ENTITY rdf 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'>
<!ENTITY rdfs 'http://www.w3.org/2000/01/rdf-schema#'>
<!ENTITY demo 'http://jena.hpl.hp.com/demo#'>
]>
<rdf:RDF xmlns:rdf="&rdf;"
xmlns:rdfs="&rdfs;"
xmlns:demo="&demo;"
xmlns="&demo;"
>
<demo:TransProp rdf:about="&demo;p" />
<rdf:Description rdf:about="&demo;a">
<p rdf:resource="&demo;b" />
</rdf:Description>
<rdf:Description rdf:about="&demo;c">
<p rdf:resource="&demo;a" />
</rdf:Description>
<rdf:Description rdf:about="&demo;b">
<p rdf:resource="&demo;d" />
</rdf:Description>
</rdf:RDF>
规则文件是:“file.rules”
@prefix = "http://www.snee.com/ns/demo#";
[transitiveRule: (?A demo:p ?B), (?B demo:p ?C) -> (?A > demo:p ?C) ];
我使用JDK 1.7在Eclipse SW Version:Luna Service Release 2(4.4.2)中运行 我得到了这些错误:
log4j:WARN No appenders could be found for logger (org.apache.jena.riot.system.stream.JenaIOEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
The model has 4 statements
com.hp.hpl.jena.shared.PrefixMapping$IllegalPrefixException: =
at com.hp.hpl.jena.shared.impl.PrefixMappingImpl.checkLegal(PrefixMappingImpl.java:157)
at com.hp.hpl.jena.shared.impl.PrefixMappingImpl.setNsPrefix(PrefixMappingImpl.java:67)
at com.hp.hpl.jena.shared.impl.PrefixMappingImpl.setNsPrefixes(PrefixMappingImpl.java:147)
at com.hp.hpl.jena.reasoner.rulesys.Rule$Parser.registerPrefixMap(Rule.java:716)
at com.hp.hpl.jena.reasoner.rulesys.Rule.rulesParserFromReader(Rule.java:567)
at com.hp.hpl.jena.reasoner.rulesys.Util.loadRuleParserFromResourceFile(Util.java:264)
at com.hp.hpl.jena.reasoner.rulesys.FBRuleReasoner.loadRules(FBRuleReasoner.java:270)
at com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner.doSetParameter(GenericRuleReasoner.java:301)
at com.hp.hpl.jena.reasoner.rulesys.FBRuleReasoner.doSetRDFNodeParameter(FBRuleReasoner.java:370)
at com.hp.hpl.jena.reasoner.rulesys.FBRuleReasoner.loadConfiguration(FBRuleReasoner.java:101)
at com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner.<init>(GenericRuleReasoner.java:96)
at com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasonerFactory.create(GenericRuleReasonerFactory.java:56)
at test01.ReasoningJena.reason(ReasoningJena.java:24)
at test01.ReasoningJena.main(ReasoningJena.java:55)
为什么?
===========更新2 ============ 我对“file.rules”内部代码进行了一些更改,将其更正为:
@prefix demo: <http://domain/demo#>.
[transitiveRule: (?A demo:p ?B), (?B demo:p ?C) -> (?A > demo:p ?C) ]
我获得了此错误代码:
log4j:WARN No appenders could be found for logger (org.apache.jena.riot.system.stream.JenaIOEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
org.apache.jena.riot.RiotException: [line: 1, col: 8 ] The processing instruction target matching "[xX][mM][lL]" is not allowed.
at org.apache.jena.riot.system.ErrorHandlerFactory$ErrorHandlerStd.fatal(ErrorHandlerFactory.java:136)
at org.apache.jena.riot.lang.LangRDFXML$ErrorHandlerBridge.fatalError(LangRDFXML.java:253)
at com.hp.hpl.jena.rdfxml.xmlinput.impl.ARPSaxErrorHandler.fatalError(ARPSaxErrorHandler.java:48)
at com.hp.hpl.jena.rdfxml.xmlinput.impl.XMLHandler.warning(XMLHandler.java:200)
at com.hp.hpl.jena.rdfxml.xmlinput.impl.XMLHandler.fatalError(XMLHandler.java:230)
at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
at org.apache.xerces.impl.XMLScanner.scanPIData(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanPIData(Unknown Source)
at org.apache.xerces.impl.XMLScanner.scanPI(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at com.hp.hpl.jena.rdfxml.xmlinput.impl.RDFXMLParser.parse(RDFXMLParser.java:151)
at com.hp.hpl.jena.rdfxml.xmlinput.ARP.load(ARP.java:119)
at org.apache.jena.riot.lang.LangRDFXML.parse(LangRDFXML.java:143)
at org.apache.jena.riot.RDFParserRegistry$ReaderRIOTLang.read(RDFParserRegistry.java:185)
at org.apache.jena.riot.RDFDataMgr.process(RDFDataMgr.java:906)
at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:257)
at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:243)
at org.apache.jena.riot.adapters.RDFReaderRIOT_Web.read(RDFReaderRIOT_Web.java:62)
at com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:247)
at zero.readModelFromFile(zero.java:40)
at zero.main(zero.java:50)
答案 0 :(得分:2)
这里有几个不同的问题。 Stack Overflow和其他地方有很多关于如何编写Jena规则的例子,所以我认为这里的关键问题是如何重写这个特定的 SWRL规则。如果没有您的尝试,我们不知道您遇到了什么问题,但我认为SWRL规则如下:
TriangularDistribution(?t),hasLowerLimit(?t,?a),hasUpperLimit(?t,?c),hasMode(?t,?b),add(?ab,?a,?b),add( ?abc,?ab,?c),除(?m,?abc,3)→hasMean(?t,?m)
会变成这样的东西:
@prefix : <...>
[(?x rdf:type TriangularDistribution)
(?t :hasLowerLimit ?a)
(?t :hasUpperLimit ?c)
(?t :hasMode ?b)
sum(?a, ?b, ?ab)
add(?ab, ?c, ?abc)
quotient(?abc, 3, ?m)
->
(?t :hasMean ?m)]
我不记得耶拿的数学内容,所以我查看了builtin primitives的列表。