我是OWL API的新手。如何在以下OWL文件中的rdfs:label
内获取值?
oboInOwl:hasSynonym
是一个注释属性。
<oboInOwl:hasSynonym>
<rdf:Description>
<rdf:type rdf:resource="&oboInOwl;Synonym"/>
<rdfs:label xml:lang="en">Endocardiums</rdfs:label>
</rdf:Description>
</oboInOwl:hasSynonym>
我正在使用Owl API。我不想使用XML解析器。
答案 0 :(得分:2)
这是重新创建包含您所描述的结构类型的本体的代码,并展示了如何检索您感兴趣的注释属性值。getSampleOntology()
创建本体(并打印它),并且main
显示了如何从x
开始,找到其hasSynonym
值,然后找到这些值的rdfs:label
。这基于文档中的OWL API code examples。
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>