Debian Codegraph @

ruby/lib/rdf/redland/resource.rb

raw symbols SCIP
1 require 'rdf/redland'
2
3 module Redland
4
5 #include Redland
6
7 class Resource < Node
8 # The internal librdf_node
9 attr_reader :node
10
11 # The internal model this Resource is connected to
12 attr_reader :model
13
14 # Create a Redland::Resource. Arg can be a String, or a Redland::Uri
15 #
16 def initialize(arg=nil,model=nil,new_node=true)
17 @context = nil
18 case arg
19 when nil
20 @node = Redland.librdf_new_node_from_blank_identifier($world.world,nil)
21 when String
22 @node = Redland.librdf_new_node_from_uri_string($world.world,arg)
23 when Uri
24 @node = Redland.librdf_new_node_from_uri($world.world,arg.uri)
25 when Node
26 @node = Redland.librdf_new_node_from_node(arg.node)
27 when SWIG::TYPE_p_librdf_node_s
28
29 @node = new_node ? Redland.librdf_new_node_from_node(arg): arg
30 end
31 raise RedlandError.new("Node construction failed") if not @node
32 ObjectSpace.define_finalizer(self,Node.create_finalizer(@node))
33
34 model ||= Model.new
35 @model = model
36 end
37
38 # Answer model.find(self,p,nil) in the associated model. If there are several
39 # such statements, any one of them may be returned
40 def get_property(p)
41 @model.object(self,p)
42 end
43
44
45 # Answer model.find(self,pred,nil) in the associated model
46 # If no predicate or block given, returns and array of
47 # statements matching model.find(self,nil,nil)
48 # If block_given? yields the predicate and the object
49 # resource.get_properties() do |p,o|
50 # puts "<td>#{p}</td><td>#{o}</td>"
51 # end
52 #
53 # If pred given, answers model.find(self,pred,nil)
54 # if no block return an array of the object values
55 # nicknames = resource.get_properties(FOAF::NICK)
56 #
57 # if block_given yields the objects which satisfy the predicate
58 # puts "Nicknames are"
59 # resource.get_properties(FOAF_NICK){|o| puts o}
60 def get_properties(pred=nil)
61 statements = @model.find(self,pred,nil)
62 if pred
63 if block_given?
64 statements.each{|st| yield st.object}
65 else
66 statements.map{|st| st.object}
67 end
68 else
69 return statements if not block_given?
70 statements.each{ |st| yield st.predicate,st.object}
71 end
72 end
73
74 # Finds all objects of a given predicate. Takes an optional block,
75 # yielding each statement if given.
76 def object_of_predicate(pred = nil)
77 # statements = @model.find(nil,pred,self)
78 if pred
79 if block_given?
80 @model.subjects(pred,self){|sub| yield sub}
81 else
82 return @model.subjects(pred,self)
83 end
84 else
85 statements = @model.find(nil,nil,self)
86 return statements if not block_given?
87 statements.each { |st| yield st.subject,st.predicate}
88 end
89 end
90
91 # Adds a statement to the model with the subject as the
92 # resource and p as the predicate and o as the object
93 # same as Model.add(this,p,o)
94 def add_property(p,o,context=nil)
95 @model.add(self,p,o,context)
96 return self
97 end
98
99 # Removes the property from the model with the predicate
100 # pred and optional context
101 def delete_property(pred,context=nil)
102 @model.delete(self,pred,self.get_property(pred),context)
103 return self
104 end
105
106 # Removes all of the properties from the model with this
107 # resource as the subject
108 def delete_properties()
109 self.get_properties().each{|st| @model.delete_statement(st)}
110 end
111
112 # Determine if this resource is of a given type
113 def type?(type)
114 self.model.find(self,TYPE,type).size != 0
115 end
116
117 # Change the value of the given property
118 def update_property(prop,value,context=nil)
119 self.delete_property(prop)
120 self.add_property(prop,value,context)
121 return self
122 end
123
124 # Sets the RDF type of this Resource where RDF type is
125 # <http://www.w3.org/1999/02/22-rdf-syntax-ns#TYPE>
126 def type=(type,context=@context)
127 self.delete_property(TYPE)
128 self.add_property(TYPE,type,context)
129 return self
130 end
131
132 # returns the RDF type of this Resource where RDF type is
133 # <http://www.w3.org/1999/02/22-rdf-syntax-ns#TYPE>
134 def type
135 self.get_property(TYPE)
136 end
137
138 # Set the type of this Resource
139 def set_type(type,context=@context)
140 self.delete_property(TYPE)
141 self.add_property(TYPE,type,context)
142 return self
143 end
144
145 # Defines a context for this resource
146 def set_context(context)
147 @context = context
148 end
149
150 end
151 end
152