Class: Journey::GTG::TransitionTable

### Home Classes Methods

### In Files

- lib/journey/gtg/transition_table.rb

### Parent

Object

### Methods

- ::new
- #[]=
- #accepting?
- #accepting_states
- #add_accepting
- #add_memo
- #eclosure
- #memo
- #move
- #states
- #to_json
- #to_svg
- #transitions
- #visualizer

### Included Modules

- Journey::NFA::Dot

### Files

- CHANGELOG.rdoc
- Manifest.txt
- README.rdoc

### Class/Module Index

Quicksearch

- Journey
- Journey::Formatter
- Journey::GTG
- Journey::GTG::Builder
- Journey::GTG::MatchData
- Journey::GTG::Simulator
- Journey::GTG::TransitionTable
- Journey::NFA
- Journey::NFA::Builder
- Journey::NFA::MatchData
- Journey::NFA::Simulator
- Journey::NFA::TransitionTable
- Journey::NFA::Visitor
- Journey::Nodes
- Journey::Nodes::Binary
- Journey::Nodes::Cat
- Journey::Nodes::Dummy
- Journey::Nodes::Group
- Journey::Nodes::Literal
- Journey::Nodes::Or
- Journey::Nodes::Star
- Journey::Nodes::Symbol
- Journey::Nodes::Terminal
- Journey::Nodes::Unary
- Journey::Parser
- Journey::Path
- Journey::Path::Pattern
- Journey::Path::Pattern::MatchData
- Journey::Route
- Journey::Router
- Journey::Router::RoutingError
- Journey::Router::Strexp
- Journey::Router::Utils
- Journey::Router::Utils::UriEscape
- Journey::Routes
- Journey::Scanner
- Rack

No matching classes.

# Journey::GTG::TransitionTable

### Attributes

memos[R]

### Public Class Methods

new() click to toggle source

    # File lib/journey/gtg/transition_table.rb, line 10
    def initialize
      @regexp_states = Hash.new { |h,k| h[k] = {} }
      @string_states = Hash.new { |h,k| h[k] = {} }
      @accepting     = {}
      @memos         = Hash.new { |h,k| h[k] = [] }
    end

### Public Instance Methods

[]=(from, to, sym) click to toggle source

    # File lib/journey/gtg/transition_table.rb, line 112
    def []= from, to, sym
      case sym
      when String
        @string_states[from][sym] = to
      when Regexp
        @regexp_states[from][sym] = to
      else
        raise ArgumentError, 'unknown symbol: %s' % sym.class
      end
    end

accepting?(state) click to toggle source

    # File lib/journey/gtg/transition_table.rb, line 25
    def accepting? state
      @accepting[state]
    end

accepting_states() click to toggle source

    # File lib/journey/gtg/transition_table.rb, line 21
    def accepting_states
      @accepting.keys
    end

add_accepting(state) click to toggle source

    # File lib/journey/gtg/transition_table.rb, line 17
    def add_accepting state
      @accepting[state] = true
    end

add_memo(idx, memo) click to toggle source

    # File lib/journey/gtg/transition_table.rb, line 29
    def add_memo idx, memo
      @memos[idx] << memo
    end

eclosure(t) click to toggle source

    # File lib/journey/gtg/transition_table.rb, line 37
    def eclosure t
      Array(t)
    end

memo(idx) click to toggle source

    # File lib/journey/gtg/transition_table.rb, line 33
    def memo idx
      @memos[idx]
    end

move(t, a) click to toggle source

    # File lib/journey/gtg/transition_table.rb, line 41
    def move t, a
      move_string(t, a).concat move_regexp(t, a)
    end

states() click to toggle source

    # File lib/journey/gtg/transition_table.rb, line 123
    def states
      ss = @string_states.keys + @string_states.values.map(&:values).flatten
      rs = @regexp_states.keys + @regexp_states.values.map(&:values).flatten
      (ss + rs).uniq
    end

to_json() click to toggle source

    # File lib/journey/gtg/transition_table.rb, line 45
    def to_json
      require 'json'

      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

      JSON.dump({
        :regexp_states => simple_regexp,
        :string_states => @string_states,
        :accepting     => @accepting
      })
    end

to_svg() click to toggle source

    # File lib/journey/gtg/transition_table.rb, line 63
    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

transitions() click to toggle source

    # File lib/journey/gtg/transition_table.rb, line 129
    def transitions
      @string_states.map { |from, hash|
        hash.map { |s, to| [from, s, to] }
      }.flatten(1) + @regexp_states.map { |from, hash|
        hash.map { |s, to| [from, s, to] }
      }.flatten(1)
    end

visualizer(paths, title = 'FSM') click to toggle source

    # File lib/journey/gtg/transition_table.rb, line 73
    def visualizer paths, title = 'FSM'
      viz_dir   = File.join File.dirname(__FILE__), '..', '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.shuffle.first(3).map do |ast|
        ast.map { |n|
          case n
          when Nodes::Symbol
            case n.left
            when ':id' then rand(100).to_s
            when ':format' then %{ xml json }.shuffle.first
            else
              'omg'
            end
          when Nodes::Terminal then n.symbol
          else
            nil
          end
        }.compact.join
      end

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

      # Annoying hack for 1.9 warnings
      fun_routes  = fun_routes
      stylesheets = stylesheets
      svg         = svg
      javascripts = javascripts

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.