baseimage-tutorial-framework/tfw/fsm/linear_fsm.py

33 lines
1.2 KiB
Python
Raw Normal View History

from transitions import State
from .fsm_base import FSMBase
2018-04-07 13:21:13 +00:00
class LinearFSM(FSMBase):
# pylint: disable=anomalous-backslash-in-string
2018-04-18 17:44:26 +00:00
"""
This is a state machine for challenges with linear progression, consisting of
a number of steps specified in the constructor. It automatically sets up 2
actions (triggers) between states as such:
(0) -- step_1 --> (1) -- step_2 --> (2) -- step_3 --> (3) ...
(0) -- step_next --> (1) -- step_next --> (2) -- step_next --> (3) ...
2018-04-18 17:44:26 +00:00
"""
2018-04-07 13:21:13 +00:00
def __init__(self, number_of_steps):
"""
:param number_of_steps: how many states this FSM should have
"""
self.states = [State(name=str(index)) for index in range(number_of_steps)]
self.transitions = []
for state in self.states[:-1]:
self.transitions.append({
'trigger': f'step_{int(state.name)+1}',
'source': state.name,
'dest': str(int(state.name)+1)
})
self.transitions.append({
'trigger': 'step_next',
'source': state.name,
'dest': str(int(state.name)+1)
})
2018-04-07 13:21:13 +00:00
super(LinearFSM, self).__init__()