RDF.rb: Linked Data for Ruby
This is a pure-Ruby library for working with [Resource Description Framework (RDF)] data.
Features
-
100% pure Ruby with minimal dependencies and no bloat.
-
100% free and unencumbered [public domain](unlicense.org/) software.
-
Provides a clean, well-designed RDF object model and related APIs.
-
Supports parsing and serializing N-Triples out of the box, with more serialization format support available through add-on plugins.
-
Includes in-memory graph and repository implementations, with more storage adapter support available through add-on plugins.
-
Implements basic graph pattern (BGP) query evaluation.
-
Plays nice with others: entirely contained in the ‘RDF` module, and does not modify any of Ruby’s core classes or standard library.
-
Based entirely on Ruby’s autoloading, meaning that you can generally make use of any one part of the library without needing to load up the rest.
-
Compatible with Ruby 1.8.7+, Ruby 1.9.x, and JRuby 1.4/1.5.
-
Compatible with older Ruby versions with the help of the [Backports][] gem.
-
Performs auto-detection of input to select appropriate Reader class if one cannot be determined from file characteristics.
Tutorials
-
[Getting data from the Semantic Web using Ruby and RDF.rb](semanticweb.org/wiki/Getting_data_from_the_Semantic_Web_%28Ruby%29)
-
[Using RDF.rb and Spira to process RDF data from the British Ordnance Survey](stephenpope.co.uk/?p=85)
-
[Getting started with RDF and SPARQL using 4store and RDF.rb](www.jenitennison.com/blog/node/152)
Command Line
When installed, RDF.rb includes a ‘rdf` shell script which acts as a wrapper to perform a number of different operations on RDF files using available readers and writers.
-
‘serialize`: Parse an RDF input and re-serializing to N-Triples or another available format using `–output-format` option.
-
‘count`: Parse and RDF input and count the number of statements.
-
‘subjects`: Returns unique subjects from parsed input.
-
‘objects`: Returns unique objects from parsed input.
-
‘predicates`: Returns unique objects from parsed input.
Examples
require 'rdf'
include RDF
### Writing RDF data using the N-Triples format
require 'rdf/ntriples'
graph = RDF::Graph.new << [:hello, RDF::DC.title, "Hello, world!"]
graph.dump(:ntriples)
or
RDF::Writer.open("hello.nt") { |writer| writer << graph }
### Reading RDF data in the N-Triples format
require 'rdf/ntriples'
graph = RDF::Graph.load("http://rdf.rubyforge.org/doap.nt")
or
RDF::Reader.open("http://rdf.rubyforge.org/doap.nt") do |reader|
reader.each_statement do |statement|
puts statement.inspect
end
end
### Reading RDF data in other formats RDF::Reader.open and RDF::Repository.load use a number of mechanisms to determine the appropriate reader to use when loading a file. The specific format to use can be forced using, e.g. ‘:format => :ntriples` option where the specific format symbol is determined by the available readers. Both also use MimeType or file extension, where available.
require 'linkeddata'
graph = RDF::Graph.load("etc/doap.nq", :format => :nquads)
A specific sub-type of Reader can also be invoked directly:
require 'rdf/nquads'
RDF::NQuads::Reader.open("http://rdf.rubyforge.org/doap.nq") do |reader|
reader.each_statement do |statement|
puts statement.inspect
end
end
Reader/Writer implementations may override RDF::Format.detect, which takes a small sample if input and return a boolean indicating if it matches that specific format. In the case that a format cannot be detected from filename or other options, or that more than one format is identified, RDF::Format.for will query each loaded format by invoking it’s ‘detect` method, and the first successful match will be used to read the input.
### Writing RDF data using other formats RDF::Writer.open, RDF::Enumerable#dump, RDF::Writer.dump take similar options to RDF::Reader.open to determine the appropriate writer to use.
require 'linkeddata'
RDF::Writer.open("hello.nq", :format => :nquads) do |writer|
writer << RDF::Repository.new do |repo|
repo << RDF::Statement.new(:hello, RDF::DC.title, "Hello, world!", :context => RDF::URI("context"))
end
end
A specific sub-type of Writer can also be invoked directly:
graph.dump(:nq)
### Querying RDF data using basic graph patterns (BGPs)
require 'rdf/ntriples'
graph = RDF::Graph.load("http://rdf.rubyforge.org/doap.nt")
query = RDF::Query.new({
:person => {
RDF.type => FOAF.Person,
FOAF.name => :name,
FOAF.mbox => :email,
}
})
query.execute(graph).each do |solution|
puts "name=#{solution.name} email=#{solution.email}"
end
A separate [SPARQL][SPARQL doc] gem builds on basic BGP support to provide full support for [SPARQL 1.0](www.w3.org/TR/rdf-sparql-query/) queries.
### Using pre-defined RDF vocabularies
DC.title #=> RDF::URI("http://purl.org/dc/terms/title")
FOAF.knows #=> RDF::URI("http://xmlns.com/foaf/0.1/knows")
RDF.type #=> RDF::URI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
RDFS.seeAlso #=> RDF::URI("http://www.w3.org/2000/01/rdf-schema#seeAlso")
RSS.title #=> RDF::URI("http://purl.org/rss/1.0/title")
OWL.sameAs #=> RDF::URI("http://www.w3.org/2002/07/owl#sameAs")
XSD.dateTime #=> RDF::URI("http://www.w3.org/2001/XMLSchema#dateTime")
### Using ad-hoc RDF vocabularies
foaf = RDF::Vocabulary.new("http://xmlns.com/foaf/0.1/")
foaf.knows #=> RDF::URI("http://xmlns.com/foaf/0.1/knows")
foaf[:name] #=> RDF::URI("http://xmlns.com/foaf/0.1/name")
foaf['mbox'] #=> RDF::URI("http://xmlns.com/foaf/0.1/mbox")
Documentation
### RDF Object Model
<blog.datagraph.org/2010/03/rdf-for-ruby>
### RDF Serialization
<blog.datagraph.org/2010/04/parsing-rdf-with-ruby>
### RDF Serialization Formats
The following is a partial list of RDF formats implemented either natively, or through the inclusion of other gems:
- JSON::LD][JSONLD doc
-
(plugin)
-
[RDF::JSON](rdf.rubyforge.org/json/) (plugin)
- RDF::Microdata][Microdata doc
-
(plugin)
- RDF::N3][N3 doc
-
(plugin)
-
[RDF::Raptor::RDFXML](rdf.rubyforge.org/raptor/) (plugin)
-
[RDF::Raptor::Turtle](rdf.rubyforge.org/raptor/) (plugin)
- RDF::RDFa][RDFa doc
-
(plugin)
- RDF::RDFXML][RDFXML doc
-
(plugin)
-
[RDF::Trix](rdf.rubyforge.org/trix/) (plugin)
- RDF::Turtle][Turtle doc
-
(plugin)
The meta-gem [LinkedData][LinkedData doc] includes many of these gems.
### RDF Storage
<blog.datagraph.org/2010/04/rdf-repository-howto>
-
[RDF::AllegroGraph](rubydoc.info/github/emk/rdf-agraph/master/frames) (plugin)
-
[RDF::Mongo](rubydoc.info/github/pius/rdf-mongo/master/frames) (plugin)
-
[RDF::DataObjects](rdf.rubyforge.org/do/) (plugin)
-
[RDF::Sesame](rdf.rubyforge.org/sesame/) (plugin)
### RDF Querying
-
[SPARQL](rubydoc.info/github/gkellogg/sparql/frames) (plugin)
### RDF Vocabularies
-
RDF - Resource Description Framework (RDF)
-
RDF::CC - Creative Commons (CC)
-
RDF::CERT - W3 Authentication Certificate (CERT)
-
RDF::DC - Dublin Core (DC)
-
RDF::DC11 - Dublin Core 1.1 (DC11) deprecated
-
RDF::DOAP - Description of a Project (DOAP)
-
RDF::EXIF - Exchangeable Image File Format (EXIF)
-
RDF::FOAF - Friend of a Friend (FOAF)
-
RDF::GEO - WGS84 Geo Positioning (GEO)
-
RDF::HTTP - Hypertext Transfer Protocol (HTTP)
-
RDF::OWL - Web Ontology Language (OWL)
-
RDF::RDFS - RDF Schema (RDFS)
-
RDF::RSA - W3 RSA Keys (RSA)
-
RDF::RSS - RDF Site Summary (RSS)
-
RDF::SIOC - Semantically-Interlinked Online Communities (SIOC)
-
RDF::SKOS - Simple Knowledge Organization System (SKOS)
-
RDF::WOT - Web of Trust (WOT)
-
RDF::XHTML - Extensible HyperText Markup Language (XHTML)
-
RDF::XSD - XML Schema (XSD)
Dependencies
-
[Ruby](ruby-lang.org/) (>= 1.8.7) or (>= 1.8.1 with [Backports][])
-
[Addressable](rubygems.org/gems/addressable) (>= 2.2.0)
Installation
The recommended installation method is via [RubyGems](rubygems.org/). To install the latest official release of RDF.rb, do:
% [sudo] gem install rdf # Ruby 1.8.7+ or 1.9.x
% [sudo] gem install backports rdf # Ruby 1.8.1+
Download
To get a local working copy of the development repository, do:
% git clone git://github.com/bendiken/rdf.git
Alternatively, download the latest development version as a tarball as follows:
% wget http://github.com/bendiken/rdf/tarball/master
Resources
Mailing List
Authors
-
[Arto Bendiken](github.com/bendiken) - <ar.to/>
-
[Ben Lavender](github.com/bhuga) - <bhuga.net/>
-
[Gregg Kellogg](github.com/gkellogg) - <kellogg-assoc.com/>
Contributors
-
[Călin Ardelean](github.com/clnx) - <github.com/clnx>
-
[Danny Gagne](github.com/danny) - <www.dannygagne.com/>
-
[Joey Geiger](github.com/jgeiger) - <github.com/jgeiger>
-
[Fumihiro Kato](github.com/fumi) - <fumi.me/>
-
[Naoki Kawamukai](github.com/kna) - <github.com/kna>
-
[Hellekin O. Wolf](github.com/hellekin) - <hellekin.cepheide.org/>
-
[John Fieber](github.com/jfieber) - <github.com/jfieber>
-
[Keita Urashima](github.com/ursm) - <ursm.jp/>
-
[Pius Uzamere](github.com/pius) - <pius.me/>
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][YARD-GS
-
or just look at the existing code for examples.
-
Don’t touch the ‘.gemspec` or `VERSION` 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.
-
Don’t touch the ‘AUTHORS` file. If your contributions are significant enough, be assured we will eventually add you in there.
-
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 <unlicense.org/> or the accompanying UNLICENSE file.
[RDF]: www.w3.org/RDF/ [YARD]: yardoc.org/ [YARD-GS]: rubydoc.info/docs/yard/file/docs/GettingStarted.md [PDD]: lists.w3.org/Archives/Public/public-rdf-ruby/2010May/0013.html [Backports]: rubygems.org/gems/backports [JSONLD doc]: rubydoc.info/github/gkellogg/json-ld/frames [LinkedData doc]: rubydoc.info/github/datagraph/linkeddata/master/frames [Microdata doc]: rubydoc.info/github/gkellogg/rdf-microdata/frames [N3 doc]: rubydoc.info/github/gkellogg/rdf-n3/master/frames [RDFa doc]: rubydoc.info/github/gkellogg/rdf-rdfa/master/frames [RDFXML doc]: rubydoc.info/github/gkellogg/rdf-rdfxml/master/frames [Turtle doc]: rubydoc.info/github/gkellogg/rdf-turtle/master/frames [SPARQL doc]: rubydoc.info/github/gkellogg/sparql/frames [SPARQL 1.0]: www.w3.org/TR/rdf-sparql-query/