Read class restriction or axiom via OWL API 5


In this tutorial we demonstrate how to read a restriction or axiom from a OWLClass using the OWL API 5 and the visitor pattern. The example maven project is available on Github.

We use the OWL API version 5.1.4 and a small example ontology.

1. Prerequisites

  • Install Maven to build the project (explained here)
  • Java 1.8 ( OWL API uses streams etc. )
  • Some understanding of OWL and ontologies in general

2. The example ontology

The ontology is only for demonstration purposes. It is about a vehicle (truck) that has some components like tires, doors and a steering wheel. Have a look at the important part:

We use the following restriction properties:

  • SomeValuesFrom (e.g. at least one door)
  • ExactCardinalty ( e.g. exactly one steering wheel)
  • MinCardinality (e.g. at least 4 tires)
  • MaxCardinality (e.g. at max 12 tires)

Check out this Protégé Screenshot for better visualization:

Protégé Ontology Screenshot
Protégé Ontology Screenshot

3. The example code

The code is pretty simple and basically reads the example ontology first. Afterwards, the restrictions and axioms are read for each class and printed to the console.

  1. Read the example ontology via the OWLOntologyManager
  2. Retrieve all classes contained in the ontology using ontology.classesInSignature(). This returns a stream which requires Java 1.8
  3. For each class retrieve the OWLAxiom via ontology.axioms( oc )
  4. Create an OWLObjectVisitor to visit the OWLSubClassOfAxiom surrounding the owl:Restriction
  5. Get the superclass of the OWLSubClassOfAxiom and create another OWLObjectVisitor to retrieve the owl:Restriction
  6. Print / Save / Process the contained values

4. Restriction output

The generated output looks like this:

As you can see we have access to all the important properties. Cardinality, the expression type as well as the property and the object.


We had to read that data in order to transform OWL data into another format (OPC-UA). Normally, you probably do not have to read that data. You would rather add axioms, infer and reason over data.

The visitor pattern is something to read about and get used to, but once understood it is a very powerful tool.

The OWL API 5 has a lot of changes in comparison to version 4. In our understanding, there are just very few good tutorials / documentation examples out there yet.

If you have questions or problems, feel free to comment and ask.


Leave a Reply