简体   繁体   中英

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.

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

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.

Documentation is available here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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