• Stars
    star
    382
  • Rank 112,241 (Top 3 %)
  • Language
    Ruby
  • License
    The Unlicense
  • Created almost 15 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

RDF.rb is a pure-Ruby library for working with Resource Description Framework (RDF) data.

RDF.rb: Linked Data for Ruby

This is a pure-Ruby library for working with Resource Description Framework (RDF) data.

Gem Version Build Status Coverage Status Gitter chat

Table of contents

  1. Features
  2. Differences between RDF 1.0 and RDF 1.1
  3. Tutorials
  4. Command Line
  5. Examples
  6. Reader/Writer convenience methods
  7. RDF* (RDFStar)
  8. Documentation
  9. Dependencies
  10. Installation
  11. Download
  12. Resources
  13. Mailing List
  14. Authors
  15. Contributors
  16. Contributing
  17. License

Features

  • 100% pure Ruby with minimal dependencies and no bloat.
  • Fully compatible with RDF 1.1 specifications.
  • 100% free and unencumbered public domain software.
  • Provides a clean, well-designed RDF object model and related APIs.
  • Supports parsing and serializing N-Triples and N-Quads out of the box, with more serialization format support available through add-on extensions.
  • Includes in-memory graph and repository implementations, with more storage adapter support available through add-on extensions.
  • 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 Ruby >= 2.4, Rubinius and JRuby 9.0+.
    • Note, changes in mapping hashes to keyword arguments for Ruby 2.7+ may require that arguments be passed more explicitly, especially when the first argument is a Hash and there are optional keyword arguments. In this case, Hash argument may need to be explicitly included within {} and the optional keyword arguments may need to be specified using **{} if there are no keyword arguments.
  • Performs auto-detection of input to select appropriate Reader class if one cannot be determined from file characteristics.
  • Provisional support for RDF*.

HTTP requests

RDF.rb uses Net::HTTP for retrieving HTTP and HTTPS resources. If the RestClient gem is included, that will be used instead to retrieve remote resources. Clients may also consider using RestClient Components to enable client-side caching of HTTP results using Rack::Cache or other Rack middleware.

See {RDF::Util::File} for configuring other mechanisms for retrieving resources.

Term caching and configuration

RDF.rb uses a weak-reference cache for storing internalized versions of URIs and Nodes. This is particularly useful for Nodes as two nodes are equivalent only if they're the same node.

By default, each cache can grow to an unlimited size, but this can be configured using {RDF.config}, for general limits, along with URI- or Node-specific limits.

For example, to limit the size of the URI intern cache only:

RDF.config.uri_cache_size = 10_000

The default for creating new caches without a specific initialization size can be set using:

RDF.config.cache_size = 100_000

Differences between RDF 1.0 and RDF 1.1

This version of RDF.rb is fully compatible with RDF 1.1, but it creates some marginal incompatibilities with RDF 1.0, as implemented in versions prior to the 1.1 release of RDF.rb:

  • Introduces {RDF::IRI}, as a synonym for {RDF::URI} either {RDF::IRI} or {RDF::URI} can be used interchangeably. Versions of RDF.rb prior to the 1.1 release were already compatible with IRIs. Internationalized Resource Identifiers (see [RFC3987][]) are a super-set of URIs (see [RFC3986][]) which allow for characters other than standard US-ASCII.
  • {RDF::URI} no longer uses the Addressable gem. As URIs typically don't need to be parsed, this provides a substantial performance improvement when enumerating or querying graphs and repositories.
  • {RDF::List} no longer emits a rdf:List type. However, it will now recognize any subjects that are {RDF::Node} instances as being list elements, as long as they have both rdf:first and rdf:rest predicates.
  • {RDF::Graph} adding a graph_name to a graph may only be done when the underlying storage model supports graph_names (the default {RDF::Repository} does). The notion of graph_name in RDF.rb is treated equivalently to Named Graphs within an RDF Dataset, and graphs on their own are not named.
  • {RDF::Graph}, {RDF::Statement} and {RDF::List} now include {RDF::Value}, and not {RDF::Resource}. Made it clear that using {RDF::Graph} does not mean that it may be used within an {RDF::Statement}, for this see {RDF::Term}.
  • {RDF::Statement} now is stricter about checking that all elements are valid when validating.
  • {RDF::NTriples::Writer} and {RDF::NQuads::Writer} now default to validate output, only allowing valid statements to be emitted. This may disabled by setting the :validate option to false.
  • {RDF::Dataset} is introduced as a class alias of {RDF::Repository}. This allows closer alignment to the RDF concept of Dataset.
  • The graph_name of a graph within a Dataset or Repository may be either an {RDF::IRI} or {RDF::Node}. Implementations of repositories may restrict this to being only {RDF::IRI}.
  • There are substantial and somewhat incompatible changes to {RDF::Literal}. In RDF 1.1, all literals are typed, including plain literals and language tagged literals. Internally, plain literals are given the xsd:string datatype and language tagged literals are given the rdf:langString datatype. Creating a plain literal, without a datatype or language, will automatically provide the xsd:string datatype; similar for language tagged literals. Note that most serialization formats will remove this datatype. Code which depends on a literal having the xsd:string datatype being different from a plain literal (formally, without a datatype) may break. However note that the #has\_datatype? will continue to return false for plain or language-tagged literals.
  • {RDF::Query#execute} now accepts a block and returns {RDF::Query::Solutions}. This allows enumerable.query(query) to behave like query.execute(enumerable) and either return an enumerable or yield each solution.
  • {RDF::Queryable#query} now returns {RDF::Query::Solutions} instead of an Enumerator if it's argument is an {RDF::Query}.
  • {RDF::Util::File.open_file} now performs redirects and manages base_uri based on W3C recommendations:
    • base_uri is set to the original URI if a status 303 is provided, otherwise any other redirect will set base_uri to the redirected location.
    • base_uri is set to the content of the Location header if status is success.
  • Additionally, {RDF::Util::File.open_file} sets the result encoding from charset if provided, defaulting to UTF-8. Other access methods include last_modified and content_type,
  • {RDF::StrictVocabulary} added with an easy way to keep vocabulary definitions up to date based on their OWL or RDFS definitions. Most vocabularies are now StrictVocabularies meaning that an attempt to resolve a particular term in that vocabulary will error if the term is not defined in the vocabulary.
  • New vocabulary definitions have been added for ICal, Media Annotations (MA), Facebook OpenGraph (OG), PROV, SKOS-XL (SKOSXL), Data Vocabulary (V), VCard, VOID, Powder-S (WDRS), and XHV.

Notably, {RDF::Queryable#query} and {RDF::Query#execute} are now completely symmetric; this allows an implementation of {RDF::Queryable} to optimize queries using implementation-specific logic, allowing for substantial performance improvements when executing BGP queries.

Tutorials

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.

  • count: Parse and RDF input and count the number of statements.
  • predicates: Returns unique objects from parsed input.
  • objects: Returns unique objects from parsed input.
  • serialize: Parse an RDF input and re-serializing to N-Triples or another available format using --output-format option.
  • subjects: Returns unique subjects from parsed input.

The serialize command can also be used to serialize as a vocabulary.

Different RDF gems will augment the rdf script with more capabilities, which may require specifying the appropriate --input-format option to revel.

Examples

require 'rdf'
include RDF

Writing RDF data using the N-Triples format

require 'rdf/ntriples'
graph = RDF::Graph.new << [:hello, RDF::RDFS.label, "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 'rdf/nquads'

graph = RDF::Graph.load("https://ruby-rdf.github.io/rdf/etc/doap.nq", format: :nquads)

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

require 'rdf/nquads'

RDF::NQuads::Reader.open("https://ruby-rdf.github.io/rdf/etc/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::RDFS.label, "Hello, world!", graph_name: RDF::URI("http://example/graph_name"))
  end
end

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

require 'rdf/nquads'

repo = RDF::Repository.new << RDF::Statement.new(:hello, RDF::RDFS.label, "Hello, world!", graph_name: RDF::URI("http://example/graph_name"))
File.open("hello.nq", "w") {|f| f << repo.dump(:nquads)}

Reader/Writer convenience methods

{RDF::Enumerable} implements to_{format} for each available instance of {RDF::Reader}. For example, if rdf/turtle is loaded, this allows the following:

graph = RDF::Graph.new << [:hello, RDF::RDFS.label, "Hello, world!"]
graph.to_ttl

Similarly, {RDF::Mutable} implements from_{format} for each available instance of {RDF::Writer}. For example:

graph = RDF::Graph.new
graph.from_ttl("[ a <http://www.w3.org/1999/02/22-rdf-syntax-ns#Resource>]")

Note that no prefixes are loaded automatically, however they can be provided as arguments:

graph.from_ttl("[ a rdf:Resource]", prefixes: {rdf: RDF.to_uri})

Querying RDF data using basic graph patterns (BGPs)

require 'rdf/ntriples'

graph = RDF::Graph.load("https://ruby-rdf.github.io/rdf/etc/doap.nt")
query = RDF::Query.new({
  person: {
    RDF.type  => FOAF.Person,
    FOAF.name => :name,
    FOAF.mbox => :email,
  }
}, **{})

query.execute(graph) do |solution|
  puts "name=#{solution.name} email=#{solution.email}"
end

The same query may also be run from the graph:

graph.query(query) do |solution|
  puts "name=#{solution.name} email=#{solution.email}"
end

In general, querying from using the queryable instance allows a specific implementation of queryable to perform query optimizations specific to the datastore on which it is based.

A separate SPARQL gem builds on basic BGP support to provide full support for SPARQL 1.1 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")

RDF* (RDFStar)

RDF.rb includes provisional support for RDF* with an N-Triples/N-Quads syntax extension that uses inline statements in the subject or object position.

Internally, an RDF::Statement is treated as another resource, along with RDF::URI and RDF::Node, which allows an RDF::Statement to have a #subject or #object which is also an RDF::Statement.

Note: This feature is subject to change or elimination as the standards process progresses.

Serializing a Graph containing embedded statements

require 'rdf/ntriples'
statement = RDF::Statement(RDF::URI('bob'), RDF::Vocab::FOAF.age, RDF::Literal(23))
graph = RDF::Graph.new << [statement, RDF::URI("ex:certainty"), RDF::Literal(0.9)]
graph.dump(:ntriples, validate: false)
# => '<<<bob> <http://xmlns.com/foaf/0.1/age> "23"^^<http://www.w3.org/2001/XMLSchema#integer>>> <ex:certainty> "0.9"^^<http://www.w3.org/2001/XMLSchema#double> .'

Reading a Graph containing embedded statements

By default, the N-Triples reader will reject a document containing a subject resource.

nt = '<<<bob> <http://xmlns.com/foaf/0.1/age> "23"^^<http://www.w3.org/2001/XMLSchema#integer>>> <ex:certainty> "0.9"^^<http://www.w3.org/2001/XMLSchema#double> .'
graph = RDF::Graph.new do |graph|
  RDF::NTriples::Reader.new(nt) {|reader| graph << reader}
end
# => RDF::ReaderError

Readers support a boolean valued rdfstar option.

graph = RDF::Graph.new do |graph|
  RDF::NTriples::Reader.new(nt, rdfstar: true) {|reader| graph << reader}
end
graph.count #=> 1

Documentation

https://ruby-rdf.github.io/rdf

RDF Object Model

  • {RDF::Value}
    • {RDF::Term}
      • {RDF::Literal}
        • {RDF::Literal::Boolean}
        • {RDF::Literal::Date}
        • {RDF::Literal::DateTime}
        • {RDF::Literal::Decimal}
        • {RDF::Literal::Double}
        • {RDF::Literal::Integer}
        • {RDF::Literal::Time}
        • RDF::XSD (extension)
      • {RDF::Resource}
        • {RDF::Node}
        • {RDF::URI}
    • {RDF::List}
    • {RDF::Graph}
    • {RDF::Statement}

RDF Serialization

  • {RDF::Format}
  • {RDF::Reader}
  • {RDF::Writer}

RDF Serialization Formats

The following is a partial list of RDF formats implemented either natively, or through the inclusion of other gems:

The meta-gem LinkedData includes many of these gems.

RDF Datatypes

RDF.rb only implements core datatypes from the RDF Datatype Map. Most other XSD and RDF datatype implementations can be find in the following:

  • {RDF::XSD}

Graph Isomorphism

Two graphs may be compared with each other to determine if they are isomorphic. As BNodes within two different graphs are no equal, graphs may not be directly compared. The RDF::Isomorphic gem may be used to determine if they make the same statements, aside from BNode identity (i.e., they each entail the other)

  • RDF::Isomorphic

RDF Storage

RDF Querying

  • {RDF::Query}
    • {RDF::Query::HashPatternNormalizer}
    • {RDF::Query::Pattern}
    • {RDF::Query::Solution}
    • {RDF::Query::Solutions}
    • {RDF::Query::Variable}
  • SPARQL (extension)

RDF Vocabularies

  • {RDF} - Resource Description Framework (RDF)
  • {RDF::OWL} - Web Ontology Language (OWL)
  • {RDF::RDFS} - RDF Schema (RDFS)
  • {RDF::RDFV} - RDF Vocabulary (RDFV)
  • {RDF::XSD} - XML Schema (XSD)

Dependencies

  • Ruby (>= 2.6)
  • [LinkHeader][] (>= 0.0.8)
  • Soft dependency on RestClient (>= 2.1)

Installation

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

% [sudo] gem install rdf             # Ruby 2.6+

Download

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

% git clone git://github.com/ruby-rdf/rdf.git

Alternatively, download the latest development version as a tarball as follows:

% wget https://github.com/ruby-rdf/rdf/tarball/master

Resources

Mailing List

Authors

Contributors

Contributing

This repository uses Git Flow to mange development and release activity. All submissions must be on a feature branch based on the develop branch to ease staging and integration.

  • 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. Before committing, run git diff --check to make sure of this.
  • 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 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, which you will be asked to agree to on the first commit to a repo within the organization. Note that the agreement applies to all repos in the Ruby RDF organization.

License

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

More Repositories

1

json-ld

Ruby JSON-LD reader/writer for RDF.rb
HTML
231
star
2

sparql-client

SPARQL client for Ruby.
Ruby
112
star
3

sparql

Ruby SPARQL library
Ruby
89
star
4

spira

Spira is a framework for viewing RDF data as model objects
Ruby
58
star
5

linkeddata

A metadistribution of RDF.rb including all parsing/serialization plugins.
Ruby
51
star
6

rdf-vocab

Common RDF Vocabularies
Ruby
50
star
7

rdf-rdfa

Ruby RDFa reader/writer for RDF.rb.
HTML
35
star
8

trinity

[UNMAINTAINED] A minimalistic Ruby web framework for publishing Linked Data.
JavaScript
34
star
9

rdf-microdata

Ruby Microdata parser for RDF.rb
Ruby
33
star
10

rdf-turtle

Turtle reader/writer for Ruby
Ruby
31
star
11

rdf-raptor

RDF.rb extension for parsing/serializing RDF/XML, Turtle and RDFa data using the Raptor RDF library.
Ruby
27
star
12

rack-linkeddata

Rack middleware for Linked Data content negotiation.
Ruby
25
star
13

rdf-n3

Ruby Notation-3 reader/writer for RDF.rb.
HTML
24
star
14

rdf-sesame

RDF.rb plugin for accessing Sesame 2.0-compatible repositories.
Ruby
23
star
15

rdf-reasoner

Creates triples for various entailment regimes of a vocabulary for enabling SPARQL 1.1 Entailment
Ruby
18
star
16

rdf-virtuoso

Ruby Virtuoso repository adapter for RDF.rb
Ruby
17
star
17

rdf-tabular

Tabular Data RDF Reader and JSON serializer
HTML
17
star
18

rdf-rdfxml

Ruby RDF/XML reader/writer for RDF.rb.
Ruby
17
star
19

rdf-json

RDF.rb plugin for parsing/serializing RDF/JSON data.
Ruby
16
star
20

rdfs

[OBSOLETE] A forward-chaining inference engine that implements the RDFS entailment rules.
Ruby
15
star
21

sinatra-linkeddata

Sinatra extension for Linked Data content negotiation.
Ruby
13
star
22

rdf-ldp

A suite of LDP software and middleware for RDF.rb & Rack
Ruby
13
star
23

shex

HTML
12
star
24

rdf-do

DataObjects-backed repositories for RDF.rb
Ruby
12
star
25

rdf-spec

RDF.rb plugin that provides RSpec matchers and shared examples for RDF objects.
Ruby
12
star
26

rdf-trig

TriG reader/writer for RDF.rb
Ruby
9
star
27

shacl

Implementation of Shapes Constraint Language (SHACL) for RDF.rb
Ruby
8
star
28

rdf-blazegraph

A Ruby RDF::Repository for Blazegraph
Ruby
7
star
29

json-ld-preloaded

A meta-release of the json-ld gem including preloaded vocabularies
Ruby
7
star
30

rdf-normalize

RDF Graph normalizer for Ruby.
Ruby
7
star
31

rdf-xsd

Extended XSD Datatypes for RDF.rb
Ruby
6
star
32

rack-sparql

Rack middleware providing a SPARQL 1.0-compatible HTTP endpoint.
Ruby
6
star
33

ld-patch

Ruby implementation of the W3C Linked Data Patch Format
HTML
6
star
34

rdf-talis

[OBSOLETE] Talis storage adapter for RDF.rb
Ruby
5
star
35

rdf-source

[UNMAINTAINED] Module to simplify creation of RDF from existing Ruby stuff
Ruby
5
star
36

rdf-aggregate-repo

An aggregate RDF::Repository supporting a subset of named graphs and zero or more named graphs mapped to the default graph.
Ruby
5
star
37

rdf-trix

RDF.rb plugin for parsing/serializing TriX data.
Ruby
4
star
38

rdf-benchmark

A benchmark framework for RDF.rb
Ruby
4
star
39

rdf-marmotta

RDF::Repository implementation for Marmotta
Ruby
4
star
40

rdf-isomorphic

Isomorphism mixin for RDF.rb
Ruby
4
star
41

rack-sesame

Rack middleware providing a Sesame 2.0-compatible HTTP endpoint.
Ruby
4
star
42

rdf-vcf

RDF.rb reader for Variant Call Format (VCF) files.
Ruby
3
star
43

rdf-rasqal

[OBSOLETE] RDF.rb plugin for SPARQL query evaluation using the Rasqal RDF Query Library.
Ruby
3
star
44

yaml-ld

Ruby YAML-LD reader/writer for RDF.rb
Ruby
3
star
45

ruby-rdf.github.com

Source repository for the ruby-rdf.github.com website.
JavaScript
2
star
46

rdf-ordered-repo

An implementation of RDF::Repository using native hash structures with insertion-order preserving behavior.
Ruby
2
star
47

rdf-bert

[OBSOLETE] RDF.rb plugin for proxying repository operations over BERT-RPC.
Ruby
2
star
48

sparql-service-description

SPARQL Service Description Gem for creating SPARQL service description documents for datasets.
Ruby
1
star
49

rdf-arq

[OBSOLETE] RDF.rb plugin for using the ARQ query engine for SPARQL.
Java
1
star