-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizer.rb
More file actions
executable file
·46 lines (37 loc) · 1.31 KB
/
visualizer.rb
File metadata and controls
executable file
·46 lines (37 loc) · 1.31 KB
1
2
3
4
5
6
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
34
35
36
37
38
39
40
41
42
43
44
45
46
require 'graphviz'
class Visualizer
attr_reader :graph
def initialize(state_machine, graph_name)
@state_machine = state_machine
@graph_name = graph_name
@nodes = Hash.new
end
def visualize
@graph = GraphViz.new(@graph_name, :strict => false, :rankdir => 'LR', :nodesep => 0.5)
append_nodes()
end
def print_graph
@graph.output(:png => "#{@graph_name}.png")
end
def append_nodes
@state_machine.states.each do |state_array|
state = @state_machine.states[state_array.first].first
state.initial? ? @nodes[state.name] = @graph.add_node(state.name, :shape => 'ellipse', :style => 'filled') : @nodes[state.name] = @graph.add_node(state.name, :shape => 'ellipse')
append_transitions(state)
end
end
def append_transitions(state)
state.transitions.each do |key, transition_array|
transition_array.each do |transition|
create_transition(state, transition)
end
end
end
def create_transition(state, transition)
from = state.name
to = (transition.to.nil? or transition.to.empty?) ? from : transition.to
label = transition.input + ((transition.action.nil? or transition.action.empty?) ? '' : '/' + transition.action)
@graph.add_edge(from, to, :label => label)
end
private :append_nodes, :append_transitions, :create_transition
end