Class | Redland::Resource |
In: | rdf/redland/resource.rb |
Parent: | Node |
include Redland
model | [R] | The internal model this Resource is connected to |
node | [R] | The internal librdf_node |
Create a Redland::Resource. Arg can be a String, or a Redland::Uri
# File rdf/redland/resource.rb, line 16 def initialize(arg=nil,model=nil,new_node=true) @context = nil case arg when nil @node = Redland.librdf_new_node_from_blank_identifier($world.world,nil) when String @node = Redland.librdf_new_node_from_uri_string($world.world,arg) when Uri @node = Redland.librdf_new_node_from_uri($world.world,arg.uri) when Node @node = Redland.librdf_new_node_from_node(arg.node) when SWIG::TYPE_p_librdf_node_s @node = new_node ? Redland.librdf_new_node_from_node(arg): arg end raise RedlandError.new("Node construction failed") if not @node ObjectSpace.define_finalizer(self,Node.create_finalizer(@node)) model ||= Model.new @model = model end
Removes all of the properties from the model with this resource as the subject
# File rdf/redland/resource.rb, line 108 def delete_properties() self.get_properties().each{|st| @model.delete_statement(st)} end
Removes the property from the model with the predicate pred and optional context
# File rdf/redland/resource.rb, line 101 def delete_property(pred,context=nil) @model.delete(self,pred,self.get_property(pred),context) return self end
Answer model.find(self,pred,nil) in the associated model If no predicate or block given, returns and array of statements matching model.find(self,nil,nil) If block_given? yields the predicate and the object
resource.get_properties() do |p,o| puts "<td>#{p}</td><td>#{o}</td>" end
If pred given, answers model.find(self,pred,nil) if no block return an array of the object values
nicknames = resource.get_properties(FOAF::NICK)
if block_given yields the objects which satisfy the predicate
puts "Nicknames are" resource.get_properties(FOAF_NICK){|o| puts o}
# File rdf/redland/resource.rb, line 60 def get_properties(pred=nil) statements = @model.find(self,pred,nil) if pred if block_given? statements.each{|st| yield st.object} else statements.map{|st| st.object} end else return statements if not block_given? statements.each{ |st| yield st.predicate,st.object} end end
Answer model.find(self,p,nil) in the associated model. If there are several such statements, any one of them may be returned
# File rdf/redland/resource.rb, line 40 def get_property(p) @model.object(self,p) end
Finds all objects of a given predicate. Takes an optional block, yielding each statement if given.
# File rdf/redland/resource.rb, line 76 def object_of_predicate(pred = nil) # statements = @model.find(nil,pred,self) if pred if block_given? @model.subjects(pred,self){|sub| yield sub} else return @model.subjects(pred,self) end else statements = @model.find(nil,nil,self) return statements if not block_given? statements.each { |st| yield st.subject,st.predicate} end end
Defines a context for this resource
# File rdf/redland/resource.rb, line 146 def set_context(context) @context = context end
returns the RDF type of this Resource where RDF type is <www.w3.org/1999/02/22-rdf-syntax-ns#TYPE>
# File rdf/redland/resource.rb, line 134 def type self.get_property(TYPE) end
Sets the RDF type of this Resource where RDF type is <www.w3.org/1999/02/22-rdf-syntax-ns#TYPE>
# File rdf/redland/resource.rb, line 126 def type=(type,context=@context) self.delete_property(TYPE) self.add_property(TYPE,type,context) return self end
(C) Copyright 2004-2011 Dave Beckett, (C) Copyright 2004-2005 University of Bristol