
net.pechorina.kairos.automat.builder.StateConfig.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kairos-automat Show documentation
Show all versions of kairos-automat Show documentation
Kairos Automat is a finite-state machine library
The newest version!
package net.pechorina.kairos.automat.builder
import net.pechorina.kairos.automat.Action
import net.pechorina.kairos.automat.State
data class StateConfig(
val state: S? = null,
var parent: S? = null,
var initialState: Boolean = false,
var finalState: Boolean = false,
var entryAction: Action? = null,
var exitAction: Action? = null) {
fun asState(): State {
if (state == null) throw RuntimeException("State state is not defined")
return State(
value = state,
onEntry = entryAction,
onExit = exitAction,
initialState = initialState,
finalState = finalState
)
}
fun hasParent(): Boolean {
return parent != null
}
fun getChildParentPair(stateFinder: (state: S) -> State): Pair, State> {
if (parent == null) throw RuntimeException("No parent defined")
if (state == null) throw RuntimeException("No state defined")
val child: State = stateFinder(state)
val parent: State = stateFinder(parent!!)
return Pair(child, parent)
}
}