Class: StateMachine

Inherits:
Object
  • Object
show all
Defined in:
state_machine.rb

Overview

An implementation of a state machine. It's states are represented with the State class while it's transitions are instances of Transition. It logs all movements to a logger and moves between states according to line.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ StateMachine


18
19
20
21
22
23
# File 'state_machine.rb', line 18

def initialize(args = {})
  self.current_state = args.fetch(:state, nil)
  self.line = args.fetch(:line, nil)
  self.logger = args.fetch(:logger, self)
  self.token = ''
end

Instance Attribute Details

#current_stateState


13
14
15
# File 'state_machine.rb', line 13

def current_state
  @current_state
end

#last_stateState


16
17
18
# File 'state_machine.rb', line 16

def last_state
  @last_state
end

#lineString


7
8
9
# File 'state_machine.rb', line 7

def line
  @line
end

#loggerLogger


10
11
12
# File 'state_machine.rb', line 10

def logger
  @logger
end

#tokenString (protected)

the whole token the state machine has processed until now.


81
82
83
# File 'state_machine.rb', line 81

def token
  @token
end

Instance Method Details

#executenil

Starts the execution of the state machine


33
34
35
36
37
38
39
40
41
42
# File 'state_machine.rb', line 33

def execute
  state = current_state
  line.each_char do |character|
    state = current_state
    transition = process_character(character)
    log_initial_transition(transition, state)
    self.token += character
  end
  logger.log(state: state, character: token)
end

#get_and_set_next_transition(character) ⇒ Transition

Gets the next transition that is valid for the character. And sets the state machine's current state. Will set #current_state to Transition#to_state.


61
62
63
64
65
# File 'state_machine.rb', line 61

def get_and_set_next_transition(character)
  transition = get_transition(character)
  self.current_state = transition.to_state
  transition
end

#get_transition(character) ⇒ Transition (protected)

Gets the next transition that is valid for the character. Will set #current_state to Transition#to_state.


88
89
90
# File 'state_machine.rb', line 88

def get_transition(character)
  current_state.get_next_state(character)
end

#log_initial_transition(transition, state) ⇒ nil (protected)

Calls Logger#log if the transition is initial. It will also reset the state machine's token.


95
96
97
98
99
100
# File 'state_machine.rb', line 95

def log_initial_transition(transition, state)
  if transition.initial?
    logger.log(state: state, transition: transition, character: token)
    reset_token
  end
end

#process_character(character) ⇒ Transition

Gets the next transition after processing a character. Won't return a transition that responds true for transition#initial?


49
50
51
52
53
# File 'state_machine.rb', line 49

def process_character(character)
  transition = get_next_transition(character)
  get_and_set_next_transition(character) if transition.initial?
  transition
end

#resetnil

Resets the State Machine's state.


27
28
29
# File 'state_machine.rb', line 27

def reset
  reset_token
end

#reset_tokennil (protected)

Resets the state machine's token


104
105
106
# File 'state_machine.rb', line 104

def reset_token
  self.token = ''
end