ruby-rdf / sparql-client

SPARQL client for Ruby.
http://rubygems.org/gems/sparql-client
The Unlicense
112 stars 58 forks source link

SPARQL Query => SPARQL::Client::Query API Interface #60

Closed ckristo closed 7 years ago

ckristo commented 9 years ago

Hi,

I want to demonstrate the functionality of sparql-clients querying API; therefore I want to translate the following query string into API calls:

%q(
    PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
    PREFIX dbpprop: <http://dbpedia.org/property/>
    SELECT ?country ?population
    WHERE {
        ?country a dbpedia-owl:Country .
        ?country dbpprop:populationCensus ?population .
        FILTER ( isNumeric(?population) && ?population > 50000000 )
    }
    ORDER BY DESC(?population)
    LIMIT 5
)

=>

# - define ad-hoc vocabs
DBP_ONT  = RDF::Vocabulary.new('http://dbpedia.org/ontology/')
DBP_PROP = RDF::Vocabulary.new('http://dbpedia.org/property/')

client = SPARQL::Client.new('http://dbpedia.org/sparql')

# - define SPARQL query
query = client.select(:country, :population)
  .where([ :country, RDF.type, DBP_ONT.Country ])
  .where([ :country, DBP_PROP.populationCensus, :population ])
  # ...

but I just don't know how to "translate" the FILTER and the ORDER BY DESC parts. Anyone done that and can support me here?

no-reply commented 9 years ago

I can get you pretty close:

client.select(:country, :population)
  .where([ :country, RDF.type, DBP_ONT.Country ])
  .where([ :country, DBP_PROP.populationCensus, :population ])
  .filter('isNumeric(?population) && ?population > 50000000 ')
  .order_by(:country)

To my knowledge, we have no DSL support for DESC or for constructing filters.

The filters seem like a fairly substantial work chunk, and maybe wouldn't present any usability over just expecting the user to input a filter string.

DESC/ASC might be more manageable, though.

ping: @gkellogg, @bendiken

gkellogg commented 9 years ago

DESCRIBE/ASC definitely easy to do. We also need some better uodate DSL.

ckristo commented 9 years ago

@gkellogg How can I specify the DESC part of an ORDER BY clause with SPARQL::Client::Query's .order_by method? I didn't get it, sorry..

@no-reply Thanks for your input / your help! :-)

EDIT: After looking at the relevant code in query.rb, I found out how to specify it; you can simply use a string instead of a symbol -- the complete example:

query = client.select(:country, :population)
  .where([ :country, RDF.type, DBP_ONT.Country ])
  .where([ :country, DBP_PROP.populationCensus, :population ])
  .filter('isNumeric(?population) && ?population > 50000000')
  .order_by('DESC(?population)')
  .limit(5)

It would be nice if there's a way to specify the DESC part as symbol though, e.g.

 query.order_by([ [:var1, :desc], [ :var2, :asc ], :var3, ... ]) # :var3 = [ :var, :asc ]

EDIT2: It seems that something similar was planned when looking at query_spec.rb

For filter(), I agree with @no-reply. Nevertheless, it would be really nice to have a DSL for the FILTER part as well (for a coherent API).

gkellogg commented 9 years ago

That's a feature that needs to be added. I'll get around to looking at it soon.

ckristo commented 9 years ago

Created a PR that adds support for specifying the sort order direction with .order() :: #61