Skip to content

Commit

Permalink
refactor to match Kotlin Style guide for layout and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
corneil committed Aug 15, 2019
1 parent d0eb320 commit ee80d2d
Show file tree
Hide file tree
Showing 30 changed files with 88 additions and 89 deletions.
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile

plugins {
id 'org.jetbrains.kotlin.multiplatform' version '1.3.41'
Expand All @@ -16,7 +15,7 @@ repositories {
mavenCentral()
}
group = 'io.jumpco.open'
version = '0.9.2'
version = '0.9.3-SNAPSHOT'
description = 'Kotlin Finite-state machine'

java {
Expand All @@ -30,6 +29,8 @@ apply from: "$rootProject.projectDir/gradle/publish.gradle"

def useTarget = project.ext.useTarget



kotlin {
sourceSets {
commonTest {
Expand All @@ -50,7 +51,7 @@ kotlin {

dokka {
// master configuration in gradle/docsgradle
samples = ['src/commonTest/kotlin/io/jumpco/open/kfsm/turnstile.kt']
samples = ['src/commonTest/kotlin/TurnstileTypes.kt']
}

ktlint {
Expand Down
15 changes: 8 additions & 7 deletions gradle/platform.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,18 @@ kotlin {
}
}
if (useTarget['js']) {
if(project.ext.has('nodeInstall')) {
if (project.ext.has('nodeInstall')) {
// tasks.findByPath(':compileKotlinsJs')?.dependsOn('kotlinNodeJsSetup')
tasks.findByPath(':kotlinNodeJsSetup')?.onlyIf { true }
}
js {
browser {
}
nodejs {
}
mavenPublication {
artifactId = "${project.name}-js"
compilations.main {
kotlinOptions {
metaInfo = true
sourceMap = true
verbose = true
moduleKind = "umd"
}
}
}
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,6 @@ 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) {
}

internal val namedInstances: MutableMap<String, StateMapInstance<S, E, C>> = mutableMapOf()

internal val mapStack: Stack<StateMapInstance<S, E, C>> = Stack()
Expand All @@ -51,13 +41,23 @@ class StateMachineInstance<S, E : Enum<E>, C>(
*/
internal var currentStateMap: StateMapInstance<S, E, C>

val currentState: S
get() = currentStateMap.currentState

/**
* 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) {
}

init {
currentStateMap = definition.create(context, this, initialState, initialExternalState)
}

val currentState: S
get() = currentStateMap.currentState

internal fun pushMap(
defaultInstance: StateMapInstance<S, E, C>,
initial: S,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ enum class LockEvents {
}

class LockFSM(context: Lock) {
private val fsm = definition.create(context)

fun allowedEvents() = fsm.allowed()
fun unlock() = fsm.sendEvent(LockEvents.UNLOCK)
fun lock() = fsm.sendEvent(LockEvents.LOCK)

companion object {
private val definition = stateMachine(LockStates.values().toSet(), LockEvents::class, Lock::class) {
initial {
Expand Down Expand Up @@ -98,10 +104,4 @@ class LockFSM(context: Lock) {
}
}.build()
}

private val fsm = definition.create(context)

fun allowedEvents() = fsm.allowed()
fun unlock() = fsm.sendEvent(LockEvents.UNLOCK)
fun lock() = fsm.sendEvent(LockEvents.LOCK)
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ 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)

fun coin(value: Int) {
println("sendEvent:COIN:$value")
fsm.sendEvent(PayingTurnstileEvents.COIN, value)
}

fun pass() {
println("sendEvent:PASS")
fsm.sendEvent(PayingTurnstileEvents.PASS)
}

fun allowedEvents() = fsm.allowed().map { it.name.toLowerCase() }.toSet()
fun externalState() = fsm.externalState()

companion object {
val definition = stateMachine(
setOf(PayingTurnstileStates.LOCKED, PayingTurnstileStates.UNLOCKED),
Expand Down Expand Up @@ -165,19 +180,4 @@ class PayingTurnstileFSM(turnstile: PayingTurnstile, initialState: ExternalState
}
}.build()
}

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

fun coin(value: Int) {
println("sendEvent:COIN:$value")
fsm.sendEvent(PayingTurnstileEvents.COIN, value)
}

fun pass() {
println("sendEvent:PASS")
fsm.sendEvent(PayingTurnstileEvents.PASS)
}

fun allowedEvents() = fsm.allowed().map { it.name.toLowerCase() }.toSet()
fun externalState() = fsm.externalState()
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ enum class TurnstileEvents {
* @suppress
*/
class TurnstileFSM(turnstile: Turnstile, savedState: TurnstileStates? = null) {
private val fsm = definition.create(turnstile, savedState)

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

companion object {
private val definition =
stateMachine(TurnstileStates.values().toSet(), TurnstileEvents::class, Turnstile::class) {
Expand Down Expand Up @@ -99,13 +106,6 @@ class TurnstileFSM(turnstile: Turnstile, savedState: TurnstileStates? = null) {
fun possibleEvents(state: TurnstileStates, includeDefault: Boolean = false) =
definition.possibleEvents(state, includeDefault)
}

private val fsm = definition.create(turnstile, savedState)

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

/**
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,23 @@ class DetailMockedTests {
}

class TestDetailFSM(context: TestContext) {
private val fsm = definition.create(context)
fun event1() {
println("--event1")
fsm.sendEvent(TestEvents.EVENT1)
}

fun event2() {
println("--event2")
fsm.sendEvent(TestEvents.EVENT2)
}

fun event3() {
println("--event3")
fsm.sendEvent(TestEvents.EVENT3)
}
companion object {
private val definition by lazy { define() }
private fun define() = StateMachineBuilder<TestStates, TestEvents, TestContext>(TestStates.values().toSet())
.stateMachine {
initial {
Expand Down Expand Up @@ -134,24 +150,6 @@ class DetailMockedTests {
}
}
}.build()

private val definition by lazy { define() }
}

private val fsm = definition.create(context)
fun event1() {
println("--event1")
fsm.sendEvent(TestEvents.EVENT1)
}

fun event2() {
println("--event2")
fsm.sendEvent(TestEvents.EVENT2)
}

fun event3() {
println("--event3")
fsm.sendEvent(TestEvents.EVENT3)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ class DetailTests {
}

class TestDetailFSM(context: TestContext) {
val fsm = definition.create(context)
fun allowedEvents() = fsm.allowed()
fun eventAllowed(event: TestEvents, includeDefaults: Boolean = false) =
fsm.eventAllowed(event, includeDefaults)

fun event1(msg: String) {
println("--event1")
fsm.sendEvent(TestEvents.EVENT1, msg)
}

fun event2(msg: String) {
println("--event2")
fsm.sendEvent(TestEvents.EVENT2, msg)
}

fun event3(msg: String) {
println("--event3")
fsm.sendEvent(TestEvents.EVENT3, msg)
}
companion object {
val definition = StateMachineBuilder<TestStates, TestEvents, TestContext>(TestStates.values().toSet())
.stateMachine {
Expand Down Expand Up @@ -166,26 +185,6 @@ class DetailTests {
}
}.build()
}

val fsm = definition.create(context)
fun allowedEvents() = fsm.allowed()
fun eventAllowed(event: TestEvents, includeDefaults: Boolean = false) =
fsm.eventAllowed(event, includeDefaults)

fun event1(msg: String) {
println("--event1")
fsm.sendEvent(TestEvents.EVENT1, msg)
}

fun event2(msg: String) {
println("--event2")
fsm.sendEvent(TestEvents.EVENT2, msg)
}

fun event3(msg: String) {
println("--event3")
fsm.sendEvent(TestEvents.EVENT3, msg)
}
}

@Test
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit ee80d2d

Please sign in to comment.