2013-10-10 63 views
2

我正在使用Java芝麻。我從一個教程後面的例子開始。我正在嘗試構建一個簡單的語句,如代碼所示。我的問題是我不知道如何打印出來,例如,該陳述的主題或對象。任何人都可以幫助我嗎?這裏是我的代碼:在Java芝麻中打印聲明

public static void main(String[] args) 
{ 
    ValueFactory factory = ValueFactoryImpl.getInstance(); 
    URI bob = factory.createURI("http://example.org/bob"); 
    URI name = factory.createURI("http://example.org/name"); 
    Literal bobsName = factory.createLiteral("Bob"); 
    Statement nameStatement = factory.createStatement(bob, name, bobsName); 
    Statement typeStatement = factory.createStatement(bob, RDF.TYPE,FOAF.PERSON); 
} 

我應該使用以下行:

model.filter(null, RDF.TYPE, FOAF.PERSON).subjects(); 

我應該使用一個代碼像上面的一個,但不知道如何定義模型,以及如何打印出的聲明或至少是主題。非常感激你的幫助。

+1

給編輯試圖把它變成'公共實例(){公共靜態無效main(...)...}}',public Example(){...}'是一個默認的構造函數。雖然整個類將被命名爲'Example',但聲明'main'方法在構造函數中沒有任何意義。 –

回答

4

根據javadoc for org.openrdf.Statement,您將使用getSubject(),getPredicate()getObject()。作爲pointed out in the comments,聲明,StatementImpl的共同實施,提供了toString()方法的實現,讓你甚至可以只打印聲明:

Statement s = /* ... */; 
System.out.println(s); 
+1

它有這樣的方法:http://openrdf.callimachus.net/sesame/2.7/apidocs/org/openrdf/model/impl/StatementImpl.html#toString() – Rhand

+0

的主語,謂語和對象(其是所有亞型'Value')也都有一個可打印的'toString()'。 –

+0

Thanks @Rhand,我已經更新了答案。 –