Class: StateFu::Plotter

Inherits:
Object show all
Defined in:
lib/support/plotter.rb

Constant Summary collapse

OUTPUT_HELPER =
Module.new do

  def save!
    Tempfile.new(['state_fu_graph','.dot']) do |fh|
      fh.write( self )
    end.path
  end

  def save_as( filename )
    File.open(filename, 'w') { |fh| fh.write( self ) }
  end

  def save_png(filename)
    raise NotImplementedError
    # dot graph.dot -Tpng -O
  end

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine, options = {}) ⇒ Plotter

Returns a new instance of Plotter.

Raises:

  • (RuntimeError)


30
31
32
33
34
35
36
37
# File 'lib/support/plotter.rb', line 30

def initialize( machine, options={} )
  raise RuntimeError, machine.class.to_s unless machine.is_a?(StateFu::Machine)
  @machine = machine
  @options = options.symbolize_keys!
  @states  = {}
  @events  = {}
  # generate
end

Instance Attribute Details

#dotObject (readonly)

Returns the value of attribute dot.



5
6
7
# File 'lib/support/plotter.rb', line 5

def dot
  @dot
end

#eventsObject (readonly)

Returns the value of attribute events.



5
6
7
# File 'lib/support/plotter.rb', line 5

def events
  @events
end

#graphObject (readonly)

Returns the value of attribute graph.



5
6
7
# File 'lib/support/plotter.rb', line 5

def graph
  @graph
end

#machineObject (readonly)

Returns the value of attribute machine.



5
6
7
# File 'lib/support/plotter.rb', line 5

def machine
  @machine
end

#statesObject (readonly)

Returns the value of attribute states.



5
6
7
# File 'lib/support/plotter.rb', line 5

def states
  @states
end

Instance Method Details

#generateObject



39
40
41
# File 'lib/support/plotter.rb', line 39

def generate
  @dot ||= generate_dot!.extend( OUTPUT_HELPER )
end

#generate_dot!Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/support/plotter.rb', line 43

def generate_dot!
  @graph = Vizier::Graph.new(:state_machine) do |g|
    g.node :shape => 'doublecircle'
    machine.state_names.map.each do |s|
      @states[s] = g.add_node(s.to_s)
    end
    machine.events.map.each do |e|
      e.origins.map(&:name).each do |from|
        e.targets.map(&:name).each do |to|
          g.connect( @states[from], @states[to], :label => e.name.to_s )
        end
      end
      # @events[s] = g.add_node(s.to_s)
    end
  end
  @graph.generate!
end

#outputObject



26
27
28
# File 'lib/support/plotter.rb', line 26

def output
  generate
end