这是重新创建包含您所描述的结构类型的本体的代码,并显示如何检索您感兴趣的注释属性值。 getSampleOntology()
创建本体(并打印它),并main
显示您可以如何开始x
,找到它的hasSynonym
值,然后找到rdfs:label
这些值的 s。这是基于文档中的OWL API 代码示例。
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.OWLOntologyDocumentTarget;
import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;
import org.semanticweb.owlapi.io.SystemOutDocumentTarget;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAnnotation;
import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom;
import org.semanticweb.owlapi.model.OWLAnnotationProperty;
import org.semanticweb.owlapi.model.OWLAnnotationSubject;
import org.semanticweb.owlapi.model.OWLAnonymousIndividual;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyFormat;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;
public class OWLAPIAnnotations {
final static OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
final static OWLDataFactory factory = manager.getOWLDataFactory();
final static String ns = "http://example.org/";
final static OWLNamedIndividual x = factory.getOWLNamedIndividual( IRI.create( ns+"x" ));
final static OWLAnonymousIndividual y = factory.getOWLAnonymousIndividual();
final static OWLObjectProperty hasSynonym = factory.getOWLObjectProperty(IRI.create(ns + "hasSynonym"));
public static OWLOntology getSampleOntology() throws OWLOntologyCreationException, OWLOntologyStorageException {
OWLOntology ontology = manager.createOntology();
manager.addAxiom( ontology, factory.getOWLClassAssertionAxiom( factory.getOWLClass(IRI.create(ns + "Synonym")), y));
manager.addAxiom(ontology, factory.getOWLObjectPropertyAssertionAxiom(hasSynonym, x, y));
OWLAnnotation ann = factory.getOWLAnnotation( factory.getOWLAnnotationProperty(OWLRDFVocabulary.RDFS_LABEL.getIRI()), factory.getOWLLiteral("Endocardium", "en"));
manager.addAxiom(ontology, factory.getOWLAnnotationAssertionAxiom(y, ann));
manager.saveOntology(ontology, System.out);
return ontology;
}
public static void main(String[] args) throws OWLOntologyCreationException, OWLOntologyStorageException {
OWLOntology ontology = getSampleOntology();
for( OWLIndividual object : x.getObjectPropertyValues( hasSynonym, ontology ) ) {
for ( OWLAnnotationAssertionAxiom aAxiom : ontology.getAnnotationAssertionAxioms( (OWLAnnotationSubject) object ) ) {
if ( aAxiom.getProperty().equals( factory.getRDFSLabel() ) ) {
System.out.println( aAxiom.getValue() );
}
}
}
}
}
最后的输出(显示注释值)是:
"Endocardium"@en
生成的本体是(删除注释,用于空间):
<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.w3.org/2002/07/owl#"
xml:base="http://www.w3.org/2002/07/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#"
xmlns:example="http://example.org/">
<ObjectProperty rdf:about="http://example.org/hasSynonym"/>
<Class rdf:about="http://example.org/Synonym"/>
<NamedIndividual rdf:about="http://example.org/x">
<example:hasSynonym>
<rdf:Description>
<rdf:type rdf:resource="http://example.org/Synonym"/>
<rdfs:label xml:lang="en">Endocardium</rdfs:label>
</rdf:Description>
</example:hasSynonym>
</NamedIndividual>
</rdf:RDF>