All Projects → anupcowkur → Statelin

anupcowkur / Statelin

Licence: mit
A finite state machine for Kotlin and Android

Programming Languages

java
68154 projects - #9 most used programming language
kotlin
9241 projects

Projects that are alternatives of or similar to Statelin

Afsm
C++14 Finite State Machine library
Stars: ✭ 113 (-15.67%)
Mutual labels:  finite-state-machine, fsm
Statecharts.github.io
There is no state but what we make. Feel free to pitch in.
Stars: ✭ 265 (+97.76%)
Mutual labels:  finite-state-machine, fsm
simple-state-machine
A simple Java state machine for Spring Boot projects
Stars: ✭ 25 (-81.34%)
Mutual labels:  fsm, finite-state-machine
Fsm
Finite State Machine for Go
Stars: ✭ 1,269 (+847.01%)
Mutual labels:  finite-state-machine, fsm
Automata
A Python library for simulating finite automata, pushdown automata, and Turing machines
Stars: ✭ 121 (-9.7%)
Mutual labels:  finite-state-machine, fsm
fsm
Finite State Machine for Go inspired by Akka FSM
Stars: ✭ 59 (-55.97%)
Mutual labels:  fsm, finite-state-machine
statemachine-go
🚦 Declarative Finite-State Machines in Go
Stars: ✭ 47 (-64.93%)
Mutual labels:  fsm, finite-state-machine
as fsm
A finite state machine implementation for elixir
Stars: ✭ 14 (-89.55%)
Mutual labels:  fsm, finite-state-machine
Stateless4j
Lightweight Java State Machine
Stars: ✭ 658 (+391.04%)
Mutual labels:  finite-state-machine, fsm
Libfsm
DFA regular expression library & friends
Stars: ✭ 512 (+282.09%)
Mutual labels:  finite-state-machine, fsm
Finity
A finite state machine library for Node.js and the browser with a friendly configuration DSL.
Stars: ✭ 88 (-34.33%)
Mutual labels:  finite-state-machine, fsm
Alexafsm
With alexafsm, developers can model dialog agents with first-class concepts such as states, attributes, transition, and actions. alexafsm also provides visualization and other tools to help understand, test, debug, and maintain complex FSM conversations.
Stars: ✭ 103 (-23.13%)
Mutual labels:  finite-state-machine, fsm
pastafarian
A tiny event-based finite state machine
Stars: ✭ 20 (-85.07%)
Mutual labels:  fsm, finite-state-machine
Jstate
Advanced state machines in Java.
Stars: ✭ 84 (-37.31%)
Mutual labels:  finite-state-machine, fsm
use-state-machine
Use Finite State Machines with React Hooks
Stars: ✭ 28 (-79.1%)
Mutual labels:  fsm, finite-state-machine
UnityHFSM
A simple yet powerful class based hierarchical finite state machine for Unity3D
Stars: ✭ 243 (+81.34%)
Mutual labels:  fsm, finite-state-machine
FiniteStateMachine
This project is a finite state machine designed to be used in games.
Stars: ✭ 45 (-66.42%)
Mutual labels:  fsm, finite-state-machine
xstate
State machines and statecharts for the modern web.
Stars: ✭ 21,286 (+15785.07%)
Mutual labels:  fsm, finite-state-machine
Fsm As Promised
A finite state machine library using ES6 promises
Stars: ✭ 446 (+232.84%)
Mutual labels:  finite-state-machine, fsm
Floatsidebar.js
Lightweight (2kb gzipped), zero-dependency javascript library for making float sidebars based on the finite state machine
Stars: ✭ 56 (-58.21%)
Mutual labels:  finite-state-machine, fsm

Deprecated

This project is no longer maintained. No new issues or pull requests will be accepted. You can still use the source or fork the project to suit your needs.

Statelin

CircleCI

Statelin is a finite state machine implemented purely in Kotlin.

Installation

MAVEN

<repositories>
    <repository>
      <id>jcenter</id>
      <url>https://jcenter.bintray.com/</url>
    </repository>
</repositories>
<dependency>
  <groupId>com.anupcowkur</groupId>
  <artifactId>statelin</artifactId>
  <version>0.1.0</version>
</dependency>

GRADLE

repositories {
    jcenter()
}
compile 'com.anupcowkur:statelin:0.1.0'

Usage

Create a machine with an initial state:

val stateA = State("A")
val machine = Machine(stateA)

Create a new state with optional enter and exit callbacks:

val stateB = State(name = "B",
                    onEnter = { print("Entering state B") },
                    onExit = { print("Exiting state B") })

Transition from A to B:

machine.state = stateB // "Entering state B" will be printed

Add a trigger with a handler:

val onSubmitClick = Trigger("onSubmitClick")
machine.addTriggerHandler(TriggerHandler(stateB, onSubmitClick, {
                // Do whatever. Handle business logic, set a new state etc
            }))

Note: There can be only one trigger handlers for each state and trigger pair. i.e. stateA and triggerX can have only one handler. Adding another handler for the same state and trigger will throw an exception.

That's all there is to know!

What can I use this for?

State machines are useful for modeling any kind of stateful UI or business logic. Let's look at an example.

Let's model an autocompleting search box like google search.

  • Our machine will be initialized in Init state.
  • Once users enter a text, we will trigger onTextEntered and put it into the Loading state.
  • Once we have the results from an API call, we can put the machine into a Showing results state.
  • If new text is entered, we will trigger onTextEntered again and the process repeats.

Our states will be:

val stateInit = State("Init")
val stateShowingResults = State(name = "Showing results",
        onEnter = {
            populateOrUpdateList()
        })
val stateLoading = State(name = "Loading",
        onEnter = {
            setUiToLoading()
            results = callAPI()
            machine.state = stateShowingResults
        })

Our triggers will be:

val triggerOnTextEntered = Trigger("onTextEntered")

And here's our machine and trigger handlers:

val machine = Machine(stateInit)

machine.addTriggerHandler(TriggerHandler(stateInit, triggerOnTextEntered, {
    machine.state = stateLoading
}))
machine.addTriggerHandler(TriggerHandler(stateShowingResults, triggerOnTextEntered, {
    machine.state = stateLoading
}))

State machines allow us to explicitly model the possible states and transitions of our applications. This allows us to define what's possible in the system upfront and prevent bugs like "When X happens and Y happens and Z happens, some random thing happens".

State machines are useful simply because it's easier to program for what's possible than to defend against all the things that should not be possible.

Contributing

You can report bugs in the issues tracker. Please add a sample app with a minimal test case that reproduces the bug you are reporting.

Feature requests are also welcome, preferably with a pull request and tests :-)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].