Search code examples
javajenasemantic-webontologypellet

How to use Openllet OWL2 reasoner (or any other) with Jena TDB?


I have already found a way to infer the contents of the triple store using Openllet reasoner and Jena TDB, but it does not seem optimal. Here is my solution:

Reasoner reasoner = PelletReasonerFactory.theInstance().create();
InfModel infModel = ModelFactory
                        .createInfModel(reasoner, dataset.getNamedModel(KD.URI));

/*
 * To extract the model, a transaction must be open in READ mode.
 */

dataset.begin(ReadWrite.READ);

ModelExtractor me = new ModelExtractor(infModel);

dataset.end();

/*
 * To replace a currently existing named model within the dataset, a transaction must be open in WRITE mode.
 */

dataset.begin(ReadWrite.WRITE);
dataset.replaceNamedModel(KD.URI, me.extractModel());
dataset.commit();
dataset.end();

This works, but I want to know of a more elegant way than to actually replace the named model in the dataset. In an ideal scenario I would also like the inference to be continuous (once a triple is inserted into the graph data is inferred automatically on the spot) but I do not know if it is possible.


Solution

  • For continuous inference processus, you need to set up Fuseki configuration.

    Here is an quick example of my configuration on a persistent TDB database with an Openllet reasoner.

    @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#> .
    
    :service_tdb_all  a                   fuseki:Service ;
            rdfs:label                    "LABEL_OF_YOUR_SERVICE" ;
            fuseki:dataset                :dataset ;
            fuseki:name                   "NAME_OF_YOUR_SERVICE" ;
            fuseki:serviceQuery           "query" , "sparql" ;
            fuseki:serviceReadGraphStore  "get" ;
            fuseki:serviceReadWriteGraphStore
                    "data" ;
            fuseki:serviceUpdate          "update" ;
            fuseki:serviceUpload          "upload" .
    
    :dataset a ja:RDFDataset ;
      ja:defaultGraph <#modeInf>; .
    
    <#modeInf> a ja:InfModel;
      ja:baseModel <#tdbGraph>;
      ja:reasoner   [
        ja:reasonerClass    "openllet.jena.PelletReasonerFactory" ]
      .  
    
    <#tdbGraph> rdf:type tdb:GraphTDB ;
      tdb:dataset :tdb_dataset_readwrite  .
    
    :tdb_dataset_readwrite  a            
      tdb:DatasetTDB ;  tdb:location 
      "PATH_TO_YOUR_TDB"
      .