Skip to content

San68bot/AGStateMachineBuilder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 

Repository files navigation

AGStateMachine Framework: Asynchronous State Machines for Modern Systems

Kotlin Version License State Pattern

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.


🧠 Theoretical Foundations

State Machine Theory

State machines formalize systems that transition between predefined states based on events/conditions (Hopcroft et al., 2006). This implementation extends Mealy machine concepts with:

  1. Hierarchical States: Nest states via DSL composition
  2. Timed Transitions: Execute actions after/before specific durations
  3. Asynchronous Operation: Non-blocking execution model
  4. Transition Guards: Conditional state progression

Key Innovations

  • 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() }

🚀 Features

Core Capabilities

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)

Performance Characteristics

  • Zero-Allocation Design: Avoids GC pauses in critical paths
  • Constant-Time State Switching: Hash-based state lookups
  • Precise Timing: µs-resolution via ActionTimer class

📚 Implementation Details

Architectural Overview

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]
Loading

Critical Components

  1. State Registry
    Maintains O(1) access to states via name hashing
    private val states = mutableListOf<AGState>()
  2. Timing System
    Uses monotonic clock for drift-resistant timing
    private val stateTimer = ActionTimer() // Nanosecond precision
  3. Transition Engine
    Implements conflict-free state switching
    fun nextState(name: String? = null) {
        // Atomic state index update
    }

🛠️ Usage Example

Sample State Machine

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
}

Execution Flow

  1. State Entry: One-time initialization
  2. Loop Evaluation: Continuously checked predicate
  3. State Exit: Cleanup before transition
  4. Transition Hooks: Custom logic execution

📚 Research Context

Academic Basis

  1. Finite State Machines
  2. Real-Time Systems
  3. Concurrency Models

Industry Applications

  • Robotics (ROS2 State Machines)
  • IoT Device Control (AWS IoT Greengrass)
  • Automotive Systems (AUTOSAR)

📦 Installation

Gradle Dependency

implementation("com.san68bot:agstatemachine:1.2.0")

Build from Source

git clone https://github.com/San68bot/AGStateMachine.git
./gradlew publishToMavenLocal

License

MIT 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}
}

About

Asynchronous State Machines for Modern Systems

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors