Redland Ruby binding
From GetSemantic
Redland is an library written in C to read and write RDF. It comes with a variety of bindings, including for the Ruby programming language. The following document is a guide to installing Redland, the Ruby bindings and then giving brief "Hello World"-esque examples of how to use Redland in Ruby.
Contents |
[edit] Installation
[edit] Redland install
- Download redland-1.0.6.tar.gz
- gunzip and tar xvf it into a temporary directory
- cd into temp directory
- ./configure
- make
- make check (optional)
- sudo make install
[edit] Ruby binding install
- Download redland-bidnings-1.0.6.1.tar.gz
- gunzip and tar xvf into a temporary directory
- cd into temp directory
- ./configure
- cd to the ruby directory
- make
- make check (optional)
- make install
Redland also comes with bindings for other languages - Python, Java, C#, Objective-C, Perl, PHP and Tcl. For each of these, you can do the same procedure, and hopefully they will be covered in future GetSemantic tutorials.
[edit] Testing Ruby binding install
Once installed, execute:
- ruby example.ruby file:../data/dc.rdf rdfxml
[edit] Usage
In the following examples, you'll need to:
require 'rdf/redland'
[edit] Creating a model
Creating a model is as simple as:
model = Redland::Model.new()
This model isn't terrifically useful, as you haven't got a store for the triples. You can use a number of different stores built-in to Redland, but we'll start with a memory store. If you build bigger applications, you'll probably want to use a FileStore or a relational database. We'll create our model with a MemoryStore:
model = Redland::Model.new(Redland::MemoryStore.new())
This creates a new Model, backed with a MemoryStore.
Relevant RDoc documentation: Redland::Model, Redland::MemoryStore
[edit] Creating a node
You can create nodes, which you can piece together into Statements. There are three node types available:
Node contains a Uri, which is a class with a URI inside (naturally). The BNode class can have an ID, but otherwise points to a Blank node. The Literal class takes a string and optionally a language code (like "en").
Let's create a Node:
tom_node = Redland::Node.new(Redland::Uri.new("http://tommorris.org/foaf#me"))
And another node:
foaf_name = Redland::Node.new(Redland::Uri.new("http://xmlns.com/foaf/0.1/name"))
Finally, let's make a Literal:
tom_name = Redland::Literal.new("Tom Morris")
[edit] Making a statement
Let's put our three nodes together into a statement:
tom_name_statement = Redland::Statement.new(tom_node, foaf_name, tom_name)
You can, of course, create the nodes directly in the Statement.new method, rather than assigning them their own objects.
Let's inspect our statement:
puts tom_name_statement.to_s()
This should return:
{[http://tommorris.org/foaf#me], [http://xmlns.com/foaf/0.1/name], "Tom Morris"}
=> nil
Now, let's add the statement to the model:
model.add_statements(tom_name_statement)
You can also use the add method on the Model to avoid having to create a separate Statement object. Condensing the code together, we get:
model.add(Redland::Uri.new("http://tommorris.org/foaf#me"), Redland::Uri.new("http://xmlns.com/foaf/0.1/name"), Redland::Literal.new("Tom Morris"))
See Redland::Statement RDoc
[edit] Serialize it out again
If you are just testing these commands interactively, you can see what the model contains using a Model method called dump_model. Thus:
model.dump_model()
should return:
[http://tommorris.org/foaf#me]:[http://xmlns.com/foaf/0.1/name]:Tom Morris
=> nil
But, what if we wish to get the data out as RDF/XML or another serialization format? The to_string method allows you to serialize:
puts model.to_string()
returns:
<?xml version="1.0" encoding="utf-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="http://tommorris.org/foaf#me">
<ns0:name xmlns:ns0="http://xmlns.com/foaf/0.1/">Tom Morris</ns0:name>
</rdf:Description>
</rdf:RDF>
=> nil
[edit] See Also
- Redland Ruby Bindings docs on librdf.org
Categories: Redland | Tutorials | Ruby

