Re: select.where clause

On Apr 12, 2012, at 2:40 PM, Basil Bimblebaum wrote:

> Hi there,
> 
> recently getting into ruby and rdf, I am now running queries with
> sparql-client and its pretty sweet.
> 
> Up to now, I just lazyly pass the query as a string to the client, but I
> what to define the select clause properly.
> 
> ##Starting from here (this works fine),
> client = SPARQL::Client.new(target)
> result = client.select.where(
> 	"SELECT distinct ?herp ?derp WHERE {
> 	?herp <http://www.w3.org/2000/01/rdf-schema#comment> ?derp.
> } LIMIT 5"
> )
> result.each do |asolution|
> asolution.each {|key, value| puts "#{ key} has value #{value}" }
> end
> ##
> 
> Starting from the documentation I tried some stuff like this but.. what
> am I doing wrong here?
> 
> s1 = RDF::Query::Variable.new(:herp)
> p1 = RDF::Query::Variable.new(:p1,
> "<http://www.w3.org/2000/01/rdf-schema#comment>")
> o1 = RDF::Query::Variable.new(:derp)
> result = client.select.where([:herp, :p1, :derp]).limit(5)

You don't need to explicitly declare the variables, but if you do, you need to pass the variables, not symbols. Symbols imply variables, and they are created automatically.

In the case of p1, if you want to bind as a URI, you'll need to create a URI as follows:

p1 = RDF::URI.new("http://www.w3.org/2000/01/rdf-schema#comment")

Or, you can just use the built-in vocabulary:

p1 = RDF::RDFS.comment

You can then construct a query as follows:

result = client.select.where([:herp, p1, :derp]).limit(5)

Or, just skip the p1 variable and use the URI directly:

result = client.select.where([:herp, RDF::RDFS.comment, :derp]).limit(5)

Gregg

> Regards
> Peter
> 
> 

Received on Monday, 16 April 2012 10:21:40 UTC