Skip to content

Commit

Permalink
release 0.9.4
Browse files Browse the repository at this point in the history
  • Loading branch information
corneil committed Sep 5, 2019
1 parent 8293d19 commit 06065ef
Show file tree
Hide file tree
Showing 51 changed files with 805 additions and 312 deletions.
10 changes: 5 additions & 5 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ repositories {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-jvm:0.9.3'
implementation 'io.jumpco.open:kfsm-jvm:0.9.4'
}
----

Expand All @@ -233,7 +233,7 @@ dependencies {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-js:0.9.3'
implementation 'io.jumpco.open:kfsm-js:0.9.4'
}
----

Expand All @@ -242,7 +242,7 @@ dependencies {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-linuxX64:0.9.3'
implementation 'io.jumpco.open:kfsm-linuxX64:0.9.4'
}
----

Expand All @@ -251,7 +251,7 @@ dependencies {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-mingwX64:0.9.3'
implementation 'io.jumpco.open:kfsm-mingwX64:0.9.4'
}
----

Expand All @@ -260,7 +260,7 @@ dependencies {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-macosX64:0.9.3'
implementation 'io.jumpco.open:kfsm-macosX64:0.9.4'
}
----

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

java {
Expand Down
13 changes: 9 additions & 4 deletions gradle/docs.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,14 @@ dokka {
}

asciidoctor {
logDocuments = true
inputs.files(fileTree('src/docs/asciidoc/*'))
requires ['asciidoctor-prism-extension']

sources {
include 'index.adoc'
}

resources {
from(sourceDir) {
include '**/*.png'
Expand All @@ -66,10 +71,10 @@ asciidoctor {
}
}
attributes toc: 'left',
'source-highlighter': 'rouge',
idprefix: '',
idseparator: '-',
docinfo1: ''
'source-highlighter': 'prism',
idprefix: '',
idseparator: '-',
docinfo1: ''
}

task docs(type: Zip, dependsOn: [asciidoctor, dokka]) {
Expand Down
6 changes: 3 additions & 3 deletions src/commonMain/kotlin/CommonTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ enum class TransitionType {
* @param contextClass The class of the context
* @sample io.jumpco.open.kfsm.TurnstileFSM.definition
*/
inline fun <S, E : Enum<E>, C : Any> stateMachine(
inline fun <S, E, C : Any> stateMachine(
validStates: Set<S>,
eventClass: KClass<E>,
validEvents: Set<E>,
contextClass: KClass<out C>,
handler: DslStateMachineHandler<S, E, C>.() -> Unit
) = StateMachineBuilder<S, E, C>(validStates).stateMachine(handler)
) = StateMachineBuilder<S, E, C>(validStates, validEvents).stateMachine(handler)

/**
* An extension function that evaluates the expression and invokes the provided `block` if true or the `otherwise` block is false.
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/DefaultTransition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package io.jumpco.open.kfsm
* @param targetState when optional represents an internal transition
* @param action optional lambda will be invoked when transition occurs.
*/
class DefaultTransition<E : Enum<E>, S, C>(
class DefaultTransition<E, S, C>(
internal val event: E,
targetState: S?,
targetMap: String?,
Expand Down
26 changes: 13 additions & 13 deletions src/commonMain/kotlin/DslStateMachineHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package io.jumpco.open.kfsm
/**
* This handler will be active inside the top level of the stateMachine definition.
*/
class DslStateMachineHandler<S, E : Enum<E>, C>(private val fsm: StateMachineBuilder<S, E, C>) {
class DslStateMachineHandler<S, E, C>(private val fsm: StateMachineBuilder<S, E, C>) {
/**
* Defines an expression that will determine the initial state of the state machine based on the values of the context.
* @param deriveInitialState A lambda expression receiving context:C and returning state S.
Expand All @@ -26,20 +26,20 @@ class DslStateMachineHandler<S, E : Enum<E>, C>(private val fsm: StateMachineBui
* Provides for a list of pairs with state and map name that will be pushed and the last entry will be popped and become the current map.
* This is required when using state machine with named maps.
*
```
initialMap {
```
initialMap {
mutableListOf<StateMapItem<PayingTurnstileStates>>().apply {
if (locked) {
this.add(PayingTurnstileStates.LOCKED to "default")
} else {
this.add(PayingTurnstileStates.UNLOCKED to "default")
}
if (coins > 0) {
this.add(PayingTurnstileStates.COINS to "coins")
}
if (locked) {
this.add(PayingTurnstileStates.LOCKED to "default")
} else {
this.add(PayingTurnstileStates.UNLOCKED to "default")
}
}
```
if (coins > 0) {
this.add(PayingTurnstileStates.COINS to "coins")
}
}
}
```
*/
fun initialMap(deriveInitialMap: StateMapQuery<C, S>): DslStateMachineHandler<S, E, C> {
fsm.initialMap(deriveInitialMap)
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/DslStateMapDefaultEventHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package io.jumpco.open.kfsm
/**
* This handler will be active inside the default section of the statemachine.
*/
class DslStateMapDefaultEventHandler<S, E : Enum<E>, C>(private val fsm: StateMapBuilder<S, E, C>) {
class DslStateMapDefaultEventHandler<S, E, C>(private val fsm: StateMapBuilder<S, E, C>) {
/**
* Define a default action that will be applied when no other transitions are matched.
* @param action Will be invoked when no transitions matches
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/DslStateMapEventHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package io.jumpco.open.kfsm
/**
* This class is used in dsl to handle the state declarations.
*/
class DslStateMapEventHandler<S, E : Enum<E>, C>(
class DslStateMapEventHandler<S, E, C>(
private val currentState: S,
private val fsm: StateMapBuilder<S, E, C>
) {
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/DslStateMapHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package io.jumpco.open.kfsm
/**
* This handler will be active inside the top level of the stateMachine definition.
*/
class DslStateMapHandler<S, E : Enum<E>, C>(private val fsm: StateMapBuilder<S, E, C>) {
class DslStateMapHandler<S, E, C>(private val fsm: StateMapBuilder<S, E, C>) {
/**
* Defines a section for a specific state.
* @param currentState The give state
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/GuardedTransition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package io.jumpco.open.kfsm
* @param guard Expression lambda returning a Boolean
* @param action An optional lambda that will be invoked.
*/
open class GuardedTransition<S, E : Enum<E>, C>(
open class GuardedTransition<S, E, C>(
startState: S,
event: E?,
targetState: S?,
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/SimpleTransition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package io.jumpco.open.kfsm
* @param targetState when optional represents an internal transition
* @param action An optional lambda that will be invoked.
*/
open class SimpleTransition<S, E : Enum<E>, C>(
open class SimpleTransition<S, E, C>(
internal val startState: S,
internal val event: E?,
targetState: S?,
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/StateMachineBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package io.jumpco.open.kfsm
* @param E is en enum representing all the events the FSM may receive
* @param C is the class of the Context where the action will be applied.
*/
class StateMachineBuilder<S, E : Enum<E>, C>(validMapStates: Set<S>) {
class StateMachineBuilder<S, E, C>(validMapStates: Set<S>, internal val validEvents: Set<E>) {
private var completed = false
private var deriveInitialState: StateQuery<C, S>? = null
private var deriveInitialStateMap: StateMapQuery<C, S>? = null
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/StateMachineDefinition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package io.jumpco.open.kfsm
/**
* This class represents an immutable definition of a state machine.
*/
class StateMachineDefinition<S, E : Enum<E>, C>(
class StateMachineDefinition<S, E, C>(
private val deriveInitialState: StateQuery<C, S>?,
private val deriveInitialMap: StateMapQuery<C, S>?,
/**
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/StateMachineInstance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package io.jumpco.open.kfsm
* @param definition The defined state machine that provides all the behaviour
* @param initialState The initial state of the instance.
*/
class StateMachineInstance<S, E : Enum<E>, C>(
class StateMachineInstance<S, E, C>(
/**
* The transition actions are performed by manipulating the context.
*/
Expand Down
12 changes: 11 additions & 1 deletion src/commonMain/kotlin/StateMapBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ package io.jumpco.open.kfsm
* The build will be created to assist with create the top level or named state maps.
* All transitions are assigned to a state map.
*/
class StateMapBuilder<S, E : Enum<E>, C>(
class StateMapBuilder<S, E, C>(
/**
* The set of states the state map supports
*/
Expand Down Expand Up @@ -47,6 +47,7 @@ class StateMapBuilder<S, E : Enum<E>, C>(
* @param action The optional action will be executed when the transition occurs.
*/
fun transition(startState: S, event: E, targetState: S, guard: StateGuard<C>, action: StateAction<C>?) {
require(parentBuilder.validEvents.contains(event)) { "$event must be one of ${parentBuilder.validEvents}" }
require(validStates.contains(startState)) { "$startState must be one of $validStates" }
val key = Pair(startState, event)
val transitionRule = transitionRules[key]
Expand Down Expand Up @@ -78,6 +79,7 @@ class StateMapBuilder<S, E : Enum<E>, C>(
* @param action The optional action will be executed when the transition occurs.
*/
fun transition(startState: S, event: E, guard: StateGuard<C>, action: StateAction<C>?) {
require(parentBuilder.validEvents.contains(event)) { "$event must be one of ${parentBuilder.validEvents}" }
require(validStates.contains(startState)) { "$startState must be one of $validStates" }
val key = Pair(startState, event)
val transitionRule = transitionRules[key]
Expand Down Expand Up @@ -109,6 +111,7 @@ class StateMapBuilder<S, E : Enum<E>, C>(
* @param action The actions will be invoked
*/
fun transition(startState: S, event: E, targetState: S, action: StateAction<C>?) {
require(parentBuilder.validEvents.contains(event)) { "$event must be one of ${parentBuilder.validEvents}" }
require(validStates.contains(startState)) { "$startState must be one of $validStates" }
val key = Pair(startState, event)
val transitionRule = transitionRules[key]
Expand Down Expand Up @@ -136,6 +139,7 @@ class StateMapBuilder<S, E : Enum<E>, C>(
* @param action actions will be invoked
*/
fun transition(startState: S, event: E, action: StateAction<C>?) {
require(parentBuilder.validEvents.contains(event)) { "$event must be one of ${parentBuilder.validEvents}" }
require(validStates.contains(startState)) { "$startState must be one of $validStates" }
val key = Pair(startState, event)
val transitionRule = transitionRules[key]
Expand Down Expand Up @@ -165,6 +169,7 @@ class StateMapBuilder<S, E : Enum<E>, C>(
* @param action The optional action that will be invoked.
*/
fun popTransition(startState: S, event: E, targetState: S?, targetMap: String?, action: StateAction<C>?) {
require(parentBuilder.validEvents.contains(event)) { "$event must be one of ${parentBuilder.validEvents}" }
val key = Pair(startState, event)
val transitionRule = transitionRules[key]
val transition = SimpleTransition(
Expand Down Expand Up @@ -201,6 +206,7 @@ class StateMapBuilder<S, E : Enum<E>, C>(
guard: StateGuard<C>,
action: StateAction<C>?
) {
require(parentBuilder.validEvents.contains(event)) { "$event must be one of ${parentBuilder.validEvents}" }
require(validStates.contains(startState)) { "$startState must be one of $validStates" }
val key = Pair(startState, event)
val transitionRule = transitionRules[key]
Expand Down Expand Up @@ -232,6 +238,7 @@ class StateMapBuilder<S, E : Enum<E>, C>(
* @param action The optional action will be invoked
*/
fun pushTransition(startState: S, event: E, targetMap: String, targetState: S, action: StateAction<C>?) {
require(parentBuilder.validEvents.contains(event)) { "$event must be one of ${parentBuilder.validEvents}" }
require(validStates.contains(startState)) { "$startState must be one of $validStates" }
require(parentBuilder.namedStateMaps.containsKey(targetMap)) { "$targetMap map not found in ${parentBuilder.namedStateMaps.keys}" }
val key = Pair(startState, event)
Expand Down Expand Up @@ -262,6 +269,7 @@ class StateMapBuilder<S, E : Enum<E>, C>(
guard: StateGuard<C>,
action: StateAction<C>?
) {
require(parentBuilder.validEvents.contains(event)) { "$event must be one of ${parentBuilder.validEvents}" }
require(validStates.contains(startState)) { "$startState must be one of $validStates" }
require(parentBuilder.namedStateMaps.containsKey(targetMap)) { "$targetMap map not found in ${parentBuilder.namedStateMaps.keys}" }
val key = Pair(startState, event)
Expand Down Expand Up @@ -530,6 +538,7 @@ class StateMapBuilder<S, E : Enum<E>, C>(
* @param action The option action will be executed when this default transition occurs.
*/
fun default(event: EventState<E, S>, action: StateAction<C>?) {
require(parentBuilder.validEvents.contains(event.first)) { "$event must be one of ${parentBuilder.validEvents}" }
require(defaultTransitions[event.first] == null) { "Default transition for ${event.first} already defined" }
defaultTransitions[event.first] =
DefaultTransition(event.first, event.second, null, false, TransitionType.NORMAL, action)
Expand All @@ -541,6 +550,7 @@ class StateMapBuilder<S, E : Enum<E>, C>(
* @param action The option action will be executed when this default transition occurs.
*/
fun default(event: E, action: StateAction<C>?) {
require(parentBuilder.validEvents.contains(event)) { "$event must be one of ${parentBuilder.validEvents}" }
require(defaultTransitions[event] == null) { "Default transition for $event already defined" }
defaultTransitions[event] = DefaultTransition<E, S, C>(event, null, null, false, TransitionType.NORMAL, action)
}
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/StateMapDefinition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package io.jumpco.open.kfsm
/**
* Contains the definition of a state map. A state machine has at least one top-level state map.
*/
class StateMapDefinition<S, E : Enum<E>, C>(
class StateMapDefinition<S, E, C>(
/**
* The name of the statemap. The top-level state map name is `null`
*/
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/StateMapInstance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package io.jumpco.open.kfsm
/**
* @suppress
*/
class StateMapInstance<S, E : Enum<E>, C>(
class StateMapInstance<S, E, C>(
val context: C,
val newState: S,
val name: String?,
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/Transition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package io.jumpco.open.kfsm
* @param targetState when optional represents an internal transition
* @param action optional lambda will be invoked when transition occurs.
*/
open class Transition<S, E : Enum<E>, C>(
open class Transition<S, E, C>(
val targetState: S? = null,
val targetMap: String? = null,
val automatic: Boolean = false,
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/TransitionRules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package io.jumpco.open.kfsm
* @param guardedTransitions The list of guarded transitions
* @param transition The transition to use if there are no guarded transitions or no guarded transitions match.
*/
class TransitionRules<S, E : Enum<E>, C>(
class TransitionRules<S, E, C>(
private val guardedTransitions: MutableList<GuardedTransition<S, E, C>> = mutableListOf(),
internal var transition: SimpleTransition<S, E, C>? = null
) {
Expand Down
6 changes: 5 additions & 1 deletion src/commonTest/kotlin/LockTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ class LockFSM(context: Lock) {
fun lock() = fsm.sendEvent(LockEvents.LOCK)

companion object {
private val definition = stateMachine(LockStates.values().toSet(), LockEvents::class, Lock::class) {
private val definition = stateMachine(
LockStates.values().toSet(),
LockEvents.values().toSet(),
Lock::class
) {
initial {
when (locked) {
0 -> LockStates.UNLOCKED
Expand Down
7 changes: 5 additions & 2 deletions src/commonTest/kotlin/PayingTurnstileTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ enum class PayingTurnstileEvents {
* @suppress
*/
class PayingTurnstileFSM(turnstile: PayingTurnstile, initialState: ExternalState<PayingTurnstileStates>? = null) {
val fsm = if (initialState != null) definition.create(turnstile, initialState) else definition.create(turnstile, PayingTurnstileStates.LOCKED)
val fsm = if (initialState != null) definition.create(turnstile, initialState) else definition.create(
turnstile,
PayingTurnstileStates.LOCKED
)

fun coin(value: Int) {
println("sendEvent:COIN:$value")
Expand All @@ -99,7 +102,7 @@ class PayingTurnstileFSM(turnstile: PayingTurnstile, initialState: ExternalState
companion object {
val definition = stateMachine(
setOf(PayingTurnstileStates.LOCKED, PayingTurnstileStates.UNLOCKED),
PayingTurnstileEvents::class,
PayingTurnstileEvents.values().toSet(),
PayingTurnstile::class
) {
default {
Expand Down
6 changes: 5 additions & 1 deletion src/commonTest/kotlin/TurnstileTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ class TurnstileFSM(turnstile: Turnstile, savedState: TurnstileStates? = null) {

companion object {
private val definition =
stateMachine(TurnstileStates.values().toSet(), TurnstileEvents::class, Turnstile::class) {
stateMachine(
TurnstileStates.values().toSet(),
TurnstileEvents.values().toSet(),
Turnstile::class
) {
initial {
if (locked)
TurnstileStates.LOCKED
Expand Down
Loading

0 comments on commit 06065ef

Please sign in to comment.