Configuring Apache Jena Fuseki 2.4.1 inference and reasoning support using SPARQL 1.1: Jena inference rules, RDFS Entailment Regimes and OWL reasoning
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:
- Enable inference using Jena Rules reasoning.
- Enable inference using RDFS Entailment Regimes reasoning
- 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:
- SPARQL 1.1 Entailment Regimes
- Jena Assembler howto
- PRIMER. OWL 2 Web Ontology Language
- Fuseki: Configuring Fuseki
- Jena reasoning with rules
- Configurar Joseki para inferencia en un modelo OWL + Reglas
- EasyRDF Converter
- Reasoners and rule engines: Jena inference support
- Entailments under the OWL 2 RDF-Based Semantics
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#>> .
@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
) thatns: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
) toC:\apache-jena-fuseki-2.4.1\run\databases\
- Create a new file called
ElQuijote.ttl
(or whatever you like) in the locationC:\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
-
I show how to enable inference and reasoning in en Apache Jena Fuseki 2.4.1.
-
I show how to do inference with Apache Jena Fuseki using reasoning rules (Jena Rules). These rules can be very complex in expressiveness to infer any fact from the data model and its instances.
-
I show how to do inference in Apache Jena Fuseki using RDFS-based reasoning (Entailment Regimes). This reasoner is limited by features inherent to OWL 2 RDF-Based semantics and infers some facts from the data model.
-
I show you how to do complete inference in Apache Jena Fuseki using OWL-based reasoner (without Jena Rules).
-
According to the Jena documentation, it is possible to use a reasoner and inference rules file with reasoners, however, not all reasoners support inference rules.
-
Very poor official documentation on this topic we get, I mean, Apache Jena Fuseki inference/reasoning topic. There is a lot of documentation with Java examples using Apache Jena API, however, to enable inference/reasoning using Apache Jena Fuseki, the documentation is very poor in examples and explanations.
-
From what I have searched, reasoners available in Apache Jena Fuseki URIs, are:
- http://jena.hpl.hp.com/2003/OWLMicroFBRuleReasoner
- http://jena.hpl.hp.com/2003/RDFSExptRuleReasoner
- http://jena.hpl.hp.com/2003/OWLMiniFBRuleReasoner
- http://jena.hpl.hp.com/2003/OWLMiniFBRuleReasoner
- http://jena.hpl.hp.com/2003/RDFSExptRuleReasoner
- http://jena.hpl.hp.com/2003/TransitiveReasoner
- http://jena.hpl.hp.com/2003/GenericRuleReasoner
- http://jena.hpl.hp.com/2003/DAMLMicroReasonerFactory
- http://jena.hpl.hp.com/2003/OWLFBRuleReasoner
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.