简体   繁体   English

SPARQL查询在Fuseki接口上有效,但在耶拿(Jena)中

[英]SPARQL query works on the Fuseki interface but in Jena

THis is my query 这是我的查询

    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix no: <http://www.newontology.org/no#>
prefix rs: <http://semanticrecommender.com/rs#>
prefix mo: <http://music.org/musicontology/mo#>
prefix : <http://www.MusicSemanticOntology.com/mso#>

select ?item (SUM(?similarity * ?importance * ?levelImportance * ?ratingValue) as ?summedSimilarity) 
(group_concat(distinct ?x) as ?commingFromLikingThisInstance)
(group_concat(?becauseOf ; separator = " ,and ") as ?reason)
where
{
  values ?user { :ania }
  #the variable ?x is bound to the items the user :ania has liked.
  ?user rs:hasRated ?ratings.
  ?ratings a rs:Likes.
  ?ratings rs:about ?x.
  ?ratings rs:ratesBy ?ratingValue.
 ?ratings rs:createdOn ?ratingDate.

  #level 0 class similarities
  {
    #extract all the items that are from the same class (type) as the liked items.
    #I assumed the being from the same class accounts for 50% of the similarities.
    #This value can be changed according to the test or the application domain.
    values ?classImportance {0.5} #class level
    ?x  a ?class .
    ?item a ?class .
    ?class rs:hasSimilarityValue ?similarity .
    bind (?classImportance as ?importance)
    bind( 4/7 as ?levelImportance)
    bind (concat("it shares the same class, which is ", str(?class), " with ", str(?x)) as ?becauseOf)
  }


}
group by ?item
order by desc(?summedSimilarity)

it works if i put it in fuseki sparql interface, but if i put it in a file and call that file from jena, i get the follwong exception: 如果我将它放在fuseki sparql接口中,它会起作用,但是如果我将其放在文件中并从耶拿调用该文件,则会收到以下follwong异常:

EVERE: Servlet.service() for servlet [com.semanticrecommender.web.Main] in context with path [/SemanticRecommender] threw exception
HttpException: 400
    at org.apache.jena.sparql.engine.http.HttpQuery.rewrap(HttpQuery.java:411)
    at org.apache.jena.sparql.engine.http.HttpQuery.execPost(HttpQuery.java:399)
    at org.apache.jena.sparql.engine.http.HttpQuery.exec(HttpQuery.java:291)
    at org.apache.jena.sparql.engine.http.QueryEngineHTTP.execResultSetInner(QueryEngineHTTP.java:359)
    at org.apache.jena.sparql.engine.http.QueryEngineHTTP.execSelect(QueryEngineHTTP.java:351)

eventhough I print the query that jena loads from the file and copied it to fuseki and it works perfectly on fuseki 尽管我打印了耶拿从文件中加载的查询,并将其复制到fuseki,它在fuseki上工作得很好

This is how I load the query (but I am sure this is not related to the actual problem) 这就是我加载查询的方式(但是我确定这与实际问题无关)

InputStream testIn = getClass().getResourceAsStream("/recommend.rq");
        String queryTemplate = IOUtils.toString(testIn);
System.out.println(queryTemplate);
        QueryExecution x = QueryExecutionFactory.sparqlService(
                "http://localhost:3030/rs/query", queryTemplate);
        ResultSet results = x.execSelect();
        ResultSetFormatter.out(System.out, results);

Update 更新

This code work 此代码有效

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
prefix no: <http://www.newontology.org/no#>
prefix rs: <http://semanticrecommender.com/rs#>
prefix mo: <http://music.org/musicontology/mo#>
prefix : <http://www.MusicSemanticOntology.com/mso#>

select *
where
{
  values ?user { :ania }
  #the variable ?x is bound to the items the user :ania has liked.
  ?user rs:hasRated ?ratings.
  ?ratings a rs:Likes.
  ?ratings rs:about ?x.
  ?ratings rs:ratesBy ?ratingValue.
 ?ratings rs:createdOn ?ratingDate.

  #level 0 class similarities
  {
    #extract all the items that are from the same class (type) as the liked items.
    #I assumed the being from the same class accounts for 50% of the similarities.
    #This value can be changed according to the test or the application domain.
    values ?classImportance {0.5} #class level
    ?x  a ?class .
    ?item a ?class .
    ?class rs:hasSimilarityValue ?similarity .
    bind (?classImportance as ?importance)
    bind( 4/7 as ?levelImportance)
    bind (concat("it shares the same class, which is ", str(?class), " with ", str(?x)) as ?becauseOf)
  }


}

These two queries are identical except the group by, the one with group by works just on Fuseki interface, not eclipse java, but the other works with both 除了group by以外,这两个查询是相同的,带有group by的查询仅在Fuseki接口上工作,而不是在eclipse java上工作,而另一个在两者上均可工作

Update3 UPDATE3

The problem happens in these two lines 问题发生在这两行

(group_concat(distinct ?x) as ?commingFromLikingThisInstance)
(group_concat(?becauseOf ; separator = " ,and ") as ?reason)

when I remove them, everything works fine, but when I put them I got that error 当我删除它们时,一切正常,但是当我放置它们时,我得到了那个错误

Update 4 更新4

The log is: 日志是:

2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "Error 400: Parse error: [\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "PREFIX  :     <http://www.MusicSemanticOntology.com/mso#>[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "PREFIX  rs:   <http://semanticrecommender.com/rs#>[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "SELECT  ?item (SUM(( ( ( ?similarity * ?importance ) * ?levelImportance ) * ?ratingValue )) AS ?summedSimilarity) (GROUP_CONCAT DISTINCT (?x) AS ?commingFromLikingThisInstance) (GROUP_CONCAT (?becauseOf ; separator=' ,and ') AS ?reason)[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "WHERE[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "  { VALUES ?user { :ania }[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "    ?user     rs:hasRated  ?ratings .[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "    ?ratings  rdf:type     rs:Likes ;[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "              rs:about     ?x ;[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "              rs:ratesBy   ?ratingValue[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "    { VALUES ?classImportance { 0.5 }[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "      BIND(?classImportance AS ?importance)[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "      BIND(( 4 / 7 ) AS ?levelImportance)[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "      ?x      rdf:type              ?class .[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "      ?item   rdf:type              ?class .[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "      ?class  rs:hasSimilarityValue  ?similarity[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "      BIND(concat("it shares the same class, which is ", str(?class), " with ", str(?x)) AS ?becauseOf)[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "    }[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "  }[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "GROUP BY ?item[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "ORDER BY DESC(?summedSimilarity)[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "[\r]Encountered " "distinct" "DISTINCT "" at line 6, column 129.[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "Was expecting:[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "    "(" ...[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "    [\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.wire:63 - << "Fuseki - version 2.3.1 (Build date: 2015-12-08T09:24:07+0000)[\n]"
2016-03-28 11:17:50 DEBUG org.apache.http.impl.conn.PoolingClientConnectionManager:274 - Connection [id: 0][route: {}->http://localhost:3030] can be kept alive indefinitely
2016-03-28 11:17:50 DEBUG org.apache.http.impl.conn.PoolingClientConnectionManager:281 - Connection released: [id: 0][route: {}->http://localhost:3030][total kept alive: 1; route allocated: 1 of 5; total allocated: 1 of 10]

Update 5 更新5

Now I found the real problem, it is the word DISTINCT , when I remove it, everything works fine, when I put it back, it just works from the fuseki interface and not from jena java :( help guys please 现在我发现了真正的问题,它是单词DISTINCT ,当我删除它时,一切正常,当我放回它时,它只能在fuseki界面上工作,而不是在耶拿java上工作:(伙计们,请

I think you might want to have Jena avoid the local parsing if you can, in this case, and send the query directly to the remote endpoint. 我认为,在这种情况下,如果可以的话,您可能希望让Jena避免进行本地解析,并将查询直接发送到远程端点。 That approach is described in the jena throws QueryParsingException on correct but non-standard SPARQL question on answers.semanticweb.com. 耶拿(Jena)answer.semanticweb.com上正确但非标准的SPARQL问题上抛出QueryParsingException,描述了这种方法。 The idea would be to create a QueryEngineHTTP with the query string. 这个想法是用查询字符串创建一个QueryEngineHTTP

As to why you're getting this error, I think it might be a bug on Jena's end. 至于为什么收到此错误,我认为这可能是耶拿(Jena)的错误。 I have a little bit of evidence, and a little bit of hypothesis. 我有一点证据,也有一点假设。 Investigating a bit more, and playing with sparql.org's query validator (which is backed by Jena), there's something weird going on. 进行更多调查,并使用sparql.org的查询验证器 (由Jena支持),这是一件奇怪的事情。 If you enter the query 如果输入查询

select (group_concat(distinct ?x) as ?y) (sum(distinct ?x) as ?z) {}

into the parser, the formatted, parsed query appears as: 进入解析器,格式化后的解析查询显示为:

SELECT  (GROUP_CONCAT DISTINCT (?x) AS ?y) (SUM(DISTINCT ?x) AS ?z) WHERE {}

which is not legal. 这是合法的。 (Note the off placement of distinct with the GROUP_CONCAT. Also note that it happens with group_concat , but not with sum .) (请注意,使用GROUP_CONCAT放置了distinct的偏移。还请注意,它发生在group_concat上 ,但是与sum 无关 。)

When a query is sent to a remote endpoint using Jena, if Jena first parses the input query, and then sends the reformatted query off to the remote endpoint, that would explain debug log messages and the parse error, but I'm not sure whether that's how things are implemented or not. 当使用Jena将查询发送到远程端点时,如果Jena首先解析输入查询,然后将重新格式化的查询发送到远程端点,这将解释调试日志消息和解析错误,但是我不确定是否这就是事情执行与否的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM