2
votes

I'm trying to reproduce the class hierarchy displayed to me in Protege for the OWL ontology (owl.ttl) which you can find at the standard URI location http://www.w3.org/2002/07/owl# for download.

I'm trying to do this using Jena's API, by loading into an OntModel, then going to fetch the hierarchy root classes. I then want to recurse down to build the hierarchy.

The problem I'm getting is that when I call to get the hierarchy root classes, I get zero results returned. So I have no root classes from which to recurse down and build the hierarchy.

===========================================

When I load the OWL ontology at http://www.w3.org/2002/07/owl# into Protege, I get a nice class hierarchy absolutely fine. Yet when I load into both a reasoned, or unreasoned model in Jena, I get no hierarchy classes like so:

OntModel reasonedModel = com.hp.hpl.jena.rdf.model.ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MINI_RULE_INF);
OntModel unreasonedModel = com.hp.hpl.jena.rdf.model.ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);

// <Code that loads in the ontology syntax into model skipped here>

// Now get the sets of root classes
reasonedModel.listHierarchyRootClasses();     // Returns empty set
unreasonedModel.listHierarchyRootClasses();   // Returns empty set

Both the calls on the reasoned or unreasoned model returns zero results.

==============================================

Now I try something else. I know that rdfs:Resource is always the top level class of any RDFS/OWL model. So, when I do:

OntClass topLevel = reasonedModel.getOntClass("http://www.w3.org/2000/01/rdf-schema#Resource");

// Get direct subclasses...
topLevel.listSubClasses(true);

And recurse down from here, I get a full class hierarchy, including the inferred relationships because I've chosen a reasoned model.

My question is, is the latter approach the right way of doing this? Shouldn't I be able to ask Jena to tell me what the root level classes of the model are rather then me tell Jena that it's rdfs:Resource?

========================================

Update: To parse the ontology, which is the OWL2 ontology, I had to set strict mode off because Jena isn't compatible with OWL2 ontologies at the moment (I'm using version 2.7.4).

When I call .listHierarchyRootClasses() with either OWL_MEM or RDFS_MEM, I get zero root classes returned. If I call .listClasses() and find all classes without super classes to find the roots, in RDFS_MEM I get the following hierarchy:

Class [http://www.w3.org/2002/07/owl#Axiom]
Class [http://www.w3.org/2002/07/owl#NegativePropertyAssertion]
Class [http://www.w3.org/2002/07/owl#Ontology]
Class [http://www.w3.org/2002/07/owl#AllDisjointClasses]
Class [http://www.w3.org/2002/07/owl#Annotation]
Class [http://www.w3.org/2002/07/owl#AllDifferent]
Class [http://www.w3.org/2002/07/owl#AllDisjointProperties]
Class [http://www.w3.org/2002/07/owl#OntologyProperty]
Class [http://www.w3.org/2002/07/owl#AnnotationProperty]
Class [http://www.w3.org/2002/07/owl#DatatypeProperty]
Class [http://www.w3.org/2002/07/owl#ObjectProperty]
        Class [http://www.w3.org/2002/07/owl#InverseFunctionalProperty]
        Class [http://www.w3.org/2002/07/owl#IrreflexiveProperty]
        Class [http://www.w3.org/2002/07/owl#AsymmetricProperty]
        Class [http://www.w3.org/2002/07/owl#TransitiveProperty]
        Class [http://www.w3.org/2002/07/owl#SymmetricProperty]
        Class [http://www.w3.org/2002/07/owl#ReflexiveProperty]
Class [http://www.w3.org/2002/07/owl#DeprecatedProperty]
Class [http://www.w3.org/2002/07/owl#FunctionalProperty]
Class [http://www.w3.org/2002/07/owl#DeprecatedClass]
Class [http://www.w3.org/2002/07/owl#Class]
        Class [http://www.w3.org/2002/07/owl#Restriction]
Class [http://www.w3.org/2002/07/owl#DataRange]
Class [http://www.w3.org/2002/07/owl#NamedIndividual]
Class [http://www.w3.org/2002/07/owl#Nothing]

In OWL_MEM I get the following:

Class [http://www.w3.org/2002/07/owl#NamedIndividual]
Class [http://www.w3.org/2002/07/owl#Nothing]

Again, neither of which reflect the same hierarchy as I see when loading into Protege.

I'm not clear on what I'm doing wrong here, could it be because I am parsing the OWL ontology and this in itself is confusing Jena (whether looking at it as an RDFS ontology or an OWL ontology)?

2
Update: I've now tested the class hierarchy example provided with the Jena source code. It works fine on an ontology that is not the OWL ontology.MarkS

2 Answers

2
votes

listHierarchyRootClasses() states in its javadoc that the root it will use is owl:Thing. So it is not equivalent to the approach you used later, and which works for this ontology.

Note that the ontology you're using is a very particular one, as it is an ontology modeling parts of the language itself. In most ontologies, using owl:Thing is the right strategy.

0
votes

Ignazio was correct - I have tried using the standard tutorial class hierarchy builder included with Jena, which you can find on GitHub here: https://github.com/apache/jena/blob/master/jena-core/src-examples/jena/examples/ontology/classHierarchy/ClassHierarchy.java.

This works, if the ontology that you are parsing is not the OWL ontology. So, it appears as though the ontology confuses the underlying framework because it contains a reproduction of entities hard-coded into the framework.

This took me half a day to ascertain - but at least I know now that if someone tries to view a class hierarchy of the OWL ontology, the Jena framework should not be used!