SPARQL查询,属性路径不起作用

问题描述 投票:0回答:1

我想做一个执行以下操作的查询:如果存在从(s,p,o)s的至少2条边长度的路径,并且属性为o,则选择所有三元组p。所以路径的所有边缘都必须用p标记。我尝试了以下方法:

select  ?s <http://dbpedia.org/ontology/isPartOf> ?o
WHERE { 
?s <http://dbpedia.org/ontology/isPartOf>{2,} ?o.
?s <http://dbpedia.org/ontology/isPartOf> ?o 
}

我用Jena API执行它:

ParameterizedSparqlString parameterizedSparql = new ParameterizedSparqlString(model);
parameterizedSparql.setCommandText(sparql);
Query query = QueryFactory.create(parameterizedSparql.asQuery().toString(), Syntax.syntaxARQ);
QueryExecutionFactory.create(query, model).execSelect();

我使用Syntax.syntaxARQ以便它应该理解属性路径。

它给了我以下错误:

Exception in thread "main" org.apache.jena.query.QueryParseException: Encountered " "{" "{ "" at line 3, column 42.
Was expecting one of:
<IRIref> ...
<PNAME_NS> ...
<PNAME_LN> ...
<BLANK_NODE_LABEL> ...
<VAR1> ...
<VAR2> ...

你能告诉我如何正确地进行查询吗?

sparql jena propertypath
1个回答
1
投票

另外,正如@AKSW指出的那样,{2,}语法from the SPARQL 1.1 Working Draft没有进入the final SPARQL 1.1 spec,所以你不能依赖每个SPARQL处理器支持它。

你可以use the {2,} syntax with Virtuoso,它是the public DBpedia endpoint的引擎,但是要通过Jena这样做,你必须使用“扩展语法”(Syntax.syntaxARQ)或bypass the ARQ parser

您的直接问题似乎归结为耶拿的一个错误,其中ParameterizedSparqlString.asQuery() does not currently support "extended syntax" (Syntax.syntaxARQ) queries;正如@AndyS评论的那样,parameterizedSparql.toString()应该足够了。

© www.soinside.com 2019 - 2024. All rights reserved.