1
我尝试使用OWL API来创建OWL本体。我可以定义他们之间的阶级,个人和关系。OWL API:如何确保域和范围限制?
当我定义一个ObjectProperty #hasPart
域#A
和范围#B
,我预计这个属性可以通过适用于这两个类的个人。但事实上,API不关心的限制,因此可以分配#hasPart
也#C
类的两个成员之间例如:
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.*;
public class OwlTest
{
public static void main(String[] args)
throws org.semanticweb.owlapi.model.OWLOntologyStorageException, org.semanticweb.owlapi.model.OWLOntologyCreationException, Exception
{
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLDataFactory df = manager.getOWLDataFactory();
OWLOntology o = manager.createOntology();
//------------------------------------------------------------------
OWLClass clsA = df.getOWLClass(IRI.create("#A"));
OWLClass clsB = df.getOWLClass(IRI.create("#B"));
OWLClass clsC = df.getOWLClass(IRI.create("#C"));
OWLObjectProperty hasPart = df.getOWLObjectProperty(IRI.create("#hasPart"));
OWLObjectPropertyDomainAxiom domainAxiom = df.getOWLObjectPropertyDomainAxiom(hasPart, clsA);
OWLObjectPropertyRangeAxiom rangeAxiom = df.getOWLObjectPropertyRangeAxiom(hasPart, clsB);
manager.addAxiom(o, domainAxiom);
manager.addAxiom(o, rangeAxiom);
//------------------------------------------------------------------
OWLNamedIndividual a1 = df.getOWLNamedIndividual(IRI.create("a1"));
OWLNamedIndividual b1 = df.getOWLNamedIndividual(IRI.create("b1"));
OWLNamedIndividual c1 = df.getOWLNamedIndividual(IRI.create("c1"));
OWLNamedIndividual c2 = df.getOWLNamedIndividual(IRI.create("c2"));
manager.addAxiom(o, df.getOWLClassAssertionAxiom(clsA, a1));
manager.addAxiom(o, df.getOWLClassAssertionAxiom(clsB, b1));
manager.addAxiom(o, df.getOWLClassAssertionAxiom(clsC, c1));
manager.addAxiom(o, df.getOWLClassAssertionAxiom(clsC, c2));
manager.addAxiom(o, df.getOWLObjectPropertyAssertionAxiom(hasPart, c1, c2)); // ObjectProperty '#hasPart' should only work for objects from Domain 'clsA' and Range 'clsB'
//------------------------------------------------------------------
manager.saveOntology(o, IRI.create("file:/tmp/data.owl"));
}
}
输出/tmp/data.owl
:
...
<ObjectProperty rdf:about="#hasPart">
<rdfs:domain rdf:resource="#A"/>
<rdfs:range rdf:resource="#B"/>
</ObjectProperty>
<Class rdf:about="#A"/>
<Class rdf:about="#B"/>
<Class rdf:about="#C"/>
<NamedIndividual rdf:about="a1">
<rdf:type rdf:resource="#A"/>
</NamedIndividual>
<NamedIndividual rdf:about="b1">
<rdf:type rdf:resource="#B"/>
</NamedIndividual>
<NamedIndividual rdf:about="c1">
<rdf:type rdf:resource="#C"/>
<p1:hasPart rdf:resource="c2"/>
</NamedIndividual>
...
我现在找推荐的方式来编程处理这种限制..?提前谢谢了!
Michael从这个答案是正确的,但是,它不能解决建模问题。 您的案例的解决方案:使所有三个类不相交。 这告诉系统,在一个类中的东西不能在任何其他类中。 这样,你的代码就可以像你期望的那样工作。 – Decrayer 2015-09-25 09:23:51