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:
- JSON-LD (through JSON::LD)
- Microdata (through RDF::Microdata),
- N-Quads (through RDF.rb)
- N-Triples (through RDF.rb)
- Notation3 (through RDF::N3),
- RDF/JSON (through RDF::JSON),
- CSV (through RDF::Tabular),
- RDF/XML (through RDF::RDFXML and RDF::Raptor),
- RDFa (through RDF::RDFa and RDF::Raptor),
- TriG (through RDF::TriG),
- TriX (through RDF::TriX),
- Turtle (through RDF::Turtle and RDF::Raptor),
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
- Ruby SemWeb – Introduction to RDF.rb given at the Lotico Semantic Web meetup in San Francisco on 06 December 2011 by Gregg.
- Belin Lotico – Tutorial on RDF.rb given at the Lotico Semantic Web meetup in Berlin on 18 June 2015 by Arto.
- JSON-LD: JSON for Linking Data – Introduction to JSON-LD given at the Semantic Technologies Conference in San Francisco on 7 June 2012 by Gregg.
- Presenting RDF – Discussion of the technology behind the RDF Distiller and Structured Data Linter by Gregg.
- Tabular Data on the Web - Discussion of the CSV on the Web proposed standard given at Smart Data conference in San Jose on 18 August 2015 by Gregg.
- Linked Data with Ruby and RDF.rb – Workshop given at Hydra Connect 2015 on 21 September 2015 by Gregg.
- Maintaining RDF Vocabularies in Spreadsheets – DC-2017, WASHINGTON, D.C. by Gregg.