commonMain.io.jumpco.open.kfsm.Transition.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kfsm-jvm Show documentation
Show all versions of kfsm-jvm Show documentation
Kotlin Finite-state machine
/*
* Copyright (c) 2019. Open JumpCO
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program. If not, see .
*/
package io.jumpco.open.kfsm
/**
* @suppress
* The base for all transitions.
* @param targetState when optional represents an internal transition
* @param action optional lambda will be invoked when transition occurs.
*/
open class Transition, C>(
val targetState: S? = null,
val targetMap: String? = null,
val automatic: Boolean = false,
val type: TransitionType = TransitionType.NORMAL,
private val action: StateAction? = null
) {
init {
if (type == TransitionType.PUSH) {
require(targetState != null) { "targetState is required for push transition" }
}
}
/**
* Executed exit, optional and entry actions specific in the transition.
*/
open fun execute(context: C, instance: StateMapInstance, args: Array) {
if (isExternal()) {
instance.executeExit(context, targetState!!, args)
}
action?.invoke(context, args)
if (isExternal()) {
instance.executeEntry(context, targetState!!, args)
}
}
/**
* Executed exit, optional and entry actions specific in the transition.
*/
open fun execute(
context: C,
sourceMap: StateMapInstance,
targetMap: StateMapInstance?,
args: Array
) {
if (isExternal()) {
sourceMap.executeExit(context, targetState!!, args)
}
action?.invoke(context, args)
if (targetMap != null && isExternal()) {
targetMap.executeEntry(context, targetState!!, args)
}
}
/**
* This function provides an indicator if a Transition is internal or external.
* When there is no targetState defined a Transition is considered internal and will not trigger entry or exit actions.
*/
fun isExternal(): Boolean = targetState != null
}