Rixiform Inc.

ActiveRDF and Redland - Up and Running

November 4th 2008

Conveniently the Redland libraries have been added to macports. To install them you will need to install both the Redland and Redland-Bindings library. In my case I installed them with the ruby option although it’s hard to tell if this is required given sparse documentation:

sudo port install redland  ruby
sudo port install redland-bindngs  ruby

Then to test that Redland is working it is recommended that you run the example.rb file. Since it’s good to have the source around anyway you can get it from the Redland subversion repository here:

svn export http://svn.librdf.org/repository/bindings/trunk/

To run the example and make sure things are working, cd into the ruby directory and enter this per the Redland Ruby Binding instructions :

ruby -I. -Ilib example.rb file:../data/dc.rdf rdfxml

You should get output like this:

found statement: {[http://www.dajobe.org/], [http://purl.org/dc/elements/1.1/description], "The generic home page of Dave Beckett."}
found statement: {[http://www.dajobe.org/], [http://purl.org/dc/elements/1.1/creator], "Dave Beckett"}
found statement: {[http://www.dajobe.org/], [http://purl.org/dc/elements/1.1/title], "Dave Beckett's Home Page"}
Parsing added 3 statements
Printing all statements
Statement: {[http://www.dajobe.org/], [http://purl.org/dc/elements/1.1/title], "Dave Beckett's Home Page"}
Statement: {[http://www.dajobe.org/], [http://purl.org/dc/elements/1.1/creator], "Dave Beckett"}
Statement: {[http://www.dajobe.org/], [http://purl.org/dc/elements/1.1/description], "The generic home page of Dave Beckett."}
Querying for dc:titles:
{
  0= [http://www.dajobe.org/]
  1= Dave Beckett's Home Page
}
Serialized query results to JSON as a string size 296 bytes
Writing model to test-out.rdf as rdf/xml
Serialized to ntriples as a string size 292 bytes
Done

Also you should have the test output in the test-out.rdf file. Something like this:

<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description rdf:about="http://www.dajobe.org/">
    <dc:title>Dave Beckett's Home Page</dc:title>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.dajobe.org/">
    <dc:creator>Dave Beckett</dc:creator>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.dajobe.org/">
    <dc:description>The generic home page of Dave Beckett.</dc:description>
  </rdf:Description>
</rdf:RDF>
</code></pre></p>

Cool!

Next, lets setup ActiveRDF. If something goes wrong in your install or you want more information, the ActiveRDF install instructions are here

Now, for ActiveRDF install these gems:

sudo gem install activerdf --include-dependencies -y
sudo gem install activerdf_sparql
sudo gem install activerdf_redland

Don’t forget the activerdf_redland gem or you will get the error: unknown adapter redland

Enter this code from the ActiveRDF getting started guide into a file called active_rdf_demo.rb:

require 'rubygems'
require 'active_rdf'
adapter = ConnectionPool.add_data_source :type => :redland
adapter.load "test_person_data.nt" 
Namespace.register :test, 'http://activerdf.org/test/'
ObjectManager.construct_classes

# we can access all RDF properties of a person as Ruby attributes:
eyal = TEST::Person.new 'http://activerdf.org/test/eyal'
puts eyal.age
puts eyal.eye
puts eyal.class

# lets create an instance of one of the classes, that were constructed in that 
# way armin will be of rdfs:type test:person
armin = TEST::Person.new 'http://armin-haller.com/#me'

# now lets search for something in the triple store
all_resources = RDFS::Resource.find_all

# print all the people, and their friends
all_people = TEST::Person.find_all
all_people.each do |person|
    puts "#{person} has #{person.eye} eyes" 
end

# find all people aged 27
almost_thirties = TEST::Person.find_by_age(27)

puts "the following people are almost thirty: #{almost_thirties}" 

Download the test data here and put it in the same folder as your active_rdf_demo.rb file.

Run active_rdf_demo.rb:

ruby active_rdf_demo.rb

You should get some output like this:

27
blue
TEST::Person
<http: /> has blue eyes
the following people are almost thirty: 

Congrats! ActiveRDF is now working with Redland using the Redland Ruby bindings.

Now if by chance you get this error next time you run script/server for a rails app:

/opt/local/lib/ruby/vendor_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require': no such file to load -- /opt/local/lib/ruby/gems/1.8/gems/activerdf-1.6.10/lib/activerdf/init.rb (MissingSourceFile)

This is because some versions of the ActiveRDF gem (e.g. 1.6.10) do not have an init.rb file created. This is a bug. Here’s how you can get around it (the directory to cd into will be the one from your error):

sudo su
cd /opt/local/lib/ruby/gems/1.8/gems/activerdf-1.6.10/lib/
mkdir activerdf
touch activerdf/init.rb