Class: Utilrb::RubyObjectGraph
- Defined in:
- lib/utilrb/ruby_object_graph.rb
Defined Under Namespace
Classes: ObjectRef
Instance Attribute Summary collapse
-
#graph ⇒ Object
readonly
Returns the value of attribute graph.
-
#references ⇒ Object
readonly
Returns the value of attribute references.
Class Method Summary collapse
Instance Method Summary collapse
- #add_reference(obj_ref, var_ref, desc) ⇒ Object
- #collapse(*klasses) ⇒ Object
-
#initialize ⇒ RubyObjectGraph
constructor
A new instance of RubyObjectGraph.
- #register_references_to(klass, orig_options = Hash.new) ⇒ Object
- #test_and_add_reference(obj_ref, var, desc) ⇒ Object
- #to_dot ⇒ Object
Constructor Details
#initialize ⇒ RubyObjectGraph
Returns a new instance of RubyObjectGraph.
29 30 31 32 |
# File 'lib/utilrb/ruby_object_graph.rb', line 29 def initialize @graph = BGL::Graph.new @references = Hash.new end |
Instance Attribute Details
#graph ⇒ Object (readonly)
Returns the value of attribute graph.
11 12 13 |
# File 'lib/utilrb/ruby_object_graph.rb', line 11 def graph @graph end |
#references ⇒ Object (readonly)
Returns the value of attribute references.
12 13 14 |
# File 'lib/utilrb/ruby_object_graph.rb', line 12 def references @references end |
Class Method Details
.snapshot ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/utilrb/ruby_object_graph.rb', line 14 def self.snapshot # Doing any Ruby code that do not modify the heap is impossible. List # all existing objects once, and only then do the processing. # +live_objects+ will be referenced in itself, but we are going to # remove it later GC.disable live_objects = [] ObjectSpace.each_object do |obj| if obj != live_objects live_objects << obj end end GC.enable end |
Instance Method Details
#add_reference(obj_ref, var_ref, desc) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/utilrb/ruby_object_graph.rb', line 49 def add_reference(obj_ref, var_ref, desc) if graph.linked?(obj_ref, var_ref) obj_ref[var_ref, graph] << desc else graph.link(obj_ref, var_ref, [desc].to_set) end end |
#collapse(*klasses) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/utilrb/ruby_object_graph.rb', line 149 def collapse(*klasses) vertices = graph.vertices.dup vertices.each do |v| case v.obj when *klasses next if v.root?(graph) || v.leaf?(graph) v.each_parent_vertex(graph) do |parent| all_parent_info = parent[v, graph] v.each_child_vertex(graph) do |child| all_child_info = v[child, graph] all_parent_info.each do |parent_info| all_child_info.each do |child_info| add_reference(parent, child, parent_info + "." + child_info) end end end end graph.remove(v) end end end |
#register_references_to(klass, orig_options = Hash.new) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/utilrb/ruby_object_graph.rb', line 57 def register_references_to(klass, = Hash.new) = Kernel. , :roots => nil roots = [:roots] live_objects = RubyObjectGraph.snapshot # Create a single ObjectRef per (interesting) live object, so that we # can use a BGL::Graph to represent the reference graph. This will be # what we are going to access later on. Use object IDs since we really # want to refer to objects and not use eql? comparisons desired_seeds = [] excludes = [live_objects, self, , , roots].to_value_set live_objects.delete_if do |obj| if excludes.include?(obj) true else references[obj.object_id] ||= ObjectRef.new(obj) if obj.kind_of?(klass) desired_seeds << obj.object_id end false end end desired_seeds.each do |obj_id| graph.insert(references[obj_id]) end ignored_enumeration = Hash.new loop do old_graph_size = graph.size live_objects.each do |obj| obj_ref = references[obj.object_id] test_and_add_reference(obj_ref, obj.class, "class") for var_name in obj.instance_variables var = obj.instance_variable_get(var_name) test_and_add_reference(obj_ref, var, var_name.to_s) end case obj when Array, ValueSet for var in obj test_and_add_reference(obj_ref, var, "ValueSet[]") end when BGL::Graph obj.each_vertex do test_and_add_reference(obj_ref, var, "Vertex[]") end obj.each_edge do |_, _, info| test_and_add_reference(obj_ref, info, "Edge[]") end when Hash for var in obj 2.times do |i| test_and_add_reference(obj_ref, var[i], "Hash[]") end end when Proc if obj.respond_to?(:references) for var in obj.references begin test_and_add_reference(obj_ref, ObjectSpace._id2ref(var), "Proc") rescue RangeError end end end else if obj.respond_to?(:each) if obj.kind_of?(Module) || obj.kind_of?(Class) if !ignored_enumeration[obj] ignored_enumeration[obj] = true puts "ignoring enumerator class/module #{obj}" end else if !ignored_enumeration[obj.class] ignored_enumeration[obj.class] = true puts "ignoring enumerator object #{obj.class}" end end end end end if old_graph_size == graph.size break end end return graph end |
#test_and_add_reference(obj_ref, var, desc) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/utilrb/ruby_object_graph.rb', line 42 def test_and_add_reference(obj_ref, var, desc) var_ref = references[var.object_id] if graph.include?(var_ref) add_reference(obj_ref, var_ref, desc) end end |
#to_dot ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/utilrb/ruby_object_graph.rb', line 172 def to_dot if !roots roots = graph.vertices.find_all do |v| v.root?(graph) end.to_value_set else roots = roots.to_value_set end io = StringIO.new("") all_seen = ValueSet.new io.puts "digraph {" roots.each do |obj_ref| seen = ValueSet.new graph.each_dfs(obj_ref, BGL::Graph::ALL) do |from_ref, to_ref, all_info, kind| info = all_info.to_a.join(",") if all_seen.include?(from_ref) graph.prune else from_id = from_ref.obj.object_id to_id = to_ref.obj.object_id io.puts "obj#{from_id} -> obj#{to_id} [label=\"#{info}\"]" end seen << from_ref << to_ref end seen.each do |obj_ref| if !all_seen.include?(obj_ref) obj = obj_ref.obj obj_id = obj_ref.obj.object_id str = if obj.respond_to?(:each) "#{obj.class}" else obj.to_s end color = if obj.kind_of?(Roby::EventGenerator) "cyan" elsif obj.kind_of?(Roby::Task) "blue" elsif obj.kind_of?(BGL::Graph) "magenta" elsif obj.kind_of?(Hash) || obj.kind_of?(Array) || obj.kind_of?(ValueSet) "green" else "black" end io.puts "obj#{obj_id} [label=\"#{str}\",color=#{color}];" end end all_seen.merge(seen) end io.puts "}" io.string end |