Skip to content

Commit

Permalink
Add reccurent handling of imported ontologies
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 23, 2023
1 parent 32a704e commit d77057d
Showing 1 changed file with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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 @@ -32,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 @@ -123,16 +125,14 @@ private LoadedOntologyData loadOntologiesFromIris(OWLOntologyManager ontologyMan

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());
OWLDocumentFormat ontologyFormat = ontologyManager.getOntologyFormat(currentOntology);
if (ontologyFormat != null) {
sourceNamespacesMap.putAll(ontologyFormat.asPrefixOWLDocumentFormat().getPrefixName2PrefixMap());
}
gatherNamespacesFromOntologies(ontologyManager, sourceNamespacesMap, currentOntology, seenOntologies);

var ontologyIri = currentOntology.getOntologyID().getOntologyIRI().orElse(ontologySource.getAsIri());
ontologyIrisToPaths.put(ontologyIri, ontologySource.getAsIri());
Expand All @@ -155,4 +155,22 @@ private LoadedOntologyData loadOntologiesFromIris(OWLOntologyManager ontologyMan

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));
}
}
}

0 comments on commit d77057d

Please sign in to comment.