项目作者: nsk90

项目描述 :
Kotlin state machine library (FSM) with DSL syntax. Supports nested states (HSM), guarded and conditional transitions. Has export features for visualization.
高级语言: Kotlin
项目地址: git://github.com/nsk90/kstatemachine.git
创建时间: 2020-09-16T20:38:57Z
项目社区:https://github.com/nsk90/kstatemachine

开源协议:MIT License

下载


KStateMachine

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

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

KStateMachine

Documentation |
KDoc |
Sponsors |
Quick start |
Samples |
Install |
Contribution |
Support |
Roadmap |
License |
Discussions

KStateMachine is a powerful Kotlin Multiplatform library with clean DSL syntax for creating
complex state machines
and statecharts driven by
Kotlin Coroutines.

🌏 Overview

📦 Integration features

  • 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, main library artifact does not depend on any third party libraries
    or Android SDK.

⚙️ State management features

📄 Documentation

[!IMPORTANT]

❤️ Sponsors

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

🚀 Quick start sample

🚦Finishing traffic light

  1. stateDiagram-v2
  2. direction TB
  3. classDef red fill:#f00,color:white,font-weight:bold,stroke-width:2px,stroke:black
  4. classDef yellow fill:yellow,color:black,font-weight:bold,stroke-width:2px,stroke:black
  5. classDef green fill:green,color:white,font-weight:bold,stroke-width:2px,stroke:black
  6. [*] --> RedState
  7. RedState --> YellowState: SwitchEvent
  8. YellowState --> GreenState: SwitchEvent
  9. GreenState --> [*]
  10. class RedState red
  11. class YellowState yellow
  12. class GreenState green
  1. // define your Events
  2. object SwitchEvent : Event
  3. // define your States as classes or objects
  4. sealed class States : DefaultState() {
  5. object RedState : States()
  6. object YellowState : States()
  7. // machine finishes when enters [FinalState]
  8. object GreenState : States(), FinalState
  9. }
  10. fun main() = runBlocking {
  11. // create state machine and configure its structure in a setup block
  12. val machine = createStateMachine(scope = this) {
  13. addInitialState(RedState) {
  14. // add state listeners
  15. onEntry {
  16. println("Enter red")
  17. // you can call suspendable code if necessary
  18. delay(10)
  19. }
  20. onExit { println("Exit red") }
  21. // setup transition
  22. transition<SwitchEvent> {
  23. targetState = YellowState
  24. // add transition listener
  25. onTriggered { println("Transition triggered") }
  26. }
  27. }
  28. addState(YellowState) {
  29. transition<SwitchEvent>(targetState = GreenState)
  30. }
  31. addFinalState(GreenState)
  32. onFinished { println("Finished") }
  33. }
  34. // you can observe state machine changes using [Flow] along with simple listeners
  35. val statesFlow = machine.activeStatesFlow()
  36. // you can process events after state machine has been started even from listener callbacks
  37. machine.processEvent(SwitchEvent) // machine goes to [YellowState]
  38. machine.processEvent(SwitchEvent) // machine goes to [GreenState]
  39. }

✍️ Publications

🧪 Samples

💾 Install

KStateMachine is available on Maven Central and JitPack repositories.

See install section in the docs for details.

Maven Central

  1. dependencies {
  2. // multiplatform artifacts, where <Tag> is a library version.
  3. implementation("io.github.nsk90:kstatemachine:<Tag>")
  4. implementation("io.github.nsk90:kstatemachine-coroutines:<Tag>")
  5. implementation("io.github.nsk90:kstatemachine-serialization:<Tag>")
  6. }

🏗️ Build

Run ./gradlew build or build with Intellij IDEA.

🤝 Contribution

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

🙋 Support

I am open to answer you questions and feature requests. Fill free to use any of communication channels to
give your feedback.

If you use some other platforms to ask questions or mention the library, I recommend adding a link to this
GitHub project or using #kstatemachine tag.

🗺️ Roadmap

  • Create Intellij IDEA Plugin for state machine visualization and edition

🏅 Thanks to supporters

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

🖋️ License

Licensed under permissive Boost Software License