Getting superclasses in imported OWL ontology
Asked Answered
H

1

1

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]
            }
        }
    }
Horseshoe answered 12/7, 2013 at 9:16 Comment(0)
H
3

OK, after some research, I found out that OWLReasoner also has a function to get super classes. This method includes the super classes from imported ontologies and has even the possibility to distinguish between direct and indirect superclasses. It's a bit strange though that getClassesInSignature() includes those without accessing the reasoner but this works fine and solved my problem.

The code would be

NodeSet<OWLClass> superclasses = reasoner.getSuperClasses(c, true);

to get the classes. The return type is different, whch is why the following has to be changed as well:

for (org.semanticweb.owlapi.reasoner.Node<OWLClass> parentOWLNode: superclasses) {                      
    OWLClassExpression parent = parentOWLNode.getRepresentativeElement();
Horseshoe answered 12/7, 2013 at 10:14 Comment(1)
Any chance you could share this in a blog or so - implementing OWLAPI on top of Neo4j? Would be supercool.Highness

© 2022 - 2024 — McMap. All rights reserved.