0

我正在使用 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)
4

1 回答 1

3

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语法中的产生式需要一个或多个GroupConditions,它们的形式由这个产生式定义:

GroupCondition ::= BuiltInCall | 函数调用 | '(' 表达式 ('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" ...
     ...
于 2013-10-30T19:49:11.323 回答