如何在Jena的Sparql API中设置属性路径?

时间:2016-03-02 11:58:54

标签: java sparql rdf jena arq

我想避免将SPARQL查询作为字符串传递。因此,我使用Jena的API来创建查询。现在我在查询中需要一个PropertyPath,但我找不到支持它的任何Java类。你能给我一个暗示吗?

这是一些示例代码,我想插入它(Jena 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;
}

1 个答案:

答案 0 :(得分: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"));