|
| 1 | +from ..event_data import EventData |
| 2 | +from ..event_data import TriggerData |
| 3 | +from ..exceptions import TransitionNotAllowed |
| 4 | +from ..transition import Transition |
| 5 | +from .base import BaseEngine |
| 6 | + |
| 7 | + |
| 8 | +class StateChartEngine(BaseEngine): |
| 9 | + def __init__(self, sm, rtc: bool = True): |
| 10 | + super().__init__(sm, rtc) |
| 11 | + self.activate_initial_state() |
| 12 | + |
| 13 | + def activate_initial_state(self): |
| 14 | + """ |
| 15 | + Activate the initial state. |
| 16 | +
|
| 17 | + Called automatically on state machine creation from sync code, but in |
| 18 | + async code, the user must call this method explicitly. |
| 19 | +
|
| 20 | + Given how async works on python, there's no built-in way to activate the initial state that |
| 21 | + may depend on async code from the StateMachine.__init__ method. |
| 22 | + """ |
| 23 | + return self.processing_loop() |
| 24 | + |
| 25 | + def processing_loop(self): |
| 26 | + """Process event triggers. |
| 27 | +
|
| 28 | + The simplest implementation is the non-RTC (synchronous), |
| 29 | + where the trigger will be run immediately and the result collected as the return. |
| 30 | +
|
| 31 | + .. note:: |
| 32 | +
|
| 33 | + While processing the trigger, if others events are generated, they |
| 34 | + will also be processed immediately, so a "nested" behavior happens. |
| 35 | +
|
| 36 | + If the machine is on ``rtc`` model (queued), the event is put on a queue, and only the |
| 37 | + first event will have the result collected. |
| 38 | +
|
| 39 | + .. note:: |
| 40 | + While processing the queue items, if others events are generated, they |
| 41 | + will be processed sequentially (and not nested). |
| 42 | +
|
| 43 | + """ |
| 44 | + if not self._rtc: |
| 45 | + # The machine is in "synchronous" mode |
| 46 | + trigger_data = self._external_queue.popleft() |
| 47 | + return self._trigger(trigger_data) |
| 48 | + |
| 49 | + # We make sure that only the first event enters the processing critical section, |
| 50 | + # next events will only be put on the queue and processed by the same loop. |
| 51 | + if not self._processing.acquire(blocking=False): |
| 52 | + return None |
| 53 | + |
| 54 | + # We will collect the first result as the processing result to keep backwards compatibility |
| 55 | + # so we need to use a sentinel object instead of `None` because the first result may |
| 56 | + # be also `None`, and on this case the `first_result` may be overridden by another result. |
| 57 | + first_result = self._sentinel |
| 58 | + try: |
| 59 | + # Execute the triggers in the queue in FIFO order until the queue is empty |
| 60 | + while self._external_queue: |
| 61 | + trigger_data = self._external_queue.popleft() |
| 62 | + try: |
| 63 | + result = self._trigger(trigger_data) |
| 64 | + if first_result is self._sentinel: |
| 65 | + first_result = result |
| 66 | + except Exception: |
| 67 | + # Whe clear the queue as we don't have an expected behavior |
| 68 | + # and cannot keep processing |
| 69 | + self._external_queue.clear() |
| 70 | + raise |
| 71 | + finally: |
| 72 | + self._processing.release() |
| 73 | + return first_result if first_result is not self._sentinel else None |
| 74 | + |
| 75 | + def _trigger(self, trigger_data: TriggerData): |
| 76 | + event_data = None |
| 77 | + if trigger_data.event == "__initial__": |
| 78 | + transition = Transition(None, self.sm._get_initial_state(), event="__initial__") |
| 79 | + transition._specs.clear() |
| 80 | + event_data = EventData(trigger_data=trigger_data, transition=transition) |
| 81 | + self._activate(event_data) |
| 82 | + return self._sentinel |
| 83 | + |
| 84 | + state = self.sm.current_state |
| 85 | + for transition in state.transitions: |
| 86 | + if not transition.match(trigger_data.event): |
| 87 | + continue |
| 88 | + |
| 89 | + event_data = EventData(trigger_data=trigger_data, transition=transition) |
| 90 | + args, kwargs = event_data.args, event_data.extended_kwargs |
| 91 | + self.sm._get_callbacks(transition.validators.key).call(*args, **kwargs) |
| 92 | + if not self.sm._get_callbacks(transition.cond.key).all(*args, **kwargs): |
| 93 | + continue |
| 94 | + |
| 95 | + result = self._activate(event_data) |
| 96 | + event_data.result = result |
| 97 | + event_data.executed = True |
| 98 | + break |
| 99 | + else: |
| 100 | + if not self.sm.allow_event_without_transition: |
| 101 | + raise TransitionNotAllowed(trigger_data.event, state) |
| 102 | + |
| 103 | + return event_data.result if event_data else None |
| 104 | + |
| 105 | + def _activate(self, event_data: EventData): |
| 106 | + args, kwargs = event_data.args, event_data.extended_kwargs |
| 107 | + transition = event_data.transition |
| 108 | + source = event_data.state |
| 109 | + target = transition.target |
| 110 | + |
| 111 | + result = self.sm._get_callbacks(transition.before.key).call(*args, **kwargs) |
| 112 | + if source is not None and not transition.internal: |
| 113 | + self.sm._get_callbacks(source.exit.key).call(*args, **kwargs) |
| 114 | + |
| 115 | + result += self.sm._get_callbacks(transition.on.key).call(*args, **kwargs) |
| 116 | + |
| 117 | + self.sm.current_state = target |
| 118 | + event_data.state = target |
| 119 | + kwargs["state"] = target |
| 120 | + |
| 121 | + if not transition.internal: |
| 122 | + self.sm._get_callbacks(target.enter.key).call(*args, **kwargs) |
| 123 | + self.sm._get_callbacks(transition.after.key).call(*args, **kwargs) |
| 124 | + |
| 125 | + if len(result) == 0: |
| 126 | + result = None |
| 127 | + elif len(result) == 1: |
| 128 | + result = result[0] |
| 129 | + |
| 130 | + return result |
0 commit comments