2019-06-10 13:32:45 +00:00
|
|
|
import logging
|
2018-07-04 13:48:16 +00:00
|
|
|
from collections import defaultdict
|
2018-07-24 15:16:57 +00:00
|
|
|
from datetime import datetime
|
2019-12-19 12:58:48 +00:00
|
|
|
from contextlib import suppress
|
2018-03-25 13:43:59 +00:00
|
|
|
|
2018-07-06 14:40:27 +00:00
|
|
|
from transitions import Machine, MachineError
|
2017-12-06 00:29:09 +00:00
|
|
|
|
2019-07-24 13:17:16 +00:00
|
|
|
from tfw.internals.callback_mixin import CallbackMixin
|
2018-03-07 09:12:58 +00:00
|
|
|
|
2018-07-04 13:48:16 +00:00
|
|
|
LOG = logging.getLogger(__name__)
|
2017-12-06 00:29:09 +00:00
|
|
|
|
2018-07-04 13:48:16 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
"""
|
2018-01-31 14:10:05 +00:00
|
|
|
states, transitions = [], []
|
2017-12-06 00:29:09 +00:00
|
|
|
|
2018-07-04 13:48:16 +00:00
|
|
|
def __init__(self, initial=None, accepted_states=None):
|
2018-07-27 13:03:16 +00:00
|
|
|
"""
|
|
|
|
: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]
|
2018-07-04 13:48:16 +00:00
|
|
|
self.trigger_predicates = defaultdict(list)
|
2018-07-24 15:16:57 +00:00
|
|
|
self.event_log = []
|
2018-07-04 13:48:16 +00:00
|
|
|
|
|
|
|
Machine.__init__(
|
|
|
|
self,
|
2018-06-04 20:16:44 +00:00
|
|
|
states=self.states,
|
|
|
|
transitions=self.transitions,
|
|
|
|
initial=initial or self.states[0],
|
|
|
|
send_event=True,
|
|
|
|
ignore_invalid_triggers=True,
|
|
|
|
after_state_change='execute_callbacks'
|
|
|
|
)
|
2017-12-06 00:29:09 +00:00
|
|
|
|
2018-02-23 11:07:30 +00:00
|
|
|
def execute_callbacks(self, event_data):
|
2018-03-07 09:12:58 +00:00
|
|
|
self._execute_callbacks(event_data.kwargs)
|
2018-02-09 16:27:51 +00:00
|
|
|
|
|
|
|
def is_solved(self):
|
2018-07-04 13:48:16 +00:00
|
|
|
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):
|
2018-07-10 13:40:10 +00:00
|
|
|
predicate_results = (
|
2018-07-04 13:48:16 +00:00
|
|
|
predicate()
|
|
|
|
for predicate in self.trigger_predicates[trigger]
|
2018-07-10 13:40:10 +00:00
|
|
|
)
|
2018-07-04 13:48:16 +00:00
|
|
|
|
|
|
|
if all(predicate_results):
|
2019-12-19 12:58:48 +00:00
|
|
|
with suppress(AttributeError, MachineError):
|
2018-07-24 15:16:57 +00:00
|
|
|
from_state = self.state
|
2019-12-19 12:58:48 +00:00
|
|
|
if self.trigger(trigger):
|
|
|
|
self.update_event_log(from_state, trigger)
|
|
|
|
return True
|
|
|
|
LOG.debug('FSM failed to execute nonexistent trigger: "%s"', trigger)
|
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,
|
2019-08-23 13:27:03 +00:00
|
|
|
'timestamp': datetime.utcnow().isoformat()
|
2018-07-24 15:16:57 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
@property
|
|
|
|
def in_accepted_state(self):
|
|
|
|
return self.state in self.accepted_states
|