简体   繁体   English

如何使用 Java 或颗粒将 SWRL 规则添加到本体?

[英]How to add SWRL rules to an Ontology using Java or pellet?

Instead of using Protégé to add rules to my ontology, I want to use Java or pellet to add SWRL rule to the ontology.我不想使用 Protégé 向我的本体添加规则,而是想使用 Java 或颗粒将 SWRL 规则添加到本体中。

For example, this is the rule that I want to import into my ontology:例如,这是我想导入到我的本体中的规则:

[Course(?x),teacherOf(?y,?x),worksFor(?y,?z)] => [coursePresentedInUniversity(?x,?z)] [Course(?x),teacherOf(?y,?x),worksFor(?y,?z)] => [coursePresentedInUniversity(?x,?z)]

I want to add the following code into the ontology:我想将以下代码添加到本体中:

<swrl:Imp rdf:about="#CoursePresentedInUniversityRule">
    <swrl:head rdf:parseType="Collection">  
        <swrl:IndividualPropertyAtom>
                <swrl:propertyPredicate rdf:resource="#coursePresentedInUniversity" />
                <swrl:argument1 rdf:resource="#x" />
                <swrl:argument2 rdf:resource="#z" />
        </swrl:IndividualPropertyAtom>
    </swrl:head>
    <swrl:body rdf:parseType="Collection">
        <swrl:ClassAtom>
            <swrl:classPredicate rdf:resource="#Course" />
            <swrl:argument1 rdf:resource="#x" />
        </swrl:ClassAtom>

        <swrl:IndividualPropertyAtom>
            <swrl:propertyPredicate rdf:resource="#teacherOf" />
            <swrl:argument1 rdf:resource="#y" />
            <swrl:argument2 rdf:resource="#x" />
        </swrl:IndividualPropertyAtom>
        <swrl:IndividualPropertyAtom>
            <swrl:propertyPredicate rdf:resource="#worksFor" />
            <swrl:argument1 rdf:resource="#y" />
            <swrl:argument2 rdf:resource="#z" />
        </swrl:IndividualPropertyAtom>

    </swrl:body>
</swrl:Imp>

Could anybody point me out to a sample code to do that?有人可以指出我的示例代码来做到这一点吗?

Actually, I wrote the following code, but it didn't work!其实,我写了下面的代码,但是没有用!

    Rule mynewRule=new Rule(ruleHead,ruleBody);
    PelletReasoner pelletReasoner =com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory.getInstance().createReasoner(testOntology );
    KnowledgeBase knowledgeBase=pelletReasoner.getKB();
    knowledgeBase.addRule(mynewRule);

One way with Java code is through the OWL API - the OWLDataFactory class has methods for creating SWRL rules, and the resulting rules can be added to an ontology and saved - it is the same process used by Protege 4 and 5. Java 代码的一种方法是通过 OWL API—— OWLDataFactory类具有创建 SWRL 规则的方法,生成的规则可以添加到本体并保存——这与 Protege 4 和 5 使用的过程相同。

Documentation is available here文档可在此处获得

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM