2012-08-29 21 views
2

我是OWL API的新手。我如何獲得以下OWL文件中rdfs:label的值? oboInOwl:hasSynonym是一個註釋屬性。如何閱讀使用OWL API的註解內部的rdf:description

<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

我認爲你需要展示更多的本體論。你顯示的片段只是三部分的一部分。 'oboInOwl:hasSynonym'是一個屬性,該對象是一個帶有'rdf:type'和'rdfs:label'的空白節點。知道三元組的_subject可能會有所幫助,因爲那樣我們就可以將它識別爲一個個體並詢問它的屬性。 –

回答

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>