2

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)?

SPARQLGuy
  • 63
  • 8
  • 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. – SPARQLGuy Nov 03 '15 at 07:52

2 Answers2

2

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.

Ignazio
  • 10,504
  • 1
  • 14
  • 25
  • Hi Ignazio - thank you for the response, appreciate this is a tricky or specialist topic. I can confirm listHierarchyRootClasses() doesn't return owl:Thing when parsing the owl ontology - it's possible it's because, as you say, it is the OWL ontology itself. The problem is it is a dependency of all OWL ontologies - and so rather concerning that it is quite so difficult to parse. – SPARQLGuy Nov 02 '15 at 17:56
  • 1
    No, regular owl ontologies do not import this ontology - if you check the comments inside, the authors state that owl ontologies should /not/ import it, as it will make them owl full ontologies. The language primitives are encoded directly in supporting tools, so usually they are unnecessary in the explicit content. – Ignazio Nov 02 '15 at 18:01
  • I've updated my post with my findings after some experimentation. Appreciate the comment about built in vocabulary entities potentially confusing the library, so I'll test with an OWL based ontology that isn't the OWL ontology itself! :) – SPARQLGuy Nov 02 '15 at 18:09
  • Incidently - I would have thought the OWL ontology itself was an RDFS ontology that defines the OWL classes (which are all rdfs:Class subclasses?). So actually I would still expect .listHierarchyRootClasses() to work when loading it as an RDFS OntModel - but you may still be correct, it may still be confusing the library. – SPARQLGuy Nov 02 '15 at 18:10
0

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!

SPARQLGuy
  • 63
  • 8