2018-07-20 13:03:28 +00:00
|
|
|
from transitions import State
|
|
|
|
|
2019-07-24 13:17:16 +00:00
|
|
|
from .fsm_base import FSMBase
|
2018-04-07 13:21:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LinearFSM(FSMBase):
|
2018-05-04 15:45:16 +00:00
|
|
|
# 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
|
2018-05-04 15:45:16 +00:00
|
|
|
a number of steps specified in the constructor. It automatically sets up 2
|
|
|
|
actions (triggers) between states as such:
|
2018-07-27 12:00:07 +00:00
|
|
|
(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):
|
2018-07-27 13:03:16 +00:00
|
|
|
"""
|
|
|
|
:param number_of_steps: how many states this FSM should have
|
|
|
|
"""
|
2018-07-20 13:03:28 +00:00
|
|
|
self.states = [State(name=str(index)) for index in range(number_of_steps)]
|
2018-05-04 15:45:16 +00:00
|
|
|
self.transitions = []
|
2018-07-20 13:03:28 +00:00
|
|
|
for state in self.states[:-1]:
|
2018-06-04 20:16:44 +00:00
|
|
|
self.transitions.append({
|
2018-07-20 13:03:28 +00:00
|
|
|
'trigger': f'step_{int(state.name)+1}',
|
|
|
|
'source': state.name,
|
|
|
|
'dest': str(int(state.name)+1)
|
2018-06-04 20:16:44 +00:00
|
|
|
})
|
|
|
|
self.transitions.append({
|
|
|
|
'trigger': 'step_next',
|
2018-07-20 13:03:28 +00:00
|
|
|
'source': state.name,
|
|
|
|
'dest': str(int(state.name)+1)
|
2018-06-04 20:16:44 +00:00
|
|
|
})
|
2018-04-07 13:21:13 +00:00
|
|
|
super(LinearFSM, self).__init__()
|