与Jena的SPARQL解析错误,但DBpedia接受该查询 [英] SPARQL parse error with Jena, but DBpedia accepts the query
问题描述
我正在使用Jena启动SPARQL查询。我有这个代码,产生错误。
我不明白这个错误的原因,因为将查询放入 DBpedia SPARQL端点工作!我认为我正确地编写了查询字符串。什么是错误?
I'm using Jena to launch a SPARQL query. I have this code, which produces an error. I don't understand the reason for this error, since putting the query into the DBpedia SPARQL endpoint works! I think that I wrote the query string correctly. What's the error?
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);
错误
Error
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
的换行符,则非常清楚,这样可以更容易地查看错误的位置。例如,当我尝试编译以下代码时,我得到以下运行时错误。
Dont' put parentheses around the GROUP BY
variables. That is, it should be group by ?sub ?super
,
and not group by (?sub ?super)
. This is pretty clear if you add newlines with \n
to your query, so that it's easier to see where the error is. E.g., when I try to compile the following code, I get the following run time error.
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);
}
}
线程中的异常 maincom.hp.hpl.jena.query.QueryParseException:Encountered?super在第6行第16列。
Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "?super "" at line 6, column 16.
错误指向有问题的行。这里不需要括号,如 GroupClause
语法中的生产需要一个或多个 GroupCondition
s,其格式由此产品定义:
The error points right to the problematic line. Parentheses aren't needed here, as the GroupClause
production in the grammar expects one or more GroupCondition
s, which have a form defined by this production:
GroupCondition :: = BuiltInCall | FunctionCall | '('表达式('AS'Var)?')'| Var
GroupCondition ::= BuiltInCall | FunctionCall | '(' Expression ( 'AS' Var )? ')' | Var
如果有一个 GROUP BY(...)
它应该是类似于
If there's a GROUP BY (...)
it's supposed to be something like
GROUP BY ( ?a+?b )
GROUP BY ( ?a+?b as ?abSum )
你也可以测试过这个通过粘贴您的查询
You could also have tested this by pasting your query
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的查询验证器您将获得输出:
输入:
Input:
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)
语法错误
Syntax Error
Encountered " "?super "" at line 6, column 16.
Was expecting one of:
"not" ...
"as" ...
"in" ...
...
这篇关于与Jena的SPARQL解析错误,但DBpedia接受该查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!