HSTsou / kstatemachine

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

Home Page:https://nsk90.github.io/kstatemachine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

KStateMachine

Build and test with Gradle Quality Gate Status Maven Central Dependencies none codecov Android Arsenal

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

Overview

Main features are:

The library is currently in a development phase. You are welcome to propose useful features.

Don't forget to push the ⭐ if you like this project.

Quick start sample (finishing traffic light)

Traffic light diagram

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() {
    // Create state machine and configure its states in a setup block
    val machine = createStateMachine {
        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.

Maven Central

Add the dependency:

dependencies {
    implementation 'io.github.nsk90:kstatemachine:<Tag>'
}

Where <Tag> is a library version.

JitPack

Add the JitPack repository to your build file. Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
        //  ...
        maven { url 'https://jitpack.io' }
    }
}

Add the dependency:

dependencies {
    implementation 'com.github.nsk90:kstatemachine:<Tag>'
}

Where <Tag> is a library version.

Build

Run ./gradlew build or build with Intellij IDEA.

To run tests from IDE download official Kotest plugin.

Licensed under permissive Boost Software License

Thanks to supporters

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

About

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

https://nsk90.github.io/kstatemachine

License:Boost Software License 1.0


Languages

Language:Kotlin 100.0%