Class: Syskit::Graphviz

Inherits:
Object show all
Defined in:
lib/syskit/graphviz.rb

Overview

General support to export a generated plan into a dot-compatible format

This class generates the dot specification files (and runs dot for you), exporting the component-related information out of a plan.

It also contains an API that allows to add “annotations” to the generated graph. Four types of annotations can be generated:

  • port annotations: text is added to the port descriptions (#add_port_annotation)

  • task annotations: text is added to the task description (#add_task_annotation)

  • additional vertices (#add_vertex)

  • additional edges (#add_edge)

Defined Under Namespace

Classes: Colors, DummyPage

Constant Summary collapse

COLORS =
{
    normal: Colors.new("#000000", "red", "#55aaff"),
    toned_down: Colors.new("#D3D7CF", "#D3D7CF", "#c2cbd7")
}
HTML_CHAR_CLASSES =
Hash[
    '<' => '&lt;',
    '>' => '&gt;'
]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plan, page = DummyPage.new, typelib_resolver: nil) ⇒ Graphviz

Returns a new instance of Graphviz



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/syskit/graphviz.rb', line 76

def initialize(plan, page = DummyPage.new, typelib_resolver: nil)
    @plan = plan
    @page = page
    @make_links = true
    @typelib_resolver = typelib_resolver

    @colors = COLORS.dup

    @task_annotations = Hash.new { |h, k| h[k] = Hash.new { |a, b| a[b] = Array.new } }
    @port_annotations = Hash.new { |h, k| h[k] = Hash.new { |a, b| a[b] = Array.new } }
    @conn_annotations = Hash.new { |h, k| h[k] = Array.new }
    @additional_vertices = Hash.new { |h, k| h[k] = Array.new }
    @additional_edges    = Array.new
end

Class Attribute Details

.available_graph_annotationsSet<String> (readonly)

Returns set of annotation names that make sense only for tasks that are part of a graph

Returns:

  • (Set<String>)

    set of annotation names that make sense only for tasks that are part of a graph



61
62
63
# File 'lib/syskit/graphviz.rb', line 61

def available_graph_annotations
  @available_graph_annotations
end

.available_task_annotationsSet<String> (readonly)

Returns set of annotation names that make sense for a task alone

Returns:

  • (Set<String>)

    set of annotation names that make sense for a task alone



58
59
60
# File 'lib/syskit/graphviz.rb', line 58

def available_task_annotations
  @available_task_annotations
end

Instance Attribute Details

#additional_edgesObject (readonly)

Additional edges that should be added to the generated graph



50
51
52
# File 'lib/syskit/graphviz.rb', line 50

def additional_edges
  @additional_edges
end

#additional_verticesObject (readonly)

Additional vertices that should be added to the generated graph



48
49
50
# File 'lib/syskit/graphviz.rb', line 48

def additional_vertices
  @additional_vertices
end

#conn_annotationsObject (readonly)

Annotations for connections



42
43
44
# File 'lib/syskit/graphviz.rb', line 42

def conn_annotations
  @conn_annotations
end

#page#link_to(object[, text]) (readonly)

A rendering context for the SVG

Returns:

  • (#link_to(object[, text]))


53
54
55
# File 'lib/syskit/graphviz.rb', line 53

def page
  @page
end

#planObject (readonly)

The plan object containing the structure we want to display



40
41
42
# File 'lib/syskit/graphviz.rb', line 40

def plan
  @plan
end

#port_annotationsObject (readonly)

Annotations for ports



46
47
48
# File 'lib/syskit/graphviz.rb', line 46

def port_annotations
  @port_annotations
end

#task_annotationsObject (readonly)

Annotations for tasks



44
45
46
# File 'lib/syskit/graphviz.rb', line 44

def task_annotations
  @task_annotations
end

Class Method Details

.available_annotationsObject



352
353
354
355
356
357
358
# File 'lib/syskit/graphviz.rb', line 352

def self.available_annotations
    instance_methods.map do |m|
        if m.to_s =~ /^add_(\w+)_annotations/
            $1
        end
    end.compact
end

.dot_iolabel(name, inputs, outputs) ⇒ Object



765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
# File 'lib/syskit/graphviz.rb', line 765

def self.dot_iolabel(name, inputs, outputs)
    label = "{{"
    if !inputs.empty?
        label << inputs.sort.map do |port_name|
                "<#{port_name}> #{port_name}"
        end.join("|")
        label << "|"
    end
    label << "<main> #{name}"

    if !outputs.empty?
        label << "|"
        label << outputs.sort.map do |port_name|
                "<#{port_name}> #{port_name}"
        end.join("|")
    end
    label << "}}"
end

Instance Method Details

#add_connection_policy_annotationsObject



384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/syskit/graphviz.rb', line 384

def add_connection_policy_annotations
    plan.find_local_tasks(TaskContext).each do |source_task|
        source_task.each_concrete_output_connection do |source_port, sink_port, sink_task, policy|
            policy = policy.dup
            policy.delete(:fallback_policy)
            if policy.empty?
                policy_s = "(no policy)"
            else
                policy_s = if policy.empty? then ""
                           elsif policy[:type] == :data then 'data'
                           elsif policy[:type] == :buffer then  "buffer:#{policy[:size]}"
                           else policy.to_s
                           end
            end
            conn_annotations[[source_task, source_port, sink_task, sink_port]] << policy_s
        end
    end
end

#add_edge(from, to, label = nil) ⇒ Object



182
183
184
# File 'lib/syskit/graphviz.rb', line 182

def add_edge(from, to, label = nil)
    additional_edges << [from, to, label]
end

#add_port_annotation(task, port_name, name, ann) ⇒ void

This method returns an undefined value.

Add an annotation block to a port label.

Parameters:

  • task (Component)

    the task that contains the port

  • port_name (String)

    the port name

  • name (String)

    the annotation name. It appears on the left column of the task label

  • ann (Array<String>)

    the annotation itself, as an array. Each line in the array is displayed as a separate line in the label.



162
163
164
165
166
# File 'lib/syskit/graphviz.rb', line 162

def add_port_annotation(task, port_name, name, ann)
    port_annotations[[task, port_name]].merge!(name => ann) do |_, old, new|
        old + new
    end
end

#add_port_details_annotationsObject



360
361
362
363
364
365
366
367
# File 'lib/syskit/graphviz.rb', line 360

def add_port_details_annotations
    plan.find_local_tasks(AbstractComponent).each do |task|
        task.model.each_port do |p|
            port_type = Roby.app.default_loader.opaque_type_for(p.type)
            add_port_annotation(task, p.name, "Type", port_type.name)
        end
    end
end

#add_task_annotation(task, name, ann) ⇒ void

This method returns an undefined value.

Add an annotation block to a task label.

Parameters:

  • task (Component)

    is the task to which the information should be added

  • name (String)

    is the annotation name. It appears on the left column of the task label

  • ann (Array<String>)

    is the annotation itself, as an array. Each line in the array is displayed as a separate line in the label.



130
131
132
133
134
135
136
137
138
# File 'lib/syskit/graphviz.rb', line 130

def add_task_annotation(task, name, ann)
    if !ann.respond_to?(:to_ary)
        ann = [ann]
    end

    task_annotations[task].merge!(name => ann) do |_, old, new|
        old + new
    end
end

#add_task_info_annotationsObject



370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/syskit/graphviz.rb', line 370

def add_task_info_annotations
    plan.find_local_tasks(AbstractComponent).each do |task|
        arguments = task.arguments.map { |k, v| "#{k}: #{v}" }
        task.model.arguments.each do |arg_name|
            if !task.arguments.has_key?(arg_name)
                arguments << "#{arg_name}: (unset)"
            end
        end
        add_task_annotation(task, "Arguments", arguments.sort)
        add_task_annotation(task, "Roles", task.roles.to_a.sort.join(", "))
    end
end

#add_trigger_annotationsObject



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/syskit/graphviz.rb', line 404

def add_trigger_annotations
    plan.find_local_tasks(TaskContext).each do |task|
        task.model.each_port do |p|
            if dyn = task.port_dynamics[p.name]
                ann = dyn.triggers.map do |tr|
                    "#{tr.name}[p=#{tr.period},s=#{tr.sample_count}]"
                end
                port_annotations[[task, p.name]]['Triggers'].concat(ann)
            end
        end
        if dyn = task.dynamics
            ann = dyn.triggers.map do |tr|
                    "#{tr.name}[p=#{tr.period},s=#{tr.sample_count}]"
            end
            task_annotations[task]['Triggers'].concat(ann)
        end
    end
end

#add_vertex(task, vertex_name, vertex_label) ⇒ Object



178
179
180
# File 'lib/syskit/graphviz.rb', line 178

def add_vertex(task, vertex_name, vertex_label)
    additional_vertices[task] << [vertex_name, vertex_label]
end

#annotate_connections(annotations) ⇒ Object



168
169
170
171
172
173
174
175
176
# File 'lib/syskit/graphviz.rb', line 168

def annotate_connections(annotations)
    conn_annotations.merge!(annotations) do |_, old, new|
        if new.respond_to?(:to_ary)
            old.concat(new)
        else
            old << new
        end
    end
end

#annotate_ports(annotations) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/syskit/graphviz.rb', line 140

def annotate_ports(annotations)
    port_annotations.merge!(annotations) do |_, old, new|
        old.merge!(new) do |_, old_array, new_array|
            if new_array.respond_to?(:to_ary)
                old_array.concat(new_array)
            else
                old_array << new_array
            end
        end
    end
end

#annotate_tasks(annotations) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/syskit/graphviz.rb', line 108

def annotate_tasks(annotations)
    task_annotations.merge!(annotations) do |_, old, new|
        old.merge!(new) do |_, old_array, new_array|
            if new_array.respond_to?(:to_ary)
                old_array.concat(new_array)
            else
                old_array << new_array
            end
        end
    end
end

#dataflow(options = Hash.new, excluded_models = Set.new, annotations = Set.new) ⇒ Object

Generates a dot graph that represents the task dataflow in this deployment



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'lib/syskit/graphviz.rb', line 426

def dataflow(options = Hash.new, excluded_models = Set.new, annotations = Set.new)
    # For backward compatibility with the signature
    # dataflow(remove_compositions = false, excluded_models = Set.new, annotations = Set.new)
    if !options.kind_of?(Hash)
        options = { :remove_compositions => options, :excluded_models => excluded_models, :annotations => annotations }
    end

    options = Kernel.validate_options options,
        :remove_compositions => false,
        :excluded_models => Set.new,
        :annotations => Set.new,
        :highlights => Set.new,
        :show_all_ports => true
    excluded_models = options[:excluded_models]

    port_annotations.clear
    task_annotations.clear

    annotations = options[:annotations].to_set
    annotations.each do |ann|
        send("add_#{ann}_annotations")
    end

    output_ports = Hash.new { |h, k| h[k] = Set.new }
    input_ports  = Hash.new { |h, k| h[k] = Set.new }
    connected_ports  = Hash.new { |h, k| h[k] = Set.new }
    port_annotations.each do |task, p|
        connected_ports[task] << p
    end
    additional_edges.each do |(from_id, from_task), (to_id, to_task), _|
        from_id = from_task.find_port(from_id) if !from_id.respond_to?(:name)
        connected_ports[from_task] << from_id
        to_id = to_task.find_port(to_id) if !to_id.respond_to?(:name)
        connected_ports[to_task] << to_id
    end
    connections = Hash.new

    all_tasks = plan.find_local_tasks(Deployment).to_set

    # Register all ports and all connections
    #
    # Note that a connection is not guaranteed to be from an output
    # to an input: on compositions, exported ports are represented
    # as connections between either two inputs or two outputs
    plan.find_local_tasks(AbstractComponent).each do |source_task|
        next if options[:remove_compositions] && source_task.kind_of?(Composition)
        next if excluded_models.include?(source_task.concrete_model)

        source_task.each_input_port do |port|
            input_ports[source_task] << port
        end
        source_task.each_output_port do |port|
            output_ports[source_task] << port
        end

        all_tasks << source_task

        if !source_task.kind_of?(Composition)
            source_task.each_concrete_output_connection do |source_port, sink_port, sink_task, policy|
                next if excluded_models.include?(sink_task.concrete_model)
                connections[[source_task, source_port, sink_port, sink_task]] = policy
            end
        end
        source_task.each_output_connection do |source_port, sink_port, sink_task, policy|
            next if connections.has_key?([source_port, sink_port, sink_task])
            next if excluded_models.include?(sink_task.concrete_model)
            next if options[:remove_compositions] && sink_task.kind_of?(Composition)
            connections[[source_task, source_port, sink_port, sink_task]] = policy
        end
    end

    # Register ports that are part of connections, but are not
    # defined on the task's interface. They are dynamic ports.
    connections.each do |(source_task, source_port, sink_port, sink_task), policy|
        source_port = source_task.find_port(source_port)
        connected_ports[source_task] << source_port
        sink_port   = sink_task.find_port(sink_port)
        connected_ports[sink_task]   << sink_port
        if !input_ports[source_task].include?(source_port)
            output_ports[source_task] << source_port
        end
        if !output_ports[sink_task].include?(sink_port)
            input_ports[sink_task] << sink_port
        end
    end

    result = []

    # Finally, emit the dot code for connections
    connections.each do |(source_task, source_port, sink_port, sink_task), policy|
        source_port = source_task.find_port(source_port)
        sink_port   = sink_task.find_port(sink_port)
        if source_task.kind_of?(Syskit::Composition) || sink_task.kind_of?(Syskit::Composition)
            style = "color=\"#{@colors[:normal].composition}\","
        end

        source_port_id = dot_id(source_port, source_task)
        sink_port_id   = dot_id(sink_port, sink_task)

        label = conn_annotations[[source_task, source_port.name, sink_task, sink_port.name]].join(",")
        result << "  #{source_port_id} -> #{sink_port_id} [#{style}label=\"#{label}\"];"
    end

    # Group the tasks by deployment
    clusters = Hash.new { |h, k| h[k] = Array.new }
    all_tasks.each do |task|
        if !task.kind_of?(Deployment)
            clusters[task.execution_agent] << task
        end
    end

    # Allocate one color for each task. The ideal would be to do a
    # graph coloring so that two related tasks don't get the same
    # color, but that's TODO
    task_colors = Hash.new
    used_deployments = all_tasks.map(&:execution_agent).to_set
    used_deployments.each do |task|
        task_colors[task] = Syskit.allocate_color
    end

    clusters.each do |deployment, task_contexts|
        if deployment
            result << "  subgraph cluster_#{deployment.dot_id} {"
            task_label, task_dot_attributes = format_task_label(deployment, task_colors)
            label = "  <TABLE ALIGN=\"LEFT\" COLOR=\"white\" BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\">\n"
            label << "    #{task_label}\n"
            label << "  </TABLE>"
            result << "      label=< #{label} >;"
        end

        task_contexts.each do |task|
            if !task
                raise "#{task} #{deployment} #{task_contexts.inspect}"
            end
            if options[:highlights].include?(task)
                style = "penwidth=3;"
            end
            inputs  = input_ports[task]
            outputs = output_ports[task]
            if !options[:show_all_ports]
                inputs  = (inputs & connected_ports[task]).to_a.sort_by(&:name)
                outputs = (outputs & connected_ports[task]).to_a.sort_by(&:name)
            end
            result << render_task(task, inputs, outputs, style)
        end

        if deployment
            result << "  };"
        end
    end

    additional_edges.each do |from, to, label|
        from_id = dot_id(*from)
        to_id   = dot_id(*to)
        result << "  #{from_id} -> #{to_id} [#{label}];"
    end

    if result.empty?
        # This workarounds a dot bug in which some degenerate graphs
        # (only one node) crash it
        return "digraph { }"
    else
        ["digraph {",
         "  rankdir=LR;",
         "  node [shape=none,margin=0,height=.1,fontname=\"Arial\"];"].
        concat(result).
        concat(["}"]).
        join("\n")
    end
end

#dot_id(object, context = nil) ⇒ Object



601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
# File 'lib/syskit/graphviz.rb', line 601

def dot_id(object, context = nil)
    case object
    when Syskit::TaskContext
        "label#{object.dot_id}"
    when Syskit::InputPort, OroGen::Spec::InputPort
        "inputs#{context.dot_id}:#{dot_id(object.name)}"
    when Syskit::OutputPort, OroGen::Spec::OutputPort
        "outputs#{context.dot_id}:#{dot_id(object.name)}"
    else
        if object.respond_to?(:to_str)
            if !context
                return dot_symbol_quote(object)
            elsif context.respond_to?(:dot_id)
                return "#{dot_symbol_quote(object)}#{context.dot_id}"
            end
        end

        raise ArgumentError, "don't know how to generate a dot ID for #{object} in context #{context}"
    end
end

#dot_symbol_quote(string) ⇒ Object



597
598
599
# File 'lib/syskit/graphviz.rb', line 597

def dot_symbol_quote(string)
    string.gsub(/[^\w]/, '_')
end

#escape_dot(string) ⇒ Object



103
104
105
106
# File 'lib/syskit/graphviz.rb', line 103

def escape_dot(string)
    escape_dot_uri(string).
        gsub(/[^\[\]&;:\w\. ]/, "_")
end

#escape_dot_uri(string) ⇒ Object



97
98
99
100
101
# File 'lib/syskit/graphviz.rb', line 97

def escape_dot_uri(string)
    string.
        gsub(/</, "&lt;").
        gsub(/>/, "&gt;")
end

#format_annotations(annotations, key = nil, include_empty: false) ⇒ Object



682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
# File 'lib/syskit/graphviz.rb', line 682

def format_annotations(annotations, key = nil, include_empty: false)
    if key
        if !annotations.has_key?(key)
            return
        end
        ann = annotations[key]
    else
        ann = annotations
    end

    result = []
    result = ann.map do |category, values|
        # Values are allowed to be an array of strings or plain strings, normalize to array
        values = [*values]
        next if (values.empty? && !include_empty)

        values = values.map { |v| v.tr("<>", "[]") }
        values = values.map { |v| v.tr("{}", "[]") }

       "<TR><TD ROWSPAN=\"#{values.size()}\" VALIGN=\"TOP\" ALIGN=\"RIGHT\">#{category}</TD><TD ALIGN=\"LEFT\">#{values.first}</TD></TR>\n" +
       values[1..-1].map { |v| "<TR><TD ALIGN=\"LEFT\">#{v}</TD></TR>" }.join("\n")
    end.flatten

    if !result.empty?
        result.map { |l| "    #{l}" }.join("\n")
    end
end

#format_edge_info(value) ⇒ Object



252
253
254
255
256
257
258
259
260
# File 'lib/syskit/graphviz.rb', line 252

def format_edge_info(value)
    if value.respond_to?(:to_str)
        value.to_str
    elsif value.respond_to?(:each)
        value.map { |v| format_edge_info(v) }.join(",")
    else
        value.to_s
    end
end

#format_task_label(task, task_colors = Hash.new) ⇒ Object



711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
# File 'lib/syskit/graphviz.rb', line 711

def format_task_label(task, task_colors = Hash.new)
    label = []

    if task.placeholder?
        name = task.proxied_data_service_models.map do |model|
            model.name
        end
        if ![Syskit::Component, Syskit::TaskContext, Syskit::Composition].include?(task.model.superclass) &&
            name = [task.model.superclass.name] + name
        end
        name = escape_dot(name.join(","))
        if task.model.respond_to?(:tag_name)
            name = "#{task.model.tag_name}_tag(#{name})"
        end
        if task.transaction_proxy?
            name = "[T] #{name}"
        end
        label << "<TR><TD COLSPAN=\"2\">#{escape_dot(name)}</TD></TR>"
    else
        annotations = Array.new
        if task.model.respond_to?(:is_specialization?) && task.model.is_specialization?
            annotations = [["Specialized On", [""]]]
            name = task.model.root_model.name || ""
            task.model.specialized_children.each do |child_name, child_models|
                child_models = child_models.map(&:short_name)
                annotations << [child_name, child_models.shift]
                child_models.each do |m|
                    annotations << ["", m]
                end
            end

        else
            name = task.concrete_model.name || ""
        end

        name = name.dup
        if task.execution_agent && task.respond_to?(:orocos_name)
            name << "[#{task.orocos_name}]"
        end
        if task.transaction_proxy?
            name = "[T] #{name}"
        end
        label << "<TR><TD COLSPAN=\"2\">#{escape_dot(name)}</TD></TR>"
        ann = format_annotations(annotations)
        label << ann
    end

    if ann = format_annotations(task_annotations, task)
        label << ann
    end

    return "    " + label.join("\n    ")
end

#hierarchy(options = Hash.new) ⇒ Object

Generates a dot graph that represents the task hierarchy in this deployment

It takes no options. The options argument is used to have a common signature with #dataflow



348
349
350
# File 'lib/syskit/graphviz.rb', line 348

def hierarchy(options = Hash.new)
    relation_to_dot(:accessor => :each_child)
end

#make_links=(value) ⇒ Boolean

Parameters:

  • (Boolean)

    value

Returns:

  • (Boolean)


38
# File 'lib/syskit/graphviz.rb', line 38

attr_predicate :make_links?, true

#make_links?Boolean

Returns:

  • (Boolean)


38
# File 'lib/syskit/graphviz.rb', line 38

attr_predicate :make_links?, true

#relation_to_dot(options = Hash.new) ⇒ Object

Generates a dot graph that represents the task hierarchy in this deployment



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/syskit/graphviz.rb', line 264

def relation_to_dot(options = Hash.new)
    options = Kernel.validate_options options,
        :accessor => nil,
        :dot_edge_mark => "->",
        :dot_graph_type => 'digraph',
        :highlights => [],
        :toned_down => [],
        :displayed_options => [],
        :annotations => ['task_info']

    if !options[:accessor]
        raise ArgumentError, "no :accessor option given"
    end

    port_annotations.clear
    task_annotations.clear

    options[:annotations].each do |ann_name|
        send("add_#{ann_name}_annotations")
    end

    result = []

    all_tasks = Set.new

    plan.find_local_tasks(AbstractComponent).each do |task|
        all_tasks << task
        task.send(options[:accessor]) do |child_task, edge_info|
            label = []
            options[:displayed_options].each do |key|
                label << "#{key}=#{format_edge_info(edge_info[key])}"
            end
            all_tasks << child_task
            result << "  #{task.dot_id} #{options[:dot_edge_mark]} #{child_task.dot_id} [label=\"#{label.join("\\n")}\"];"
        end
    end

    all_tasks.each do |task|
        attributes = []
        task_label = format_task_label(task)
        label = "  <TABLE ALIGN=\"LEFT\" COLOR=\"white\" BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\">\n#{task_label}</TABLE>"
        attributes << "label=<#{label}>"
        if make_links?
            attributes << "href=\"plan://syskit/#{task.dot_id}\""
        end
        color_set =
            if options[:toned_down].include?(task)
                @colors[:toned_down]
            else @colors[:normal]
            end
        color =
            if task.abstract? then color_set.abstract
            elsif task.kind_of?(Syskit::Composition)
                color_set.composition
            else color_set.normal
            end
        attributes << "color=\"#{color}\""
        if options[:highlights].include?(task)
            attributes << "penwidth=3"
        end

        result << "  #{task.dot_id} [#{attributes.join(" ")}];"
    end

    if result.empty?
        # This workarounds a dot bug in which some degenerate graphs
        # (only one node) crash it
        return "#{options[:dot_graph_type]} { }"
    else
        ["#{options[:dot_graph_type]} {",
         "  mindist=0",
         "  rankdir=TB",
         "  node [shape=record,height=.1,fontname=\"Arial\"];"].
        concat(result).
        concat(["}"]).
        join("\n")
    end
end

#render_task(task, input_ports, output_ports, style = nil) ⇒ Object



627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
# File 'lib/syskit/graphviz.rb', line 627

def render_task(task, input_ports, output_ports, style = nil)
    task_link = if make_links?
                    "href=\"plan://syskit/#{task.dot_id}\""
                end

    result = []
    result << "    subgraph cluster_#{task.dot_id} {"
    result << "        #{task_link};"
    result << "        label=\"\";"
    if task.abstract?
        result << "      color=\"#{@colors[:normal].abstract}\";"
    elsif task.kind_of?(Syskit::Composition)
        result << "      color=\"#{@colors[:normal].composition}\";"
    end
    result << style if style

    additional_vertices[task].each do |vertex_name, vertex_label|
        result << "      #{dot_id(vertex_name, task)} [#{vertex_label}];"
    end

    task_label, attributes = format_task_label(task)
    task_label = "  <TABLE ALIGN=\"LEFT\" COLOR=\"white\" BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\">#{task_label}</TABLE>"
    result << "    label#{task.dot_id} [#{task_link},shape=none,label=< #{task_label} >];";

    if !input_ports.empty?
        input_port_label = "<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\">"
        input_ports.each do |p|
            port_type = Roby.app.default_loader.opaque_type_for(p.type)
            port_id = dot_id(p.name)
            ann = format_annotations(port_annotations, [task, p.name])
            doc = escape_dot(p.model.doc || '<no documentation for this port>')
            input_port_label << "<TR><TD HREF=\"#{uri_for(port_type)}\" TITLE=\"#{doc}\"><TABLE BORDER=\"0\" CELLBORDER=\"0\"><TR><TD PORT=\"#{port_id}\" COLSPAN=\"2\">#{p.name}</TD></TR>#{ann}</TABLE></TD></TR>"
        end
        input_port_label << "\n</TABLE>"
        result << "    inputs#{task.dot_id} [label=< #{input_port_label} >,shape=none];"
        result << "    inputs#{task.dot_id} -> label#{task.dot_id} [style=invis];"
    end

    if !output_ports.empty?
        output_port_label = "<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\">"
        output_ports.each do |p|
            port_type = Roby.app.default_loader.opaque_type_for(p.type)
            port_id = dot_id(p.name)
            ann = format_annotations(port_annotations, [task, p.name])
            doc = escape_dot(p.model.doc || '<no documentation for this port>')
            output_port_label << "<TR><TD HREF=\"#{uri_for(port_type)}\" TITLE=\"#{doc}\"><TABLE BORDER=\"0\" CELLBORDER=\"0\"><TR><TD PORT=\"#{port_id}\" COLSPAN=\"2\">#{p.name}</TD></TR>#{ann}</TABLE></TD></TR>"
        end
        output_port_label << "\n</TABLE>"
        result << "    outputs#{task.dot_id} [label=< #{output_port_label} >,shape=none];"
        result << "    label#{task.dot_id} -> outputs#{task.dot_id} [style=invis];"
    end

    result << "    }"
    result.join("\n")
end

#run_dot_with_retries(retry_count, command) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/syskit/graphviz.rb', line 186

def run_dot_with_retries(retry_count, command)
    retry_count.times do |i|
        Tempfile.open('roby_orocos_graphviz') do |io|
            dot_graph = yield
            io.write dot_graph
            io.flush

            graph = `#{command % [io.path]}`
            if $?.exited?
                return graph
            end
            puts "dot crashed, retrying (#{i}/#{retry_count})"
        end
    end
    nil
end

#to_file(kind, format, output_io, options = Hash.new) ⇒ Object

Generate a svg file representing the current state of the deployment



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
# File 'lib/syskit/graphviz.rb', line 205

def to_file(kind, format, output_io, options = Hash.new)
    # For backward compatibility reasons
    filename ||= kind
    if File.extname(filename) != ".#{format}"
        filename += ".#{format}"
    end

    file_options, display_options = Kernel.filter_options options,
        :graphviz_tool => "dot"

    graph = run_dot_with_retries(20, "#{file_options[:graphviz_tool]} -T#{format} %s") do
        send(kind, display_options)
    end
    graph ||= run_dot_with_retries(20, "#{file_options[:graphviz_tool]} -Tpng %s") do
        send(kind, display_options)
    end

    if !graph
        Syskit.debug do
            i = 0
            pattern = "syskit_graphviz_%i.dot"
            while File.file?(pattern % [i])
                i += 1
            end
            path = pattern % [i]
            File.open(path, 'w') { |io| io.write send(kind, display_options) }
            "saved graphviz input in #{path}"
        end
        raise DotFailedError, "dot reported an error generating the graph"
    end

    if output_io.respond_to?(:to_str)
        File.open(output_io, 'w') do |io|
            io.puts(graph)
        end
    else
        output_io.puts(graph)
        output_io.flush
    end
end

#uri_for(type) ⇒ Object



91
92
93
94
95
# File 'lib/syskit/graphviz.rb', line 91

def uri_for(type)
    if @typelib_resolver
        "link://metaruby/" + escape_dot_uri(@typelib_resolver.split_name(type).join("/"))
    end
end