Skip to content

Commit

Permalink
Fixes #362: Wrong namespaces for merge-imports goal
Browse files Browse the repository at this point in the history
Signed-off-by: Rafał Trójczak <[email protected]>
  • Loading branch information
trojczak committed Jun 16, 2023
1 parent bff669b commit 32a704e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class OntologyManager {
private Map<String, IRI> locationToIriMapping = new HashMap<>();
private Set<OWLOntology> ontologies;
private Set<MissingImport> missingImports;
private Map<String, String> sourceNamespacesMap = new HashMap<>();

public OWLOntology getOntology() {
return ontology;
Expand Down Expand Up @@ -63,4 +64,12 @@ public Set<MissingImport> getMissingImports() {
public void setMissingImports(Set<MissingImport> missingImports) {
this.missingImports = missingImports;
}

public Map<String, String> getSourceNamespacesMap() {
return sourceNamespacesMap;
}

public void setSourceNamespacesMap(Map<String, String> sourceNamespacesMap) {
this.sourceNamespacesMap = sourceNamespacesMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -120,12 +122,18 @@ private LoadedOntologyData loadOntologiesFromIris(OWLOntologyManager ontologyMan
ontologyManager.addMissingImportListener(missingImportListenerImpl);

var umbrellaOntology = ontologyManager.createOntology();
Map<String, String> 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);
Expand All @@ -145,6 +153,6 @@ private LoadedOntologyData loadOntologiesFromIris(OWLOntologyManager ontologyMan
}
}

return new LoadedOntologyData(umbrellaOntology, ontologyIrisToPaths, ontologyPathsToIris);
return new LoadedOntologyData(umbrellaOntology, ontologyIrisToPaths, ontologyPathsToIris, sourceNamespacesMap);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,31 @@ public class LoadedOntologyData {
private final Map<IRI, IRI> irisToPathsMapping;
private final Map<String, IRI> pathsToIrisMapping;
private final LoadingDetails loadingDetails;
private final Map<String, String> sourceNamespacesMap;

public LoadedOntologyData(OWLOntology ontology, Map<IRI, IRI> 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<IRI, IRI> irisToPathsMapping,
Map<String, IRI> pathsToIrisMapping) {
Map<String, IRI> pathsToIrisMapping, Map<String, String> 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) {
this.ontology = loadedOntologyData.getOntology();
this.irisToPathsMapping = loadedOntologyData.getIrisToPathsMapping();
this.pathsToIrisMapping = loadedOntologyData.getPathsToIrisMapping();
this.loadingDetails = loadingDetails;
this.sourceNamespacesMap = loadedOntologyData.getSourceNamespacesMap();
}

public OWLOntology getOntology() {
Expand All @@ -53,6 +57,10 @@ public LoadingDetails getLoadingDetails() {
return loadingDetails;
}

public Map<String, String> getSourceNamespacesMap() {
return sourceNamespacesMap;
}

public static class LoadingDetails {

private final List<MissingImport> missingImports;
Expand All @@ -65,4 +73,4 @@ public List<MissingImport> getMissingImports() {
return missingImports;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit 32a704e

Please sign in to comment.