I'm trying to parse an ontology (complete including the imported ontology) to store it into a graph database. To do this, I first list all classes in the ontology and then link them to their respective super classes.
The code works fine, except for imported super classes. I can link to super classes within my own ontology but not from a class whose superclass is in the imported ontology. The superclass exists, I can see it if I print it after the getClasesInSignature() method call because I specified true to add imported classes.
In this code example, an output of the superclasses set would be empty for classes as described above. Is there a way to include them?
public void importOntology(String ontologyFile) throws Exception {
try {
File file = new File(ontologyFile);
if (file.exists()) {
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology ontology = manager.loadOntologyFromOntologyDocument(file);
OWLReasonerFactory reasonerFactory = PelletReasonerFactory.getInstance();
OWLReasoner reasoner = reasonerFactory.createReasoner(ontology, new SimpleConfiguration());
if (!reasoner.isConsistent()) {
throw new Exception("Ontology is inconsistent");
}
Transaction tx = db.beginTx();
try {
//create starting node
Node thingNode = getOrCreateNodeWithUniqueFactory("owl:Thing");
//add all classes
for (OWLClass c :ontology.getClassesInSignature(true)) {
String classString = c.toString();
if (classString.contains("#")) {
classString = classString.substring(classString.indexOf("#")+1,classString.lastIndexOf(">"));
}
//create node
Node classNode = getOrCreateNodeWithUniqueFactory(classString);
Set<OWLClassExpression> superclasses = c.getSuperClasses(ontology);
//top level node
if (superclasses.isEmpty()) {
//link to thing
} else {
//link to superclass(es)
}
//[rest of code removed]
}
}
}