This is a pure-Ruby library for working with Resource Description Framework (RDF) data in Active Record-based applications.
- 100% pure Ruby with minimal dependencies and no bloat.
- 100% free and unencumbered public domain software.
- Implements RDF::Repository interface as a concern for ActiveRecord::Base derivatives.
- Plays nice with others: compatible with both RDF.rb and Active Record query interfaces.
require 'active_record'
require 'acts_as_statement'
class RdfStatement < ActiveRecord::Base
acts_as_statement
end
acts_as_statement uses an indexed, four-column, relational database schema.
The MD5 check-sum of the empty string "d41d8cd98f00b204e9800998ecf8427e" is used as a substitute for NULL.
class CreateRdfStatements < ActiveRecord::Migration
def change
create_table :rdf_statements do |t|
t.timestamps
# hexdigests
t.string :s, :null => false, :limit => 32
t.string :p, :null => false, :limit => 32
t.string :o, :null => false, :limit => 32
t.string :c, :null => false, :limit => 32
# quad
t.text :subject
t.text :predicate
t.text :object
t.text :context
end
# triplestore indices
add_index :rdf_statements, :s
add_index :rdf_statements, :p
add_index :rdf_statements, :o
add_index :rdf_statements, [:s, :p]
add_index :rdf_statements, [:s, :o]
add_index :rdf_statements, [:p, :o]
add_index :rdf_statements, [:s, :p, :o]
# quadstore indices
add_index :rdf_statements, :c
add_index :rdf_statements, [:s, :c]
add_index :rdf_statements, [:p, :c]
add_index :rdf_statements, [:o, :c]
add_index :rdf_statements, [:s, :p, :c]
add_index :rdf_statements, [:s, :o, :c]
add_index :rdf_statements, [:p, :o, :c]
add_index :rdf_statements, [:s, :p, :o, :c], :unique => true
end
end
acts_as_statement provides 7x scoped relations:
- with_subject: all quads with the specified
subject
. - with_predicate: all quads with the specified
predicate
. - with_object: all quads with the specified
object
. - with_context: all quads with the specified
context
. - for_statement: all quads with the specified RDF::Statement.
- for_triple: all quads with the specified
[subject, predicate, object]
tuple. - for_quad: all quads with the specified
[subject, predicate, object, context]
tuple.
Arguments to "with_*" relations have the following semantics:
- RDF::URI: all quads with the specified URI.
- RDF::Literal: all quads with the specified literal.
true
: all quads with a non-NULL value for the column denoted by (*).false
: all quads with a NULL value for the column denoted by (*).nil
: all quads.
# find all quads with the specified subject and any context...
RdfStatement.with_context(nil).with_subject(RDF::URI.new('https://github.com/markborkum/acts_as_statement'))
acts_as_statement implements the RDF::Repository interface, so you can use any RDF.rb mechanism to query the quad-store.
query = RDF::Query.new({
RDF::URI.new('https://github.com/markborkum/acts_as_statement') => {
:predicate => :object,
},
})
solutions = query.execute(RdfStatement.repository)
solutions.each do |solution|
puts "predicate=#{solution[:predicate]} object=#{solution[:object]} (context=#{solution[:context]})"
end
TODO
TODO
To get a local working copy of the development repository, do:
% git clone git://github.com/markborkum/acts_as_statement.git
Alternatively, download the latest development version as a tarball as follows:
% wget https://github.com/markborkum/acts_as_statement/tarball/master
TODO
TODO
This is free and unencumbered public domain software. For more information, see http://unlicense.org/ or the accompanying {file:UNLICENSE} file.