As explained in a previous article, Fuseki is an Apache Jena extension which allows you to play with triples and SPARQL before venturing into more enterprise-level offerings like AllegroGraph and Stardog. Doesn’t mean Jena is not a solid piece of software just that, like everything Apache, you have to deal with open-source type of issues.

Rather than explaining the obvious setup and usage in this article I want to show how you can enable inference in Jena.

External inference through Java

Writing rules is as simple as creating a little text file rules.txt and adding something like:

Now, open your favorite Java or Scala IDE and create something like the following:

This will loop of the triples and apply the predefined rule to your data to infer gender. Note the advantage of using an external text file: you edit and augement your (intelligent) rules on the fly.

The Jena inference documentation will give you all you need to continue in this direction.

Internal inference in Jena

Inference can also happen on the fly when you run a SPARQL query. To demonstrate this we’ll create a database in Fuseki via a configuration file rather than via the admin interface.

The easiest way to have Fuseki up and running on Linux and Mac is via the brew install fueski command. It install everything in one go and you end up with a Fuseki installation in /usr/local/Cellar/fuseki with data in /usr/local/var/fuseki/databases.

Add a folder Network in the databases and create three files in it: model.ttl, data.ttl and myrules.rules.

The content of the model.ttl is the ontology:

while the data.ttl is some data fitting the ontology:

In the myrules.ttl you place the inference rules you like, say:

All of which is rather obvious conceptually and fairly easy to write as well.

Inside the Fuseki configuration folder you add the database Network.ttl via the following definition:

This defines the data, model and rules. Please alter the absolute paths above to reflect your situation.

Once all of this is in place, simply restart the server with something like

Using your favorite programming language or with YASGUI you can now query the Network database and you will see something like below:


In this result set you clearly can see than there is more than our initial dataset data.ttl. The SPARQL engine has included the inferences on the fly. Go ahead and play with the rule in the myrules.ttl file. You need to restart the server when altering the rules althgouhg I’m sure there is somewhere a Jena settings which updates things dynamically.

This type of inference also allows you to solve some intricate things like the famous Einstein riddle (Zebra puzzle). You can find the OWL implementation here as well as elsewhere.