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

commonMain.s2.automate.core.TransitionStateGuard.kt Maven / Gradle / Ivy

package s2.automate.core

import s2.automate.core.context.TransitionContext
import s2.automate.core.error.ERROR_INVALID_TRANSITION
import s2.automate.core.guard.GuardAdapter
import s2.automate.core.guard.GuardResult
import s2.dsl.automate.Automate
import s2.dsl.automate.S2State
import s2.dsl.automate.model.WithS2Id
import s2.dsl.automate.model.WithS2State

class TransitionStateGuard
	: GuardAdapter() where
STATE : S2State,
ENTITY : WithS2State,
ENTITY : WithS2Id,
AUTOMATE : Automate {

	override suspend fun evaluateTransition(context: TransitionContext): GuardResult {
		val state = context.entity.s2State()
		val cmd = context.command
		val isCmdValid = context.automateContext.automate.isAvailableTransition(state, cmd)
		return if (isCmdValid) {
			GuardResult.valid()
		} else {
			GuardResult.error(
				ERROR_INVALID_TRANSITION(state.toString(), "$cmd")
			)
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy