假设我们有以下规则:


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


颗粒或Java中是否有任何库可将上述规则转换为SWRL代码?例如,以下内容:

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


我知道小球可以做相反的事情(使用reasoner.getKB().getRules()),但是我不知道是否有任何将表示形式转换为SWRL XML代码的方法。
谢谢!

最佳答案

为了将字符串转换为本体中的SWRL规则,应根据this document进行一些步骤:1)应对字符串进行解析和标记化。 2)应该使用SWRLRule和SWRLObjectProperties创建SWRL规则。 3)应用并保存本体中的更改,
例如,对于teacherOf(?y,?x),我们可以编写以下代码:

    OWLObjectProperty teacherP= factory.getOWLObjectProperty(IRI
            .create(ontologyIRI + "#teacherOf"));

    SWRLVariable var1 = factory.getSWRLVariable(IRI.create(ontologyIRI
            + "#y"));
    SWRLVariable var2 = factory.getSWRLVariable(IRI.create(ontologyIRI
            + "#x"));
    SWRLObjectPropertyAtom teacherAtom = factory.getSWRLObjectPropertyAtom(
            teacherP, var1, var2);
    Set<SWRLAtom> SWRLatomList= new HashSet<SWRLAtom>();
    SWRLatomList.add(teacherAtom);


...

    SWRLRule teacherRule = factory.getSWRLRule(SWRLatomList,
            Collections.singleton(headAtom));
    ontologyManager.applyChange(new AddAxiom(testOntology, teacherRule ));
    ontologyManager.saveOntology(testOntology);

关于java - 如何将规则转换为SWRL代码?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37449215/

10-10 19:50