簡體   English   中英

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

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

這是我的查詢

    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)

如果我將它放在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)

盡管我打印了耶拿從文件中加載的查詢,並將其復制到fuseki,它在fuseki上工作得很好

這就是我加載查詢的方式(但是我確定這與實際問題無關)

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);

更新

此代碼有效

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)
  }


}

除了group by以外,這兩個查詢是相同的,帶有group by的查詢僅在Fuseki接口上工作,而不是在eclipse java上工作,而另一個在兩者上均可工作

UPDATE3

問題發生在這兩行

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

當我刪除它們時,一切正常,但是當我放置它們時,我得到了那個錯誤

更新4

日志是:

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]

更新5

現在我發現了真正的問題,它是單詞DISTINCT ,當我刪除它時,一切正常,當我放回它時,它只能在fuseki界面上工作,而不是在耶拿java上工作:(伙計們,請

我認為,在這種情況下,如果可以的話,您可能希望讓Jena避免進行本地解析,並將查詢直接發送到遠程端點。 耶拿(Jena)answer.semanticweb.com上正確但非標准的SPARQL問題上拋出QueryParsingException,描述了這種方法。 這個想法是用查詢字符串創建一個QueryEngineHTTP

至於為什么收到此錯誤,我認為這可能是耶拿(Jena)的錯誤。 我有一點證據,也有一點假設。 進行更多調查,並使用sparql.org的查詢驗證器 (由Jena支持),這是一件奇怪的事情。 如果輸入查詢

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

進入解析器,格式化后的解析查詢顯示為:

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

這是合法的。 (請注意,使用GROUP_CONCAT放置了distinct的偏移。還請注意,它發生在group_concat上 ,但是與sum 無關 。)

當使用Jena將查詢發送到遠程端點時,如果Jena首先解析輸入查詢,然后將重新格式化的查詢發送到遠程端點,這將解釋調試日志消息和解析錯誤,但是我不確定是否這就是事情執行與否的方式。

暫無
暫無

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

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