Parent

Included Modules

Journey::GTG::TransitionTable

Attributes

memos[R]

Public Class Methods

new() click to toggle source
    # File lib/journey/gtg/transition_table.rb, line 10
10:       def initialize
11:         @regexp_states = Hash.new { |h,k| h[k] = {} }
12:         @string_states = Hash.new { |h,k| h[k] = {} }
13:         @accepting     = {}
14:         @memos         = Hash.new { |h,k| h[k] = [] }
15:       end

Public Instance Methods

[]=(from, to, sym) click to toggle source
     # File lib/journey/gtg/transition_table.rb, line 112
112:       def []= from, to, sym
113:         case sym
114:         when String
115:           @string_states[from][sym] = to
116:         when Regexp
117:           @regexp_states[from][sym] = to
118:         else
119:           raise ArgumentError, 'unknown symbol: %s' % sym.class
120:         end
121:       end
accepting?(state) click to toggle source
    # File lib/journey/gtg/transition_table.rb, line 25
25:       def accepting? state
26:         @accepting[state]
27:       end
accepting_states() click to toggle source
    # File lib/journey/gtg/transition_table.rb, line 21
21:       def accepting_states
22:         @accepting.keys
23:       end
add_accepting(state) click to toggle source
    # File lib/journey/gtg/transition_table.rb, line 17
17:       def add_accepting state
18:         @accepting[state] = true
19:       end
add_memo(idx, memo) click to toggle source
    # File lib/journey/gtg/transition_table.rb, line 29
29:       def add_memo idx, memo
30:         @memos[idx] << memo
31:       end
eclosure(t) click to toggle source
    # File lib/journey/gtg/transition_table.rb, line 37
37:       def eclosure t
38:         Array(t)
39:       end
memo(idx) click to toggle source
    # File lib/journey/gtg/transition_table.rb, line 33
33:       def memo idx
34:         @memos[idx]
35:       end
move(t, a) click to toggle source
    # File lib/journey/gtg/transition_table.rb, line 41
41:       def move t, a
42:         move_string(t, a).concat move_regexp(t, a)
43:       end
states() click to toggle source
     # File lib/journey/gtg/transition_table.rb, line 123
123:       def states
124:         ss = @string_states.keys + @string_states.values.map(&:values).flatten
125:         rs = @regexp_states.keys + @regexp_states.values.map(&:values).flatten
126:         (ss + rs).uniq
127:       end
to_json() click to toggle source
    # File lib/journey/gtg/transition_table.rb, line 45
45:       def to_json
46:         require 'json'
47: 
48:         simple_regexp = Hash.new { |h,k| h[k] = {} }
49: 
50:         @regexp_states.each do |from, hash|
51:           hash.each do |re, to|
52:             simple_regexp[from][re.source] = to
53:           end
54:         end
55: 
56:         JSON.dump({
57:           :regexp_states => simple_regexp,
58:           :string_states => @string_states,
59:           :accepting     => @accepting
60:         })
61:       end
to_svg() click to toggle source
    # File lib/journey/gtg/transition_table.rb, line 63
63:       def to_svg
64:         svg = IO.popen("dot -Tsvg", 'w+') { |f|
65:           f.write to_dot
66:           f.close_write
67:           f.readlines
68:         }
69:         3.times { svg.shift }
70:         svg.join.sub(/width="[^"]*"/, '').sub(/height="[^"]*"/, '')
71:       end
transitions() click to toggle source
     # File lib/journey/gtg/transition_table.rb, line 129
129:       def transitions
130:         @string_states.map { |from, hash|
131:           hash.map { |s, to| [from, s, to] }
132:         }.flatten(1) + @regexp_states.map { |from, hash|
133:           hash.map { |s, to| [from, s, to] }
134:         }.flatten(1)
135:       end
visualizer(paths, title = 'FSM') click to toggle source
     # File lib/journey/gtg/transition_table.rb, line 73
 73:       def visualizer paths, title = 'FSM'
 74:         viz_dir   = File.join File.dirname(__FILE__), '..', 'visualizer'
 75:         fsm_js    = File.read File.join(viz_dir, 'fsm.js')
 76:         fsm_css   = File.read File.join(viz_dir, 'fsm.css')
 77:         erb       = File.read File.join(viz_dir, 'index.html.erb')
 78:         states    = "function tt() { return #{to_json}; }"
 79: 
 80:         fun_routes = paths.shuffle.first(3).map do |ast|
 81:           ast.map { |n|
 82:             case n
 83:             when Nodes::Symbol
 84:               case n.left
 85:               when ':id' then rand(100).to_s
 86:               when ':format' then %{ xml json }.shuffle.first
 87:               else
 88:                 'omg'
 89:               end
 90:             when Nodes::Terminal then n.symbol
 91:             else
 92:               nil
 93:             end
 94:           }.compact.join
 95:         end
 96: 
 97:         stylesheets = [fsm_css]
 98:         svg         = to_svg
 99:         javascripts = [states, fsm_js]
100: 
101:         # Annoying hack for 1.9 warnings
102:         fun_routes  = fun_routes
103:         stylesheets = stylesheets
104:         svg         = svg
105:         javascripts = javascripts
106: 
107:         require 'erb'
108:         template = ERB.new erb
109:         template.result(binding)
110:       end

Private Instance Methods

move_regexp(t, a) click to toggle source
     # File lib/journey/gtg/transition_table.rb, line 138
138:       def move_regexp t, a
139:         return [] if t.empty?
140: 
141:         t.map { |s|
142:           @regexp_states[s].map { |re,v| re === a ? v : nil }
143:         }.flatten.compact.uniq
144:       end
move_string(t, a) click to toggle source
     # File lib/journey/gtg/transition_table.rb, line 146
146:       def move_string t, a
147:         return [] if t.empty?
148: 
149:         t.map { |s| @string_states[s][a] }.compact
150:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.