Skip to content

Commit

Permalink
Merge pull request #363 from edmcouncil/362-fix-wrong-namespaces-for-…
Browse files Browse the repository at this point in the history
…merge-imports

362 fix wrong namespaces for merge imports
  • Loading branch information
mereolog authored Jun 29, 2023
2 parents 48ee857 + d77057d commit 8ebf726
Show file tree
Hide file tree
Showing 4 changed files with 52 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,12 @@
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.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyAlreadyExistsException;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyDocumentAlreadyExistsException;
Expand All @@ -30,6 +33,7 @@

public class CommandLineOntologyLoader extends AbstractOntologyLoader {

private static final IRI ANONYMOUS_ONTOLOGY = IRI.create("https://ontoviewer.spec.edmcouncil.org/anonymous/");
private static final Logger LOGGER = LoggerFactory.getLogger(CommandLineOntologyLoader.class);

private final ConfigurationData configurationData;
Expand Down Expand Up @@ -120,12 +124,16 @@ private LoadedOntologyData loadOntologiesFromIris(OWLOntologyManager ontologyMan
ontologyManager.addMissingImportListener(missingImportListenerImpl);

var umbrellaOntology = ontologyManager.createOntology();
Map<String, String> sourceNamespacesMap = new HashMap<>();
Set<IRI> seenOntologies = new HashSet<>();

for (OntologySource ontologySource : ontologySources) {
LOGGER.debug("Loading ontology from IRI '{}'...", ontologySource);

try {
var currentOntology = ontologyManager.loadOntology(ontologySource.getAsIri());
gatherNamespacesFromOntologies(ontologyManager, sourceNamespacesMap, currentOntology, seenOntologies);

var ontologyIri = currentOntology.getOntologyID().getOntologyIRI().orElse(ontologySource.getAsIri());
ontologyIrisToPaths.put(ontologyIri, ontologySource.getAsIri());
ontologyPathsToIris.put(ontologySource.getOriginalLocation(), ontologyIri);
Expand All @@ -145,6 +153,24 @@ private LoadedOntologyData loadOntologiesFromIris(OWLOntologyManager ontologyMan
}
}

return new LoadedOntologyData(umbrellaOntology, ontologyIrisToPaths, ontologyPathsToIris);
return new LoadedOntologyData(umbrellaOntology, ontologyIrisToPaths, ontologyPathsToIris, sourceNamespacesMap);
}

private void gatherNamespacesFromOntologies(OWLOntologyManager ontologyManager,
Map<String, String> sourceNamespacesMap,
OWLOntology ontology,
Set<IRI> seenOntologies) {
var ontologIri = ontology.getOntologyID().getOntologyIRI().orElse(ANONYMOUS_ONTOLOGY);
if (!seenOntologies.contains(ontologIri) || ontologIri.equals(ANONYMOUS_ONTOLOGY)) {
OWLDocumentFormat ontologyFormat = ontologyManager.getOntologyFormat(ontology);
if (ontologyFormat != null) {
sourceNamespacesMap.putAll(ontologyFormat.asPrefixOWLDocumentFormat().getPrefixName2PrefixMap());
}

seenOntologies.add(ontologIri);

ontology.imports().forEach(importedOntology ->
gatherNamespacesFromOntologies(ontologyManager, sourceNamespacesMap, importedOntology, seenOntologies));
}
}
}
}
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 8ebf726

Please sign in to comment.