ruby-rdf / sparql

Ruby SPARQL library
http://rubygems.org/gems/sparql
The Unlicense
89 stars 14 forks source link

Property path operator * unbinds variable #27

Closed Simaris closed 7 years ago

Simaris commented 7 years ago

Hallo, after encountering a bug in my main project I decided to set up the following test code:

require 'linkeddata'
require 'sparql'
require 'pp'

graph = RDF::Graph.load(File.join(File.dirname("__FILE__"),"_rdf_data/simpsons.ttl"))
sparql = SPARQL::Client.new(graph)

query = "SELECT ?r ?f WHERE{
  <http://www.ifi.uio.no/INF3580/simpson-collection#Collection> <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest>* ?r.
  ?r <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> ?f
}"
pp sparql.query(query)

and the knowledge base:

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix simc: <http://www.ifi.uio.no/INF3580/simpson-collection#> .
@prefix sim: <http://www.ifi.uio.no/INF3580/simpsons#>.

simc:Collection a rdf:List.
simc:Collection rdf:first sim:Homer.
simc:Collection rdf:rest simc:c1.
simc:c1 rdf:first sim:Marge.
simc:c1 rdf:rest simc:c2.
simc:c2 rdf:first sim:Bart.
simc:c2 rdf:rest simc:c3.
simc:c3 rdf:first sim:Maggie.
simc:c3 rdf:rest simc:c4.
simc:c4 rdf:first sim:Lisa.
simc:c4 rdf:rest rdf:nil.

The output I get from this is: [#<RDF::Query::Solution:0x1621e38({:r=>#<RDF::URI:0x169f220 URI:http://www.ifi.uio.no/INF3580/simpson-collection#Collection>})>,

<RDF::Query::Solution:0x161ea94({:r=>#<RDF::URI:0x10b1e94 URI:http://www.ifi.uio.no/INF3580/simpson-collection#c1>})>,

<RDF::Query::Solution:0x161c3fc({:r=>#<RDF::URI:0x108f830 URI:http://www.ifi.uio.no/INF3580/simpson-collection#c2>})>,

<RDF::Query::Solution:0x161abec({:r=>#<RDF::URI:0x1087874 URI:http://www.ifi.uio.no/INF3580/simpson-collection#c3>})>,

<RDF::Query::Solution:0x16193a0({:r=>#<RDF::URI:0x1077e88 URI:http://www.ifi.uio.no/INF3580/simpson-collection#c4>})>,

<RDF::Query::Solution:0x1617d5c({:r=>#<RDF::URI:0x105d128 URI:http://www.w3.org/1999/02/22-rdf-syntax-ns#nil>})>]

However, variable ?f is nowhere to be found and the last solution should not be in the answer set. I also tried the same query on a virtuoso server and got the desired results: r f
http://www.ifi.uio.no/INF3580/simpson-collection#c3 http://www.ifi.uio.no/INF3580/simpsons#Maggie
http://www.ifi.uio.no/INF3580/simpson-collection#Collection http://www.ifi.uio.no/INF3580/simpsons#Homer
http://www.ifi.uio.no/INF3580/simpson-collection#c1 http://www.ifi.uio.no/INF3580/simpsons#Marge
http://www.ifi.uio.no/INF3580/simpson-collection#c2 http://www.ifi.uio.no/INF3580/simpsons#Bart
http://www.ifi.uio.no/INF3580/simpson-collection#c4 http://www.ifi.uio.no/INF3580/simpsons#Lisa

Did I missed something that would bind ?f ?.

gkellogg commented 7 years ago

It seems to be a parser bug. The generated SSE is the following:

(project
 (?r ?f)
 (path <http://www.ifi.uio.no/INF3580/simpson-collection#Collection>
  (path* <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest>) ?r ))

The ?f is missing here, so won't appear in a result set.

I've added some test cases for this and will look into it shortly.

gkellogg commented 7 years ago

@Simaris see if that does it using the GitHub repo and I'll push a gem update.

Simaris commented 7 years ago

That solved my problem. Thanks a lot!

white-gecko commented 7 years ago

@gkellogg would it be possible for you to push a pre-release with this feature to rubygems? Maybe you also want to automate pre-release publication with travis: https://docs.travis-ci.com/user/deployment/rubygems/

gkellogg commented 7 years ago

Released now, sorry for the delay.