AllegroGraph on Rails: Preliminary Tutorial

This project is still not ready for production use. But if you'd like to check it out of Subversion and take a look, here's how.

We eventually hope to simplify many of these steps.

1. Install AllegroGraph

Download the AllegroGraph Free Server Edition, and install it normally.

Find the entry for "Start AllegroGraph Server Edition" in your start menu, right click on it, and select "Properties".

Add the arguments -http 8111 to the command line, click "OK", and start the server.

2. Install ActiveRDF and Ruby on Rails

Install Ruby and Ruby on Rails in the usual fashion. On some platforms, you may also need to install RubyGems.

Next, install ActiveRDF and some support libraries:


gem install activerdf activerdf_sparql builder uuidtools

Depending on your operating system, you may need to use sudo gem install.

The AllegroGraph adapter is pre-release software, but you can get it from Subversion:


svn co http://activerdfagraph.rubyforge.org/svn/trunk/ agraph

cd agraph

rake package

gem install pkg/activerdf_agraph-*.gem

3. Create a repository

You can create a new RDF repository using the AllegroGraph server interface:


$ irb

> server = AllegroGraph::Server.new('http://localhost:8111/sesame')

> repo = server.new_repository("example")

To use the new repository with ActiveRDF, we also need to define any classes we plan to use. In this example, we create a class to represent a guestbook entry:


> repo.define_class('http://activerdfagraph.rubyforge.org/guestbook/Entry')

(This particular interface will be mostly likely be replaced by something better in the future.)

4. Build a guestbook application

Create a new Rails application:


rails guestbook

cd guestbook

Since we'll be using ActiveRDF in place of ActiveRecord, we also need to edit the config/environment.rb file. We begin by disabling ActiveRecord (look for the config.frameworks line near the top of the file):


config.frameworks -= [ :active_record ]

Then, down at the bottom of the file, add some code to set up ActiveRDF:


require 'activerdf_agraph'



$repository_uri = 'http://localhost:8111/sesame/repositories/example'

$guestbook_uri = 'http://activerdfagraph.rubyforge.org/guestbook/'



ConnectionPool.add_data_source(:type => :agraph, :url => $repository_uri)

Namespace.register :guest, $guestbook_uri

ObjectManager.construct_classes

Create a file app/controllers/guest_book_controller.rb:


class GuestBookController < ApplicationController

  def index

    @new_entry = GUEST::Entry.new(unique_uri)

    @entries = GUEST::Entry.find_all

  end



  def sign

    @entry = GUEST::Entry.new(unique_uri)

    @entry.save

    @entry.guest::user_name = @params['new_entry']['user_name']

    @entry.guest::comments = @params['new_entry']['comments']

    redirect_to home_url

  end



  private



  def unique_uri

    "#{$guestbook_uri}thing/#{UUID.timestamp_create().to_s}"

  end

end

And add a file app/views/guest_book/index.rhtml:


<html>

  <head>

    <title>Guest Book</title>

  </head>

  <body>

    <h1>Guest Book</h1>



    <% form_for :new_entry, @new_entry, :url => { :action => 'sign' } do |f| %>

      Name: <%= f.text_field :user_name %><br />

      Comments:<br />

      <%= f.text_area :comments, :rows => 5 %><br />

      <input type="submit" value="Sign">

    <% end %>



    <h2>Signatures</h2>



    <% for entry in @entries %>

      <p><b><%=h entry.user_name %></b>: <%=h entry.comments %></p>

    <% end %>

  </body>

</html>

Copyright © 2023 Franz Inc., All Rights Reserved | Privacy Statement Twitter