3

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.

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

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);
4

1 回答 1

3

使用 Java 代码的一种方法是通过 OWL API -OWLDataFactory该类具有创建 SWRL 规则的方法,并且可以将生成的规则添加到本体并保存 - 这与 Protege 4 和 5 使用的过程相同。

文档可在此处获得

于 2016-05-28T09:28:44.243 回答