【问题标题】:Basic RDFS inferencing with the Jena API使用 Jena API 进行基本 RDFS 推理
【发布时间】:2013-04-04 16:45:02
【问题描述】:

我目前正在学习 Jena API 推理教程:

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

作为一个测试我理解的练习,我想重写第一个示例,该示例演示了从以编程方式构建的模型进行的简单 RDFS 推理:

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

public class Test1 {
    static public void main(String...argv) {
        String NS = "foo:";
        Model m = ModelFactory.createDefaultModel();
        Property p = m.createProperty(NS, "p");
        Property q = m.createProperty(NS, "q");
        m.add(p, RDFS.subPropertyOf, q);
        m.createResource(NS + "x").addProperty(p, "bar");
        InfModel im = ModelFactory.createRDFSModel(m);
        Resource x = im.getResource(NS + "x");
        // verify that property q of x is "bar" (which follows 
        // from x having property p, and p being a subproperty of q)
        System.out.println("Statement: " + x.getProperty(q));
    }
}

做同样的事情,但是使用从这个 Turtle 文件中读取的模型(这是我自己对上述内容的翻译,因此可能有问题):

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix foo: <http://example.org/foo#>.

foo:p a rdf:Property.
foo:q a rdf:Property.
foo:p rdfs:subPropertyOf foo:q.
foo:x foo:p "bar".

使用此代码:

public class Test2 {
    static public void main(String...argv) {
        String NS = "foo:";
        Model m = ModelFactory.createDefaultModel();
        m.read("foo.ttl");
        InfModel im = ModelFactory.createRDFSModel(m);
        Property q = im.getProperty(NS + "q");
        Resource x = im.getResource(NS + "x");
        System.out.println("Statement: " + x.getProperty(q));
    }
}

这似乎不是正确的方法(我特别怀疑我对q 属性的提取在某种程度上是不正确的)。我做错了什么?

【问题讨论】:

    标签: java semantic-web jena reasoning inference-engine


    【解决方案1】:
    String NS = "foo:";
    m.createResource(NS + "x")
    

    创建一个 URI,但 Turtle 版本有 foo:x = http://example.org/foo#x

    通过打印模型im.write(System.out, "TTL");查看差异

    NS = "foo:" 更改为NS = "http://example.org/foo#"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-26
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多