Version: 0.0.1

The Node Type System

As mentioned in the Design Overview, Redleaf is intended to be used naturally from within Ruby, and to do so provides conversion of regular Ruby objects to equivalent nodes in RDF and vice-versa. This type system is extensible, meaning you can add translations for your own object classes. You just need to tell Redleaf how to convert an object of that type to its canonical representation as a typed literal and back again.

For instance, if you wanted to store IPAddr objects in the graph, and wanted them to map back to IPAddr instances when they came back into Ruby:

require 'redleaf'
require 'ipaddr'

# There's almost certainly an actual URI for IP addresses, but I 
# can't find it
IANA_NUMBERS = Redleaf::Namespace.new( 'http://www.iana.org/numbers/' )

# Make a canonical string representation for both ipv4 and ipv6 addresses
Redleaf::NodeUtils.register_new_class( IPAddr, IANA_NUMBERS[:ipaddr] ) do |addr|
	af = case addr.family
	     when Socket::AF_INET then "ipv4"
	     when Socket::AF_INET6 then "ipv6"
	     else "unknown" end
	ip = addr.to_string

	# Regretably the only way I could find to get the mask_addr as a string
	mask = addr.send( :_to_string, addr.instance_variable_get(:@mask_addr) )

	"%s:%s/%s" % [ af, ip, mask ]
end

Redleaf::NodeUtils.register_new_type( IANA_NUMBERS[:ipaddr] ) do |literal_string|
	IPAddr.new( literal_string[/.*:(.*)/, 1] )
end

addr = IPAddr.new( '192.168.16.96/27' )   # => 

# Make a tuple that contains the typed literal's type URI and its 
# canonical representation
literal = Redleaf::NodeUtils.make_object_typed_literal( addr )
literal # => 

# Now translate that tuple back to a Ruby object
literal_string, typeuri = *literal
Redleaf::NodeUtils.make_typed_literal_object( typeuri, literal_string )
# =>
Declare a custom mapping for IPAddr objects

The type-conversion system is still experimental. We’d welcome your input on ways to make it better, appropriate defaults, etc.