An open API service indexing awesome lists of open source software.

https://github.com/werbhelius/statemachine

state machine by kotlin
https://github.com/werbhelius/statemachine

Last synced: 2 months ago
JSON representation

state machine by kotlin

Awesome Lists containing this project

README

        

# statemachine

### Dependency
```gradle
implementation 'com.werb.statemachine:statemachine:0.0.1'
```

### Example
![statemachine](./statemachine.png)

### Code

```kotlin
class StateMachineTest {

enum class State { Solid, Liquid, Gas }

enum class Transition { OnMelted, OnFroze, OnVaporized, OnCondensed }

private val stateMachine: StateMachine by lazy {
StateMachine().apply {

state(State.Solid) {
lastState?.apply {
println("I have been ${State.Solid} from $this")
} ?: with(State.Solid) {
println("I have been initialState with $this")
}
}

state(State.Liquid) {
lastState?.apply {
println("I have been ${State.Liquid} from $this")
} ?: with(State.Liquid) {
println("I have been initialState with $this")
}
}

state(State.Gas) {
lastState?.apply {
println("I have been ${State.Gas} from $this")
} ?: with(State.Gas) {
println("I have been initialState with $this")
}
}

addTransition(State.Solid, Transition.OnMelted, State.Liquid)
addTransition(State.Liquid, Transition.OnFroze, State.Solid)
addTransition(State.Liquid, Transition.OnVaporized, State.Gas)
addTransition(State.Gas, Transition.OnCondensed, State.Liquid)
}
}

@Test
fun initialState() {
stateMachine.initialState(State.Solid)
}

@Test
fun solid_onMelted_to_liquid() {
stateMachine.initialState(State.Solid)
stateMachine.executeTransition(Transition.OnMelted)
}

@Test
fun liquid_onFroze_to_solid() {
stateMachine.initialState(State.Liquid)
stateMachine.executeTransition(Transition.OnFroze)
}

@Test
fun liquid_onVaporized_to_gas() {
stateMachine.initialState(State.Liquid)
stateMachine.executeTransition(Transition.OnVaporized)
}

@Test
fun gas_onCondensed_to_liquid() {
stateMachine.initialState(State.Gas)
stateMachine.executeTransition(Transition.OnCondensed)
}

}
```