Search code examples
jenafusekiswrlapache-jenajena-rules

How to define very simple own rule in fuseki step by step?


I'm asking your help to understand how to create my own property rules in fuseki. I've read the documentation of Jena and Fuseki about reasoners and rules but I don't understand how to create (step by step) simple rules. And the examples that I've found are focus on OWL reasoners.

I don't clearly understand what write in the config.ttl file in order to fuseki takes into account my rules on my ontology. I use the Sparql point with python through a sparql-client, and I don't master java code and applications.

My purpose is to create the next rules:

@prefix rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
@prefix ex: http://example.com/
@prefix xs: http://www.w3.org/2001/XMLSchema#
@prefix ont: http://www.myownontolongy/ontology/

[dependsOfExchange:
(?e ont:refersToPriorActivityData ?a)
(?a ont:hasExchange ?es)
->
(?e rme:dependsOfExchange ?es)
]

The principle in SWRL syntax is:

refersToPriorActivityData(?e,?a) ^ hasExchange(?a,?es) => dependsOfExchange(?e,?es)

Thanks in advance.


EDIT with one solution

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

[] ja:loadClass "com.hp.hpl.jena.tdb.TDB" .
tdb:DatasetTDB  rdfs:subClassOf  ja:RDFDataset .
tdb:GraphTDB    rdfs:subClassOf  ja:Model .

[] rdf:type fuseki:Server ;
   # Timeout - server-wide default: milliseconds.
   # Format 1: "1000" -- 1 second timeout
   # Format 2: "10000,60000" -- 10s timeout to first result, then 60s timeout for the rest of query.
   # See java doc for ARQ.queryTimeout
   # ja:context [ ja:cxtName "arq:queryTimeout" ;  ja:cxtValue "10000" ] ;

   # ja:loadClass "your.code.Class" ;

   fuseki:services (
     <#reminer>
   ) .

## ---------------------------------------------------------------
## Updatable in-memory dataset.

<#reminer> rdf:type fuseki:Service ;
    # URI of the dataset -- http://host:port/reminer
    fuseki:name                        "name_of_your_database" ; 
    fuseki:serviceQuery                "sparql" ;
    fuseki:serviceQuery                "query" ;
    fuseki:serviceUpdate               "update" ;
    fuseki:serviceUpload               "upload" ;
    fuseki:serviceReadWriteGraphStore  "data" ;     
    fuseki:serviceReadGraphStore       "get" ;
    fuseki:serviceReadGraphStore       "" ;      
    fuseki:dataset                     <#myDataset> ;
    .

## In-memory, initially empty.
## This database set-up allows OWL inference.
<#myDataset> rdf:type ja:RDFDataset ;
                tdb:unionDefaultGraph true ; 
                ja:defaultGraph <#infGraph> .

<#infGraph> rdf:type ja:InfModel ;
             ja:reasoner [ ja:rulesFrom <file:rules/inference_rules.rules> ; ] ; #the rules directory is in the "run" directory
             ja:baseModel <#baseGraph> .

<#baseGraph> rdf:type tdb:GraphTDB;
             tdb:location "path_of_your_database" .

And the inference_rules.rules for an example:

@prefix rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
@prefix ex: http://example.com/
@prefix xs: http://www.w3.org/2001/XMLSchema#
@prefix mO: http://www.semanticweb.org/myOntology/

[dependsOfExchange:
(?e mO:producedBy ?a)
(?a mO:hasExchange ?es)
                 ->
                 (?e rme:dependsOf ?es)
         ]

Solution

  • The .ttl use through the cmd line: ./fuseki-server --config=/path/of/your/custom-config.ttl

    @prefix :        <#> .
    @prefix fuseki:  <http://jena.apache.org/fuseki#> .
    @prefix rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
    @prefix rdfs:   <http://www.w3.org/2000/01/rdf-schema#> .
    @prefix tdb:     <http://jena.hpl.hp.com/2008/tdb#> .
    @prefix ja:      <http://jena.hpl.hp.com/2005/11/Assembler#> .
    
    [] ja:loadClass "com.hp.hpl.jena.tdb.TDB" .
    tdb:DatasetTDB  rdfs:subClassOf  ja:RDFDataset .
    tdb:GraphTDB    rdfs:subClassOf  ja:Model .
    
    [] rdf:type fuseki:Server ;
       # Timeout - server-wide default: milliseconds.
       # Format 1: "1000" -- 1 second timeout
       # Format 2: "10000,60000" -- 10s timeout to first result, then 60s timeout for the rest of query.
       # See java doc for ARQ.queryTimeout
       # ja:context [ ja:cxtName "arq:queryTimeout" ;  ja:cxtValue "10000" ] ;
    
       # ja:loadClass "your.code.Class" ;
    
       fuseki:services (
         <#reminer>
       ) .
    
    ## ---------------------------------------------------------------
    ## Updatable in-memory dataset.
    
    <#reminer> rdf:type fuseki:Service ;
        # URI of the dataset -- http://host:port/reminer
        fuseki:name                        "name_of_your_database" ; 
        fuseki:serviceQuery                "sparql" ;
        fuseki:serviceQuery                "query" ;
        fuseki:serviceUpdate               "update" ;
        fuseki:serviceUpload               "upload" ;
        fuseki:serviceReadWriteGraphStore  "data" ;     
        fuseki:serviceReadGraphStore       "get" ;
        fuseki:serviceReadGraphStore       "" ;      
        fuseki:dataset                     <#myDataset> ;
        .
    
    ## In-memory, initially empty.
    ## This database set-up allows OWL inference.
    <#myDataset> rdf:type ja:RDFDataset ;
                    tdb:unionDefaultGraph true ; 
                    ja:defaultGraph <#infGraph> .
    
    <#infGraph> rdf:type ja:InfModel ;
                 ja:reasoner [ ja:rulesFrom <file:rules/inference_rules.rules> ; ] ; #the rules directory is in the "run" directory
                 ja:baseModel <#baseGraph> .
    
    <#baseGraph> rdf:type tdb:GraphTDB;
                 tdb:location "path_of_your_database" .
    

    And the inference_rules.rules for an example:

    @prefix rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns#
    @prefix ex: http://example.com/
    @prefix xs: http://www.w3.org/2001/XMLSchema#
    @prefix mO: http://www.semanticweb.org/myOntology/
    
    [dependsOfExchange:
    (?e mO:producedBy ?a)
    (?a mO:hasExchange ?es)
                     ->
                     (?e rme:dependsOf ?es)
             ]