Skip to content

Configuring Apache Jena Fuseki 2.4.1 inference and reasoning support using SPARQL 1.1: Jena inference rules, RDFS Entailment Regimes and OWL reasoning

Juan Felipe Muñoz Fernández edited this page Apr 18, 2020 · 18 revisions

In this post, I will show you how to configure Apache Jena Fuseki 2.4.1 to enable inferece support using SPARQL 1.1. This is it what we will do:

  1. Enable inference using Jena Rules reasoning.
  2. Enable inference using RDFS Entailment Regimes reasoning
  3. Enable inference using OWL reasoning.

Content:

  • Some useful links
  • Ontology files: model.ttl, data.ttl, myrules.rules
  • Apache Jena Fuseki 2.4.1 configuration: ElQuijote.ttl
  • Files
  • Citing this post

Some useful links:


Ontology files

I have the ontology files below. I will use these files to infer new knowledge using SPARQL 1.1 querys through reasoners. I will use two files: one for the data model model.ttl and another for the instances of the data model data.ttl

model.ttl

@prefix ns:   <http://www.example.org/ns#&gt> .
@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl:  <http://www.w3.org/2002/07/owl#> .
@prefix xsd:  <http://www.w3.org/2001/XMLSchema#> .

ns:Person       rdf:type        owl:Class .
ns:Person       owl:unionOf     (ns:Man ns:Woman) .
ns:Man          rdf:type        owl:Class .
ns:Woman        rdf:type        owl:Class .
ns:knows        rdf:type        owl:SymmetricProperty .
ns:knows        rdfs:domain     ns:Person .
ns:knows        rdfs:range      ns:Person .
ns:Horse        rdf:type        owl:Class .

data.ttl

@prefix ns:   <http://www.example.org/ns#> .
@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl:  <http://www.w3.org/2002/07/owl#> .
@prefix xsd:  <http://www.w3.org/2001/XMLSchema#> .

ns:Sancho       rdf:type    ns:Man .
ns:Don_Quijote  rdf:type    ns:Man .
ns:Don_Quijote  ns:knows    ns:Sancho .
ns:Rocinante    rdf:type    ns:Horse .
ns:Dulcinea     rdf:type    ns:Woman .

I will define some reasoner rules in the following file: myrules.rules

myrules.rules

@prefix ns:   <http://www.example.org/ns#> .
@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl:  <http://www.w3.org/2002/07/owl#> .
@prefix xsd:  <http://www.w3.org/2001/XMLSchema#> .

[ruleKnows: (ns:knows rdf:type owl:SymmetricProperty) (?s ns:knows ?o) --> (?o ns:knows ?s)]

The rule file above defines a reasoning rules for the property ns:knows that declares:

  • IF ns:knows is a symmetric property AND exist a subject (?s) that ns:knows an object (?o), THEN, object (?o) ns:knows subject (?s) too.

Apache Jena Fuseki 2.4.1 configuration

  • Location of Apache Jena Fuseki installation (Windows) is in C:\apache-jena-fuseki-2.4.1\: please copy the ontology and rule files (model.ttl, data.ttl, myrules.rules) to C:\apache-jena-fuseki-2.4.1\run\databases\
  • Create a new file called ElQuijote.ttl (or whatever you like) in the location C:\apache-jena-fuseki-2.4.1\run\configuration\ with the following lines:

ElQuijote.ttl

@prefix :      <http://base/#> .
@prefix tdb:   <http://jena.hpl.hp.com/2008/tdb#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix ja:    <http://jena.hpl.hp.com/2005/11/Assembler#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix fuseki: <http://jena.apache.org/fuseki#> .

#This line is a comment

:service1	a                         fuseki:Service ;
        fuseki:dataset                    :dataset ;
        fuseki:name                       "ElQuijote" ;
        fuseki:serviceQuery               "query" , "sparql" ;
        fuseki:serviceReadGraphStore      "get" ;
        fuseki:serviceReadWriteGraphStore "data" ;
        fuseki:serviceUpdate              "update" ;
        fuseki:serviceUpload              "upload" .

:dataset	rdf:type 	ja:RDFDataset ;
    rdfs:label "ElQuijote" ;
    ja:defaultGraph
      [ rdfs:label "ElQuijote" ;
        a ja:InfModel ;

        #Reference to model.ttl file
        ja:content [ja:externalContent <file:///C:/apache-jena-fuseki-2.4.1/run/databases/model.ttl> ] ;

        #Reference to data.ttl file
        ja:content [ja:externalContent <file:///C:/apache-jena-fuseki-2.4.1/run/databases/data.ttl> ] ;

        #Disable OWL-based reasoner
        #ja:reasoner [ja:reasonerURL <http://jena.hpl.hp.com/2003/OWLFBRuleReasoner>] ;

        #Disable RDFS-based reasoner
        #ja:reasoner [ja:reasonerURL <http://jena.hpl.hp.com/2003/RDFSExptRuleReasoner>] ;

        #Enable Jena Rules-based reasoner and we point the location of myrules.rules file
        ja:reasoner [
            ja:reasonerURL <http://jena.hpl.hp.com/2003/GenericRuleReasoner> ;
            ja:rulesFrom <file:///C:/apache-jena-fuseki-2.4.1/run/databases/myrules.rules> ;			
        ] ; 
      ] ;
     .
  • Start Apache Jena Fuseki server and execute the following SPARQL querys on "ElQuijote" dataset:
PREFIX ns:<http://www.example.org/ns#>

SELECT *
WHERE {
  ?s ns:knows ?o .
}
  • The following lines are the resultset of the query above. These lines are the result of the rules-based reasoner given myrules.rules file:
ns:Don_Quijote ns:knows   ns:Sancho      --> This relationship is declared in data.ttl
ns:Sancho      ns:knows   ns:Don_Quijote --> This relationship was inferred given the rule in myrules.rules file
  • The following query should return all persons defined in data.ttl (Don_Quijote, Sancho and Dulcinea), however, returns nothing (Why?)
PREFIX ns:<http://www.example.org/ns#>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT *
WHERE {
	?s rdf:type ns:Person .
}

It is required to add the following lines in myrules.rules to query returns all persons: ns:Don_Quijote, ns:Sancho and ns:Dulcinea:

  • Add the lines below to myrules.rules file:
[isPerson1: (?s rdf:type ns:Man) -> (?s rdf:type ns:Person)]
[isPerson2: (?s rdf:type ns:Woman) -> (?s rdf:type ns:Person)]

Previous rules indicates: IF a subject (?s) is ns:Man or ns:Woman, THEN that subject (?s) is a ns:Person.

To resolve this without add two rules above in myrules.rules, we can enable RDFS-based reasoner that is disabled in ElQuijote.ttl file, since ns:Person class is declared as the union (owl:unionOf) of ns:Man and ns:Woman classes, (see model.ttl file), and this fact is enough to infer on data model (model.ttl) and its instances (data.ttl), that ns:Don_Quijote, ns:Sancho and ns:Dulcinea are persons.

  • In the following lines from ElQuijote.ttl file (excerpt) I've disabled Jena Rules based reasoner and enabled RDFS based reasoner:

ElQuijote.ttl

	#Lines before the next ones remains untouchable:

	#Enable RDFS-based reasoner **<-- Changed below**
	ja:reasoner [ja:reasonerURL <http://jena.hpl.hp.com/2003/RDFSExptRuleReasoner>] ;

	#Disable Jena Rules-based reasoner and its rules file myrules.rules **<-- Changed below**
	#ja:reasoner [
	#    ja:reasonerURL <http://jena.hpl.hp.com/2003/GenericRuleReasoner> ;
	#    ja:rulesFrom <file:///C:/apache-jena-fuseki-2.4.1/run/databases/myrules.rules> ;			
	#] ; 
   ] ;
  .

In this configuration I've enabled RDFS-based reasoner (Entailment Regimes) and because of this, the last SPARQL query resultset is:

 ns:Don_Quijote
 ns:Sancho 

And what happend with ns:Dulcinea? Why ns:Dulcinea does not included in the resultset?

Notice how ns:Don_Quijote and ns:Sancho are related through ns:knows symmetric property in data.ttl file. Notice how ns:knows property has a ns:Person class as a domain and range. If the domain of the ns:knows property is omitted (i.e, deleted), last SPARQL query obtains an empty resultset. This is because a limitation in the the OWL 2 RDF-Based semantics, since classes are treated as individuals that refer to elements of the domain. I suggest a reading of the following document: Entailments under the OWL 2 RDF-Based Semantics. In this case, ns:Dulcinea isn't realated with any other instance of the domain of the class ns:Person.

However, if I enable the OWL-based reasoner, the last SPARQL query resultset is not empty, I mean, I obtain the expected inferred instances: ns:Don_Quijote, ns:Sancho and ns:Dulcinea.

With OWL based reasoner, the first SPARQL query has the same resultset that we got with the rule ruleKnows in myrules.rules file. I mean:

ns:Don_Quijote ns:knows ns:Sancho      --> This relationship is declared in data.ttl 
ns:Sancho      ns:knows ns:Don_Quijote --> This relationship was inferred by the OWL based reasoner

Why the last resultset? Because ns:knows property is a symmetric property and OWL-based reasoner infers the second relationship in the resultset without using explit rules.

To enable OWL-based reasoner, disable the current RDFS based reasoner and enable the following lines in ElQuijote.ttl file:

ElQuijote.ttl(excerpt)

	#Lines before the next ones remains untouchable:

	#Enable OWL-based reasoner **<-- Changed below**
	ja:reasoner [ja:reasonerURL <http://jena.hpl.hp.com/2003/OWLFBRuleReasoner>] ;

	#Disable RDFS-based reasoner <-- Changed below
	#ja:reasoner [ja:reasonerURL <http://jena.hpl.hp.com/2003/RDFSExptRuleReasoner>] ;

	#Disable Jena Rules-based reasoner and its rules file myrules.rules
	#ja:reasoner [
	#    ja:reasonerURL <http://jena.hpl.hp.com/2003/GenericRuleReasoner> ;
	#    ja:rulesFrom <file:///C:/apache-jena-fuseki-2.4.1/run/databases/myrules.rules> ;			
	#] ;
   ] ;
  . 

Conclusions

For a complete inference support in Apache Jena, go to: Reasoners and rule engines: Jena inference support. This document describe reasoners capabilities and limitations. This document also describes Jena Rules.


Files

All files used in this document can be download from: Jena-Fuseki-Reasoner-Inference


Citing this post

Muñoz-Fernández, J. F. (2016, December 30). Configuring Apache Jena Fuseki 2.4.1 inference and reasoning support using SPARQL 1.1: Jena inference rules, RDFS Entailment Regimes and OWL reasoning. Retrieved <Month> <Day>, <Year>, from https://github.com/jfmunozf/Jena-Fuseki-Reasoner-Inference/wiki/Configuring-Apache-Jena-Fuseki-2.4.1-inference-and-reasoning-support-using-SPARQL-1.1:-Jena-inference-rules,-RDFS-Entailment-Regimes-and-OWL-reasoning

Thank you!

Juan Felipe Muñoz Fernández


CHANGELOG:

  • 17-04-2020: Post migrated to markwdown. HTML was awful for this post.