diff --git a/onto-viewer-core/src/main/java/org/edmcouncil/spec/ontoviewer/core/ontology/OntologyManager.java b/onto-viewer-core/src/main/java/org/edmcouncil/spec/ontoviewer/core/ontology/OntologyManager.java index 6b14b5af..c78f27aa 100644 --- a/onto-viewer-core/src/main/java/org/edmcouncil/spec/ontoviewer/core/ontology/OntologyManager.java +++ b/onto-viewer-core/src/main/java/org/edmcouncil/spec/ontoviewer/core/ontology/OntologyManager.java @@ -21,6 +21,7 @@ public class OntologyManager { private Map locationToIriMapping = new HashMap<>(); private Set ontologies; private Set missingImports; + private Map sourceNamespacesMap = new HashMap<>(); public OWLOntology getOntology() { return ontology; @@ -63,4 +64,12 @@ public Set getMissingImports() { public void setMissingImports(Set missingImports) { this.missingImports = missingImports; } + + public Map getSourceNamespacesMap() { + return sourceNamespacesMap; + } + + public void setSourceNamespacesMap(Map sourceNamespacesMap) { + this.sourceNamespacesMap = sourceNamespacesMap; + } } \ No newline at end of file diff --git a/onto-viewer-core/src/main/java/org/edmcouncil/spec/ontoviewer/core/ontology/loader/CommandLineOntologyLoader.java b/onto-viewer-core/src/main/java/org/edmcouncil/spec/ontoviewer/core/ontology/loader/CommandLineOntologyLoader.java index 265daedb..b4e4f9e0 100644 --- a/onto-viewer-core/src/main/java/org/edmcouncil/spec/ontoviewer/core/ontology/loader/CommandLineOntologyLoader.java +++ b/onto-viewer-core/src/main/java/org/edmcouncil/spec/ontoviewer/core/ontology/loader/CommandLineOntologyLoader.java @@ -16,9 +16,11 @@ import org.edmcouncil.spec.ontoviewer.core.ontology.loader.listener.MissingImportListenerImpl; import org.edmcouncil.spec.ontoviewer.core.ontology.loader.zip.ViewerZipFilesOperations; import org.semanticweb.owlapi.apibinding.OWLManager; +import org.semanticweb.owlapi.formats.PrefixDocumentFormat; import org.semanticweb.owlapi.model.AddImport; import org.semanticweb.owlapi.model.IRI; import org.semanticweb.owlapi.model.MissingImportHandlingStrategy; +import org.semanticweb.owlapi.model.OWLDocumentFormat; import org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException; import org.semanticweb.owlapi.model.OWLOntologyCreationException; import org.semanticweb.owlapi.model.OWLOntologyDocumentAlreadyExistsException; @@ -120,12 +122,18 @@ private LoadedOntologyData loadOntologiesFromIris(OWLOntologyManager ontologyMan ontologyManager.addMissingImportListener(missingImportListenerImpl); var umbrellaOntology = ontologyManager.createOntology(); + Map sourceNamespacesMap = new HashMap<>(); for (OntologySource ontologySource : ontologySources) { LOGGER.debug("Loading ontology from IRI '{}'...", ontologySource); try { var currentOntology = ontologyManager.loadOntology(ontologySource.getAsIri()); + OWLDocumentFormat ontologyFormat = ontologyManager.getOntologyFormat(currentOntology); + if (ontologyFormat != null) { + sourceNamespacesMap.putAll(ontologyFormat.asPrefixOWLDocumentFormat().getPrefixName2PrefixMap()); + } + var ontologyIri = currentOntology.getOntologyID().getOntologyIRI().orElse(ontologySource.getAsIri()); ontologyIrisToPaths.put(ontologyIri, ontologySource.getAsIri()); ontologyPathsToIris.put(ontologySource.getOriginalLocation(), ontologyIri); @@ -145,6 +153,6 @@ private LoadedOntologyData loadOntologiesFromIris(OWLOntologyManager ontologyMan } } - return new LoadedOntologyData(umbrellaOntology, ontologyIrisToPaths, ontologyPathsToIris); + return new LoadedOntologyData(umbrellaOntology, ontologyIrisToPaths, ontologyPathsToIris, sourceNamespacesMap); } -} +} \ No newline at end of file diff --git a/onto-viewer-core/src/main/java/org/edmcouncil/spec/ontoviewer/core/ontology/loader/LoadedOntologyData.java b/onto-viewer-core/src/main/java/org/edmcouncil/spec/ontoviewer/core/ontology/loader/LoadedOntologyData.java index cd0dd7c3..f590bf82 100644 --- a/onto-viewer-core/src/main/java/org/edmcouncil/spec/ontoviewer/core/ontology/loader/LoadedOntologyData.java +++ b/onto-viewer-core/src/main/java/org/edmcouncil/spec/ontoviewer/core/ontology/loader/LoadedOntologyData.java @@ -14,20 +14,23 @@ public class LoadedOntologyData { private final Map irisToPathsMapping; private final Map pathsToIrisMapping; private final LoadingDetails loadingDetails; + private final Map sourceNamespacesMap; public LoadedOntologyData(OWLOntology ontology, Map irisToPathsMapping) { this.ontology = ontology; this.irisToPathsMapping = irisToPathsMapping; this.pathsToIrisMapping = new HashMap<>(); this.loadingDetails = new LoadingDetails(Collections.emptyList()); + this.sourceNamespacesMap = new HashMap<>(); } public LoadedOntologyData(OWLOntology ontology, Map irisToPathsMapping, - Map pathsToIrisMapping) { + Map pathsToIrisMapping, Map sourceNamespacesMap) { this.ontology = ontology; this.irisToPathsMapping = irisToPathsMapping; this.pathsToIrisMapping = pathsToIrisMapping; this.loadingDetails = new LoadingDetails(Collections.emptyList()); + this.sourceNamespacesMap = sourceNamespacesMap; } public LoadedOntologyData(LoadedOntologyData loadedOntologyData, LoadingDetails loadingDetails) { @@ -35,6 +38,7 @@ public LoadedOntologyData(LoadedOntologyData loadedOntologyData, LoadingDetails this.irisToPathsMapping = loadedOntologyData.getIrisToPathsMapping(); this.pathsToIrisMapping = loadedOntologyData.getPathsToIrisMapping(); this.loadingDetails = loadingDetails; + this.sourceNamespacesMap = loadedOntologyData.getSourceNamespacesMap(); } public OWLOntology getOntology() { @@ -53,6 +57,10 @@ public LoadingDetails getLoadingDetails() { return loadingDetails; } + public Map getSourceNamespacesMap() { + return sourceNamespacesMap; + } + public static class LoadingDetails { private final List missingImports; @@ -65,4 +73,4 @@ public List getMissingImports() { return missingImports; } } -} +} \ No newline at end of file diff --git a/onto-viewer-toolkit/src/main/java/org/edmcouncil/spec/ontoviewer/toolkit/OntoViewerToolkitCommandLine.java b/onto-viewer-toolkit/src/main/java/org/edmcouncil/spec/ontoviewer/toolkit/OntoViewerToolkitCommandLine.java index b6aae370..aefc22a0 100644 --- a/onto-viewer-toolkit/src/main/java/org/edmcouncil/spec/ontoviewer/toolkit/OntoViewerToolkitCommandLine.java +++ b/onto-viewer-toolkit/src/main/java/org/edmcouncil/spec/ontoviewer/toolkit/OntoViewerToolkitCommandLine.java @@ -190,9 +190,12 @@ public void run(String... args) throws Exception { if (outputOption.isPresent()) { var output = outputOption.get(); LOGGER.info("Saving merged ontology to '{}'...", output); + RDFXMLDocumentFormat outputDocumentFormat = new RDFXMLDocumentFormat(); + ontologyManager.getSourceNamespacesMap().forEach(outputDocumentFormat::setPrefix); + owlOntologyManager.saveOntology( mergedOntology, - new RDFXMLDocumentFormat(), + outputDocumentFormat, IRI.create(new File(output))); } else { throw new OntoViewerToolkitRuntimeException("Error: 'output' argument is not present."); @@ -330,6 +333,7 @@ private LoadedOntologyData loadOntology(Goal goal) throws OntoViewerToolkitExcep ontologyManager.updateOntology(loadedOntology); ontologyManager.setLocationToIriMapping(loadedOntologyData.getPathsToIrisMapping()); + ontologyManager.setSourceNamespacesMap(loadedOntologyData.getSourceNamespacesMap()); if (shouldPopulateOntologyResources(goal)) { resourcesPopulate.populateOntologyResources(); }