我是OWL API的新手,并试图尝试使用它。我使用OWL API在eclipse中创建了一个简单的本体,用于保护和互惠应用。本体结构如下: -
----------------------------------------------------------------------------------------
Class Object (Data Property)StringValue ObjectProperty relatedTo
----------------------------------------------------------------------------------------
WordString WS1 "One" DS1
WS2 "Two" DS2
WS3 "Three" DS3
DigiString DS1 "1"
DS2 "2"
DS3 "3"
----------------------------------------------------------------------------
完整的本体论如下: -
<rdf:RDF xmlns="http://localhost:3030/DigiWord.owl#"
xml:base="http://localhost:3030/DigiWord.owl"
xmlns:DigiWord="http://localhost:3030/DigiWord.owl#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<owl:Ontology rdf:about="http://localhost:3030/DigiWord.owl"/>
<!--
// Object Properties
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://localhost:3030/DigiWord.owl#relatedTo -->
<owl:ObjectProperty rdf:about="&DigiWord;relatedTo">
<rdfs:domain rdf:resource="&DigiWord;DigiString"/>
<rdfs:range rdf:resource="&DigiWord;WordString"/>
</owl:ObjectProperty>
<!--
//
// Data properties
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://localhost:3030/DigiWord.owl#stringValue -->
<owl:DatatypeProperty rdf:about="&DigiWord;stringValue">
<rdfs:domain rdf:resource="&DigiWord;DigiString"/>
<rdfs:domain rdf:resource="&DigiWord;WordString"/>
<rdfs:range rdf:resource="&xsd;string"/>
</owl:DatatypeProperty>
<!--
// Classes
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://localhost:3030/DigiWord.owl#DigiString -->
<owl:Class rdf:about="&DigiWord;DigiString"/>
<!-- http://localhost:3030/DigiWord.owl#WordString -->
<owl:Class rdf:about="&DigiWord;WordString"/>
<!--
// Individuals
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://localhost:3030/DigiWord.owl#DS1 -->
<owl:NamedIndividual rdf:about="&DigiWord;DS1">
<rdf:type rdf:resource="&DigiWord;DigiString"/>
<stringValue rdf:datatype="&xsd;string">1</stringValue>
<relatedTo rdf:resource="&DigiWord;WS1"/>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#DS2 -->
<owl:NamedIndividual rdf:about="&DigiWord;DS2">
<rdf:type rdf:resource="&DigiWord;DigiString"/>
<stringValue rdf:datatype="&xsd;string">2</stringValue>
<relatedTo rdf:resource="&DigiWord;WS2"/>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#DS3 -->
<owl:NamedIndividual rdf:about="&DigiWord;DS3">
<rdf:type rdf:resource="&DigiWord;DigiString"/>
<stringValue rdf:datatype="&xsd;string"></stringValue>
<relatedTo rdf:resource="&DigiWord;WS3"/>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#WS1 -->
<owl:NamedIndividual rdf:about="&DigiWord;WS1">
<rdf:type rdf:resource="&DigiWord;WordString"/>
<stringValue rdf:datatype="&xsd;string">One</stringValue>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#WS2 -->
<owl:NamedIndividual rdf:about="&DigiWord;WS2">
<rdf:type rdf:resource="&DigiWord;WordString"/>
<stringValue rdf:datatype="&xsd;string">Two</stringValue>
</owl:NamedIndividual>
<!-- http://localhost:3030/DigiWord.owl#WS3 -->
<owl:NamedIndividual rdf:about="&DigiWord;WS3">
<rdf:type rdf:resource="&DigiWord;WordString"/>
<stringValue rdf:datatype="&xsd;string">Three</stringValue>
</owl:NamedIndividual>
</rdf:RDF>
我想为DS1检索对象WS1,为DS2检索WS2,即提供字符串&#34; 1&#34;,&#34; 2&#34;我的代码应该检索&#34; One&#34; ,&#34;两个&#34;分别。我没有在网上遇到过这种类型的相关代码。任何帮助都将不胜感激。在此先感谢。
答案 0 :(得分:1)
以下示例(OWLAPI 4)应该有所帮助:
OWLOntology o = ... your ontology here
String ns = "http://localhost:3030/DigiWord.owl#";
OWLDataFactory df = o.getOWLOntologyManager().getOWLDataFactory();
OWLObjectProperty relatedto = df.getOWLObjectProperty(IRI.create(ns + "relatedTo"));
OWLDataProperty stringValue = df.getOWLDataProperty(IRI.create(ns + "stringValue"));
SimpleRenderer renderer = new SimpleRenderer();
for (OWLNamedIndividual i : o.getIndividualsInSignature()) {
for (OWLLiteral lit : EntitySearcher.getDataPropertyValues(i, stringValue, o)) {
System.out.println(i + " has values " + renderer.render(lit));
}
for (OWLIndividual related : EntitySearcher.getObjectPropertyValues(i, relatedto, o)) {
for (OWLLiteral lit : EntitySearcher.getDataPropertyValues(related, stringValue, o)) {
System.out.println(i + " related to " + related + " which has values " + renderer.render(lit));
}
}
}
编辑:查找您的查询匹配项:
for (OWLNamedIndividual i : o.getIndividualsInSignature()) {
for (OWLIndividual related : EntitySearcher.getObjectPropertyValues(i, relatedto, o)) {
for (OWLLiteral lit : EntitySearcher.getDataPropertyValues(related, stringValue, o)) {
if(lit.getLexicalForm().equals("1")){
for (OWLLiteral lit : EntitySearcher.getDataPropertyValues(i, stringValue, o)) {
System.out.println(i + " has values " + renderer.render(lit));
}
}
}
}
}