[redland-dev] ruby bindings and typed literals
Benno Blumenthal
benno at iri.columbia.edu
Fri Feb 16 19:35:24 UTC 2007
Dave Beckett wrote:
>Benno Blumenthal wrote:
>
>
>>Hello All,
>>
>>I am interested in modifying ActiveRDF so that typed literals are
>>handled properly.
>>In the Redland Ruby bindings, there is a literal subclass of node which
>>has a lang and value properties.
>>And it is possible to define a literal node with the datatype property
>>set correctly.
>>
>>But I cannot see how to get the datatype property back out with the ruby
>>bindings.
>>
>>So what happens to numeric/date/XML literals in the ruby bindings? Are
>>they already converted to Ruby datatypes, or am I missing something?
>>
>>
>
>As far as I can tell, this method of the Node class does return the
>datatype URI since it's put into hash_uri and used
>
> # create a literal from another Node
> def Literal.from_node(node)
> lang = Redland.librdf_node_get_literal_value_language(node) if
>Redland.librdf_node_get_literal_value_language(node)
> str = Redland.librdf_node_get_literal_value(node)
> hash_uri = Redland.librdf_node_get_literal_value_datatype_uri(node)
> hash_uri = Uri.new(Redland.librdf_uri_to_string(hash_uri)) if hash_uri
> return Literal.new(str,lang,hash_uri)
> end
>
>Or if this isn't what you are talking about, the above code gives you
>the fragment you need to make such a method of the base Node class:
>(hand edited, not tested)
> def datatype(node)
> uri = Redland.librdf_node_get_literal_value_datatype_uri(self.node)
> uri = Uri.new(Redland.librdf_uri_to_string(uri)) if uri
> return uri
> end
>
>Dave
>
>
>
Sorry for taking so long to respond -- I am on a bit of a learning
curve. Thank you very much, the second alternative is what I need.
Let me take it back a step (though it is essentially the same issue) --
how is one supposed to get the value of a literal node in the Ruby Bindings?
I managed two ways, neither of them particularly graceful or documented
# node is the query result for one binding
node = query_results.binding_value(i)
# we determine the node type
if node.literal?
# for literal nodes we just return the value
#
node.to_s
# returns string^^type or string at lang or string for typed, language,
untyped
#
Redland::Literal.from_node(node.node).value # technique 1
always returns string
Redland.librdf_node_get_literal_value(node.node) # technique 2 always
returns string
There is both a Node class and and a librdf_node class, where the node
method of the Node class gets you the librdf_node object, and the
librdf_node object has a full set of Redland methods with redland
names. Which is a point of confusion to me, anyway.
Benno
--
Dr. M. Benno Blumenthal benno at iri.columbia.edu
International Research Institute for climate and society
The Earth Institute at Columbia University
Lamont Campus, Palisades NY 10964-8000 (845) 680-4450
More information about the redland-dev
mailing list