Comparing
version 3 and
version 2 backh2. Full Code for the Examples
Just copy the code from one example to somefile.rb on your system, and run it with "ruby somefile.rb".
h2. Code for the Read-Only Example
require 'rubygems'
require 'active_rdf'
ConnectionPool.add_data_source :type => :sparql, :engine => :joseki, :url => "http://m3pe.org:8080/repositories/test-people", :results => :sparql_xml
# we need to register a short-hand notation for the namespace used in this test data
Namespace.register :test, 'http://activerdf.org/test/'
# after registering the namespace, we now tell the ObjectManager to search for all
# RDFS classes in the triple store, and construct corresponding Ruby classes,
# so that there is no difference between constructing standard Ruby objects, and
# Ruby objects, which are completly backed by the triple store
ObjectManager.construct_classes
# we can access all RDF properties of a person as Ruby attributes:
eyal = RDFS::Resource.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'
# we cannot change anything, since SPARQL endpoints have just read-only access
# 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}"
h2. Code for the Read-And-Write Example
require 'rubygems'
require 'active_rdf'
# open an empty, in-memory RDFLite triple store
adapter = ConnectionPool.add_data_source :type => :rdflite
# alternatively to use redland: adapter = ConnectionPool.add_data_source :type => :redland
# now load the test data file into the triple store
adapter.load "/tmp/test_person_data.nt"
# register the namespace, like above
Namespace.register(:test, 'http://activerdf.org/test/')
# construct the necessary Ruby Modules and Classes to use the Namespace
ObjectManager.construct_classes
# we want to access the properties of this URI
eyal = TEST::Person.new 'http://activerdf.org/test/eyal'
# now we can read the age associated with eyal
puts eyal.age
# prints "27"
# lets try to write a new age
eyal.age = 18
# if we now output the age again, there will be two values for it
# because there is no way to decide which one is the real age
# however, this behaviour might change in the future..
puts eyal.age
# prints "18,27"