baseimage-tutorial-framework/lib/tfw/fsm_base.py
2018-03-07 11:45:57 +01:00

26 lines
945 B
Python

from typing import List
from transitions import Machine
from tfw.components.mixins.callback_mixin import CallbackMixin
class FSMBase(CallbackMixin):
states, transitions = [], []
def __init__(self, initial: str = None, accepted_states: List[str] = None):
CallbackMixin.__init__(self)
self.accepted_states = accepted_states or [self.states[-1]]
self.machine = Machine(model=self,
states=self.states,
transitions=self.transitions,
initial=initial or self.states[0],
send_event=True,
ignore_invalid_triggers=True,
after_state_change='execute_callbacks')
def execute_callbacks(self, event_data):
self._execute_callbacks(event_data.kwargs)
def is_solved(self):
return self.state in self.accepted_states