jena Derivations是否支持RDFS推理器?

时间:2018-03-15 06:50:28

标签: jena inference

我看到jena文件说jena支持Derivations。但是,我的代码无法推导出来。 这是我的代码:

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);

   }
}

代码永远无法输入 if(derivations.hasNext())部分。我想知道jena是否支持RDFS推理器getDerivation?

1 个答案:

答案 0 :(得分:1)

您必须将推理器的PROPderivationLogging参数设置为true,具体操作如下:

  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);