Skip to content

Commit

Permalink
rename on to transition
Browse files Browse the repository at this point in the history
  • Loading branch information
corneil committed Aug 4, 2019
1 parent c217b9e commit 088c977
Show file tree
Hide file tree
Showing 17 changed files with 140 additions and 130 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,15 @@ class TurnstileFSM(turnstile: Turnstile) {
}
}
state(TurnstileStates.LOCKED) {
on(TurnstileEvents.COIN to TurnstileStates.UNLOCKED) {
transition(TurnstileEvents.COIN to TurnstileStates.UNLOCKED) {
unlock()
}
}
state(TurnstileStates.UNLOCKED) {
on(TurnstileEvents.COIN) {
transition(TurnstileEvents.COIN) {
returnCoin()
}
on(TurnstileEvents.PASS to TurnstileStates.LOCKED) {
transition(TurnstileEvents.PASS to TurnstileStates.LOCKED) {
lock()
}
}
Expand Down Expand Up @@ -145,31 +145,31 @@ Add the relevant dependency to your build file.
### Kotlin/JVM Projects
```groovy
dependencies {
implementation 'io.jumpco.open:kfsm-jvm:0.7.1'
implementation 'io.jumpco.open:kfsm-jvm:0.8.0'
}
```
### KotlinJS Projects
```groovy
dependencies {
implementation 'io.jumpco.open:kfsm-js:0.7.1'
implementation 'io.jumpco.open:kfsm-js:0.8.0'
}
```
### Kotlin/Native Projects using LinuxX64
```groovy
dependencies {
implementation 'io.jumpco.open:kfsm-linuxX64:0.7.1'
implementation 'io.jumpco.open:kfsm-linuxX64:0.8.0'
}
```
### Kotlin/Native Projects using MinGW64
```groovy
dependencies {
implementation 'io.jumpco.open:kfsm-mingwX64:0.7.1'
implementation 'io.jumpco.open:kfsm-mingwX64:0.8.0'
}
```
### Kotlin/Native Projects using macOS
```groovy
dependencies {
implementation 'io.jumpco.open:kfsm-macosX64:0.7.1'
implementation 'io.jumpco.open:kfsm-macosX64:0.8.0'
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repositories {
mavenCentral()
}
group = 'io.jumpco.open'
version = '0.7.1'
version = '0.8.0'
description = 'Kotlin Finite-state machine'

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class DslStateMachineDefaultEventHandler<S : Enum<S>, E : Enum<E>, C>(private va
* @param event Pair representing an on and targetState for transition. Can be written as EVENT to STATE
* @param action The action will be performed before transition is completed
*/
fun on(event: EventState<E, S>, action: StateAction<C>?): DslStateMachineDefaultEventHandler<S, E, C> {
fun transition(event: EventState<E, S>, action: StateAction<C>?): DslStateMachineDefaultEventHandler<S, E, C> {
fsm.default(event, action)
return this
}
Expand All @@ -58,7 +58,7 @@ class DslStateMachineDefaultEventHandler<S : Enum<S>, E : Enum<E>, C>(private va
* @param event The event that triggers this transition
* @param action The action will be invoked for this transition
*/
fun on(event: E, action: StateAction<C>?): DslStateMachineDefaultEventHandler<S, E, C> {
fun transition(event: E, action: StateAction<C>?): DslStateMachineDefaultEventHandler<S, E, C> {
fsm.default(event, action)
return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class DslStateMachineEventHandler<S : Enum<S>, E : Enum<E>, C>(
* @param event A Pair with the first being the on and the second being the targetState.
* @param action The action will be performed
*/
fun on(event: EventState<E, S>, action: StateAction<C>?): DslStateMachineEventHandler<S, E, C> {
fun transition(event: EventState<E, S>, action: StateAction<C>?): DslStateMachineEventHandler<S, E, C> {
fsm.transition(currentState, event.first, event.second, action)
return this
}
Expand All @@ -56,7 +56,7 @@ class DslStateMachineEventHandler<S : Enum<S>, E : Enum<E>, C>(
* @param guard The guard expression must be met before the transition is considered.
* @param action The optional action that may be executed
*/
fun on(
fun transition(
event: EventState<E, S>,
guard: StateGuard<C>,
action: StateAction<C>?
Expand All @@ -70,7 +70,7 @@ class DslStateMachineEventHandler<S : Enum<S>, E : Enum<E>, C>(
* @param event The event and targetState the defines the transition
* @param action The optional action that may be executed
*/
fun on(event: E, action: StateAction<C>?): DslStateMachineEventHandler<S, E, C> {
fun transition(event: E, action: StateAction<C>?): DslStateMachineEventHandler<S, E, C> {
fsm.transition(currentState, event, action)
return this
}
Expand All @@ -82,7 +82,7 @@ class DslStateMachineEventHandler<S : Enum<S>, E : Enum<E>, C>(
* @param guard The guard expression must be met before the transition is considered.
* @param action The optional action that may be executed
*/
fun on(event: E, guard: StateGuard<C>, action: StateAction<C>?): DslStateMachineEventHandler<S, E, C> {
fun transition(event: E, guard: StateGuard<C>, action: StateAction<C>?): DslStateMachineEventHandler<S, E, C> {
fsm.transition(currentState, event, guard, action)
return this
}
Expand Down
8 changes: 4 additions & 4 deletions src/commonTest/kotlin/io/jumpco/open/kfsm/lock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,20 @@ class LockFSM(context: Lock) {
}
}
state(LockStates.LOCKED) {
on(LockEvents.LOCK to LockStates.DOUBLE_LOCKED) {
transition(LockEvents.LOCK to LockStates.DOUBLE_LOCKED) {
doubleLock()
}
on(LockEvents.UNLOCK to LockStates.UNLOCKED) {
transition(LockEvents.UNLOCK to LockStates.UNLOCKED) {
unlock()
}
}
state(LockStates.DOUBLE_LOCKED) {
on(LockEvents.UNLOCK to LockStates.LOCKED) {
transition(LockEvents.UNLOCK to LockStates.LOCKED) {
doubleUnlock()
}
}
state(LockStates.UNLOCKED) {
on(LockEvents.LOCK to LockStates.LOCKED) {
transition(LockEvents.LOCK to LockStates.LOCKED) {
lock()
}
}
Expand Down
41 changes: 26 additions & 15 deletions src/commonTest/kotlin/io/jumpco/open/kfsm/turnstile-payment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,59 +123,69 @@ class PayingTurnstileFSM(turnstile: PayingTurnstile) {
}
state(PayingTurnstileStates.LOCKED) {
// The coins add up to more than required
on(PayingTurnstileEvents.COIN to PayingTurnstileStates.UNLOCKED,
guard = { args -> val value = args[0] as Int;
transition(PayingTurnstileEvents.COIN to PayingTurnstileStates.UNLOCKED,
guard = { args ->
val value = args[0] as Int;
value + this.coins > this.requiredCoins
}) { args -> val value = args[0] as Int
}) { args ->
val value = args[0] as Int
returnCoin(coin(value) - requiredCoins)
unlock()
reset()
}
// The coins add up to more than required
on(PayingTurnstileEvents.COIN to PayingTurnstileStates.COINS,
guard = { args -> val value = args[0] as Int;
transition(PayingTurnstileEvents.COIN to PayingTurnstileStates.COINS,
guard = { args ->
val value = args[0] as Int;
value + this.coins < this.requiredCoins
}) { args -> val value = args[0] as Int
}) { args ->
val value = args[0] as Int
coin(value)
println("Coins=$coins, Please add ${requiredCoins - coins}")
}
// The coin brings amount to exact amount
on(PayingTurnstileEvents.COIN to PayingTurnstileStates.UNLOCKED) { args -> val value = args[0] as Int
transition(PayingTurnstileEvents.COIN to PayingTurnstileStates.UNLOCKED) { args ->
val value = args[0] as Int
coin(value)
unlock()
reset()
}
}
state(PayingTurnstileStates.COINS) {
// The coins add up to more than required.
on(PayingTurnstileEvents.COIN to PayingTurnstileStates.UNLOCKED,
guard = { args -> val value = args[0] as Int
transition(PayingTurnstileEvents.COIN to PayingTurnstileStates.UNLOCKED,
guard = { args ->
val value = args[0] as Int
value + this.coins > this.requiredCoins
}) { args -> val value = args[0] as Int
returnCoin(coin(value) - requiredCoins)
unlock()
reset()
}
// The coins isn't enough to make total match required
on(PayingTurnstileEvents.COIN to PayingTurnstileStates.COINS,
guard = { args -> val value = args[0] as Int;
transition(PayingTurnstileEvents.COIN to PayingTurnstileStates.COINS,
guard = { args ->
val value = args[0] as Int;
value + this.coins < this.requiredCoins
}) { args -> val value = args[0] as Int
}) { args ->
val value = args[0] as Int
coin(value)
println("Coins=$coins, Please add ${requiredCoins - coins}")
}
// The coin is exact amount required
on(PayingTurnstileEvents.COIN to PayingTurnstileStates.UNLOCKED) { args -> val value = args[0] as Int
transition(PayingTurnstileEvents.COIN to PayingTurnstileStates.UNLOCKED) { args ->
val value = args[0] as Int
coin(value)
unlock()
reset()
}
}
state(PayingTurnstileStates.UNLOCKED) {
on(PayingTurnstileEvents.COIN) { args -> val value = args[0] as Int
transition(PayingTurnstileEvents.COIN) { args ->
val value = args[0] as Int
returnCoin(coin(value))
}
on(PayingTurnstileEvents.PASS to PayingTurnstileStates.LOCKED) {
transition(PayingTurnstileEvents.PASS to PayingTurnstileStates.LOCKED) {
lock()
}
}
Expand All @@ -186,4 +196,5 @@ class PayingTurnstileFSM(turnstile: PayingTurnstile) {

fun coin(value: Int) = fsm.sendEvent(PayingTurnstileEvents.COIN, value)
fun pass() = fsm.sendEvent(PayingTurnstileEvents.PASS)
fun allowedEvents() = fsm.allowed().map { it.name.toLowerCase() }.toSet()
}
8 changes: 4 additions & 4 deletions src/commonTest/kotlin/io/jumpco/open/kfsm/turnstile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ class TurnstileFSM(turnstile: Turnstile) {
}
}
state(TurnstileStates.LOCKED) {
on(TurnstileEvents.COIN to TurnstileStates.UNLOCKED) {
transition(TurnstileEvents.COIN to TurnstileStates.UNLOCKED) {
unlock()
}
}
state(TurnstileStates.UNLOCKED) {
on(TurnstileEvents.COIN) {
transition(TurnstileEvents.COIN) {
returnCoin()
}
on(TurnstileEvents.PASS to TurnstileStates.LOCKED) {
transition(TurnstileEvents.PASS to TurnstileStates.LOCKED) {
lock()
}
}
Expand All @@ -101,7 +101,7 @@ class TurnstileFSM(turnstile: Turnstile) {

fun coin() = fsm.sendEvent(TurnstileEvents.COIN)
fun pass() = fsm.sendEvent(TurnstileEvents.PASS)
fun allowedEvents() = fsm.allowed().map { it.name }.toSet()
fun allowedEvents() = fsm.allowed().map { it.name.toLowerCase() }.toSet()
}


Expand Down
42 changes: 21 additions & 21 deletions src/doc/asciidoc/documentation.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repositories {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-jvm:0.7.1'
implementation 'io.jumpco.open:kfsm-jvm:0.8.0'
}
----

Expand All @@ -25,7 +25,7 @@ dependencies {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-js:0.7.1'
implementation 'io.jumpco.open:kfsm-js:0.8.0'
}
----

Expand All @@ -34,7 +34,7 @@ dependencies {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-wasm32:0.7.1'
implementation 'io.jumpco.open:kfsm-wasm32:0.8.0'
}
----

Expand All @@ -43,7 +43,7 @@ dependencies {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-linuxX64:0.7.1'
implementation 'io.jumpco.open:kfsm-linuxX64:0.8.0'
}
----

Expand All @@ -52,7 +52,7 @@ dependencies {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-mingwX64:0.7.1'
implementation 'io.jumpco.open:kfsm-mingwX64:0.8.0'
}
----

Expand All @@ -61,7 +61,7 @@ dependencies {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-macosX64:0.7.1'
implementation 'io.jumpco.open:kfsm-macosX64:0.8.0'
}
----

Expand Down Expand Up @@ -121,7 +121,7 @@ default {
}
exit { // global state exit action
}
on { // default transitions
transition { // default transitions
}
}
----
Expand Down Expand Up @@ -174,8 +174,8 @@ exit { fromState, targetState, args ->
}
----

==== on(E [to S]) {}
* Handler: link:javadoc/kfsm/io.jumpco.open.kfsm/-dsl-state-machine-default-event-handler/on.html[DslStateMachineDefaultEventHandler::on]
==== transition(E [to S]) {}
* Handler: link:javadoc/kfsm/io.jumpco.open.kfsm/-dsl-state-machine-default-event-handler/transition.html[DslStateMachineDefaultEventHandler::transition]
* Mandatory: _Optional_
* Cardinality: _Multiple_

Expand All @@ -186,11 +186,11 @@ In both cases the lambda type is `C.(Array<out Any) -> Unit`
Example:
[source,kotlin]
----
on(Event.EVENT) { args -> // default internal state action for given event
transition(Event.EVENT) { args -> // default internal state action for given event
someFunction()
}
on(Event.EVENT to State.STATE) { args-> // default external state action for given event
transition(Event.EVENT to State.STATE) { args-> // default external state action for given event
anotherFunction()
}
----
Expand Down Expand Up @@ -231,13 +231,13 @@ state(State.STATE) {
}
exit { // exit action for State.STATE
}
on(Event.EV2 to State.S1, guard = {flag == 1 }) { // external transition with guard expression
transition(Event.EV2 to State.S1, guard = {flag == 1 }) { // external transition with guard expression
}
on(Event.EV2 to State.S1) { // external transition
transition(Event.EV2 to State.S1) { // external transition
}
on(Event.EV1, guard = { flag == 2 }) { // internal transition with guard expression
transition(Event.EV1, guard = { flag == 2 }) { // internal transition with guard expression
}
on(Event.EV1) { // internal guard expression
transition(Event.EV1) { // internal guard expression
}
}
----
Expand Down Expand Up @@ -286,8 +286,8 @@ exit { fromState, targetState, args -> // state exit action
println("Exiting:$fromState to $targetState with ${args.toList()}")
}
----
==== on(E [to S],[guard = {}]) {}
* Handler: link:javadoc/kfsm/io.jumpco.open.kfsm/-dsl-state-machine-event-handler/on.html[DslStateMachineEventHandler::on]
==== transition(E [to S],[guard = {}]) {}
* Handler: link:javadoc/kfsm/io.jumpco.open.kfsm/-dsl-state-machine-event-handler/transition.html[DslStateMachineEventHandler::transition]
* Mandatory: _Optional_
* Cardinality: _Multiple_

Expand All @@ -302,12 +302,12 @@ Their may be only one transition without a guard expression.
Examples:
[source,kotlin]
----
on(Event.EV1, guard = { flag == 1 }) { args -> // internal transition with guard expression
transition(Event.EV1, guard = { flag == 1 }) { args -> // internal transition with guard expression
}
on(Event.EV1 to State.S2, guard = { flag == 2}) { args -> // external transition with guard expression
transition(Event.EV1 to State.S2, guard = { flag == 2}) { args -> // external transition with guard expression
}
on(Event.EV1) { args -> // internal transition
transition(Event.EV1) { args -> // internal transition
}
on(Event.EV2 to State.S2) { args -> // external transition
transition(Event.EV2 to State.S2) { args -> // external transition
}
----
Loading

0 comments on commit 088c977

Please sign in to comment.