[英]Basic RDFS inferencing with the Jena API
I'm currently following the Jena API inferencing tutorial: 我目前正在关注Jena API推理教程:
https://jena.apache.org/documentation/inference/ https://jena.apache.org/documentation/inference/
and as an exercise to test my understanding, I'd like to rewrite the first example, which demonstrates a trivial RDFS reasoning from a programmatically built model: 作为测试我的理解的练习,我想重写第一个示例,该示例演示了以编程方式构建的模型的琐碎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));
}
}
to something which does the same, but with the model read from this Turtle file instead (which is my own translation of the above, and thus might be buggy): 到相同的东西,但是模型从这个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".
with this code: 使用此代码:
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));
}
}
which doesn't seem to be the right approach (I suspect in particular that my extraction of the q
property is somehow not right). 这似乎不是正确的方法(我特别怀疑我对q
属性的提取不正确)。 What am I doing wrong? 我究竟做错了什么?
String NS = "foo:";
m.createResource(NS + "x")
creates a URI but the Turtle version has foo:x = http://example.org/foo#x 创建URI,但Turtle版本具有foo:x = http://example.org/foo#x
See the differences by printing the model im.write(System.out, "TTL");
通过打印模型im.write(System.out, "TTL");
查看差异im.write(System.out, "TTL");
Change NS = "foo:"
to NS = "http://example.org/foo#"
将NS = "foo:"
更改为NS = "http://example.org/foo#"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.