State Machines in the Kotlin Ecosystem
Kotlin developers building server applications with Spring Boot or Android Automotive (AAOS) infotainment systems need state machines that integrate cleanly with their existing frameworks. Most existing solutions are either too simplistic (enum + when blocks), require manual wiring (Spring Statemachine), or lack formal specification compliance.
Existing approaches compared
| Approach | Pros | Cons |
|---|---|---|
| sealed class + when | Pure Kotlin, type-safe | Manual, no hierarchy, no parallel states, no standard |
| Spring Statemachine | Spring integration | Complex configuration, no AOT, limited Android support |
| Tinder StateMachine | Kotlin DSL, lightweight | No hierarchical states, no standard compliance |
| SCE | W3C standard, AOT, Spring Boot + Android | SCXML definition required |
Spring Boot Integration
SCE provides a Spring Boot starter with auto-configuration. Add a single Gradle dependency and you get a fully configured SCXML script engine as a Spring bean.
// build.gradle.kts
dependencies {
implementation("com.sce:sce-spring-boot-starter:1.0.0")
}
// Use in your Spring service
@Service
class WorkflowService(@Autowired val engine: ScxmlScriptEngine) {
fun processOrder(orderId: String) {
engine.createSession(orderId)
engine.evaluateCondition(orderId, "orderTotal > 100")
engine.executeScript(orderId, "applyDiscount(0.1)")
engine.destroySession(orderId)
}
}
The starter automatically configures the engine based on available dependencies, with Rhino as the default for server environments (pure JVM, zero JNI, fastest server-side performance).
Three Pluggable Script Engines
SCE's Kotlin runtime supports three script engines, each optimized for different deployment targets:
| Engine | Type | Best For | Benchmark Result |
|---|---|---|---|
| Rhino (default) | Pure JVM | Server / Spring Boot | Fastest on JVM (29/29 scenarios) |
| Lua 5.4 | JNI native | Android / AAOS | Fastest on Android (20/29 scenarios) |
| QuickJS | JNI native | Full ES6 compatibility | Modern JavaScript features |
// Switch engine via Gradle property
./gradlew test -Psce.script.engine=lua // Lua 5.4
./gradlew test -Psce.script.engine=rhino // Rhino (default)
./gradlew test -Psce.script.engine=quickjs // QuickJS
Android Automotive OS (AAOS)
For Android Automotive infotainment systems where deterministic state management is critical, SCE with the Lua engine provides native-speed SCXML processing. The Lua 5.4 engine won 20 out of 29 benchmark scenarios on Android, making it the best choice for resource-constrained automotive environments.
SCE's Kotlin modules are built with Kotlin Multiplatform support, and the Android benchmark app uses Jetpack Compose for the UI layer. The architecture follows standard Android patterns with Gradle 8.11.1, Kotlin 2.1.20, and AGP 8.9.3.
AOT Code Generation for Kotlin
SCE generates Kotlin state machine classes from SCXML at build time,
just like the C++ backend. The generated code implements the
StateMachineEngine interface and integrates with SCE's
runtime via the same code generator.
# Generate Kotlin state machine from SCXML
sce-codegen generate traffic_light.scxml -o ./generated/ -l kotlin
The code generator analyzes the SCXML features and automatically selects the optimal engine: Pure Static for simple state machines, Static Hybrid when ECMAScript expressions are present.
W3C SCXML Compliance
The Kotlin backend passes all 202 mandatory W3C SCXML conformance
tests. The test suite covers compound states,
parallel states, history states, <invoke>, delayed events
(<send> with delay), and the full ECMAScript datamodel.
All three script engines (Rhino, Lua 5.4, QuickJS) implement the same
ScxmlScriptEngine interface and can be selected at build time.
# Run W3C conformance tests
./gradlew :sce-kotlin-tests:test
# Run benchmarks
./gradlew :sce-kotlin-benchmark:jmh --no-configuration-cache
Kotlin Module Structure
| Module | Purpose |
|---|---|
sce-kotlin-runtime |
ScxmlScriptEngine interface (Kotlin Multiplatform) |
sce-kotlin-rhino |
Rhino ECMAScript engine (pure JVM) |
sce-kotlin-lua |
Lua 5.4 engine (JNI native, LuaRef registry pattern) |
sce-kotlin-quickjs |
QuickJS engine (JNI native, ES6) |
sce-spring-boot-starter |
Spring Boot auto-configuration |
sce-kotlin-tests |
W3C conformance tests (202/202) |
sce-android-app |
Android benchmark app (Compose UI) |
Getting Started
# Clone and build
git clone --recursive https://github.com/newmassrael/scxml-core-engine.git
cd scxml-core-engine
# Build Kotlin modules
./gradlew build
# Publish to local Maven for use in your project
./gradlew publishToMavenLocal
# Then in your project's build.gradle.kts:
# implementation("com.sce:sce-spring-boot-starter:1.0.0")