耶拿RDFS推理没有返回预期的结果

时间:2015-01-29 19:31:39

标签: java jena rdfs reasoning

我有以下两个三元组:

<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> .

以下耶拿代码:

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

如果我理解正确的规范,我应该从getDedunctionsModel()获取:

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

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

1 个答案:

答案 0 :(得分:0)

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

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…