与耶拿的SPARQL解析错误,但DBpedia接受查询



我正在使用Jena启动SPARQL查询。我有这个代码,它会产生错误。我不明白此错误的原因,因为将查询放入 DBpedia SPARQL 端点是有效的!我认为我正确编写了查询字符串。 错误是什么?

法典

 String sparqlQueryString=
 "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
 "select ?sub ?super (count(?mid) as ?length) where {"+
 "values ?sub { <http://dbpedia.org/ontology/Writer> }" +
 "?sub rdfs:subClassOf* ?mid ."+
 "?mid rdfs:subClassOf+ ?super .}"+
 "group by (?sub ?super)"+
 "order by (?length)";
 query = QueryFactory.create(sparqlQueryString); 
 QueryExecution qexec = 
 QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql",query);

错误

Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered "     
<VAR1> "?super "" at line 1, column 231.
Was expecting one of:
"not" ...
"as" ...
"in" ...
<INTEGER_POSITIVE> ...
<DECIMAL_POSITIVE> ...
<DOUBLE_POSITIVE> ...
<INTEGER_NEGATIVE> ...
<DECIMAL_NEGATIVE> ...
<DOUBLE_NEGATIVE> ...
")" ...
"=" ...
"!=" ...
">" ...
"<" ...
"<=" ...
">=" ...
"||" ...
"&&" ...
"+" ...
"-" ...
"*" ...
"/" ...
at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.perform(ParserSPARQL11.java:102)
at com.hp.hpl.jena.sparql.lang.ParserSPARQL11.parse$(ParserSPARQL11.java:53)
at com.hp.hpl.jena.sparql.lang.SPARQLParser.parse(SPARQLParser.java:37)
at com.hp.hpl.jena.query.QueryFactory.parse(QueryFactory.java:156)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:79)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:52)
at com.hp.hpl.jena.query.QueryFactory.create(QueryFactory.java:40)
at Query.QueryRDF.retrieveSuperClasses(QueryRDF.java:87)
at Query.QueryRDF.main(QueryRDF.java:144)

不要在GROUP BY变量两边加上括号。 也就是说,它应该是group by ?sub ?super ,而不是group by (?sub ?super).如果您在查询中添加带有n的换行符,这一点非常清楚,以便更容易看到错误所在。 例如,当我尝试编译以下代码时,出现以下运行时错误。

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
public class ParseError {
    @SuppressWarnings("unused")
    public static void main(String[] args) {
         String sparqlQueryString=
                 "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> n"+
                 "select ?sub ?super (count(?mid) as ?length) where {n"+
                 "values ?sub { <http://dbpedia.org/ontology/Writer> }n" +
                 "?sub rdfs:subClassOf* ?mid .n"+
                 "?mid rdfs:subClassOf+ ?super .}n"+
                 "group by (?sub ?super)n"+
                 "order by (?length)n";
         Query query = QueryFactory.create(sparqlQueryString); 
         QueryExecution qexec = 
                 QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql",query);
    }
}

线程"main"中的异常 com.hp.hpl.jena.query.QueryParseException:在第 6 行第 16 列遇到"?super"。

错误直接指向有问题的行。 这里不需要括号,因为语法中的GroupClause生产需要一个或多个GroupCondition,这些具有由此生产定义的形式:

组条件 ::= 内置调用 |函数调用 |'(' 表达式 ( 'AS' var )?')' |瓦尔

如果有GROUP BY (...)它应该是这样的

GROUP BY ( ?a+?b )
GROUP BY ( ?a+?b as ?abSum )

您也可以通过粘贴查询来测试这一点

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
select ?sub ?super (count(?mid) as ?length) where {
values ?sub { <http://dbpedia.org/ontology/Writer> }
?sub rdfs:subClassOf* ?mid .
?mid rdfs:subClassOf+ ?super .}
group by (?sub ?super)
order by (?length)

进入 sparql.org 的查询验证器,您可以从中获得输出:

输入:

  1 PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
  2 select ?sub ?super (count(?mid) as ?length) where {
  3 values ?sub { <http://dbpedia.org/ontology/Writer> }
  4 ?sub rdfs:subClassOf* ?mid .
  5 ?mid rdfs:subClassOf+ ?super .}
  6 group by (?sub ?super)
  7 order by (?length)

语法错误

Encountered "  "?super "" at line 6, column 16.
Was expecting one of:
    "not" ...
    "as" ...
    "in" ...
     ...

最新更新