class Fae::State

A state in the state diagram.

Attributes

accepting[RW]
name[RW]
paths[RW]

Public Class Methods

new(name, paths, accepting) click to toggle source

Creates a new state instance.

@param name [String] the state name, i.e.: “A” @param next_states [Hash] a hash of next states from this state @param valid [Boolean] whether or not this is an accepting state @example

State.new('A', { :a => 'B', :b => 'A' }, true)
# File lib/fae/state.rb, line 14
def initialize(name, paths, accepting)
  @name = name
  @paths = paths
  @accepting = accepting
end

Public Instance Methods

evaluate(string, fa) click to toggle source

Evaluates a string at this state, and passes the next string to the next state.

@param string [String] the string to evaluate @param fa [FiniteAutomata] the finite automata that this state belongs to

# File lib/fae/state.rb, line 24
def evaluate(string, fa)
  output = ""
  if (string.first.empty?)
    output << "#{@name} (#{@accepting ? 'accepting'.colorize(:green) : 'not accepting'.colorize(:red)}) "
    return { :output => output, :accepting => @accepting }
  end
  output << "#{@name} #{'->'.colorize(:light_black)} "
  
  next_state  = fa.get_state(paths[string.first.to_sym])
  next_string = string.shift_left
  result = next_state.evaluate(next_string, fa)
  output << result[:output]
  return { :output => output, :accepting => result[:accepting] }
end