A robust implementation of asynchronous state machines featuring event-driven transitions, timed state actions, and thread-safe execution. Designed for embedded systems, robotics, and IoT applications requiring deterministic behavior in concurrent environments.
State machines formalize systems that transition between predefined states based on events/conditions (Hopcroft et al., 2006). This implementation extends Mealy machine concepts with:
- Hierarchical States: Nest states via DSL composition
- Timed Transitions: Execute actions after/before specific durations
- Asynchronous Operation: Non-blocking execution model
- Transition Guards: Conditional state progression
- Kotlin DSL Syntax: Type-safe state definitions
state("Heating") { enter { startHeater() } loop { temperature < targetTemp } exit { safetyCheck() } }
- Temporal Logic Support: Precise timing controls
runAfterTime(2.5) { emergencyShutdown() } - Transition Hooks: Custom logic between states
setTransitions { logStateChange() }
| Feature | Implementation | Complexity |
|---|---|---|
| State Entry/Exit Actions | enter{}/exit{} blocks |
O(1) |
| Transition Guards | Conditional loop{} predicates |
O(n) |
| Time-Aware States | checkTime()/runAfterTime() |
O(log n) |
| State Reentrancy | refresh() method |
O(1) |
| Thread Safety | Non-blocking run() implementation |
O(1) |
- Zero-Allocation Design: Avoids GC pauses in critical paths
- Constant-Time State Switching: Hash-based state lookups
- Precise Timing: µs-resolution via
ActionTimerclass
graph TD
A[State Definition] --> B(Enter Action)
B --> C{Loop Condition}
C -->|True| D[Exit Action]
D --> E[Transition Logic]
E --> F[Next State]
- State Registry
Maintains O(1) access to states via name hashingprivate val states = mutableListOf<AGState>()
- Timing System
Uses monotonic clock for drift-resistant timingprivate val stateTimer = ActionTimer() // Nanosecond precision
- Transition Engine
Implements conflict-free state switchingfun nextState(name: String? = null) { // Atomic state index update }
val ovenController = AGStateMachine {
state("Idle") {
enter { display.off() }
loop { temperature > 50.0 }
exit { logStartup() }
}
state("Heating") {
enter { heater.on() }
loop { (temperature < target) && !errorDetected }
exit { safetyCheck() }
}
}
while (systemActive) {
ovenController.run() // Non-blocking execution
}- State Entry: One-time initialization
- Loop Evaluation: Continuously checked predicate
- State Exit: Cleanup before transition
- Transition Hooks: Custom logic execution
- Finite State Machines
- Formal verification methods (Clarke et al., 1999)
- Real-Time Systems
- Timed automata theory (Alur & Dill, 1994)
- Concurrency Models
- Actor-based state management (Hewitt et al., 1973)
- Robotics (ROS2 State Machines)
- IoT Device Control (AWS IoT Greengrass)
- Automotive Systems (AUTOSAR)
implementation("com.san68bot:agstatemachine:1.2.0")git clone https://github.com/San68bot/AGStateMachine.git
./gradlew publishToMavenLocalMIT License - Free for commercial/non-commercial use.
Contributors: San68bot
Cite This Work:
@software{AGStateMachine_2023,
author = {San68bot},
title = {Asynchronous State Machine Framework},
url = {https://github.com/San68bot/AGStateMachine}
}