Class: Roby::Log::Layout

Inherits:
Object show all
Defined in:
lib/roby/log/dot.rb

Constant Summary collapse

@@bkpindex =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bounding_rectsObject (readonly)

Returns the value of attribute bounding_rects.



273
274
275
# File 'lib/roby/log/dot.rb', line 273

def bounding_rects
  @bounding_rects
end

#displayObject (readonly)

Returns the value of attribute display.



273
274
275
# File 'lib/roby/log/dot.rb', line 273

def display
  @display
end

#dot_inputObject (readonly)

Returns the value of attribute dot_input.



179
180
181
# File 'lib/roby/log/dot.rb', line 179

def dot_input
  @dot_input
end

#object_posObject (readonly)

Returns the value of attribute object_pos.



273
274
275
# File 'lib/roby/log/dot.rb', line 273

def object_pos
  @object_pos
end

#planObject (readonly)

Returns the value of attribute plan.



273
274
275
# File 'lib/roby/log/dot.rb', line 273

def plan
  @plan
end

Instance Method Details

#<<(string) ⇒ Object



181
# File 'lib/roby/log/dot.rb', line 181

def <<(string); dot_input << string end

#applyObject



274
275
276
# File 'lib/roby/log/dot.rb', line 274

def apply
    plan.apply_layout(bounding_rects, object_pos, display)
end

#layout(display, plan) ⇒ Object



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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/roby/log/dot.rb', line 182

def layout(display, plan)
    @@index ||= 0
    @@index += 1

    # Dot input file
    @dot_input  = Tempfile.new("roby_dot")
    # Dot output file
    dot_output = Tempfile.new("roby_layout")

    dot_input << "digraph relations {\n"
    display.layout_options.each do |k, v|
 dot_input << "  #{k}=#{v};\n"
    end
    plan.to_dot(display, self, 0)

    # Take the signalling into account for the layout
    display.propagated_events.each do |_, sources, to, _|
 sources.each do |from|
      from_id, to_id = from.dot_id, to.dot_id
      if from_id && to_id
  dot_input << "  #{from.dot_id} -> #{to.dot_id}\n"
      end
 end
    end

    dot_input << "\n};"
    dot_input.flush

    # Make sure the GUI keeps being updated while dot is processing
    FileUtils.cp dot_input.path, "/tmp/dot-input-#{@@index}.dot"
    system("#{display.layout_method} #{dot_input.path} > #{dot_output.path}")
    #pid = fork do
    #    exec("#{display.layout_method} #{dot_input.path} > #{dot_output.path}")
    #end
    #while !Process.waitpid(pid, Process::WNOHANG)
    #    if Qt::Application.has_pending_events
    # Qt::Application.process_events
    #    else
    # sleep(0.05)
    #    end
    #end
    FileUtils.cp dot_output.path, "/tmp/dot-output-#{@@index}.dot"

    # Load only task bounding boxes from dot, update arrows later
    current_graph_id = nil
    bounding_rects = Hash.new
    object_pos     = Hash.new
    lines = File.open(dot_output.path) { |io| io.readlines  }
    full_line = ""
    lines.each do |line|
 line.chomp!
 full_line << line
 if line[-1] == ?\\
      full_line.chop!
      next
 end

 case full_line
 when /((?:\w+_)+\d+) \[.*pos="(\d+),(\d+)"/
      object_pos[$1] = Qt::PointF.new(Integer($2), Integer($3))
 when /subgraph cluster_(plan_\d+)/
      current_graph_id = $1
 when /graph \[bb="(\d+),(\d+),(\d+),(\d+)"\]/
      bb = [$1, $2, $3, $4].map do |c|
  c = Integer(c)
      end
      bounding_rects[current_graph_id] = [bb[0], bb[1], bb[2] - bb[0], bb[3] - bb[1]]
 end
 full_line = ""
    end

    graph_bb = bounding_rects.delete(nil)
    bounding_rects.each_value do |coords|
 coords[0] -= graph_bb[0]
 coords[1] = graph_bb[1] - coords[1] - coords[3]
    end
    object_pos.each do |id, pos|
 pos.x -= graph_bb[0]
 pos.y = graph_bb[1] - pos.y
    end

    @display         = display
    @plan            = plan
    @object_pos      = object_pos
    @bounding_rects  = bounding_rects

ensure
    dot_input.close!  if dot_input
    dot_output.close! if dot_output
end

#layout_id(object) ⇒ Object



172
173
174
175
176
# File 'lib/roby/log/dot.rb', line 172

def layout_id(object)
    id = Object.address_from_id(object.object_id).to_s
    object_ids[id] = object
    id
end