我尝试使用OWL API创建OWL Ontologies。我可以定义它们之间的类,个体和关系。
当我使用域#hasPart
和范围#A
定义ObjectProperty #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>
...
我现在正在寻找以编程方式处理这种限制的推荐方法..?非常感谢提前!
答案 0 :(得分:3)
是的,使用hasPart w / C没有任何问题,推理者会假设你最终会告诉它c1也是A,或者C与A相同。
OWL-API不会强制执行您想要的行为,听起来您正在寻找某种完整性约束,就像您进入正常的关系系统一样。您要么必须将其烘焙到您的应用程序中,要么查看Pellet的integrity constraints之类的内容,这些内容将在即将发布的Pellet 3版本中提供,目前可在Stardog中找到。