Module: Legion::Graph::Exporter

Defined in:
lib/legion/graph/exporter.rb

Class Method Summary collapse

Class Method Details

.to_dot(graph) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/legion/graph/exporter.rb', line 35

def to_dot(graph)
  Legion::Logging.debug "[Graph::Exporter] to_dot nodes=#{graph[:nodes].size} edges=#{graph[:edges].size}" if defined?(Legion::Logging)
  lines = ['digraph legion_tasks {', '  rankdir=LR;']

  graph[:nodes].each do |key, node|
    label = node[:label].gsub('"', '\\"')
    shape = node[:type] == 'trigger' ? 'box' : 'ellipse'
    lines << "  \"#{key}\" [label=\"#{label}\" shape=#{shape}];"
  end

  graph[:edges].each do |edge|
    label = edge[:label] && !edge[:label].empty? ? " [label=\"#{edge[:label]}\"]" : ''
    lines << "  \"#{edge[:from]}\" -> \"#{edge[:to]}\"#{label};"
  end

  lines << '}'
  lines.join("\n")
end

.to_mermaid(graph) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/legion/graph/exporter.rb', line 7

def to_mermaid(graph)
  Legion::Logging.debug "[Graph::Exporter] to_mermaid nodes=#{graph[:nodes].size} edges=#{graph[:edges].size}" if defined?(Legion::Logging)
  lines = ['graph TD']
  node_ids = {}
  counter = 0

  graph[:nodes].each do |key, node|
    counter += 1
    id = "N#{counter}"
    node_ids[key] = id
    lines << "  #{id}[#{node[:label]}]"
  end

  graph[:edges].each do |edge|
    from = node_ids[edge[:from]]
    to   = node_ids[edge[:to]]
    next unless from && to

    lines << if edge[:label] && !edge[:label].empty?
               "  #{from} -->|#{edge[:label]}| #{to}"
             else
               "  #{from} --> #{to}"
             end
  end

  lines.join("\n")
end