2018-04-03 12:49:14 +00:00
|
|
|
# Copyright (C) 2018 Avatao.com Innovative Learning Kft.
|
|
|
|
# All Rights Reserved. See LICENSE file for details.
|
|
|
|
|
2018-02-09 16:27:51 +00:00
|
|
|
from typing import List
|
2018-03-25 13:43:59 +00:00
|
|
|
|
2017-12-06 00:29:09 +00:00
|
|
|
from transitions import Machine
|
|
|
|
|
2018-04-14 19:15:30 +00:00
|
|
|
from tfw.mixins import CallbackMixin
|
2018-03-07 09:12:58 +00:00
|
|
|
|
2017-12-06 00:29:09 +00:00
|
|
|
|
2018-03-07 09:12:58 +00:00
|
|
|
class FSMBase(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.
|
|
|
|
TFW the transitions library for state machines, please refer to their
|
|
|
|
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-02-09 16:27:51 +00:00
|
|
|
def __init__(self, initial: str = None, accepted_states: List[str] = None):
|
|
|
|
self.accepted_states = accepted_states or [self.states[-1]]
|
2018-06-04 20:16:44 +00:00
|
|
|
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'
|
|
|
|
)
|
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-03-30 16:11:38 +00:00
|
|
|
return self.state in self.accepted_states # pylint: disable=no-member
|