Jena 虽然通过 OntModels 对 OWL 提供了一些支持,但它实际上是一个基于 RDF 的 API。 Jena 中的语句只是代表三元组的类,本身并不代表资源。如果您想创建带注释的对象属性断言,您需要查看OWL 2 Web Ontology Language Mapping to RDF Graphs 中的Section 2.3 Translation of Axioms with Annotations。具体来说,看起来2.3.1 Axioms that Generate a Main Triple 是您想要的:
如果表 1 中对应 ax' 类型的行包含一个
单个主三元组 s p xlt .,然后公理 ax 被翻译成
以下三元组:
s p xlt .
_:x rdf:type owl:Axiom .
_:x owl:annotatedSource s .
_:x owl:annotatedProperty p .
_:x owl:annotatedTarget xlt .
TANN(annotation1, _:x)
...
TANN(annotationm, _:x)
如果 ax' 的类型为 ... ObjectPropertyAssertion ...,就会出现这种情况。
引用的表 1 出现在前面的部分 2.1 Translation of Axioms without Annotations。
所以,添加三元组
Mobile hasCamera 8MP
带有注释
hasWeightScore 6.7
您可以使用以下代码:
import com.hp.hpl.jena.ontology.AnnotationProperty;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.OWL;
import com.hp.hpl.jena.vocabulary.OWL2;
public class AnnotatedAxioms {
public static void main(String[] args) {
final String ns = "http://example.org/";
final OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM );
model.setNsPrefix( "ex", ns );
final Individual mobile = model.createIndividual( ns+"Mobile", OWL.Thing );
final ObjectProperty hasCamera = model.createObjectProperty( ns+"hasCamera" );
final Individual eightMP = model.createIndividual( ns+"8MP", OWL.Thing );
final AnnotationProperty hasWeightScore = model.createAnnotationProperty( ns+"hasWeightScore" );
final Resource axiom = model.createResource( OWL2.Axiom );
axiom.addProperty( OWL2.annotatedSource, mobile );
axiom.addProperty( OWL2.annotatedProperty, hasCamera );
axiom.addProperty( OWL2.annotatedTarget, eightMP );
axiom.addLiteral( hasWeightScore, 6.7 );
model.write( System.out, "RDF/XML-ABBREV" );
}
}
产生以下本体:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:ex="http://example.org/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:ObjectProperty rdf:about="http://example.org/hasCamera"/>
<owl:AnnotationProperty rdf:about="http://example.org/hasWeightScore"/>
<owl:Axiom>
<ex:hasWeightScore rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>6.7</ex:hasWeightScore>
<owl:annotatedTarget>
<owl:Thing rdf:about="http://example.org/8MP"/>
</owl:annotatedTarget>
<owl:annotatedProperty rdf:resource="http://example.org/hasCamera"/>
<owl:annotatedSource>
<owl:Thing rdf:about="http://example.org/Mobile"/>
</owl:annotatedSource>
</owl:Axiom>
</rdf:RDF>