Jena 是一个以 RDF 为中心的 API,尽管它以OntModel的形式提供了一些抽象。即便如此,OntModels 并没有提供一种方便的方式来访问公理并对其进行注释。使用更以 OWL 为中心的 API,例如恰当命名的OWL API ,您可能会获得更好的运气。
尽管如此,OWL 可以序列化为 RDF,虽然可能存在缺陷(因为 OWL 本体序列化为 RDF 的方式可能有所不同),但您可能会得到您想要的结果。这是加载一小部分本体的 Java 代码,找到其中的owl:Axiom
s,并确定它们的哪些属性是注释属性。
import java.util.HashSet;
import java.util.Set;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.vocabulary.OWL2;
import com.hp.hpl.jena.vocabulary.RDF;
public class AnnotationExample {
/**
* @param args
*/
public static void main(String[] args) {
// create the model and load the data.
Model model = ModelFactory.createDefaultModel().read( "products.owl" );
// owlAnnotationProperties are the properties used to represent
// annotated axioms in RDF/XML.
Set<Property> owlAnnotationProperties = new HashSet<Property>() {{
add( RDF.type );
add( OWL2.annotatedProperty );
add( OWL2.annotatedSource );
add( OWL2.annotatedTarget );
}};
// Find the axioms in the model. For each axiom, iterate through the
// its properties, looking for those that are *not* used for encoding the
// annotated axiom. Those that are left are the annotations.
ResIterator axioms = model.listSubjectsWithProperty( RDF.type, OWL2.Axiom );
while ( axioms.hasNext() ) {
Resource axiom = axioms.next();
StmtIterator stmts = axiom.listProperties();
while ( stmts.hasNext() ) {
Statement stmt = stmts.next();
if ( !owlAnnotationProperties.contains( stmt.getPredicate() )) {
System.out.println( stmt );
}
}
}
}
}
输出显示您感兴趣的语句。
[630c9cd5:13f7b69db3c:-7ffe, http://www.example.com/products#isNegative, "true"^^http://www.w3.org/2001/XMLSchema#boolean]
这是我使用的小型 OWL 本体:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:products="http://www.example.com/products#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:Ontology rdf:about="http://www.example.com/products"/>
<owl:Class rdf:about="http://www.example.com/products#TV">
<rdfs:subClassOf>
<owl:Restriction>
<owl:someValuesFrom>
<owl:Class rdf:about="http://www.example.com/products#PowerConsumption"/>
</owl:someValuesFrom>
<owl:onProperty>
<owl:ObjectProperty rdf:about="http://www.example.com/products#hasFeature"/>
</owl:onProperty>
</owl:Restriction>
</rdfs:subClassOf>
</owl:Class>
<owl:AnnotationProperty rdf:about="http://www.example.com/products#isNegative"/>
<owl:Axiom>
<owl:annotatedTarget>
<owl:Restriction>
<owl:someValuesFrom rdf:resource="http://www.example.com/products#PowerConsumption"/>
<owl:onProperty rdf:resource="http://www.example.com/products#hasFeature"/>
</owl:Restriction>
</owl:annotatedTarget>
<owl:annotatedProperty rdf:resource="http://www.w3.org/2000/01/rdf-schema#subClassOf"/>
<owl:annotatedSource rdf:resource="http://www.example.com/products#TV"/>
<products:isNegative rdf:datatype="http://www.w3.org/2001/XMLSchema#boolean"
>true</products:isNegative>
</owl:Axiom>
</rdf:RDF>