You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -377,25 +377,25 @@ There's a lot more to cover, please take a look at our docs:
377
377
https://python-statemachine.readthedocs.io.
378
378
379
379
380
-
## Contributing to the project
380
+
## Contributing
381
381
382
382
* <aclass="github-button"href="https://github.com/fgmacedo/python-statemachine"data-icon="octicon-star"aria-label="Star fgmacedo/python-statemachine on GitHub">Star this project</a>
383
383
* <aclass="github-button"href="https://github.com/fgmacedo/python-statemachine/issues"data-icon="octicon-issue-opened"aria-label="Issue fgmacedo/python-statemachine on GitHub">Open an Issue</a>
384
384
* <aclass="github-button"href="https://github.com/fgmacedo/python-statemachine/fork"data-icon="octicon-repo-forked"aria-label="Fork fgmacedo/python-statemachine on GitHub">Fork</a>
385
385
386
386
- If you found this project helpful, please consider giving it a star on GitHub.
387
387
388
-
-**Contribute code**: If you would like to contribute code to this project, please submit a pull
388
+
-**Contribute code**: If you would like to contribute code, please submit a pull
389
389
request. For more information on how to contribute, please see our [contributing.md](contributing.md) file.
390
390
391
-
-**Report bugs**: If you find any bugs in this project, please report them by opening an issue
391
+
-**Report bugs**: If you find any bugs, please report them by opening an issue
392
392
on our GitHub issue tracker.
393
393
394
-
-**Suggest features**: If you have a great idea for a new feature, please let us know by opening
395
-
an issue on our GitHub issue tracker.
394
+
-**Suggest features**: If you have an idea for a new feature, of feels something being harder than it should be,
395
+
please let us know by opening an issue on our GitHub issue tracker.
396
396
397
-
-**Documentation**: Help improve this project's documentation by submitting pull requests.
397
+
-**Documentation**: Help improve documentation by submitting pull requests.
398
398
399
-
-**Promote the project**: Help spread the word about this project by sharing it on social media,
399
+
-**Promote the project**: Help spread the word by sharing on social media,
400
400
writing a blog post, or giving a talk about it. Tag me on Twitter
401
401
[@fgmacedo](https://twitter.com/fgmacedo) so I can share it too!
Copy file name to clipboardExpand all lines: docs/async.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,9 @@
4
4
Support for async code was added!
5
5
```
6
6
7
-
The {ref}`StateMachine` fully supports asynchronous code. You can write async {ref}`actions`, {ref}`guards`, and {ref}`event` triggers, while maintaining the same external API for both synchronous and asynchronous codebases.
7
+
The {ref}`StateMachine` fully supports asynchronous code. You can write async {ref}`actions`, {ref}`guards`, and {ref}`events` triggers, while maintaining the same external API for both synchronous and asynchronous codebases.
8
8
9
-
This is achieved through a new concept called "engine," an internal strategy pattern abstraction that manages transitions and callbacks.
9
+
This is achieved through a new concept called **engine**, an internal strategy pattern abstraction that manages transitions and callbacks.
10
10
11
11
There are two engines, {ref}`SyncEngine` and {ref}`AsyncEngine`.
This library supports a mini-language for boolean expressions in conditions, allowing the definition of guards that control transitions based on specified criteria. It includes basic [boolean algebra](https://en.wikipedia.org/wiki/Boolean_algebra) operators, parentheses for controlling precedence, and **names** that refer to attributes on the state machine, its associated model, or registered {ref}`Listeners`.
47
+
48
+
```{tip}
49
+
All condition expressions are evaluated when the State Machine is instantiated. This is by design to help you catch any invalid definitions early, rather than when your state machine is running.
50
+
```
51
+
52
+
The mini-language is based on Python's built-in language and the [`ast`](https://docs.python.org/3/library/ast.html) parser, so there are no surprises if you’re familiar with Python. Below is a formal specification to clarify the structure.
53
+
54
+
#### Syntax elements
55
+
56
+
1.**Names**:
57
+
- Names refer to attributes on the state machine instance, its model or listeners, used directly in expressions to evaluate conditions.
58
+
- Names must consist of alphanumeric characters and underscores (`_`) and cannot begin with a digit (e.g., `is_active`, `count`, `has_permission`).
59
+
- Any property name used in the expression must exist as an attribute on the state machine, model instance, or listeners, otherwise, an `InvalidDefinition` error is raised.
60
+
- Names can be pointed to `properties`, `attributes` or `methods`. If pointed to `attributes`, the library will create a
61
+
wrapper get method so each time the expression is evaluated the current value will be retrieved.
62
+
63
+
2.**Boolean operators and precedence**:
64
+
- The following Boolean operators are supported, listed from highest to lowest precedence:
65
+
1.`not` / `!` — Logical negation
66
+
2.`and` / `^` — Logical conjunction
67
+
3.`or` / `v` — Logical disjunction
68
+
- These operators are case-sensitive (e.g., `NOT` and `Not` are not equivalent to `not` and will raise syntax errors).
69
+
- Both formats can be used interchangeably, so `!sauron_alive` and `not sauron_alive` are equivalent.
70
+
71
+
3.**Parentheses for precedence**:
72
+
- When operators with the same precedence appear in the expression, evaluation proceeds from left to right, unless parentheses specify a different order.
73
+
- Parentheses `(` and `)` are supported to control the order of evaluation in expressions.
74
+
- Expressions within parentheses are evaluated first, allowing explicit precedence control (e.g., `(is_admin or is_moderator) and has_permission`).
75
+
76
+
#### Expression Examples
77
+
78
+
Examples of valid boolean expressions include:
79
+
-`is_logged_in and has_permission`
80
+
-`not is_active or is_admin`
81
+
-`!(is_guest ^ has_access)`
82
+
-`(is_admin or is_moderator) and !is_banned`
83
+
-`has_account and (verified or trusted)`
84
+
-`frodo_has_ring and gandalf_present or !sauron_alive`
85
+
86
+
Being used on a transition definition:
87
+
88
+
```python
89
+
start.to(end, cond="frodo_has_ring and gandalf_present or !sauron_alive")
90
+
```
91
+
92
+
#### Summary of grammar rules
93
+
94
+
The mini-language is formally specified as follows:
This release introduces powerful new features for the `StateMachine` library: {ref}`Condition expressions` and explicit definition of {ref}`Events`. These updates make it easier to define complex transition conditions and enhance performance, especially in workflows with nested or recursive event structures.
This release introduces support for conditionals with Boolean algebra. You can now use expressions like `or`, `and`, and `not` directly within transition conditions, simplifying the definition of complex state transitions. This allows for more flexible and readable condition setups in your state machine configurations.
... start.to(end, cond="used_money or used_credit"),
28
+
...name="finish order",
29
+
... )
30
+
...
31
+
... used_money: bool=False
32
+
... used_credit: bool=False
33
+
34
+
>>> sm = AnyConditionSM()
35
+
>>> sm.submit()
36
+
Traceback (most recent call last):
37
+
TransitionNotAllowed: Can't finish order when in Start.
38
+
39
+
>>> sm.used_credit =True
40
+
>>> sm.submit()
41
+
>>> sm.current_state.id
42
+
'end'
43
+
44
+
```
45
+
46
+
```{seealso}
47
+
See {ref}`Condition expressions` for more details or take a look at the {ref}`sphx_glr_auto_examples_lor_machine.py` example.
48
+
```
49
+
50
+
### Explicit event creation in 2.4.0
51
+
52
+
Now you can explicit declare {ref}`Events` using the {ref}`event` class. This allows custom naming, translations, and also helps your IDE to know that events are callable.
... start = Event(created.to(started), name="Launch the machine")
62
+
...
63
+
>>> [e.id for e in StartMachine.events]
64
+
['start']
65
+
>>> [e.name for e in StartMachine.events]
66
+
['Launch the machine']
67
+
>>> StartMachine.start.name
68
+
'Launch the machine'
69
+
70
+
```
71
+
72
+
```{seealso}
73
+
See {ref}`Events` for more details.
74
+
```
75
+
76
+
### Recursive state machines (infinite loop)
77
+
78
+
We removed a note from the docs saying to avoid recursion loops. Since the {ref}`StateMachine 2.0.0` release we've turned the RTC model enabled by default, allowing nested events to occour as all events are put on an internal queue before being executed.
79
+
80
+
```{seealso}
81
+
See {ref}`sphx_glr_auto_examples_recursive_event_machine.py` for an example of an infinite loop state machine declaration using `after` action callback to call the same event over and over again.
82
+
83
+
```
84
+
85
+
86
+
## Bugfixes in 2.4.0
87
+
88
+
- Fixes [#484](https://github.com/fgmacedo/python-statemachine/issues/484) issue where nested events inside loops could leak memory by incorrectly
89
+
referencing previous `event_data` when queuing the next event. This fix improves performance and stability in event-heavy workflows.
0 commit comments