State Machine Construction Kit for Kotlin

Related tags

Kotlin stateful4k
Overview

Stateful for Kotlin

State Machine Construction Kit for Kotlin

Stateful

This is Stateful ported to JVM. I could have re-implemented this in Java but there would be no learning factor in it. So I decided to use Kotlin. ...And, oh man, I'm glad I did.

Requirements

I had some non standard requirements for the state machine:

  • Events should be able to carry data - for example, hypothetical event KeyPressed should also carry information which key has been actually pressed;
  • States should be able hold data - for example, state collecting key presses (let's call it EnteringText) should be able to hold a list of keys pressed so far;
  • Guard statements should have access to both current state and event - for example, KeyPressed event may cause transition to different state depending which key has been pressed;

Dot Driven Development

One of the design decisions is "Dot Driven Development". The only class which you need to know upfront is StateMachine, everything else can be discovered by pressing a dot (and relying on IntelliSense tab completion, whatever you call it).

Configurator and Executor

Rules of state machine are configured by object called Configurator but executed with object called Executor. Single Configurator can be used to create multiple Executors. For example, there is shared set of rules how your communication should work (single Configurator) but you can have multiple ports open talking to different servers (many Executors).

Context, State and Event

There are three types you need to provide to Configurator - Context, State and Event. While State and Event are quite obvious, Context might need few words of explanation. Context is the data shared by all states. As it is not necessary (this shared data could be passed from one state to another) it helps reduce state bloat. Using previous example, you could store remote server address in Context. If you think you don't need it (or your religion prevents you from using shared mutable data) just use Any? (or Nothing) and pass null to Executor.

  • Context - type of shared data
  • State - base class for all states
  • Event - base class for all events

For conciseness I used C, S and E and template (generic?) parameter names for Context, State and Event respectively.

Examples

See basic example here and advanced example here.

Java interoperability

As all Kotlin libs it should just work, but I never tried. Plese do try and let me know.

You might also like...
Stresscraft - State-of-art Minecraft stressing software written in Kotlin

StressCraft (W.I.P) State-of-art Minecraft stressing software written in Kotlin.

ScopedState - Android Scoped State With Kotlin

Android Scoped State There is no need for complicated code - just define scopes

This repository contains the article describing my attempt to implement a simple state reducer based on Kotlin Flow and an example app that uses it.
This repository contains the article describing my attempt to implement a simple state reducer based on Kotlin Flow and an example app that uses it.

This repository contains the article describing my attempt to implement a simple state reducer based on Kotlin Flow and an example app that uses it.

💫 Small microservice to handle state changes of Kubernetes pods and post them to Instatus or Statuspages

💫 Kanata Small microservice to handle state changes of Kubernetes pods and post them to Instatus or Statuspages 🤔 Why? I don't really want to implem

Basic app to use different type of observables StateFlow, Flow, SharedFlow, LiveData, State, Channel...
Basic app to use different type of observables StateFlow, Flow, SharedFlow, LiveData, State, Channel...

stateflow-flow-sharedflow-livedata Basic app to use different type of observables StateFlow, Flow, SharedFlow, LiveData, State, Channel... StateFlow,

ConstraintSetChangesTest - Simple project showing Changes of ConstraintSet value as part of mutable state in JetpackCompose.

ConstraintSetChangesTest Simple project showing Changes of ConstraintSet value as part of mutable state in JetpackCompose. Version: implementation

Advanced State in Jetpack Compose Codelab

Advanced State in Jetpack Compose Codelab This folder contains the source code for the Advanced State in Jetpack Compose Codelab codelab. The project

Simplify mutating "immutable" state models

Mutekt (Pronunciation: /mjuːˈteɪt/, 'k' is silent) "Simplify mutating "immutable" state models" Generates mutable models from immutable model definiti

This Project for how to use  MVVM , state flow, Retrofit, dagger hit, coroutine , use cases with Clean architecture.
This Project for how to use MVVM , state flow, Retrofit, dagger hit, coroutine , use cases with Clean architecture.

Clean-architecture This Project for how to use MVVM , state flow, Retrofit, dagger hit, coroutine , use cases with Clean architecture. Why i should us

Comments
  • Dependency on Java

    Dependency on Java

    I was looking to use this state machine for a kotlin multiplatform project so that I can share some business logic across platforms however I hit a problem in that this project relies in java in a few places e.g. java.util, @jvmstatic, reference to Class etc.

    Is it possible to remove these dependencies so it is pure kotlin or is that out of scope for this project?

    opened by damianflannery 3
  • State machine of state machines

    State machine of state machines

    I was testing your cool library to use in my project which is fully written in Kotlin, and my question is what is the best implementation of having a main state machine which dispatch system control to other state machines and get control back and dispatch it to another one?

    To be more specific, in my Android application I have different parts that my core(which would be the main state machine) can pass control to them and every one of them are another separate state machines which can give back control to the core.

    I try to have different executor with same configurator but I am not sure if it is correct or not, however this way my configurator should include all my different state machines states and events!!

    opened by mohsenoid 2
Owner
Milosz Krajewski
Milosz Krajewski
🔴 A non-deterministic finite-state machine for Android & JVM that won't let you down

HAL is a non-deterministic finite-state machine for Android & JVM built with Coroutines StateFlow and LiveData. Why non-deterministic? Because in a no

Adriel Café 73 Nov 28, 2022
A Java Virtual Machine written in Kotlin

jvm.kotlin A Java Virtual Machine written in Kotlin. Introduction jvm.kotlin is a toy JVM programmed in Kotlin. The main purpose of this project is le

Elements 14 Aug 13, 2022
Kotlin Multiplatform Coffee Machine

Expressus KMM sample project acting as a playground to illustrate what's discussed in these articles: Details Shared Model-View-Intent architecture Fi

Guilherme Delgado 56 Dec 22, 2022
ClickMachine Fabric - Click Machine for minecraft

Minecraft mod for Fabric Adds one block to the game: Auto Clicker. This autoclic

null 0 Jan 10, 2022
KaMP Kit by Touchlab is a collection of code and tools designed to get your mobile team started quickly with Kotlin Multiplatform.

KaMP Kit Welcome to the KaMP Kit! About Goal The goal of the KaMP Kit is to facilitate your evaluation of Kotlin Multiplatform (aka KMP). It is a coll

Touchlab 1.7k Jan 3, 2023
KaMP Kit is to facilitate your evaluation of Kotlin Multiplatform (aka KMP)

KaMP Kit Welcome to the KaMP Kit! About Goal The goal of the KaMP Kit is to facilitate your evaluation of Kotlin Multiplatform (aka KMP). It is a coll

bas 0 Oct 25, 2021
The goal of the KaMP Kit is to facilitate your evaluation of Kotlin Multiplatform

KaMP Kit Welcome to the KaMP Kit! About Goal The goal of the KaMP Kit is to facilitate your evaluation of Kotlin Multiplatform (aka KMP). It is a coll

null 0 Nov 11, 2021
Starter Kit for Android projects.

About Android Starter Kit was created as a way to save time when starting a new Android project. It is a simple native single-module app based on MVVM

Roonyx 2 Oct 12, 2021
Reapp is everything you need to build amazing apps with React: a collection of packages that work together, our UI kit, and a CLI that scaffolds your app and includes a server and build system.

What is it? Reapp is everything you need to build amazing apps with React: a collection of packages that work together, our UI kit, and a CLI that sca

reapp 3.4k Nov 20, 2022
Simple State Machines in Kotlin (KSSM)

Simple State Machines in Kotlin (KSSM) What is this? KSSM (reordered: Kotlin - Simple State Machines) provides an easy and simple DSL (Domain Specific

Milos Marinkovic 22 Dec 12, 2022