What?
A simple guide usable by any Ruby developer seeking to exploit SPARQL without hassles.
Why?
SPARQL is a powerful query language, results serialization format, and an HTTP based data access protocol from the W3C. It provides a mechanism for accessing and integrating data across Deductive Database Systems (colloquially referred to as triple or quad stores in Semantic Web and Linked Data circles) -- database systems (or data spaces) that manage proposition oriented records in 3-tuple (triples) or 4-tuple (quads) form.
How?
SPARQL queries are actually HTTP payloads (typically). Thus, using a RESTful client-server interaction pattern, you can dispatch calls to a SPARQL compliant data server and receive a payload for local processing e.g. local object binding re. Ruby.
Steps:
- From your command line execute: aptitude search '^ruby', to verify Ruby is in place
- Determine which SPARQL endpoint you want to access e.g. DBpedia or a local Virtuoso instance (typically: http://localhost:8890/sparql).
- If using Virtuoso, and you want to populate its quad store using SPARQL, assign "SPARQL_SPONGE" privileges to user "SPARQL" (this is basic control, more sophisticated WebID based ACLs are available for controlling SPARQL access).
Script:
#!/usr/bin/env ruby # # Demonstrating use of a single query to populate a # Virtuoso Quad Store. #require 'net/http'
require 'cgi'
require 'csv'#
# We opt for CSV based output since handling this format is straightforward in Ruby, by default.
# HTTP URL is constructed accordingly with CSV as query results format in mind.def sparqlQuery(query, baseURL, format="text/csv")
params={
"default-graph" => "",
"should-sponge" => "soft",
"query" => query,
"debug" => "on",
"timeout" => "",
"format" => format,
"save" => "display",
"fname" => ""
}
querypart=""
params.each { |k,v|
querypart+="#{k}=#{CGI.escape(v)}&"
}
sparqlURL=baseURL+"?#{querypart}"
response = Net::HTTP.get_response(URI.parse(sparqlURL))return CSV::parse(response.body)
end# Setting Data Source Name (DSN)
dsn="http://dbpedia.org/resource/DBpedia"
#Virtuoso pragmas for instructing SPARQL engine to perform an HTTP GET
#using the IRI in FROM clause as Data Source URLquery="DEFINE get:soft \"replace\"
SELECT DISTINCT * FROM <#{dsn}> WHERE {?s ?p ?o} "#Assume use of local installation of Virtuoso
#otherwise you can change URL to that of a public endpoint
#for example DBpedia: http://dbpedia.org/sparqldata=sparqlQuery(query, "http://localhost:8890/sparql/")
puts "Got data:"
p data#
# End
Output
Got data:
[["s", "p", "o"],
["http://dbpedia.org/resource/DBpedia",
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
"http://www.w3.org/2002/07/owl#Thing"],
["http://dbpedia.org/resource/DBpedia",
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
"http://dbpedia.org/ontology/Work"],
["http://dbpedia.org/resource/DBpedia",
"http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
"http://dbpedia.org/class/yago/Software106566077"],
...
Conclusion
CSV was chosen over XML (re. output format) since this is about a "no-brainer installation and utilization" guide for a Ruby developer that already knows how to use Ruby for HTTP based data access. SPARQL just provides an added bonus to URL dexterity (delivered via URI abstraction) with regards to constructing Data Source Names or Addresses.
Thanks for your tutorial
Posted by: ruby on rails | May 30, 2011 at 05:27 AM
Thanks for your tutorial.Great Job man...Keep it up
Posted by: iPhone Application | August 16, 2011 at 03:53 AM
Thanks! Great job.
Posted by: cesped artificial precios | November 21, 2011 at 12:13 PM
thanks so much!You are so talented continue doing great topics.
Posted by: Moncler Outlet | November 23, 2011 at 01:50 AM
It marked the sixth NCAA victory in program history for F&M.
F&M will face off against Middlebury College (14-3) on Saturday at 1 p.m. in a four-team regional hosted by Skidmore
Posted by: uggboots | December 03, 2011 at 02:56 AM
Invest in a scope that you can trust when you make adjustments.
Posted by: 1911 compensator | March 05, 2012 at 08:37 AM