OWL API,从URI中提取字符串

时间:2015-01-14 16:52:27

标签: java owl-api

鉴于任意IRI,例如主要本体或其导入的本体之一,我想提取标题,但代码不会产生注释。

以下是我所谈论的一个例子,来自SKOS本体:

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

我将如何提取,&#34; SKOS Vocabulary&#34;。

以下是我目前在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的1个对象:seeAlso。

public 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();
    }


}

1 个答案:

答案 0 :(得分: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")