Class: FSM::FSMState

Inherits:
Object
  • Object
show all
Defined in:
lib/barebone-fsm.rb

Overview

FSMState class represents a state of the finite state machine.

Examples:

Setup the event code for a state through block

state = FSM::FSMState.new :state_name
state.event(:event_name) do
  puts "#{@event} triggered on state #{@state}"
  :next_state
end  

Setup multiple events specifying only the next state for each event using Hash map

state = FSM::FSMState.new :initial_state
state.event :go_left => :left_state, :go_right => :right_state

Trigger an event for the previous example

state.event :go_left

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state_machine, state_name) ⇒ FSMState

Returns a new instance of FSMState.

Parameters:

  • state_machine (FSM::FSM)

    the FSM object representing the state machine containing this state

  • state_name (Symbol)

    the unique name for this state



52
53
54
55
56
# File 'lib/barebone-fsm.rb', line 52

def initialize(state_machine, state_name) 
  @fsm = state_machine
  @state = state_name
  @events = {}
end

Instance Attribute Details

#eventsObject (readonly)

The events for this state mapped to corresponding event codes.



46
47
48
# File 'lib/barebone-fsm.rb', line 46

def events
  @events
end

#stateObject (readonly)

The name of the state.



43
44
45
# File 'lib/barebone-fsm.rb', line 43

def state
  @state
end

Instance Method Details

#build(&build_block) ⇒ Object Also known as: run

The #build/#run method sets up the events described as DSL code in the build_block. Only event method is supported within the build_block with the name of the event and an optional block supplied. The operation for each such line is carried out by the #event method.

Examples:

Using block to setup events

state.build do
  event :event_name do
    puts "#{@event} triggered on state #{@state}"
    :next_state
  end
end

Using block to trigger events

state.build do
  event :event_name
end

Parameters:

  • build_block (block)

    the block of code with sevaral event methods

See Also:



136
137
138
# File 'lib/barebone-fsm.rb', line 136

def build(&build_block)
  self.instance_eval &build_block
end

#event(event_name, &event_block) ⇒ Object #event(events_hash) ⇒ Object #event(event_name) ⇒ Object

Setup or trigger an event. It sets up a new event when the event_block is provided or event_name is a Hash map. The event_name is triggered otherwise. If the event is nil or not already setup, then the default event is triggered.

Examples:

Setup an event by block

state.event(:event_name) do
  puts "#{@event} triggered on state #{@state}"
  :next_state
end  

Setup an event by Hash

state.event :go_left => :left_state, :go_right => :right_state

Trigger an event

state.event :go_left

Overloads:

  • #event(event_name, &event_block) ⇒ Object

    Sets up an event for this state with the given event_block parameter.

    Parameters:

    • event_name (Symbol)

      the name of the event to set up

    • event_block (block)

      the block of code to run when this event will be triggered

  • #event(events_hash) ⇒ Object

    Sets up multiple events where each element of the Hash map corresponds to one event.

    Parameters:

    • events_hash (Hash<Symbol,Symbol>)

      the Hash object mapping events to the corresponding next states

  • #event(event_name) ⇒ Object

    Triggers the event named event_name for this state.

    Parameters:

    • event_name (Symbol)

      the name of the event to trigger



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/barebone-fsm.rb', line 99

def event(event_name, &event_block)
  if block_given? then
    @events[event_name] = event_block
  elsif event_name.is_a?(Hash) then
    event_name.each{ |ev, st|
      @events[ev] = Proc.new{st}
    }
  elsif event_name and @events.has_key? event_name then
    @fsm.event = event_name
    @fsm.instance_eval &@events[event_name]
  elsif @events.has_key? :default then
    @fsm.event = :default
    @fsm.instance_eval &@events[:default]
  end
end

#to_sObject

A String representation of the FSMState object.



61
62
63
64
65
66
# File 'lib/barebone-fsm.rb', line 61

def to_s() 
  @state.to_s + 
    ": [" + 
      @events.keys.map(&:to_s).join(', ') + 
    "]" 
end