代码之家  ›  专栏  ›  技术社区  ›  user1298416

OWL API-设置新本体的前缀和本体注释(属性)

  •  0
  • user1298416  · 技术社区  · 7 年前

    我使用的是一个本体,它有以下“标题”:

    @prefix : <http://purl.obolibrary.org/obo/doid.owl#> .
    @prefix owl: <http://www.w3.org/2002/07/owl#> .
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix oboInOwl: <http://www.geneontology.org/formats/oboInOwl#> .
    @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
    @prefix doid: <http://purl.obolibrary.org/obo/doid#> .
    @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
    @prefix obo: <http://purl.obolibrary.org/obo/> .
    @prefix foaf: <http://xmlns.com/foaf/0.1/> .
    @prefix fn: <http://www.w3.org/2005/xpath-functions#> .
    @prefix sesame: <http://www.openrdf.org/schema/sesame#> .
    
    obo:doid.owl a owl:Ontology ;
        oboInOwl:hasOBOFormatVersion "1.2"^^xsd:string ;
        oboInOwl:date "25:03:2016 16:27"^^xsd:string ;
        oboInOwl:auto-generated-by "OBO-Edit 2.3.1"^^xsd:string ;
        rdfs:comment "This work is licensed under a Creative Commons Attribution 3.0 Unported License http://creativecommons.org/licenses/by/3.0/."^^xsd:string ;
        oboInOwl:default-namespace "disease_ontology"^^xsd:string ;
        oboInOwl:saved-by "someone"^^xsd:string ;
        owl:versionIRI <http://purl.obolibrary.org/obo/doid/releases/2016-03-25/doid.owl> .
    

    我正在将它与另一个本体合并。合并文件具有以下标头:

    @prefix : <file:/Users/Downloads/merged.ttl#> .
    @prefix owl: <http://www.w3.org/2002/07/owl#> .
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix xml: <http://www.w3.org/XML/1998/namespace> .
    @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
    @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
    @base <file:/Users/Downloads/merged.ttl> .
    
    <file:/Users/Downloads/merged.ttl> rdf:type owl:Ontology .
    

    就是这样。我想添加一些属性,比如oboInOwl:hasOBOFormatVersion、rdfs:comment、oboInOwl:default namespace、oboInOwl:saved by等。 我该怎么做?非常感谢。

    以下是合并代码:

    OWLOntology ontology2 = CalcDiff.getOntology2();
    
    File output2 = new File("/ontology_management/DOID_MERGED/doid_do.ttl");
    
    IRI mergedOntologyIRI = IRI.create(output2);
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    
    OWLOntology ontology1 = manager.loadOntologyFromOntologyDocument(new 
    File("/ontology_management/DOID_CURRENT/doid.ttl"));
    
    OWLDocumentFormat format = manager.getOntologyFormat(ontology1);
    
    // save the merged ontology in Turtle format
    TurtleDocumentFormat newFormat = new TurtleDocumentFormat();
    
    // will copy the prefixes over so that we have nicely abbreviated IRIs
    // in the new ontology document
    if (format.isPrefixOWLOntologyFormat()) {
          newFormat.copyPrefixesFrom(format.asPrefixOWLOntologyFormat());
    }
    
    //Using HashSet to avoid duplicated entries
    Set<OWLAxiom> axiomsall = new HashSet<OWLAxiom>();
    Set<OWLImportsDeclaration> importsall = new HashSet<OWLImportsDeclaration>();
    
    try {    
        ontology1.getAxioms().forEach(c -> {axiomsall.add(c);});
        ontology1.getImportsDeclarations().forEach(c -> {importsall.add(c);});
        ontology2.getAxioms().forEach(c -> {axiomsall.add(c);});
        ontology2.getImportsDeclarations().forEach(c -> {importsall.add(c);});
    
        mergedOntology = manager.createOntology(mergedOntologyIRI);
    
        manager.addAxioms(mergedOntology, axiomsall);
    
    } catch (OWLOntologyCreationException e) {
        e.printStackTrace();
    } 
    //Adding the import declarations
    for(OWLImportsDeclaration decl : importsall){
        manager.applyChange(new AddImport(mergedOntology, decl));
    }
    

    好的,上面的代码让我明白了这一点:

    @prefix : <http://purl.obolibrary.org/obo/doid.owl#> .
    @prefix fn: <http://www.w3.org/2005/xpath-functions#> .
    @prefix obo: <http://purl.obolibrary.org/obo/> .
    @prefix owl: <http://www.w3.org/2002/07/owl#> .
    @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix xml: <http://www.w3.org/XML/1998/namespace> .
    @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
    @prefix doid: <http://purl.obolibrary.org/obo/doid#> .
    @prefix foaf: <http://xmlns.com/foaf/0.1/> .
    @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
    @prefix sesame: <http://www.openrdf.org/schema/sesame#> .
    @prefix oboInOwl: <http://www.geneontology.org/formats/oboInOwl#> .
    @base <file:/ontology_management/DOID_MERGED/doid_do.ttl> .
    
    <file:/ontology_management/DOID_MERGED/doid_do.ttl> rdf:type owl:Ontology .
    

    现在我有了所有的前缀,但本体的属性不存在(这是我最感兴趣的)

    1 回复  |  直到 7 年前
        1
  •  4
  •   UninformedUser    7 年前

    你的意思不是所谓的“属性”,而是前缀声明。下次,最好提供一些如何加载/保存本体的代码:

    OWLOntologyFormat format = manager.getOntologyFormat(YOUR_FIRST_ONTOLOGY);
    
    // save the merged ontology in RDF/XML format
    RDFXMLDocumentFormat newFormat = new RDFXMLDocumentFormat();
    
    // will copy the prefixes over so that we have nicely abbreviated IRIs
    // in the new ontology document
    if (format.isPrefixOWLOntologyFormat()) {
        newFormat.copyPrefixesFrom(format.asPrefixOWLOntologyFormat());
    }
    manager.saveOntology(YOUR_MERGED_ONTOLOGY, newFormat, IRI.create(file.toURI()));
    

    复制本体注释:

    manager.applyChanges(
                        ontology1.getAnnotations().stream()
                                .map(an -> new AddOntologyAnnotation(mergedOntology, an))
                                .collect(Collectors.toList()));
    

    关于本体的合并,使用现有方法要容易得多:

    Set<OWLOntology> ontologies = ...
    
    OWLOntology mergedOntology = manager.createOntology(mergedOntologyIRI, ontologies, false);
    

    或者直接使用这个类 org.semanticweb.owlapi.util.OWLOntologyMerger ...