Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork29
Powerful Kotlin Multiplatform library with clean DSL syntax for creating complex state machines and statecharts driven by Kotlin Coroutines.
License
KStateMachine/kstatemachine
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Documentation |KDoc |Sponsors |Quick start |Samples |Install |Contribution |Support |Roadmap |License |Discussions
KStateMachine is a powerfulKotlin Multiplatform library with clean DSL syntax for creatingcomplexstate machinesandstatecharts driven byKotlin Coroutines.
- Kotlin DSL syntax -declarative and clear state machine structure. Using without DSL is also possible.
- Kotlin Coroutinessupport -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, main library artifact does not depend on any third party librariesor Android SDK.
Event based -transitions are performed byprocessingincoming events
Reactive - listen formachine, states,state groups andtransitions
GuardedandConditional transitions - dynamictargetstate which is calculated in a moment of event processing depending on application business logic
Nested states - buildhierarchical state machines(statecharts)withcross-level transitionssupport
Composed (nested) state machines - use state machines as atomic child states
Pseudo states for additionallogic in machinebehaviour
Typesafe transitions - pass data intypesafe wayfrom event to state
Parallel states - avoid acombinatorialexplosion ofstates
Undo transitions - navigate back to previousstate (like stack based FSMs do)
Optional argument passing forevents andtransitions
Export state machine structuretoPlantUML andMermaid diagrams
Persist (serialize) state machine'sactiveconfiguration and restore it later. Built-in
kotlinx.serializationsupport.Testable - run state machine from specifiedstate and enable internal logging
Well tested - all features are coveredby tests
Important
- See full documentation onwebsite
- KDoc for classes
I highly appreciate that you donate or become a sponsor to support the project.If you find this project useful you can support it by:
- Pushing the ⭐ star-button
- Using ❤️github-sponsors button to see supported donation methods
stateDiagram-v2 direction TB classDef red fill:#f00,color:white,font-weight:bold,stroke-width:2px,stroke:black classDef yellow fill:yellow,color:black,font-weight:bold,stroke-width:2px,stroke:black classDef green fill:green,color:white,font-weight:bold,stroke-width:2px,stroke:black [*] --> RedState RedState --> YellowState: SwitchEvent YellowState --> GreenState: SwitchEvent GreenState --> [*] class RedState red class YellowState yellow class GreenState green// define your Eventsobject SwitchEvent : Event// define your States as classes or objectssealedclassStates :DefaultState() {object RedState : States()object YellowState : States()// machine finishes when enters [FinalState]object GreenState : States(), FinalState}funmain()= runBlocking {// create state machine and configure its structure in a setup blockval machine= createStateMachine(scope=this) { addInitialState(RedState) {// add state listeners onEntry {println("Enter red")// you can call suspendable code if necessary delay(10) } onExit {println("Exit red") }// setup transition transition<SwitchEvent> { targetState=YellowState// add transition listener onTriggered {println("Transition triggered") } } } addState(YellowState) { transition<SwitchEvent>(targetState=GreenState) } addFinalState(GreenState) onFinished {println("Finished") } }// you can observe state machine changes using [Flow] along with simple listenersval statesFlow= machine.activeStatesFlow()// you can process events after state machine has been started even from listener callbacks machine.processEvent(SwitchEvent)// machine goes to [YellowState] machine.processEvent(SwitchEvent)// machine goes to [GreenState]}
Android 2D shooter game sample
The library itself does not depend on Android.
Complex syntax sampleshows many syntax variants and library possibilities, so it looks messy
KStateMachine is available onMaven Central andJitPack repositories.
Seeinstall section in the docs for details.
dependencies {// multiplatform artifacts, where <Tag> is a library version. implementation("io.github.nsk90:kstatemachine:<Tag>") implementation("io.github.nsk90:kstatemachine-coroutines:<Tag>") implementation("io.github.nsk90:kstatemachine-serialization:<Tag>")}Run./gradlew build or build withIntellij IDEA.
The library is in development phase. You are welcome to propose useful features and contribute to the project.SeeCONTRIBUTING file.
I am open to answer you questions and feature requests. Fill free to use any of communication channels togive your feedback.
- Slack channel orGitHub discussions - best for questions and discussions
- GitHub issues - best for bugs and feature requests
If you use some other platforms to ask questions or mention the library, I recommend adding alink to thisGitHub project or using#kstatemachine tag.
- Create
Intellij IDEA Pluginfor state machine visualization and edition
Licensed under permissiveBoost Software License
About
Powerful Kotlin Multiplatform library with clean DSL syntax for creating complex state machines and statecharts driven by Kotlin Coroutines.
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.

