Skip to content

msporny/ruby-sparql

 
 

Repository files navigation

SPARQL for RDF.rb

This is a Ruby implementation of SPARQL for RDF.rb.

Features

  • 100% free and unencumbered public domain software.
  • SPARQL 1.0 query parsing and execution
  • SPARQL results as XML, JSON or HTML.
  • SPARQL CONSTRUCT or DESCRIBE serialized based on Format, Extension of Mime Type using available RDF Writers (see Linked Data)
  • SPARQL Client for accessing remote SPARQL endpoints.
  • Rack and Sinatra middleware to perform HTTP content negotiation for result formats
  • Compatible with Ruby 1.9.x.
  • Compatible with older Ruby versions with the help of the Backports gem.
  • Supports Unicode query strings both on Ruby 1.8.x and 1.9.x.

Description

The {SPARQL} gem implements [SPARQL 1.0 Query] and provides Rack and Sinatra middleware to provide results using HTTP Content Negotiation.

  • {SPARQL::Grammar} implements a [SPARQL 1.0 Query] parser generating SPARQL S-Expressions (SSE).
  • {SPARQL::Algebra} executes SSE against Any RDF::Graph or RDF::Repository, including compliant RDF.rb repository adaptors such as RDF::DO and RDF::Mongo.
  • {Rack::SPARQL} and {Sinatra::SPARQL} provide middleware components to format results using an appropriate format based on HTTP content negotiation.

Rack::SPARQL is a superset of Rack::LinkedData to allow content negotiated results to be returned any RDF::Enumerable or RDF::Query::Solutions compatible results. You would typically return an instance of RDF::Graph, RDF::Repository or RDF::Query::Solutions from your Rack application, and let the Rack::SPARQL::ContentNegotiation middleware take care of serializing your response into whatever format the HTTP client requested and understands.

Sinatra::SPARQL is a thin Sinatra-specific wrapper around the {Rack::SPARQL} middleware, which implements SPARQL content negotiation for Rack applications.

The middleware queries RDF.rb for the MIME content types of known RDF serialization formats, so it will work with whatever serialization plugins that are currently available for RDF.rb. (At present, this includes support for N-Triples, N-Quads, Turtle, RDF/XML, RDF/JSON, JSON-LD, RDFa, TriG and TriX.)

Examples

require 'rubygems'
require 'sparql'

Executing a SPARQL query against a repository

queryable = RDF::Repository.load("etc/doap.ttl")
sse = SPARQL.parse("SELECT * WHERE { ?s ?p ?o }")
sse.execute(queryable)

Rendering solutions as JSON, XML or HTML

queryable = RDF::Repository.load("etc/doap.ttl")
solutions = SPARQL.execute("SELECT * WHERE { ?s ?p ?o }", queryable)
solutions.to_json #to_xml #to_html

Parsing a SPARQL query string to SSE

sse = SPARQL.parse("SELECT * WHERE { ?s ?p ?o }")
sse.to_sxp

Command line processing

sparql --default-graph etc/doap.ttl etc/from_default.rq
sparql -e "SELECT * FROM <etc/doap.ttl> WHERE { ?s ?p ?o }"

# Generate SPARQL Algebra Expression (SSE) format
sparql --to-sse etc/input.rq
sparql --to-sse -e "SELECT * WHERE { ?s ?p ?o }"

# Run query using SSE input
sparql --default-graph etc/doap.ttl --sse etc/input.sse
sparql --sse -e "(dataset (<etc/doap.ttl>) (bgp (triple ?s ?p ?o))))"

Adding SPARQL content negotiation to a Rails 3.x application

# config/application.rb
require 'rack/sparql'

class Application < Rails::Application
  config.middleware.use Rack::SPARQL::ContentNegotiation
end

Adding SPARQL content negotiation to a Rackup application

#!/usr/bin/env rackup
require 'rack/sparql'

repository = RDF::Repository.new do |graph|
  graph << [RDF::Node.new, RDF::DC.title, "Hello, world!"]
end
results = SPARQL.execute("SELECT * WHERE { ?s ?p ?o }", repository)

use Rack::SPARQL::ContentNegotiation
run lambda { |env| [200, {}, results] }

Adding SPARQL content negotiation to a classic Sinatra application

#!/usr/bin/env ruby -rubygems
require 'sinatra'
require 'sinatra/sparql'

repository = RDF::Repository.new do |graph|
  graph << [RDF::Node.new, RDF::DC.title, "Hello, world!"]
end

get '/sparql' do
  SPARQL.execute("SELECT * WHERE { ?s ?p ?o }", repository)
end

Documentation

Full documentation available on Rubydoc.info

Principle Classes

  • {SPARQL}
    • {SPARQL::Algebra}
      • {SPARQL::Algebra::Expression}
      • {SPARQL::Algebra::Query}
      • {SPARQL::Algebra::Operator}
    • {SPARQL::Grammar}
      • {SPARQL::Grammar::Parser}
      • {SPARQL::Grammar::Lexer}
  • {Sinatra::SPARQL}
  • {Rack::SPARQL}
    • {Rack::SPARQL::ContentNegotiation}

Dependencies

Installation

The recommended installation method is via RubyGems. To install the latest official release of the SPARQL gem, do:

% [sudo] gem install sparql

Download

To get a local working copy of the development repository, do:

% git clone git://github.com/gkellogg/sparql.git

Mailing List

Authors

Contributing

  • Do your best to adhere to the existing coding conventions and idioms.
  • Don't use hard tabs, and don't leave trailing whitespace on any line.
  • Do document every method you add using YARD annotations. Read the tutorial or just look at the existing code for examples.
  • Don't touch the .gemspec, VERSION or AUTHORS files. If you need to change them, do so on your private branch only.
  • Do feel free to add yourself to the CREDITS file and the corresponding list in the the README. Alphabetical order applies.
  • Do note that in order for us to merge any non-trivial changes (as a rule of thumb, additions larger than about 15 lines of code), we need an explicit public domain dedication on record from you.

License

This is free and unencumbered public domain software. For more information, see http://unlicense.org/ or the accompanying {file:UNLICENSE} file.

About

Integrated Ruby SPARQL library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%