【发布时间】:2012-04-16 07:06:36
【问题描述】:
我尝试使用OWL API 来创建OWL Ontologies。我可以定义类、个体和它们之间的关系。
当我用域#A 和范围#B 定义对象属性#hasPart 时,我希望这个属性只能应用于这两个类的个体。但实际上 API 并不关心限制,因此也可以在 #C 类的两个成员之间分配 #hasPart,例如:
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>
...
我现在正在寻找以编程方式处理此类限制的推荐方法..?非常感谢!
【问题讨论】:
标签: dns range owl restriction