Search code examples
jenainference

Whether does jena Derivations support RDFS reasoner?


I see jena document that said jena support Derivations. But, my code can't derive. This is my code:

import org.apache.jena.rdf.model.*;
import org.apache.jena.reasoner.Derivation;
import org.apache.jena.reasoner.ValidityReport;
import org.apache.jena.reasoner.rulesys.RuleDerivation;
import org.apache.jena.util.FileManager;
import org.apache.jena.vocabulary.*;

import java.io.*;
import java.util.Iterator;


public class Qi2 extends Object {

  private static String fnameschema = "./data/Qischeme.rdf";
  private static String fnameinstance = "./data/QiData.rdf";
  public static void main (String args[]) {
    // create an empty model
    Model schema = FileManager.get().loadModel(fnameschema);
    Model data = FileManager.get().loadModel(fnameinstance);
    InfModel infmodel = ModelFactory.createRDFSModel(schema, data);


    int k = 0;
    final PrintWriter out = new PrintWriter(System.out);

    Resource wilson = infmodel.getResource("http://www.example.org/ustb#Wilson_Harvey");
    Resource person = infmodel.getResource("http://www.example.org/ustb#Person");
    for (StmtIterator i = infmodel.listStatements(wilson, (Property) null, person); i.hasNext(); ) {
        Statement s = i.nextStatement();
        System.out.println(s);
        final Iterator<Derivation> derivations = infmodel.getDerivation(s);
        assert( null != derivations );
        if (derivations.hasNext())
            System.out.println("have vaule");


        k++;
     } 

   System.out.println(k);

   }
}

The code forever can't enter if (derivations.hasNext()) part. I want to know whether jena support RDFS reasoner getDerivation?


Solution

  • You have to set the PROPderivationLogging parameter of the reasoner to true, which can be done as follows:

      Model schema = FileManager.get().loadModel(dataFile);
      Model data = FileManager.get().loadModel(schemaFile);
    
      Resource config = ModelFactory.createDefaultModel()
          .createResource()
          .addProperty(ReasonerVocabulary.PROPderivationLogging, "true");
      Reasoner reasoner = RDFSRuleReasonerFactory.theInstance().create(config);
    
      InfModel infModel = ModelFactory.createInfModel(reasoner, schema, data);