Class: Fae::State

Inherits:
Object
  • Object
show all
Defined in:
lib/fae/state.rb

Overview

A state in the state diagram.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, paths, accepting) ⇒ State

Creates a new state instance.

Examples:

State.new('A', { :a => 'B', :b => 'A' }, true)

Parameters:

  • name (String)

    the state name, i.e.: “A”

  • next_states (Hash)

    a hash of next states from this state

  • valid (Boolean)

    whether or not this is an accepting state



14
15
16
17
18
# File 'lib/fae/state.rb', line 14

def initialize(name, paths, accepting)
  @name = name
  @paths = paths
  @accepting = accepting
end

Instance Attribute Details

#acceptingObject

Returns the value of attribute accepting.



5
6
7
# File 'lib/fae/state.rb', line 5

def accepting
  @accepting
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/fae/state.rb', line 5

def name
  @name
end

#pathsObject

Returns the value of attribute paths.



5
6
7
# File 'lib/fae/state.rb', line 5

def paths
  @paths
end

Instance Method Details

#evaluate(string, fa) ⇒ Object

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

Parameters:

  • string (String)

    the string to evaluate

  • fa (FiniteAutomata)

    the finite automata that this state belongs to



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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