0

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?

Code

 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)
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
user2837896
  • 271
  • 3
  • 11
  • Check the string variable after concatenation, you might be missing some escape characters. Try to run the query in some tool like Protege to check its validity. – Aakash Oct 30 '13 at 17:59
  • I checked..But I can't find the error.. – user2837896 Oct 30 '13 at 18:18
  • Is the query running fine and returning results in Protege/other tool ? – Aakash Oct 30 '13 at 19:09
  • @AakashGoyal The code appears to be based on the code in [my answer](http://stackoverflow.com/a/19689477/1281433) to [user's earlier question](http://stackoverflow.com/q/19680440/1281433), but with the difference being that I'd used `group by ?sub ?super`, but user's added parentheses around `?sub ?super`. – Joshua Taylor Oct 30 '13 at 19:53
  • @user2837896 ... may be less likely to answers your questions if they think you're unlikely to accept any answers. – Joshua Taylor Oct 31 '13 at 12:05
  • This was also asked and answered on answers.semanticweb.com http://answers.semanticweb.com/questions/25036/jena-sparql-query-error. – Joshua Taylor Nov 06 '13 at 14:01
  • @user2837896 If something about the solution proposed here _didn't_ work for you, can you elaborate on what? There's a good chance that we can make it work for you… – Joshua Taylor Nov 06 '13 at 14:02

1 Answers1

3

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" ...
     ...
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
  • Ok it works! I removed the parentheses of groupBy variables and I added a space after! – user2837896 Oct 31 '13 at 09:50
  • 2
    @user2837896 Again, I can't stress enough that it's especially important to add the newlines that make the generated query the same as the one that you're copying and pasting into other things. Those newlines will make it easier to find syntax errors, because you'll have meaningful line numbers, and they'll keep you from stupid errors like getting `select ?x ?ywhere { ... }` from `"select ?x ?y" + "where { ... }"`. which has come up in past questions, too. – Joshua Taylor Oct 31 '13 at 11:52
  • 1
    @user2837896 E.g., in [this question](http://stackoverflow.com/q/19346744/1281433) someone was doing `select ?cwhere { ... }`, and in [this question](http://stackoverflow.com/q/15663510/1281433) someone was doing `select ?labelwhere { ... }`. In both cases, embedded newlines would have prevented the problem. – Joshua Taylor Oct 31 '13 at 11:58