2013-04-04 59 views
0

我目前繼耶拿API推理教程推理:基本RDFS與耶拿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)); 
    } 
} 

到一些東西,做相同,但與此龜文件,而不是讀取模型(這是我自己的上述翻譯,因此可能是越野車):

@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財產的提取是有點不正確)。我究竟做錯了什麼?

回答

1
String NS = "foo:"; 
m.createResource(NS + "x") 

創建一個URI,但龜版有富:X = http://example.org/foo#x

通過打印模式im.write(System.out, "TTL");

變化NS = "foo:"見分歧NS = "http://example.org/foo#"

+0

這也正是它,非常感謝! – cjauvin 2013-04-05 01:29:18