class Percheron::Graph

Attributes

graphs[RW]
nodes[RW]
stack[R]

Public Class Methods

new(stack) click to toggle source
# File lib/percheron/graph.rb, line 6
def initialize(stack)
  @stack = stack
  @nodes = {}
  @graphs = {}
end

Public Instance Methods

save!(file) click to toggle source
# File lib/percheron/graph.rb, line 12
def save!(file)
  generate
  save(file)
end

Private Instance Methods

add_node(unit) click to toggle source
# File lib/percheron/graph.rb, line 64
def add_node(unit)
  nodes[unit.name] = graph.add_nodes(unit.name, node_opts(unit))
end
add_nodes() click to toggle source
# File lib/percheron/graph.rb, line 58
def add_nodes
  units.each do |_, unit|
    unit.pseudo? ? add_pseudo_node(unit) : add_node(unit)
  end
end
add_pseudo_node(unit) click to toggle source
# File lib/percheron/graph.rb, line 68
def add_pseudo_node(unit)
  create_cluster(unit)
  nodes[unit.name] = graphs[unit.pseudo_name].add_nodes(unit.name, pseudo_node_opts(unit))
end
cluster_opts(unit) click to toggle source
# File lib/percheron/graph.rb, line 79
def cluster_opts(unit)
  label = '<<font face="Arial Bold">%s</font>>' % unit.pseudo_name
  { label: label, style: 'filled', color: 'lightgrey', fontsize: 13 }
end
create_cluster(unit) click to toggle source
# File lib/percheron/graph.rb, line 73
def create_cluster(unit)
  return nil if graphs[unit.pseudo_name]
  name = 'cluster%s' % graphs.keys.count
  graphs[unit.pseudo_name] = graph.add_graph(name, cluster_opts(unit))
end
generate() click to toggle source
# File lib/percheron/graph.rb, line 22
def generate
  add_nodes
  add_links
end
graph() click to toggle source
# File lib/percheron/graph.rb, line 31
def graph
  @graph ||= GraphViz.new(:G, graph_opts)
end
graph_opts() click to toggle source
# File lib/percheron/graph.rb, line 35
def graph_opts
  { type: :digraph, nodesep: 0.75, ranksep: 1.0, label: header_label }
end
header_label() click to toggle source
# File lib/percheron/graph.rb, line 39
def header_label
  '<
    <table border="0" cellborder="0">
      <tr><td height="36" valign="bottom">
        <font face="Arial Bold" point-size="14">%s</font>
      </td></tr>
      <tr><td height="18"><font face="Arial Italic" point-size="11">%s</font></td></tr>
    </table>
    >' % [ stack.name, stack_description ]
end
node_opts(unit) click to toggle source
# File lib/percheron/graph.rb, line 84
def node_opts(unit)
  shape = unit.startable? ? 'box' : 'ellipse'
  label = [ '<font face="Arial Bold" point-size="12">%s</font><br/>' % unit.name ]
  unit.ports.each do |ports|
    label << '<font point-size="11">p: %s, i: %s</font>' % ports.split(':')
  end
  { shape: shape, label: '<%s>' % [ label.join('<br/>') ], fontname: 'arial' }
end
pseudo_node_opts(unit) click to toggle source
# File lib/percheron/graph.rb, line 93
def pseudo_node_opts(unit)
  node_opts(unit).merge!(style: 'filled', color: 'white')
end
save(file) click to toggle source
# File lib/percheron/graph.rb, line 27
def save(file)
  graph.output(png: file)
end
stack_description() click to toggle source
# File lib/percheron/graph.rb, line 50
def stack_description
  stack.description || ' '
end
units() click to toggle source
# File lib/percheron/graph.rb, line 54
def units
  @units ||= stack.units
end