baseimage-tutorial-framework/lib/tfw/fsm/fsm_base.py

88 lines
2.9 KiB
Python
Raw Normal View History

# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
# All Rights Reserved. See LICENSE file for details.
2019-06-10 13:32:45 +00:00
import logging
from collections import defaultdict
2018-07-24 15:16:57 +00:00
from datetime import datetime
2018-07-06 14:40:27 +00:00
from transitions import Machine, MachineError
from tfw.mixins.callback_mixin import CallbackMixin
LOG = logging.getLogger(__name__)
class FSMBase(Machine, CallbackMixin):
2018-04-18 17:44:26 +00:00
"""
A general FSM base class you can inherit from to track user progress.
See linear_fsm.py for an example use-case.
2018-06-29 13:59:03 +00:00
TFW uses the transitions library for state machines, please refer to their
2018-04-18 17:44:26 +00:00
documentation for more information on creating your own machines:
https://github.com/pytransitions/transitions
"""
states, transitions = [], []
def __init__(self, initial=None, accepted_states=None):
"""
:param initial: which state to begin with, defaults to the last one
:param accepted_states: list of states in which the challenge should be
considered successfully completed
"""
2018-07-20 12:38:26 +00:00
self.accepted_states = accepted_states or [self.states[-1].name]
self.trigger_predicates = defaultdict(list)
2018-07-24 15:16:57 +00:00
self.event_log = []
Machine.__init__(
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'
)
2018-02-23 11:07:30 +00:00
def execute_callbacks(self, event_data):
self._execute_callbacks(event_data.kwargs)
def is_solved(self):
return self.state in self.accepted_states # pylint: disable=no-member
def subscribe_predicate(self, trigger, *predicates):
self.trigger_predicates[trigger].extend(predicates)
def unsubscribe_predicate(self, trigger, *predicates):
self.trigger_predicates[trigger] = [
predicate
for predicate in self.trigger_predicates[trigger]
not in predicates
]
def step(self, trigger):
predicate_results = (
predicate()
for predicate in self.trigger_predicates[trigger]
)
if all(predicate_results):
try:
2018-07-24 15:16:57 +00:00
from_state = self.state
self.trigger(trigger)
2018-07-24 15:16:57 +00:00
self.update_event_log(from_state, trigger)
return True
2018-07-06 14:40:27 +00:00
except (AttributeError, MachineError):
LOG.debug('FSM failed to execute nonexistent trigger: "%s"', trigger)
return False
2018-07-24 15:16:57 +00:00
def update_event_log(self, from_state, trigger):
self.event_log.append({
'from_state': from_state,
'to_state': self.state,
'trigger': trigger,
'timestamp': datetime.utcnow()
})
@property
def in_accepted_state(self):
return self.state in self.accepted_states