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

How to set a property path in Jena's Sparql API?

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

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

您可以使用 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"));