Search code examples
javajenainference-engine

How to use Transitive Reasoner with Jena?


I would like to set up a TransitiveReasoner with Jena to create a new inference model from a schema and a dataset. It works with RDFS reasoner but not with TransitiveReasoner.

This is my first experience with inference; I looked at the Jena Inference Support plus other tutorials, but could not manage to solve my problem.

Here my test code in java:

import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.reasoner.*;
import com.hp.hpl.jena.vocabulary.*;

public class TestInference 
{

    public static void myTest() throws IOException
    {
      String NS = "testInference:";
      OntModel schema = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, null);
      OntClass m = schema.createClass(NS + "Mention");
      OntClass pm = schema.createClass(NS + "PersonMention");
      pm.addProperty(RDFS.subClassOf, m);

      Model data = ModelFactory.createDefaultModel();
      Resource r = data.createResource(NS+"alberto");
      r.addProperty(RDF.type, pm);

      Reasoner rdfsReasoner = ReasonerRegistry.getRDFSSimpleReasoner();
      Reasoner transReasoner = ReasonerRegistry.getTransitiveReasoner();

      System.out.println("\n===== RDSF =====");
      InfModel rdfsInf = ModelFactory.createInfModel(rdfsReasoner, schema, data);
      rdfsInf.write(System.out, "TURTLE");

      System.out.println("\n===== Trans =====");
      InfModel transInf = ModelFactory.createInfModel(transReasoner, schema, data);
      transInf.write(System.out, "TURTLE");
}

public static void main(String[] args) throws IOException
{
    myTest();
}

Trying to change the OntModelSpec does not help.

What am I doing wrong?

Thanks in advance for your help.


Solution

  • The TransitiveReasoner only deals with the transitivity of RDFS subClassOf and RDFS subPropertyOf. It does not provide the step of rdf:type.

    A subClassOf B . B subClassOf C => A subClassOf C
    

    But not this part:

    x type T . T subClassOf S => x type S
    

    https://jena.apache.org/documentation/inference/#transitive