All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.io.jumpco.open.kfsm.DslStateMachineHandler.kt Maven / Gradle / Ivy

There is a newer version: 1.9.0-RC1
Show newest version
/*
 * 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

/**
 * This handler will be active inside the top level of the stateMachine definition.
 */
class DslStateMachineHandler, C>(private val fsm: StateMachineBuilder) {
    /**
     * 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.
     */
    fun initial(deriveInitialState: StateQuery): DslStateMachineHandler {
        fsm.initial(deriveInitialState)
        return this
    }

    /**
     * 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 {
    mutableListOf>().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")
        }
    }
}
```
     */
    fun initialMap(deriveInitialMap: StateMapQuery): DslStateMachineHandler {
        fsm.initialMap(deriveInitialMap)
        return this
    }

    /**
     * Defines a section for a specific state.
     * @param currentState The give state
     * @param handler A lambda with definitions for the given state
     */
    fun state(currentState: S, handler: DslStateMapEventHandler.() -> Unit):
        DslStateMapEventHandler =
        DslStateMapEventHandler(currentState, fsm.defaultStateMap).apply(handler)

    /**
     * Defines a section for default behaviour for the state machine.
     * @param handler A lambda with definition for the default behaviour of the state machine.
     */
    fun default(handler: DslStateMapDefaultEventHandler.() -> Unit):
        DslStateMapDefaultEventHandler =
        DslStateMapDefaultEventHandler(fsm.defaultStateMap).apply(handler)

    /**
     * Returns the completed fsm.
     */
    fun build() = fsm.complete()

    /**
     * creates a named statemap
     */
    fun stateMap(
        /**
         * The name of the state map. ,
        /**
         * The lambda to configure the statemap
         */
        handler: DslStateMapHandler.() -> Unit
    ): DslStateMapHandler {
        return DslStateMapHandler(fsm.stateMap(name.trim(), validStates)).apply(handler)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy