Linked Data for Ruby

This is the home of Ruby RDF. This project collects numerous gems supporting Linked Data and Semantic Web programming in Ruby.

The primary gem is RDF.rb, which contains the core algorithms and classes used for doing basic programming of [RDF][], including support for Repositories, Graphs, Statements, URIs, Literals, and BNodes.

The Ruby RDF account also collects numerous gems used for reading and writing different RDF formats. At present, this includes the following:

In addition to basic Query mechanisms

Web infrastructure

There are also storage adaptors for popular Triple-stores, Graph-stores SQL, and other NOSQL stores.

There is also a LinkedData gem, which combines a core set of these together.

Examples

require 'linkeddata'

Writing RDF data using the N-Triples format

require 'rdf/ntriples'
graph = RDF::Graph.new << [:hello, RDF::Vocab::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("https://ruby-rdf.github.io/rdf/etc/doap.nt")

or

RDF::Reader.open("https://ruby-rdf.github.io/rdf/etc/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'

repo = RDF::Repository.load("https://ruby-rdf.github.io/rdf-turtle/etc/doap.ttl")

A specific sub-type of Reader can also be invoked directly:

require 'rdf/turtle'

RDF::Reader.open("https://ruby-rdf.github.io/rdf-turtle/etc/doap.ttl") do |reader|
  reader.each_statement do |statement|
    puts statement.inspect
  end
end

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::NTriples::Writer.open("doap.nt") do |writer|
  writer << RDF::Reader.open("https://ruby-rdf.github.io/rdf/etc/doap.nt")
end

A specific sub-type of Writer can also be invoked directly:

require 'rdf/nquads'

graph.dump(:nquads)

Querying RDF data using basic graph patterns (BGPs)

require 'rdf/nquads'

repo = RDF::Repository.load("https://ruby-rdf.github.io/rdf/etc/doap.nq")
query = RDF::Query.new({
  :person => {
    RDF.type  => RDF::Vocab::FOAF.Person,
    RDF::Vocab::FOAF.name => :name,
    RDF::Vocab::FOAF.mbox => :email,
  }
})

query.execute(repo).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.1 queries and updates.

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")

Presentations on RDF.rb and related technologies