简体   繁体   中英

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:

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

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

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

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

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

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. That approach is described in the jena throws QueryParsingException on correct but non-standard SPARQL question on answers.semanticweb.com. The idea would be to create a QueryEngineHTTP with the query string.

As to why you're getting this error, I think it might be a bug on Jena's end. 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. 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 .)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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