OWL-API: A Java API for manipulating ontologies
Adding intelligence to data involves a knowledge enrichment stage where ontology plays a very important role. With intelligent data, a system can make deductions that are not obvious to a human being (recommendations, conflict detection, summary extraction, etc). Due to the complexity of the data, manipulating an ontology is a far from trivial operation. In this context, OWL-API is becoming a key player in the field thanks to its flexibility and richness in terms of functionality.
Since most tutorials in the field focus on the manipulation of RDF data (triplestore management), in this article we will concentrate on the ontology part and present a brief introduction to OWL-API, intended for manipulating ontologies.
What is OWL-API?
The need for a reliable tool to manipulate ontologies, especially in a dynamic environment, led computer scientists to develop OWL-API. It is a set of feature-rich interfaces that allow flexible manipulation of ontologies. Many projects use OWL-API as a development tool. Examples include Protégé-4, Swoop and OntoTrack.
Installation of OWL-API
In order to use OWL-API, you can download the jar file from this download platform. Or use Maven for dependency management.
<dependency> <groupId>net.sourceforge.owlapi</groupId> <artifactId>owlapi-distribution</artifactId> <version>4.1.2</version> </dependency>
The basic elements of OWL-API
The OWLOntologyManager class is the central point for creating, loading and updating ontologies. An ontology is an instance of the OWLOntology class which provides several manipulation mechanisms for ontologies. OWLOntology also acts as an interface for manipulating axioms and annotations. The figure below shows the UML diagram containing the entities of OWLOntologyManager.
How to create an ontology with OWL-API
As described in the previous section, the creation of an ontology must necessarily go through the OWLOntologyManager class. In the example below, we have defined a new createOntology function that calls the OWLOntologyManager entity. The latter will ensure the creation of an OWLOntology instance, via the call to the createOntology function. An ontology can also have a unique IRI (Internationalised Resource Identifier), specified during its creation.
public void createOntology() throws OWLException { OWLOntologyManager om = OWLManager.createOWLOntologyManager(); IRI pressinnov_iri = IRI.create("https://www.contentside.com/misc/exemple.owl"); OWLOntology pressInnovOntology = om.createOntology(pressinnov_iri); }
How to load an existing ontology
If you already have an .owl file representing your ontology, OWL-API takes care of analysing the file, extracting the axioms and creating the OWLOntology class. Several syntaxes are supported by OWL-API: Manchester syntax, RDF/XML, OWL/XML.
public void loadOntology() throws OWLException { OWLOntologyManager om = OWLManager.createOWLOntologyManager(); OWLOntologyDocumentSource source = new StreamDocumentSource(getClass(). getResourceAsStream("./exemple.owl")); om.loadOntologyFromOntologyDocument(source); OWLOntology pressInnovOntology = om.createOntology(); }
Add or delete axioms and classes
The ontology is defined as a set of axioms and constraints. With OWL-API, one can easily add or remove axioms and classes. The example below shows how to add two classes(Footballer and Sportsman) to an existing ontology. We also have a way to declare an axiom as shown in the example below (declare that the class Footballer is a subclass of the class Sportsman).
public void addClass() throws OWLException { OWLOntologyManager om = OWLManager.createOWLOntologyManager(); IRI pressinnov_iri = IRI.create("https://www.contentside.com/misc/personne.owl"); OWLOntology pressinnovOntology = om.createOntology(pressinnov_iri); OWLDataFactory factory = om.getOWLDataFactory(); OWLClass Sportif = factory.getOWLClass(IRI.create(pressinnov_iri + "#Sportif")); OWLClass Footballeur = factory.getOWLClass(IRI.create(pressinnov_iri + "#Footballeur")); OWLAxiom axiom = factory.getOWLSubClassOfAxiom(Footballeur, Sportif); AddAxiom addAxiom = new AddAxiom(pressinnovOntology, axiom); om.applyChange(addAxiom); }
Logical reasoning
Reasoning is a far from trivial task. From implicit facts, a reasoner is able to deduce information. And OWL-API includes several widely used reasoners (e.g. HermiT, Pellet, etc). The code below shows a function that uses a reasoner. The input parameter to this function is a logical query whose deductive answer will be returned by the reasoner. Example: Return the people who regularly play matches. In this case, the list of footballers is returned.
public static void main(String [ ] args){ String query = "personne and joueRégulièrement some match"; DLQuery(query); } static void DLQuery (String query){ OWLOntologyManager om= OWLManager.createOWLOntologyManager(); IRI pressinnov_iri = IRI.create("https://www.contentside.com/misc/exemple.owl"); OWLOntology pressInnovOntology = om.createOntology(pressinnov_iri); OWLReasoner reasoner = new Reasoner.ReasonerFactory().createReasoner(pressInnovOntology); ShortFormProvider shortFormProvider = new SimpleShortFormProvider(); DLQueryPrinter dlQueryPrinter = new DLQueryPrinter(new DLQueryEngine(reasoner, shortFormProvider), shortFormProvider); dlQueryPrinter.askQuery(query); }
The full list of features offered by OWL-API is available on their Documentation page.
Are you interested in this topic?