2016-03-02 73 views
1

我想避免將SPARQL查詢作爲字符串傳遞。因此我使用Jena的API來創建我的查詢。現在我需要在我的查詢PropertyPath,但我找不到任何Java類支持。你能給我一個提示嗎?如何在Jena的Sparql API中設置屬性路徑?

下面是一些示例代碼,我想插入這個(耶拿3.0.1):

private Query buildQuery(final String propertyPath) { 
    ElementTriplesBlock triplesBlock = new ElementTriplesBlock(); 
    triplesBlock.addTriple(
      new Triple(NodeFactory.createURI(this.titleUri.toString()), 
        //How can I set a property path as predicate here? 
        NodeFactory.???, 
        NodeFactory.createVariable("o")) 
    ); 
    final Query query = buildSelectQuery(triplesBlock); 
    return query; 
} 

private Query buildSelectQuery(final ElementTriplesBlock queryBlock) { 
    final Query query = new Query(); 
    query.setQuerySelectType(); 
    query.setQueryResultStar(true); 
    query.setDistinct(true); 
    query.setQueryPattern(queryBlock); 
    return query; 
} 
+0

只是一個問題,更多的代碼清晰度比什麼都重要:可能是更容易和更清晰的寫查詢語句的字符串*一次*解析它,然後傳遞生成的查詢對象?你仍然有對象,而不是字符串被傳遞,但它似乎將來更容易調試和修改。 –

+0

@喬斯華泰勒,是的,我認爲這是可能的。但由於propertyPath是我唯一的變量輸入,所以我不得不將它插入字符串中,這顯然是可能的。我正在尋找一個更清潔的解決方案。在此期間,我發現我可能應該使用此代碼來解析PropertyPath: 'Path p = SSE.parsePath(「dct:creator/gndo:preferredNameForThePerson」,PMAP); triplesBlock.addTriplePath( 新TriplePath(..., parsedPropertyPath, ... );' 但SSE.parsePath運行到目前的異常 – Andreas

+1

提問和回答:HTTP://郵件存檔。 apache.org/mod_mbox/jena-users/201603.mbox/%3C56D70307020000C20002033D%40gwia.bsb-muenchen.de%3E 摘要:使用'PathParser'。 – AndyS

回答

0

您可以使用PathFactory創造財產路徑

考慮下面的圖表:

@prefix dc: <http://purl.org/dc/elements/1.1/>. 
@prefix ex: <http://example.com/>. 

    ex:Manager ex:homeOffice ex:HomeOffice 
    ex:HomeOffice dc:title "Home Office Title" 

假設您想創建一個模式,如:

?x ex:homeOffice/dc:title ?title 

下面的代碼實現它:

//create the path 
Path exhomeOffice = PathFactory.pathLink(NodeFactory.createURI("http://example.com/homeOffice")); 
Path dcTitle = PathFactory.pathLink(NodeFactory.createURI("http://purl.org/dc/elements/1.1/title")); 
Path fullPath = PathFactory.pathSeq(exhomeOffice,dcTitle); 
TriplePath t = new TriplePath(Var.alloc("x"),fullPath,Var.alloc("title")); 
相關問題