簡體   English   中英

Jena的SPARQL解析錯誤,但DBpedia接受查詢

[英]SPARQL parse error with Jena, but DBpedia accepts the query

我正在使用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) ,而不是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 ,其形式由該產生定義:

GroupCondition :: = BuiltInCall | FunctionCall | '('表達式('AS'Var)?')'| 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" ... ... 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM