State Machine¶
Core FSM class: load config from YAML or dict, process transitions, and run guards and actions.
StateMachine
¶
StateMachine(
states: dict[str, State],
transitions: list[Transition],
meta: dict[str, Any] | None = None,
error_policy: ErrorPolicy | None = None,
)
Config-driven finite state machine definition and factory.
The StateMachine is stateless -- it holds the FSM definition
(states, transitions, guards, actions) but no per-entity state.
Use :meth:create to get a stateful :class:EntitySession.
Config is the single source of truth
- States, transitions, guards, and actions are declared in YAML/dict.
- Python implementations are bound via
@machine.guard("name")and@machine.action("name")decorators.
Source code in src/pystator/machine.py
from_yaml
classmethod
¶
from_yaml(
path: str | Path,
validate: bool = True,
variables: dict[str, str] | None = None,
) -> StateMachine
Create a StateMachine from a YAML configuration file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the YAML configuration file. |
required |
validate
|
bool
|
If True, validate config against schema. |
True
|
variables
|
dict[str, str] | None
|
Optional variables for |
None
|
Returns:
| Type | Description |
|---|---|
StateMachine
|
Configured StateMachine instance. |
Source code in src/pystator/machine.py
from_dict
classmethod
¶
Create a StateMachine from a configuration dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict[str, Any]
|
Configuration dictionary matching the schema. |
required |
Returns:
| Type | Description |
|---|---|
StateMachine
|
Configured StateMachine instance. |
Source code in src/pystator/machine.py
guard
¶
Decorator to bind a guard implementation to its config name.
Example
@machine.guard("is_valid") ... def is_valid(ctx): ... return ctx.get("valid", False)
Source code in src/pystator/machine.py
action
¶
Decorator to bind an action implementation to its config name.
Example
@machine.action("notify") ... def notify(ctx): ... send_email(ctx["email"], "State changed")
Source code in src/pystator/machine.py
bind_guards
¶
Bind an external guard registry.
Replaces the internal guard registry with the provided one.
Returns:
| Type | Description |
|---|---|
StateMachine
|
Self for method chaining. |
Source code in src/pystator/machine.py
bind_actions
¶
process
¶
process(
current_state: str,
event: str | Event,
context: dict[str, Any] | None = None,
) -> TransitionResult
Process an event and compute the transition result.
Pure computation -- no side effects, no state mutation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_state
|
str
|
The current state name. |
required |
event
|
str | Event
|
Event trigger string or Event object. |
required |
context
|
dict[str, Any] | None
|
Optional context dict for guards. |
None
|
Returns:
| Type | Description |
|---|---|
TransitionResult
|
TransitionResult describing the computed transition. |
Source code in src/pystator/machine.py
aprocess
async
¶
aprocess(
current_state: str,
event: str | Event,
context: dict[str, Any] | None = None,
) -> TransitionResult
Async version of :meth:process (supports async guards).
Source code in src/pystator/machine.py
create
¶
create(
*,
context: dict[str, Any] | None = None,
initial_state: str | None = None
) -> "EntitySession"
Create a stateful instance of this machine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
dict[str, Any] | None
|
Initial context dict for the instance. |
None
|
initial_state
|
str | None
|
Override the initial state (defaults to config initial). |
None
|
Returns:
| Type | Description |
|---|---|
'EntitySession'
|
A new :class: |
Source code in src/pystator/machine.py
get_state
¶
Get a state definition by name.
Source code in src/pystator/machine.py
get_initial_state
¶
get_available_transitions
¶
Get all transitions available from a state.
Source code in src/pystator/machine.py
get_available_triggers
¶
Get all trigger names available from a state.
can_transition
¶
Check if a transition is possible (convenience method).
Source code in src/pystator/machine.py
validate_state
¶
is_terminal
¶
is_initial
¶
get_state_variables
¶
Get state variable definitions from meta.
Source code in src/pystator/machine.py
validate_context
¶
Validate context against state_variables.
Returns (valid, error_messages).
Source code in src/pystator/machine.py
to_mermaid
¶
Generate Mermaid stateDiagram-v2. See :func:visualization.to_mermaid.
to_dot
¶
to_scxml
¶
get_statistics
¶
Get machine statistics. See :func:visualization.get_statistics.
StateMachineBuilder
¶
Fluent builder for StateMachine from Python code.
Source code in src/pystator/machine.py
add_state
¶
Add a state. type: initial, stable, terminal, error.
Source code in src/pystator/machine.py
add_transition
¶
add_transition(
trigger: str,
source: str | list[str],
dest: str,
**kwargs: Any
) -> "StateMachineBuilder"
Add a transition.
Source code in src/pystator/machine.py
add_state_variable
¶
add_state_variable(
key: str,
*,
type: str | None = None,
required: bool = False,
default: Any = None,
**kwargs: Any
) -> "StateMachineBuilder"
Add a state variable (context key) definition.
Source code in src/pystator/machine.py
to_dict
¶
Export to config dict.
Source code in src/pystator/machine.py
from_dict
classmethod
¶
Create builder from config dict (loads state_variables).