Class: Graphomaton::Exporters::Dot

Inherits:
Object
  • Object
show all
Includes:
Graphomaton::ExporterIntrospection
Defined in:
lib/graphomaton/exporters/dot.rb

Constant Summary collapse

DEFAULT_DIRECTION =
:lr
DEFAULT_RANK_CONSTRAINTS =
false
DIRECTION_OPTIONS =
%i[lr tb rl bt].freeze
LINE_STYLE_OPTIONS =
%i[solid dashed dotted].freeze
PSEUDOSTATE_SHAPES =
{
  choice: 'diamond',
  fork: 'point',
  join: 'point'
}.freeze

Instance Method Summary collapse

Methods included from Graphomaton::ExporterIntrospection

#capabilities, #losses_for

Constructor Details

#initialize(automaton, direction: DEFAULT_DIRECTION, theme: nil, rank_constraints: DEFAULT_RANK_CONSTRAINTS) ⇒ Dot

Returns a new instance of Dot.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/graphomaton/exporters/dot.rb', line 17

def initialize(automaton, direction: DEFAULT_DIRECTION, theme: nil, rank_constraints: DEFAULT_RANK_CONSTRAINTS)
  @automaton = automaton
  @direction = resolve_direction(direction)
  @theme = resolve_theme(theme)
  @rank_constraints = rank_constraints
  @identifiers = IdentifierAllocator.new
  @state_names = @automaton.state_records.each_key.to_h do |name|
    [name, @identifiers.allocate([:state, name], preferred: name, prefix: 'state')]
  end
  @start_name = @identifiers.allocate(:start, preferred: '__start__', prefix: '__graphomaton_start')
end

Instance Method Details

#exportObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/graphomaton/exporters/dot.rb', line 29

def export
  lines = ['digraph finite_state_machine {']
  lines << "    rankdir=#{rankdir};"
  lines << "    graph [bgcolor=\"#{escape_label(@theme[:background])}\"];" if @theme && @theme[:background]
  lines << "    node [#{node_attributes('circle')}];"
  lines << ''
  state_attributes = state_attribute_lines
  lines.concat(state_attributes)
  lines << '' if state_attributes.any?
  state_clusters = state_cluster_lines
  lines.concat(state_clusters)
  lines << '' if state_clusters.any?

  if @automaton.initial_state
    lines << "    #{quoted_id(@start_name)} [shape=point];"
    lines << "    #{quoted_id(@start_name)} -> #{quoted_state_id(@automaton.initial_state)};"
    lines << ''
  end

  rank_constraints = rank_constraint_lines
  lines.concat(rank_constraints)
  lines << '' if rank_constraints.any?

  @automaton.transition_records.each do |trans|
    lines << "    #{quoted_state_id(trans[:from])} -> #{quoted_state_id(trans[:to])} [#{edge_attributes(trans)}];"
  end

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