ActiveRDF + DBPedia + Ruby on Rails

This article show how to use ActiveRDF in Ruby on Rails to write queries for DBPedia. DBPedia is the semantic version of WikiPedia. Basically the whole knowledge in Wikipedia is available in DBPedia over RDF (Resource Definition Framework). There is a query language for RDF called SPARQL. With SPARQL and RDF you can write queries and fetch data from DBPedia/Wikipedia.

That is pretty awesome. You can see DBPedia/Wikipedia as a big Database or Knowledge base.

ActiveRDF is a Ruby GEM to use RDF in Ruby. To use it in Rails you have to add this two lines to your Gemfile

gem 'activerdf'
gem 'activerdf_sparql'

In you application.rb file you have to add this line pretty much in the top.

require 'active_rdf'

In the same file, in the class part you have to configure the connection pool for RDF.

module TrophicWeb
  class Application < Rails::Application

  adapter = ConnectionPool.add_data_source(:type => :sparql, :results => :sparql_xml, :engine => :virtuoso, :url ="")
  adapter.enabled = true
  Namespace.register(:dbpedia, "")

That’s it for the initialization. Now you can use it in your code. Here is an example how to fetch a species and the corresponding thumbnail uri.

species ="")
thumbnail = species.dbpedia::thumbnail.uri

Thats it.

Published by Robert Reiz

CEO @ VersionEye. Passionated software developer since 1998.

3 thoughts on “ActiveRDF + DBPedia + Ruby on Rails

  1. Hi,
    Any Idea why I’m getting this error?
    require’: /home/miki/.rvm/gems/ruby-1.9.2-p320/gems/activerdf-1.6.11/lib/active_rdf/objectmanager/namespace.rb:63: syntax error, unexpected ‘:’, expecting keyword_then or ‘,’ or ‘;’ or ‘\n’ (SyntaxError)
    when RDFS::Resource: resource.uri

    1. That looks like you have a Syntax error. Maybe the requested resource don’t has an uri. But I am not sure. I don’t know the context of your application.

    2. Now I have the same problem.
      With JRuby 1.9.X it works fine. But with CRuby 1.9.2 I am getting the same error. I didn’t found the root, yet.
      But with CRuby 1.8.X it works again fine.
      Will do some research the next days.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: