Skip to content

Commit

Permalink
updated documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
corneil committed Aug 13, 2019
1 parent 552d28d commit d0eb320
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 27 deletions.
10 changes: 5 additions & 5 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ repositories {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-jvm:0.9.1'
implementation 'io.jumpco.open:kfsm-jvm:0.9.2'
}
----

Expand All @@ -230,7 +230,7 @@ dependencies {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-js:0.9.1'
implementation 'io.jumpco.open:kfsm-js:0.9.2'
}
----

Expand All @@ -239,7 +239,7 @@ dependencies {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-linuxX64:0.9.1'
implementation 'io.jumpco.open:kfsm-linuxX64:0.9.2'
}
----

Expand All @@ -248,7 +248,7 @@ dependencies {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-mingwX64:0.9.1'
implementation 'io.jumpco.open:kfsm-mingwX64:0.9.2'
}
----

Expand All @@ -257,7 +257,7 @@ dependencies {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-macosX64:0.9.1'
implementation 'io.jumpco.open:kfsm-macosX64:0.9.2'
}
----

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.2-SNAPSHOT'
version = '0.9.2'
description = 'Kotlin Finite-state machine'

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class StateMachineBuilder<S, E : Enum<E>, C>(validMapStates: Set<S>) {
fun complete(): StateMachineDefinition<S, E, C> {
completed = true
if (namedStateMaps.isNotEmpty()) {
require(this.deriveInitialStateMap != null) { "initialMap must be defined when using named state maps" }
require(this.deriveInitialState == null) { "deriveInitialState cannot be used with named state maps" }
}
return StateMachineDefinition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ package io.jumpco.open.kfsm
class StateMachineDefinition<S, E : Enum<E>, C>(
private val deriveInitialState: StateQuery<C, S>?,
private val deriveInitialMap: StateMapQuery<C, S>?,
/**
* The top level state map will be created when the state machine is created.
*/
val defaultStateMap: StateMapDefinition<S, E, C>,
/**
* The named state maps can be accessed via a push transition.
*/
val namedStateMaps: Map<String, StateMapDefinition<S, E, C>>

) {
Expand Down Expand Up @@ -76,12 +82,29 @@ class StateMachineDefinition<S, E : Enum<E>, C>(
namedStateMaps[name] ?: error("Named map $name not found")
)

/**
* This function will create a state machine instance and set it to the state to a previously externalised state.
* @param context The instance will operate on the provided context
* @param initialExternalState The previously externalised state
*/
fun create(context: C, initialExternalState: ExternalState<S>): StateMachineInstance<S, E, C> =
StateMachineInstance<S, E, C>(context, this, null, initialExternalState)

/**
* This function will create a state machine instance and set it to the initial state.
* @param context The instance will operate on the provided context
* @param initial The initial state
*
*/
fun create(context: C, initial: S? = null): StateMachineInstance<S, E, C> =
StateMachineInstance<S, E, C>(context, this, initial)

/**
* This function will provide a list of possible events given a specific state.
* The actual events may fail because of guard conditions or named state maps and the default state map behaviour being different.
* @param state The given state
* @param includeDefault consider the default state and event handlers
*/
fun possibleEvents(state: S, includeDefault: Boolean): Set<E> {
val result = mutableSetOf<E>()
result.addAll(defaultStateMap.allowed(state, includeDefault))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class StateMachineInstance<S, E : Enum<E>, C>(
initialState: S? = null,
initialExternalState: ExternalState<S>? = null
) {
/**
* Create a state machine using a specific definition and a previous externalised state.
* @param context The instane will operate on the context
* @param definition The definition of the state machine instance.
* @param initialExternalState The previously externalised state.
*/
constructor(context: C, definition: StateMachineDefinition<S, E, C>, initialExternalState: ExternalState<S>) :
this(context, definition, null, initialExternalState) {
}
Expand Down
4 changes: 4 additions & 0 deletions src/commonMain/kotlin/io/jumpco/open/kfsm/StateMapBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

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>(
/**
* The set of states the state map supports
Expand Down
8 changes: 7 additions & 1 deletion src/commonMain/kotlin/io/jumpco/open/kfsm/kfsm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,26 @@ class Stack<T> {
}

/**
* Indicates that the stack is empty.
* @return true if the stack is empty
*/
fun isEmpty() = elements.isEmpty()

/**
*
* Indicates that the stack is not empty.
* @return true if the stack is not empty
*/
fun isNotEmpty() = elements.isNotEmpty()

/**
* Provides access to the top of the stack.
* @return the element at the top of the stack without removing or `null` is the stack is empty.
*/
fun peek(): T? = elements.lastOrNull()

/**
* Provides access to all the entries on the stack.
* @return An immutable list of entries with the top of the stack at the end.
*/
fun peekContent(): List<T> = elements.toList()
}
14 changes: 1 addition & 13 deletions src/commonTest/kotlin/io/jumpco/open/kfsm/turnstile-payment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,6 @@ class PayingTurnstileFSM(turnstile: PayingTurnstile, initialState: ExternalState
PayingTurnstileEvents::class,
PayingTurnstile::class
) {
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")
}
}
}
default {
entry { _, targetState, args ->
if (args.isNotEmpty()) {
Expand Down Expand Up @@ -178,7 +166,7 @@ class PayingTurnstileFSM(turnstile: PayingTurnstile, initialState: ExternalState
}.build()
}

val fsm = if (initialState != null) definition.create(turnstile, initialState) else definition.create(turnstile)
val fsm = if (initialState != null) definition.create(turnstile, initialState) else definition.create(turnstile, PayingTurnstileStates.LOCKED)

fun coin(value: Int) {
println("sendEvent:COIN:$value")
Expand Down
12 changes: 6 additions & 6 deletions src/doc/asciidoc/documentation.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repositories {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-jvm:0.9.1'
implementation 'io.jumpco.open:kfsm-jvm:0.9.2'
}
----

Expand All @@ -26,7 +26,7 @@ dependencies {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-js:0.9.1'
implementation 'io.jumpco.open:kfsm-js:0.9.2'
}
----

Expand All @@ -35,7 +35,7 @@ dependencies {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-wasm32:0.9.1'
implementation 'io.jumpco.open:kfsm-wasm32:0.9.2'
}
----

Expand All @@ -44,7 +44,7 @@ dependencies {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-linuxX64:0.9.1'
implementation 'io.jumpco.open:kfsm-linuxX64:0.9.2'
}
----

Expand All @@ -53,7 +53,7 @@ dependencies {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-mingwX64:0.9.1'
implementation 'io.jumpco.open:kfsm-mingwX64:0.9.2'
}
----

Expand All @@ -62,7 +62,7 @@ dependencies {
[source,groovy]
----
dependencies {
implementation 'io.jumpco.open:kfsm-macosX64:0.9.1'
implementation 'io.jumpco.open:kfsm-macosX64:0.9.2'
}
----
== Operation
Expand Down

0 comments on commit d0eb320

Please sign in to comment.