Jena API 规则测试:在Jena 中编写规则的方法有多少?

Jena API Rules Test : how many and what are the methods to write rules in Jena?

我成功地使用 Jena API 和 Eclipse 创建、修改、合并了本体。我也成功地在 Eclipse 中启动了 OWL 推理机和 Pellet 推理机。

现在我想了解如何使用 Jena 在 Eclipse 中编写一个简单的规则 API 以及是否有一种或多种方法可以做到这一点。

例如,在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) ];

我 运行 都在 Eclipse SW 版本中:Luna Service Release 2 (4.4.2) with JDK 1.7 我得到了这些错误:

 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)

这里有几个不同的问题。 Stack Overflow 和其他地方有很多关于如何编写 Jena 规则的示例,所以我认为这里的关键问题是如何重写 这个特定的 SWRL 规则。没有你的任何尝试,我们不知道你 运行 遇到了什么问题,但我认为 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)

会变成这样:

@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)]

我不记得 Jena 中的数学内置函数,所以我查看了 builtin primitives 的列表。