|
2 | 2 |
|
3 | 3 | # Weighted transitions |
4 | 4 |
|
5 | | -```{versionadded} 3.0.0 |
| 5 | +```{seealso} |
| 6 | +See {ref}`conditions` for how the engine selects transitions, and |
| 7 | +{ref}`actions` for callbacks that run during transitions. |
6 | 8 | ``` |
7 | 9 |
|
8 | | -The `weighted_transitions` utility lets you define **probabilistic transitions** — where |
9 | | -each transition from a state has a relative weight that determines how likely it is to be |
10 | | -selected when the event fires. |
| 10 | +The `weighted_transitions` utility lets you define **probabilistic |
| 11 | +transitions** — where each transition from a state has a relative weight |
| 12 | +that determines how likely it is to be selected when the event fires. |
11 | 13 |
|
12 | | -This is a contrib module that works entirely through the existing {ref}`guards` system. |
13 | | -No engine modifications are needed. |
| 14 | +This is a contrib module that works entirely through the existing |
| 15 | +{ref}`conditions` system. No engine modifications are needed. |
14 | 16 |
|
15 | | -## Installation |
16 | | - |
17 | | -The module is included in the `python-statemachine` package. Import it from the contrib |
18 | | -namespace: |
19 | | - |
20 | | -```python |
21 | | -from statemachine.contrib.weighted import weighted_transitions |
22 | | - |
23 | | -# Only needed when passing transition kwargs (cond, on, etc.) |
24 | | -from statemachine.contrib.weighted import to |
25 | | -``` |
26 | 17 |
|
27 | 18 | ## Basic usage |
28 | 19 |
|
29 | | -Pass a **source state** followed by `(target, weight)` tuples. The result is a regular |
30 | | -{ref}`TransitionList` that you assign to a class attribute as an event: |
| 20 | +Import `weighted_transitions` and pass a **source state** followed by |
| 21 | +`(target, weight)` tuples. The result is a regular {ref}`TransitionList` |
| 22 | +that you assign to a class attribute as an event: |
31 | 23 |
|
32 | | -```{testsetup} |
33 | | -
|
34 | | ->>> from statemachine import State, StateChart |
| 24 | +```py |
35 | 25 | >>> from statemachine.contrib.weighted import to, weighted_transitions |
36 | 26 |
|
37 | | -``` |
38 | | - |
39 | | -```py |
40 | 27 | >>> class GameCharacter(StateChart): |
41 | 28 | ... standing = State(initial=True) |
42 | 29 | ... shift_weight = State() |
@@ -243,5 +230,5 @@ Under the hood, `weighted_transitions()`: |
243 | 230 | 5. Only the selected transition's condition returns `True` — the engine picks it. |
244 | 231 |
|
245 | 232 | This means weighted transitions are fully compatible with all engine features: |
246 | | -{ref}`actions`, {ref}`validators-and-guards`, {ref}`listeners`, async engines, |
| 233 | +{ref}`actions`, {ref}`conditions`, {ref}`listeners`, async engines, |
247 | 234 | and {ref}`diagram generation <diagram>`. |
0 commit comments