|
| 1 | +(behaviour)= |
| 2 | +(statecharts)= |
| 3 | + |
| 4 | +# Behaviour |
| 5 | + |
| 6 | +The `StateChart` class follows the |
| 7 | +[SCXML specification](https://www.w3.org/TR/scxml/) by default. The |
| 8 | +`StateMachine` class extends `StateChart` but overrides several defaults to |
| 9 | +preserve backward compatibility with existing code. |
| 10 | + |
| 11 | +The behavioral differences are controlled by class-level attributes. This |
| 12 | +design allows a gradual upgrade path: start from `StateMachine` and selectively |
| 13 | +enable spec-compliant behaviors one at a time, or start from `StateChart` and |
| 14 | +get full SCXML compliance out of the box. |
| 15 | + |
| 16 | +```{tip} |
| 17 | +We **strongly recommend** that new projects use `StateChart` directly. Existing |
| 18 | +projects should consider migrating when possible, as the SCXML-compliant |
| 19 | +behavior provides more predictable semantics. |
| 20 | +``` |
| 21 | + |
| 22 | + |
| 23 | +## Comparison table |
| 24 | + |
| 25 | +| Attribute | `StateChart` | `StateMachine` | Description | |
| 26 | +|------------------------------------|---------------|----------------|-------------| |
| 27 | +| `allow_event_without_transition` | `True` | `False` | Tolerate events that don't match any transition | |
| 28 | +| `enable_self_transition_entries` | `True` | `False` | Execute entry/exit actions on self-transitions | |
| 29 | +| `atomic_configuration_update` | `False` | `True` | When to update {ref}`configuration <querying-configuration>` during a microstep | |
| 30 | +| `error_on_execution` | `True` | `False` | Catch runtime errors as `error.execution` events | |
| 31 | + |
| 32 | + |
| 33 | +## `allow_event_without_transition` |
| 34 | + |
| 35 | +When `True` (SCXML default), sending an event that does not match any enabled |
| 36 | +transition is silently ignored. When `False` (legacy default), a |
| 37 | +`TransitionNotAllowed` exception is raised, including for unknown event names. |
| 38 | + |
| 39 | +The SCXML spec requires tolerance to unmatched events, as the event-driven model |
| 40 | +expects that not every event is relevant in every state. |
| 41 | + |
| 42 | + |
| 43 | +## `enable_self_transition_entries` |
| 44 | + |
| 45 | +When `True` (SCXML default), a {ref}`self-transition <self-transition>` executes |
| 46 | +the state's exit and entry actions, just like any other transition. When `False` |
| 47 | +(legacy default), self-transitions skip entry/exit actions. |
| 48 | + |
| 49 | +The SCXML spec treats self-transitions as regular transitions that happen to |
| 50 | +return to the same state, so entry/exit actions must fire. Use an |
| 51 | +{ref}`internal transition <internal-transition>` if you need a transition that |
| 52 | +stays in the same state **without** running exit/entry actions. |
| 53 | + |
| 54 | + |
| 55 | +## `atomic_configuration_update` |
| 56 | + |
| 57 | +When `False` (SCXML default), a microstep follows the SCXML processing order: |
| 58 | +first exit all states in the exit set (running exit callbacks), then execute the |
| 59 | +transition content (`on` callbacks), then enter all states in the entry set |
| 60 | +(running entry callbacks). During the `on` callbacks, the |
| 61 | +{ref}`configuration <querying-configuration>` may be empty or partial. |
| 62 | + |
| 63 | +When `True` (legacy default), the configuration is updated atomically after the |
| 64 | +`on` callbacks, so `sm.configuration` and `state.is_active` always reflect a |
| 65 | +consistent snapshot during the transition. This was the behavior of all previous |
| 66 | +versions. |
| 67 | + |
| 68 | +```{note} |
| 69 | +When `atomic_configuration_update` is `False`, `on` callbacks can request |
| 70 | +`previous_configuration` and `new_configuration` keyword arguments to inspect |
| 71 | +which states were active before and after the microstep. See |
| 72 | +{ref}`dependency-injection` for the full parameter list. |
| 73 | +``` |
| 74 | + |
| 75 | + |
| 76 | +## `error_on_execution` |
| 77 | + |
| 78 | +When `True` (SCXML default), runtime exceptions in callbacks (guards, actions, |
| 79 | +entry/exit) are caught by the engine and result in an internal `error.execution` |
| 80 | +event. When `False` (legacy default), exceptions propagate normally to the |
| 81 | +caller. |
| 82 | + |
| 83 | +```{seealso} |
| 84 | +See {ref}`error-handling` for the full `error.execution` lifecycle, block-level |
| 85 | +error catching, and the cleanup/finalize pattern. |
| 86 | +``` |
| 87 | + |
| 88 | + |
| 89 | +## Gradual migration |
| 90 | + |
| 91 | +You can override any of these attributes individually. For example, to adopt |
| 92 | +SCXML error handling in an existing `StateMachine` without changing other |
| 93 | +behaviors: |
| 94 | + |
| 95 | +```python |
| 96 | +class MyMachine(StateMachine): |
| 97 | + error_on_execution = True |
| 98 | + # ... everything else behaves as before ... |
| 99 | +``` |
| 100 | + |
| 101 | +Or to use `StateChart` but keep the legacy atomic configuration update: |
| 102 | + |
| 103 | +```python |
| 104 | +class MyChart(StateChart): |
| 105 | + atomic_configuration_update = True |
| 106 | + # ... SCXML-compliant otherwise ... |
| 107 | +``` |
| 108 | + |
| 109 | +```{seealso} |
| 110 | +See [](releases/upgrade_2x_to_3.md) for a complete migration guide from |
| 111 | +`StateMachine` 2.x to `StateChart` 3.x. |
| 112 | +``` |
0 commit comments