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 SPAR
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);
}
}
Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "?super "" at line 6, column 16.
The error points right to the problematic line. Parentheses aren't needed here, as the GroupClause production in the grammar expects one or more GroupConditions, which have a form defined by this production:
GroupCondition ::= BuiltInCall | FunctionCall | '(' Expression ( 'AS' Var )? ')' | Var
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)
into sparql.org's query validator from which you'd get the output:
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" ... ...