OWL API - 设置新 ontology 的前缀和 ontology 注释(属性)
OWL API - setting prefixes and ontology annotations (properties) of the new ontology
我使用 ontology,它具有以下 "header":
@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> .
我正在将它与另一个 ontology 合并。合并后的文件具有以下 header:
@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 .
所以现在我有了所有前缀,但是 Ontology 的属性不存在(这是我最感兴趣的)
你的意思不叫"properties"而是前缀声明。下次,最好提供一些代码 load/save ontology:
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()));
正在复制 ontology 批注:
manager.applyChanges(
ontology1.getAnnotations().stream()
.map(an -> new AddOntologyAnnotation(mergedOntology, an))
.collect(Collectors.toList()));
关于本体的合并,使用现有的方法更容易:
Set<OWLOntology> ontologies = ...
OWLOntology mergedOntology = manager.createOntology(mergedOntologyIRI, ontologies, false);
或者只使用 class org.semanticweb.owlapi.util.OWLOntologyMerger
...
我使用 ontology,它具有以下 "header":
@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> .
我正在将它与另一个 ontology 合并。合并后的文件具有以下 header:
@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 .
所以现在我有了所有前缀,但是 Ontology 的属性不存在(这是我最感兴趣的)
你的意思不叫"properties"而是前缀声明。下次,最好提供一些代码 load/save ontology:
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()));
正在复制 ontology 批注:
manager.applyChanges(
ontology1.getAnnotations().stream()
.map(an -> new AddOntologyAnnotation(mergedOntology, an))
.collect(Collectors.toList()));
关于本体的合并,使用现有的方法更容易:
Set<OWLOntology> ontologies = ...
OWLOntology mergedOntology = manager.createOntology(mergedOntologyIRI, ontologies, false);
或者只使用 class org.semanticweb.owlapi.util.OWLOntologyMerger
...