Let's assume we have the following rule:
Course(?x), teacherOf(?y,?x), worksFor(?y,?z) => coursePresentedInUniversity(?x,?z)
Is there any library in pellet or java to convert the above rule to SWRL code? for example, to the following:
<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>
I know that pellet can do the reverse (using reasoner.getKB().getRules()
), but I don't know if there is anything to transform the representation to the SWRL XML code.
Thanks!
For converting a string as SWRL rule in an ontology, according to this document some steps should be done: 1) the string should be parsed and tokenized. 2) SWRL rule should be created using SWRLRule and SWRLObjectProperties. 3) apply and save the changes in the ontology,
For example, for teacherOf(?y,?x)
we can write the following code:
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);