How to reuse the result of a SPARQL subquery?

151 views
Skip to first unread message

Max

unread,
Dec 8, 2015, 7:25:22 AM12/8/15
to 4store-support
Hello, thank you for creating and supporting 4store!

I have a question I also asked at stackoverflow:

I modelled the songs of an album as follows:


@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

@prefix md: <http://example.com/music-discography#> .

md:album1 md:songs-of-album md:song1 .
md:album1 md:songs-of-album md:song2 .
md:album1 md:songs-of-album md:song3 .

md:song1 md:name "Song 1" .
md:song2 md:name "Song 2" .
md:song3 md:name "Song 3" .

md:song1 md:position "1"^^xsd:integer .
md:song2 md:position "2"^^xsd:integer .
md:song3 md:position "3"^^xsd:integer .


I would like to be able to access the name of the last song of an album.

Therefore I thought about first getting the md:position of the last song by calculating how many songs an album contains


SELECT (COUNT(*) AS ?lastposition)
WHERE {
  { md:album1 md:songs-of-album ?songInnerQuery }
}


which produces the correct result "3":


  <results>
    <result>
      <binding name="lastposition">
    <literal datatype="http://www.w3.org/2001/XMLSchema#integer">3</literal>
    </binding>
    </result>
  </results>


I then wanted to reuse this result by using the query above as a subquery. How can I do this? I tried the following:


SELECT ?songName WHERE {

  ?song md:position ?lastposition .
  ?song md:name ?songName

  { SELECT (COUNT(*) AS ?lastposition)
    WHERE {
      { md:album1 md:songs-of-album ?songInnerQuery }
    }
  }

}


However, this does not work and yields all three songs:


  <results>
    <result>
      <binding name="songName"><literal>Song 3</literal></binding>
    </result>
    <result>
      <binding name="songName"><literal>Song 2</literal></binding>
    </result>
    <result>
      <binding name="songName"><literal>Song 1</literal></binding>
    </result>
  </results>


Why does this happen and what is the correct way of doing this? 


-------


Joshua Taylor presented another approach for accessing the last song via this query:


SELECT ?songName WHERE {
  ?song md:position ?lastposition .
  ?song md:name ?songName

  filter not exists {
    ?song_ md:position ?position
    filter (?position > ?lastposition)
  }
}


However, if I execute this query I get "parser error: syntax error, unexpected NOT on line 9". 

Am I doing something wrong?


Best regards,

Max

swh

unread,
Dec 8, 2015, 9:48:19 AM12/8/15
to 4store-support
4store uses the Rasqal SPARQL parser, which doesn't parse all SPARQL syntax, I think.

You could check you're on the latest version of Rasqal, but even if later ones do parse FILTER NOT EXISTS, 4store might not handle it.

As someone said on stack overflow, MINUS should work in place for FILTER NOT EXISTS.

4store support of subqueries is not very complete, sadly.
Reply all
Reply to author
Forward
0 new messages