Class: ActionDispatch::Journey::GTG::TransitionTable

Inherits:
Object
  • Object
show all
Includes:
NFA::Dot
Defined in:
actionpack/lib/action_dispatch/journey/gtg/transition_table.rb

Overview

:nodoc:

Constant Summary collapse

DEFAULT_EXP =
/[^.\/?]+/
DEFAULT_EXP_ANCHORED =
/\A#{DEFAULT_EXP}\Z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NFA::Dot

#to_dot

Constructor Details

#initializeTransitionTable

Returns a new instance of TransitionTable.



18
19
20
21
22
23
24
# File 'actionpack/lib/action_dispatch/journey/gtg/transition_table.rb', line 18

def initialize
  @stdparam_states = {}
  @regexp_states   = {}
  @string_states   = {}
  @accepting       = {}
  @memos           = Hash.new { |h, k| h[k] = [] }
end

Instance Attribute Details

#memosObject (readonly)

Returns the value of attribute memos.



13
14
15
# File 'actionpack/lib/action_dispatch/journey/gtg/transition_table.rb', line 13

def memos
  @memos
end

Instance Method Details

#[]=(from, to, sym) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'actionpack/lib/action_dispatch/journey/gtg/transition_table.rb', line 169

def []=(from, to, sym)
  case sym
  when String, Symbol
    to_mapping = @string_states[from] ||= {}
    # account for symbols in the constraints the same as strings
    to_mapping[sym.to_s] = to
  when Regexp
    if sym == DEFAULT_EXP
      @stdparam_states[from] = to
    else
      to_mapping = @regexp_states[from] ||= {}
      # we must match the whole string to a token boundary
      to_mapping[/\A#{sym}\Z/] = to
    end
  else
    raise ArgumentError, "unknown symbol: %s" % sym.class
  end
end

#accepting?(state) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'actionpack/lib/action_dispatch/journey/gtg/transition_table.rb', line 34

def accepting?(state)
  @accepting[state]
end

#accepting_statesObject



30
31
32
# File 'actionpack/lib/action_dispatch/journey/gtg/transition_table.rb', line 30

def accepting_states
  @accepting.keys
end

#add_accepting(state) ⇒ Object



26
27
28
# File 'actionpack/lib/action_dispatch/journey/gtg/transition_table.rb', line 26

def add_accepting(state)
  @accepting[state] = true
end

#add_memo(idx, memo) ⇒ Object



38
39
40
# File 'actionpack/lib/action_dispatch/journey/gtg/transition_table.rb', line 38

def add_memo(idx, memo)
  @memos[idx] << memo
end

#as_json(options = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'actionpack/lib/action_dispatch/journey/gtg/transition_table.rb', line 104

def as_json(options = nil)
  simple_regexp = Hash.new { |h, k| h[k] = {} }

  @regexp_states.each do |from, hash|
    hash.each do |re, to|
      simple_regexp[from][re.source] = to
    end
  end

  {
    regexp_states:   simple_regexp,
    string_states:   @string_states,
    stdparam_states: @stdparam_states,
    accepting:       @accepting
  }
end

#eclosure(t) ⇒ Object



46
47
48
# File 'actionpack/lib/action_dispatch/journey/gtg/transition_table.rb', line 46

def eclosure(t)
  Array(t)
end

#memo(idx) ⇒ Object



42
43
44
# File 'actionpack/lib/action_dispatch/journey/gtg/transition_table.rb', line 42

def memo(idx)
  @memos[idx]
end

#move(t, full_string, token, start_index, token_matches_default) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'actionpack/lib/action_dispatch/journey/gtg/transition_table.rb', line 50

def move(t, full_string, token, start_index, token_matches_default)
  return [] if t.empty?

  next_states = []

  transitions_count = t.size
  i = 0
  while i < transitions_count
    s = t[i]
    previous_start = t[i + 1]
    if previous_start.nil?
      # In the simple case of a "default" param regex do this fast-path and add all
      # next states.
      if token_matches_default && std_state = @stdparam_states[s]
        next_states << std_state << nil
      end

      # When we have a literal string, we can just pull the next state
      if states = @string_states[s]
        state = states[token]
        next_states << state << nil unless state.nil?
      end
    end

    # For regexes that aren't the "default" style, they may potentially not be
    # terminated by the first "token" [./?], so we need to continue to attempt to
    # match this regexp as well as any successful paths that continue out of it.
    # both paths could be valid.
    if states = @regexp_states[s]
      slice_start = if previous_start.nil?
        start_index
      else
        previous_start
      end

      slice_length = start_index + token.length - slice_start
      curr_slice = full_string.slice(slice_start, slice_length)

      states.each { |re, v|
        # if we match, we can try moving past this
        next_states << v << nil if !v.nil? && re.match?(curr_slice)
      }

      # and regardless, we must continue accepting tokens and retrying this regexp. we
      # need to remember where we started as well so we can take bigger slices.
      next_states << s << slice_start
    end

    i += 2
  end

  next_states
end

#statesObject



188
189
190
191
192
193
# File 'actionpack/lib/action_dispatch/journey/gtg/transition_table.rb', line 188

def states
  ss = @string_states.keys + @string_states.values.flat_map(&:values)
  ps = @stdparam_states.keys + @stdparam_states.values
  rs = @regexp_states.keys + @regexp_states.values.flat_map(&:values)
  (ss + ps + rs).uniq
end

#to_svgObject



121
122
123
124
125
126
127
128
129
# File 'actionpack/lib/action_dispatch/journey/gtg/transition_table.rb', line 121

def to_svg
  svg = IO.popen("dot -Tsvg", "w+") { |f|
    f.write(to_dot)
    f.close_write
    f.readlines
  }
  3.times { svg.shift }
  svg.join.sub(/width="[^"]*"/, "").sub(/height="[^"]*"/, "")
end

#transitionsObject



195
196
197
198
199
200
201
202
203
# File 'actionpack/lib/action_dispatch/journey/gtg/transition_table.rb', line 195

def transitions
  @string_states.flat_map { |from, hash|
    hash.map { |s, to| [from, s, to] }
  } + @stdparam_states.map { |from, to|
    [from, DEFAULT_EXP_ANCHORED, to]
  } + @regexp_states.flat_map { |from, hash|
    hash.map { |s, to| [from, s, to] }
  }
end

#visualizer(paths, title = "FSM") ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'actionpack/lib/action_dispatch/journey/gtg/transition_table.rb', line 131

def visualizer(paths, title = "FSM")
  viz_dir   = File.join __dir__, "..", "visualizer"
  fsm_js    = File.read File.join(viz_dir, "fsm.js")
  fsm_css   = File.read File.join(viz_dir, "fsm.css")
  erb       = File.read File.join(viz_dir, "index.html.erb")
  states    = "function tt() { return #{to_json}; }"

  fun_routes = paths.sample(3).map do |ast|
    ast.filter_map { |n|
      case n
      when Nodes::Symbol
        case n.left
        when ":id" then rand(100).to_s
        when ":format" then %w{ xml json }.sample
        else
          "omg"
        end
      when Nodes::Terminal then n.symbol
      else
        nil
      end
    }.join
  end

  stylesheets = [fsm_css]
  svg         = to_svg
  javascripts = [states, fsm_js]

  fun_routes  = fun_routes
  stylesheets = stylesheets
  svg         = svg
  javascripts = javascripts

  require "erb"
  template = ERB.new erb
  template.result(binding)
end