Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
zhxnlai committed Jun 14, 2018
1 parent ddd9d86 commit 849eb5f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# StateMachine

A Kotlin library for finite state machines.

### Usage

In this example, we create a `StateMachine` from the following state diagram.

![State Diagram](./example/activity-diagram.png)

Define states:
~~~kotlin
sealed class State {
object Solid : State()
object Liquid : State()
object Gas : State()
}

sealed class Event {
object OnMelted : Event()
object OnFroze : Event()
object OnVaporized : Event()
object OnCondensed : Event()
}

sealed class SideEffect {
object LogMelted : SideEffect()
object LogFrozen : SideEffect()
object LogVaporized : SideEffect()
object LogCondensed : SideEffect()
}
~~~

Declare state transitions:
~~~kotlin
val stateMachine = StateMachine.create<State, Event, SideEffect> {
initialState(State.Solid)
state<State.Solid> {
on<Event.OnMelted> {
transitionTo(State.Liquid, SideEffect.LogMelted)
}
}
state<State.Liquid> {
on<Event.OnFroze> {
transitionTo(State.Solid, SideEffect.LogFrozen)
}
on<Event.OnVaporized> {
transitionTo(State.Gas, SideEffect.LogVaporized)
}
}
state<State.Gas> {
on<Event.OnCondensed> {
transitionTo(State.Liquid, SideEffect.LogCondensed)
}
}
onTransition {
val validTransition = it as? StateMachine.Transition.Valid ?: return@onTransition
when (validTransition.sideEffect) {
SideEffect.LogMelted -> logger.log(ON_MELTED_MESSAGE)
SideEffect.LogFrozen -> logger.log(ON_FROZEN_MESSAGE)
SideEffect.LogVaporized -> logger.log(ON_VAPORIZED_MESSAGE)
SideEffect.LogCondensed -> logger.log(ON_CONDENSED_MESSAGE)
}
}
}
~~~

### Download
Download [the latest JAR][latest-jar] or grab via Maven:
```xml
<dependency>
<groupId>com.tinder</groupId>
<artifactId>state-machine</artifactId>
<version>0.0.1</version>
</dependency>
```
or Gradle:
```groovy
implementation 'com.tinder:state-machine:0.0.1'
```

### License
Binary file added example/activity-diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 849eb5f

Please sign in to comment.