1

给定一个任意 IRI,例如主本体或它导入的一个本体,我想提取标题,但代码没有产生注释。

这是我正在谈论的一个示例,来自 SKOS 本体:

  <owl:Ontology rdf:about="http://www.w3.org/2004/02/skos/core">
    <dct:title xml:lang="en">SKOS Vocabulary</dct:title>

我将如何提取“SKOS 词汇”。

这是我目前在 OWL-API 教程中使用的一些代码。

public void testingOWL() throws OWLOntologyCreationException, OWLOntologyStorageException
{

// Get hold of an ontology manager 
OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); 

// Load an ontology from the Web.  We load the ontology from a document IRI 
IRI docIRI = IRI.create("http://www.w3.org/2009/08/skos-reference/skos.rdf"); 
OWLOntology skos = manager.loadOntologyFromOntologyDocument(docIRI); 

System.out.println("Loaded ontology: " + skos); 
System.out.println();

// Save a local copy of the ontology.  (Specify a path appropriate to your setup) 
File file = new File("e:/downloadAndSaveOWLFile.owl"); 
manager.saveOntology(skos, IRI.create(file.toURI())); 

// Ontologies are saved in the format from which they were loaded.   
// We can get information about the format of an ontology from its manager 
OWLOntologyFormat format = manager.getOntologyFormat(skos); 
System.out.println("    format: " + format); 
System.out.println();

// Save the ontology in owl/xml format 
OWLXMLOntologyFormat owlxmlFormat = new OWLXMLOntologyFormat(); 

// Some ontology formats support prefix names and prefix IRIs.   
// In our case we loaded the pizza ontology from an rdf/xml format, which supports prefixes.  
// When we save the ontology in the new format we will copy the prefixes over  
// so that we have nicely abbreviated IRIs in the new ontology document 
if(format.isPrefixOWLOntologyFormat()) 
{ 
    owlxmlFormat.copyPrefixesFrom(format.asPrefixOWLOntologyFormat()); 
} 

manager.saveOntology(skos, owlxmlFormat, IRI.create(file.toURI())); 

// Dump an ontology to System.out by specifying a different OWLOntologyOutputTarget 
// Note that we can write an ontology to a stream in a similar way  
// using the StreamOutputTarget class 
OWLOntologyDocumentTarget documentTarget = new SystemOutDocumentTarget(); 

// Try another format - The Manchester OWL Syntax 
ManchesterOWLSyntaxOntologyFormat manSyntaxFormat = new ManchesterOWLSyntaxOntologyFormat(); 

if(format.isPrefixOWLOntologyFormat()) 
{ 
    manSyntaxFormat.copyPrefixesFrom(format.asPrefixOWLOntologyFormat()); 
} 
manager.saveOntology(skos, manSyntaxFormat, documentTarget); 
}

编辑:根据以下建议更新代码,但仅返回 rdfs:seeAlso 的 1 个对象。

公共 void getData() 抛出 OWLOntologyCreationException

{
    // Get hold of an ontology manager 
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

    // Load an ontology from the Web.  We load the ontology from a document IRI 
    IRI docIRI = IRI.create("http://www.w3.org/2009/08/skos-reference/skos.rdf"); 
    OWLOntology skos = manager.loadOntologyFromOntologyDocument(docIRI); 

    for (OWLAnnotation ann: skos.getAnnotations())
    {
        System.out.println("ann: " + ann.getProperty());
        System.out.println();
    }


}
4

1 回答 1

0

您要查找的注释是本体注释,这意味着作为其主题的 IRI 是本体 IRI 本身。这与标准注释的访问方式不同。

OWLOntology o= ... // init the ontology as usual
for (OWLAnnotation ann: o.getAnnotations()){
    if(ann.getProperty().equals(dataFactory.getRDFSLabel()){
        // here you have found a rdfs:label annotation, so you can use the value for your purposes
    }
}

编辑:使用示例

public static void main(String[] args) throws OWLOntologyCreationException {
    OWLOntologyManager m = OWLManager.createOWLOntologyManager();
    OWLOntology o = m.loadOntology(IRI
            .create("http://www.w3.org/2009/08/skos-reference/skos.rdf"));
    for (OWLAnnotation a : o.getAnnotations()) {
        System.out.println("TestSkos.main() " + a);
    }
}
Output:

    TestSkos.main() Annotation(rdfs:seeAlso <http://www.w3.org/TR/skos-reference/>)
    TestSkos.main() Annotation(<http://purl.org/dc/terms/creator> "Alistair Miles")
    TestSkos.main() Annotation(<http://purl.org/dc/terms/description> "An RDF vocabulary for describing the basic structure and content of concept schemes such as thesauri, classification schemes, subject heading lists, taxonomies, 'folksonomies', other types of controlled vocabulary, and also concept schemes embedded in glossaries and terminologies."@en)
    TestSkos.main() Annotation(<http://purl.org/dc/terms/contributor> "Participants in W3C's Semantic Web Deployment Working Group.")
    TestSkos.main() Annotation(<http://purl.org/dc/terms/creator> "Sean Bechhofer")
    TestSkos.main() Annotation(<http://purl.org/dc/terms/contributor> "Nikki Rogers")
    TestSkos.main() Annotation(<http://purl.org/dc/terms/title> "SKOS Vocabulary"@en)
    TestSkos.main() Annotation(<http://purl.org/dc/terms/contributor> "Dave Beckett")
于 2015-01-15T07:40:07.997 回答