Reputation: 115
I am using OWL API in order to get information from ontologies. I need to retrieve the list of all imported ontologies used in the loaded ontology.
Is there a method in OWL API can do this task?
my code that load an ontology is:
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAnnotationProperty;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLDataProperty;
import org.semanticweb.owlapi.model.OWLImportsDeclaration;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
public class NSExtractor {
@SuppressWarnings("deprecation")
public static void main(String[] args) throws FileNotFoundException, OWLOntologyCreationException {
@SuppressWarnings("resource")
File testFile= new File("C:\\acco.n3");
OWLOntologyManager m = OWLManager.createOWLOntologyManager();
OWLDataFactory f = OWLManager.getOWLDataFactory();
OWLOntology o;
o = m.loadOntologyFromOntologyDocument(testFile);
Upvotes: 2
Views: 445
Reputation: 10659
o.importsDeclarations()
will give you a stream of imports declarations for this ontology. This is the list of IRIs declared with owl:imports
properties.
Note: these are the declared imports, not the imports closure - the difference is that the imports closure includes the ontologies imported in your ontology and the ontologies imported by these ontologies - recursively including the imported ontologies.
o.importsClosure()
will provide all ontologies that were loaded during parsing of your ontology.
Upvotes: 0
Reputation: 115
After a lot of searches, I found how to solve this task. I used OWLOntologyXMLNamespaceManager (I am using OWL API 5.1.6). Afterward, using getPrefixes and getNameSpaces you can extract prefix and namespaces, respectively, for the loaded ontology as follow:
OWLDocumentFormat format = m.getOntologyFormat(ontology);
OWLOntologyXMLNamespaceManager nsManager = new OWLOntologyXMLNamespaceManager(ontology, format);
for (String prefix : nsManager.getPrefixes()) {
System.out.println(prefix);
}
for (String ns : nsManager.getNamespaces()) {
System.out.println(ns);
}
Upvotes: 3