hfsm is a Rust library that provides tools for managing Hierarchical Finite State Machines (HFSM). This library allows you to build and run SuperStates and States using custom contexts that implement the StateContext trait.
- Creation and management of hierarchical states (
SuperStates) - Definition of individual states (
States) - Customizable contexts for handling data between states
- Flexible transitions between states
- Intuitive API for building and running HFSMs
To start using the hfsm library, follow these steps:
- Create an
HFSMinstance with an initialSuperState. - Add more
SuperStatesandStatesto theHFSMas needed. - Run the HFSM with a custom context.
use hfsm::{InMemoryStateContext, StateContext as _, StateData, SuperStateBuilder, Transition, HFSM};
fn main() {
// Create a custom context
let mut context = InMemoryStateContext::new();
// Build a SuperState
let flow_a = SuperStateBuilder::new()
.id("FlowA")
.initial_state("Task1")
.add_state("Task1",
Box::new(|ctx: &mut InMemoryStateContext| {
println!("Executing Task1 in FlowA");
ctx.set("data", StateData::Text("200".to_string()));
Transition::ToState("Task2".to_string())
}),
)
.add_state("Task2",
Box::new(|ctx: &mut InMemoryStateContext| {
println!("Executing Task2 in FlowA");
Transition::Complete
}),
)
.build()
.expect("Failed to build FlowA");
// Create and run the HFSM
let mut hfsm = HFSM::new(flow_a);
hfsm.run(&mut context);
}For more complex use cases, consider:
- Nesting
SuperStatesto create deeper hierarchies - Implementing custom contexts to handle application-specific data
- Using conditional transitions based on context state
A SuperState is a container for multiple related states.
A State represents an individual unit of behavior in the HFSM. Each state can perform actions and determine the next transition.
Transitions define how the HFSM (Hierarchical Finite State Machine) navigates between states and superstates. There are three main types of transitions:
-
ToState(next_state: String):- This transition moves the HFSM to a specific
Statewithin the currentSuperState. The state identified bynext_statewill be executed next, and the HFSM will remain within the currentSuperState.
- This transition moves the HFSM to a specific
-
ToSuperState { call_super_state: String, call_state: Option<String>, next_state: Option<String> }:- This transition moves the HFSM from the current
SuperStateto anotherSuperStatespecified bycall_super_state. - If
call_stateis provided, the transition will begin with that specificStatewithin the newSuperState. Otherwise, it will start from the initial state of theSuperState. - If
next_stateis provided, the HFSM will register a callback. Once thecall_super_statehas completed all its states, the HFSM will return to the originalSuperStateand continue execution fromnext_state.
- This transition moves the HFSM from the current
-
Complete:- This transition signifies the completion of the current
SuperState. - If a callback has been registered (from a previous
ToSuperStatetransition), the HFSM will return to theSuperStateandStatespecified by the callback and continue execution from there. - If no callback is registered, the HFSM will terminate, as there are no more states to execute.
- This transition signifies the completion of the current
The Context is a fundamental component in the HFSM (Hierarchical Finite State Machine) framework. It acts as a shared repository for data that needs to be accessed and manipulated by different states during the execution of state transitions.
The StateContext trait defines the interface that any context implementation must provide. It includes methods for setting, getting, and removing key-value pairs within the context.
Included implementations:
InMemoryStateContext:- This is an in-memory implementation of the
StateContext, optimized for scenarios where the context's lifecycle is tied to the runtime of the state machine. - It uses a
HashMapunder the hood to store key-value pairs and is capable of handling various types of data through the use ofStateData. InMemoryStateContextis suitable for most applications where the context does not need to persist beyond the state machine's execution.
- This is an in-memory implementation of the