耶拿 RDFS 推理未返回预期结果

Jena RDFS reasoning not returning expected results

我有以下两个三元组:

<http://example.com/a> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://example.com/b> . 
<http://example.com/b> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://example.com/c> .

以及以下 Jena 代码:

Reasoner reasoner = RDFSRuleReasonerFactory.theInstance().create(null);
    reasoner.setParameter(ReasonerVocabulary.PROPsetRDFSLevel, ReasonerVocabulary.RDFS_SIMPLE);
    reasoner.setDerivationLogging(true);

    Model schema = FileManager.get().loadModel(schemaFilepath);
    InfModel infmodel = ModelFactory.createInfModel(reasoner, schema);

    OutputStream output = new FileOutputStream(outputFilepath);
    RDFDataMgr.write(output, infmodel.getDeductionsModel(), RDFFormat.NT);

如果我理解规范正确,我应该从 getDeductionsModel() 获得:

<http://example.com/a> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <http://example.com/c> .  

但是我得到的是一个空集。有什么想法吗?

我认为推论模型是按需填充的,这取决于特定推理者如何使用推论模型。很有可能仅在请求时生成一些推导的三元组,并且 将它们存储在推导模型中。例如,在下面的示例中,我们打印推导模型,然后是整个模型,然后再次打印推导模型,我们可以看到所需的子类三元组仅在我们打印 entire[=17 时出现=] 模型。一般来说,您应该使用顶级模型,而不是演绎模型,除非您知道关于推理器的实现以及它将使用演绎模型的内容。

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.riot.RDFLanguages;

import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class RDFSReasoningExample {
    public static void main(String[] args) throws IOException {
        OntModel model = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_RDFS_INF);
        String content = (""
                + "@prefix : <urn:ex:>.\n"
                + "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.\n"
                + "\n"
                + ":a rdfs:subClassOf :b .\n"
                + ":b rdfs:subClassOf :c .\n");
        try ( InputStream in = new ByteArrayInputStream(content.getBytes()) ) {
            RDFDataMgr.read(model, in, RDFLanguages.TTL);
        }
        System.out.println("* Write the deductions model");
        RDFDataMgr.write(System.out, model.getDeductionsModel(), RDFLanguages.NT);
        System.out.println("* Write the model");
        RDFDataMgr.write(System.out, model, RDFLanguages.NT);
        System.out.println("* Write the deductions model again");
        RDFDataMgr.write(System.out, model.getDeductionsModel(), RDFLanguages.NT);
    }
}
* Write the deductions model
…nothing about :a, :b, or :c…
* Write the model
…
<urn:ex:a> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <urn:ex:b> .
<urn:ex:b> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <urn:ex:c> .
<urn:ex:c> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <urn:ex:c> .
<urn:ex:a> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <urn:ex:a> .
<urn:ex:a> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <urn:ex:c> .
<urn:ex:b> <http://www.w3.org/2000/01/rdf-schema#subClassOf> <urn:ex:b> .
…
* Write the deductions model again
…nothing about :a, :b, or :c…