ruby-rdf / sparql-client

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

Question about building query #38

Closed Soulou closed 6 years ago

Soulou commented 10 years ago

I'm looking to do this kind of SQPARQL query: (to http://dbpedia.org/sparql/ )

SELECT DISTINCT ?name ?country ?abstract ?coords
WHERE { ?city rdf:type dbpedia-owl:City ; 
              rdfs:label ?name ;
              dbpedia-owl:country ?country;
              dbpedia-owl:abstract ?abstract;
              grs:point ?coords
FILTER (regex(?name, 'Abilene, Texas') && langmatches(lang(?name), "EN") && langmatches(lang(?abstract), "EN"))

I've tried some way to write it with the select/where/filter methods, but without success. Is it possible it with them? Otherwise I'll stay on

sparql = SPARQL::Client.new("http://dbpedia.org/sparql")
results = sparql.query("QUERY")

Thank you

gkellogg commented 10 years ago

I think the best you can do with the DSL as is is the following:

patterns = [
        [:city, RDF.type, dbpo + "Place"],
        [:city, RDF::RDFS.label, :name],
        [:city, dbpo + "country", :country],
        [:city, dbpo + "abstract", :abstact],
        [:city, grs + "point", :coords]
]

sparql.ask.where(*patterns).
  filter("regex(?name, 'Abiline, Texas')").
  filter("langmatches(lang(?name), 'EN')").
  filter("langmatches(lang(?abstract), 'EN')")

Extending the DSL to allow constructing filters would be useful.