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

net.pechorina.kairos.automat.builder.ExternalTransitionConfig.kt Maven / Gradle / Ivy

The newest version!
package net.pechorina.kairos.automat.builder

import net.pechorina.kairos.automat.Action
import net.pechorina.kairos.automat.PayloadEvent
import net.pechorina.kairos.automat.State
import net.pechorina.kairos.automat.Transition

data class ExternalTransitionConfig(
        internal val transitionConfigurer: TransitionConfigurer,
        var source: S? = null,
        var target: S? = null,
        var event: E? = null,
        var action: Action? = null) : TransitionConfig {

    fun event(event: E): ExternalTransitionConfig {
        this.event = event
        return this
    }

    fun source(state: S): ExternalTransitionConfig {
        this.source = state
        return this
    }

    fun target(state: S): ExternalTransitionConfig {
        this.target = state
        return this
    }

    fun action(action: Action): ExternalTransitionConfig {
        this.action = action
        return this
    }

    override fun asTransition(stateFinder: (payload: S) -> State): Transition {
        if (source == null) throw RuntimeException("Source is not defined for the transition config ${this}")
        if (target == null) throw RuntimeException("Target is not defined for the transition config ${this}")
        if (event == null) throw RuntimeException("Event is not defined for the transition config ${this}")

        val sourceState = stateFinder(source!!)
        val targetState = stateFinder(target!!)
        val transitionEvent = PayloadEvent(event!!)

        return Transition(sourceState, targetState, transitionEvent, action)
    }

    override fun and(): TransitionConfigurer {
        return transitionConfigurer
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy