/kstatemachine

KStateMachine is a Kotlin DSL library for creating state machines and statecharts.

Primary LanguageKotlinBoost Software License 1.0BSL-1.0

KStateMachine

KStateMachine

Build and test with Gradle Quality Gate Status codecov Maven Central Version JitPack multiplatform support

Open Collective JetBrains support Mentioned in Awesome Kotlin Android Arsenal Share on X Share on Reddit

Documentation | Sponsors | Quick start | Samples | Install | Contribution | License | Discussions

KStateMachine is a Kotlin DSL library for creating state machines and statecharts.

Overview

Integration features are:

  • Kotlin DSL syntax. Declarative and clear state machine structure. Using without DSL is also possible.
  • Kotlin Coroutines support. Call suspendable functions within the library. You can fully use KStateMachine without Kotlin Coroutines dependency if necessary.
  • Kotlin Multiplatform support.
  • Zero dependency. It is written in pure Kotlin, it does not depend on any third party libraries or Android SDK.

State management features:

Important

SEE FULL DOCUMENTATION HERE

Sponsors ❤

I highly appreciate that you donate or become a sponsor to support the project. Use ❤️ github-sponsors button to see supported methods and push the ⭐ star-button if you like this project.

Quick start sample

Finishing traffic light

stateDiagram-v2
    direction LR
    [*] --> GreenState
    GreenState --> YellowState: SwitchEvent
    YellowState --> RedState: SwitchEvent
    RedState --> [*]

object SwitchEvent : Event

sealed class States : DefaultState() {
    object GreenState : States()
    object YellowState : States()
    object RedState : States(), FinalState // Machine finishes when enters final state
}

fun main() = runBlocking {
    // Create state machine and configure its states in a setup block
    val machine = createStateMachine(this) {
        addInitialState(GreenState) {
            // Add state listeners
            onEntry { println("Enter green") }
            onExit { println("Exit green") }

            // Setup transition
            transition<SwitchEvent> {
                targetState = YellowState
                // Add transition listener
                onTriggered { println("Transition triggered") }
            }
        }

        addState(YellowState) {
            transition<SwitchEvent>(targetState = RedState)
        }

        addFinalState(RedState)

        onFinished { println("Finished") }
    }

    // Now we can process events
    machine.processEvent(SwitchEvent)
    machine.processEvent(SwitchEvent)
}

Samples

Install

KStateMachine is available on Maven Central and JitPack repositories.

See install section in the docs for details.

Maven Central

dependencies {
    // multiplatform artifacts, where <Tag> is a library version.
    implementation("io.github.nsk90:kstatemachine:<Tag>")
    implementation("io.github.nsk90:kstatemachine-coroutines:<Tag>")
}

Build

Run ./gradlew build or build with Intellij IDEA.

Contribution

The library is in a active development phase. You are welcome to propose useful features and contribute to the project. See CONTRIBUTING file.

Thanks to supporters

Stargazers repo roster for @kstatemachine/kstatemachine Forkers repo roster for @kstatemachine/kstatemachine

License

Licensed under permissive Boost Software License