rustate Crate
This crate contains the core RuState state machine library.
Overview
RuState provides the following features:
- ✅ Finite state machines and statecharts
- ✅ Hierarchical states
- ✅ Parallel states
- ✅ Transition conditions (guards)
- ✅ Actions (side effects)
- ✅ Context (extended state)
- ✅ Type-safe API
- ✅ Serializable machines (State snapshot only; actions/guards are not fully serialized)
- ✅ Cross-crate state machine integration (via shared memory patterns)
- ✅ Actor pattern support (Basic: Actor trait, ActorRef, spawn, system)
- ❌ Model-based testing (MBT) support (Planned, not implemented)
Model-Based Testing (MBT) Integration
TODO: Implement MBT integration. (Currently not implemented)
Cross-Crate State Machine Integration
(Current approach) RuState primarily supports integrating multiple state machines across different crates using shared memory (Arc<Mutex>, Arc<RwLock>), allowing state machines within the same process to communicate via shared context or event forwarding.
Design Patterns for State Machine Integration (Shared Memory)
-
Event Forwarding Pattern: State machines communicate by forwarding events to each other
use ; use ; // Define a shared state machine in a common crate // In crate A: Create a parent machine that forwards events to child -
Context-Based Communication Pattern: Share data between machines using Context
use ; use ; // Define shared context type in a common crate // Use in machine actions across different crates -
Hierarchical Integration Pattern: Define parent-child relationships between machines
use ; // In a common crate: Define a trait for child machines // In child crate: Implement child machine // In parent crate: Create parent machine that coordinates with child
Best Practices for Cross-Crate Integration
- Define Common Types: Create a shared crate for common event and state types
- Use Trait Abstraction: Define traits for machine capabilities to allow different implementations
- Leverage Context: Use context for data sharing with clear read/write patterns
- Event Namespacing: Prefix events with module or crate names to avoid collisions
- Minimize Coupling: Design machines to be as independent as possible
- Error Handling: Use Result types for robust cross-machine communication
- Testing: Test integrated machines as a whole system. (Note: Utilizing Model-Based Testing (MBT) techniques is a goal, pending verification of MBT feature status).
This approach allows you to build complex applications with modular, type-safe state management across multiple crates, perfect for large Rust applications with distinct domains.
Key Concepts
- State: Represents a node in the statechart
- Transition: Defines movement between states in response to events
- Guard: Logic that determines transition conditions
- Action: Side effects executed during state transitions
- Context: Stores the extended state of the machine
- Cross-Crate Integration: Patterns for connecting state machines across different crates
- Actor Pattern: Concepts related to actor-based concurrency and state management within
rustate_core.
Roadmap
(Current status as of v0.3.0)
- Implement Model-Based Testing (MBT) Support: Integrate MBT capabilities for automated test generation and validation based on the state machine models. (Status: Not started)
- Explore Alternative Cross-Crate Communication: Investigate and potentially implement/document alternative communication patterns beyond shared memory (e.g., message passing, event bus). (Status: Planning/Research)
- Enhance Actor Pattern Support: Further develop and document the actor pattern features within
rustate(e.g., ask pattern, supervision). (Status: Basic implementation exists, enhancements planned) - Add Comprehensive Examples: Provide more diverse and complex usage examples. (Status: Ongoing)
- Benchmarking & Performance Optimization: Conduct performance analysis and optimize critical paths. (Status: Basic benchmark setup exists, optimization not started)
- Formalize
.ssotIntegration: Define and potentially implement a workflow for using.ssotfiles to define/generaterustatemachines. (Status: Planned) - Improve CI/CD and Testing: Enhance the testing suite (unit, integration, MBT) and automate builds/releases. (Status: Basic setup exists, enhancements planned)
- Implement Deep History: Complete the implementation for deep history states. (Status: Partially implemented/needs review)
- Stabilize Codegen Macros: Finalize and document the
create_machinemacro (potentially in a separaterustate-macroscrate). (Status: Basic macro exists but needs separate crate and testing) - Full Serialization Support: Investigate methods for serializing/deserializing actions and guards (e.g., using identifiers). (Status: Planning/Research)