wiki:Archetypes
Last modified 3 years ago Last modified on 02/11/09 19:07:44

Redleaf Archetypes

Redleaf::Archetypes is a system for extending Ruby classes with properties defined by one or more RDF vocabularies.

I'm still working out the syntax and the semantics. Redoing the FOAF example from the RubyLayer page as Ruby classes extended with http://xmlns.com/foaf/0.1/Person:

require 'redleaf'
require 'redleaf/archetypes'

FOAF = Redleaf::Namespace.new( "http://xmlns.com/foaf/0.1/" )

class Person
    include Redleaf::Archetypes
    include_archetype FOAF[:Person]
end

graph = Redleaf::Graph.new
graph.load( "http://bigasterisk.com/foaf.rdf" )
graph.load( "http://www.w3.org/People/Berners-Lee/card.rdf" )
graph.load( "http://danbri.livejournal.com/data/foaf" ) 

# Either make Redleaf::Graph know how to find Archetype-extended classes...
people = graph.find_instances_of( Person )

# ...Or make Archetype-extended classes know how to instantiate themselves from a graph.
people = Person.find_instances_in( graph )

# Use the most-consanguine 'knows' property (http://xmlns.com/foaf/0.1/knows):
people.each do |person|
    person.knows.each do |other_person|
        puts "%s knows %s" % [ person.name, other_person.name ]
    end
end

# Explicitly use the 'http://xmlns.com/foaf/0.1/knows' predicate:
people.each do |person|
    person[ FOAF[:knows] ].each do |other_person|
        puts "%s knows %s" % [ person.name, other_person.name ]
    end
end