Loading, building and reasoning with ontologies using the OWL API

This part of the DBpedia GSoC2017 warm up has been intensive.

The target is learn how to load, build and reasoning ontologies using the OWL API. And I tried my best.

I have gone through all the examples included in the well documented repository of the OWL API. I used this repository , but I recommend to use this other one. It is more actualized and maintained. And you can skip the lots of problems that I have using the last version of the API with an example file intended to be for an older version. Anyway all this process has make me dive deep in all this fantastic project looking for documentation and paying attention to the evolution it has through time.

Here is the repository in GitHub with the project that I did for learning purposes. I tried every method of the example and check every result.

Important to say is to have ready the W3C OWL 2 for studing all the concepts you are finding across this tutorial.

First lets have a look at my maven dependencies. If you want to do something similar to what I did, this can help you nicely.

<dependencies>
    <dependency>
        <groupId>net.sourceforge.owlapi</groupId>
        <artifactId>owlapi-osgidistribution</artifactId>
        <version>4.3.1</version>
    </dependency>
       <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.owlapi</groupId>
        <artifactId>org.semanticweb.hermit</artifactId>
        <version>1.3.8.413</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.owlapi</groupId>
        <artifactId>pellet-profiler-ignazio1977</artifactId>
        <version>2.4.0-ignazio1977</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.owlapi</groupId>
        <artifactId>pellet-owlapi-ignazio1977</artifactId>
        <version>2.4.0-ignazio1977</version>
        <type>jar</type>
    </dependency>
</dependencies>  

I did a main class with the intention of representing in the console all info that I needed to understand how every method works:


  OwlPedia totem = new OwlPedia();

  System.out.println("\nshouldLoad");
  totem.shouldLoad();
  System.out.println("\nshouldSaveOntologies()");
  totem.shouldSaveOntologies();
  System.out.println("\nshouldAccessEntities()");
  totem.shouldAccessEntities();
  System.out.println("\nshouldBuildDataRanges()");
  totem.shouldBuildDataRanges();
  System.out.println("\nshouldUseDataranges()");
  totem.shouldUseDataranges();
  System.out.println("\nshouldInstantiateLiterals()");
  totem.shouldInstantiateLiterals();
  System.out.println("\nshouldLoadAndSave()");
  totem.shouldLoadAndSave();
  System.out.println("\nshouldAddAxiom()");
  totem.shouldAddAxiom();
  System.out.println("\nshouldCreateOntology()");
  totem.shouldCreateOntology();
  System.out.println("\nshouldCreatePropertyAssertions()");
  totem.shouldCreatePropertyAssertions();
  System.out.println("\nshouldAddClassAssertion()");
  totem.shouldAddClassAssertion();
  System.out.println("\nshouldCreateAndSaveOntology()");
  totem.shouldCreateAndSaveOntology();
  System.out.println("\nshouldAddObjectPropertyAssertions()");
  totem.shouldAddObjectPropertyAssertions();
  System.out.println("\nshouldDeleteIndividuals()");
  totem.shouldDeleteIndividuals();
  System.out.println("\nshouldCreateRestrictions()");
  totem.shouldCreateRestrictions();
  System.out.println("\nshouldUseReasoner()");
  totem.shouldUseReasoner();
  System.out.println("\nshouldLookAtRestrictions()");
  totem.shouldLookAtRestrictions();
  System.out.println("\nshouldCreateAndReadAnnotations()");
  totem.shouldCreateAndReadAnnotations();
  System.out.println("\nshouldCreateInferredAxioms()");
  totem.shouldCreateInferredAxioms();
  System.out.println("\nshouldMergeOntologies()");
  totem.shouldMergeOntologies();
  System.out.println("\nshouldWalkOntology()");
  totem.shouldWalkOntology();
  System.out.println("\nshouldQueryWithReasoner()");
  totem.shouldQueryWithReasoner();
  System.out.println("\nshouldUseIRIMappers()");
  totem.shouldUseIRIMappers();
  System.out.println("\nshouldExtractModules()");
  totem.shouldExtractModules();
  System.out.println("\nowlPrimer()");
  totem.owlPrimer();

}

The original Examples.java file is so well commented and explained that it is a perfect training.

I will show here some methods and their outputs for those enough curious to have a look but lazy enough to don’t run this miniproject.

All the output can be seen in this GIST without all the debugging info from log4j.

If you are going to try these methods it is a good idea to activate it, uncommenting this line:

First I used some OWL ontology downloaded from Bioportal. Like the first method:

Then, when I had look into the owl file, and inspect his structure, I found the pizza owl example file much more adequate.

One important function of the API is the reasoning. shouldUseReasoner() is very well explained with an example over a proper OWL file. It proves the important of querying with a reasoner over an ontology to be able to classify all his classes obtaining all the implicit info that is not stated per se. Some of his extensive output:

I have to say, the hard part was to understood all the methods to this point. Once done that, the rest of the functionalities were easy to test and modify. So if you are studying this API don’t give up at the beginning and go on a little bit with patience. All will make sense at the end ;)

There is one last method, owlPrimer, an amazing work, building and ontology from scratch programmatically. Normally an ontology is built using the proper tool, like Protege Ontology Editor, but this example deserves a nice reading and testing. The code and output are so long for a blog, so just jump to the github repo and have a nice code time :)