Package com.tnsai.fsm
Class Transition
java.lang.Object
com.tnsai.fsm.Transition
Represents a transition between states in a Finite State Machine.
Transitions are triggered by events and can have guard conditions that must be satisfied for the transition to occur. Transitions can also have actions that execute during the transition.
Usage
// Simple transition
Transition t1 = Transition.from(idle).to(processing).on("START");
// Transition with guard
Transition t2 = Transition.from(processing).to(approved)
.on("REVIEW_COMPLETE")
.when(ctx -> ctx.getOrDefault("confidence", 0.0) > 0.9);
// Transition with action
Transition t3 = Transition.from(processing).to(rejected)
.on("REVIEW_COMPLETE")
.when(Guard.lessThan("confidence", 0.5))
.action(ctx -> ctx.set("reason", "Low confidence"));
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classFull builder for Transition.static classBuilder step for specifying event.static classBuilder step for specifying target state. -
Method Summary
Modifier and TypeMethodDescriptionbooleancanFire(StateMachineContext context) Evaluates the guard condition.voidexecuteAction(StateMachineContext context) Executes the transition action if present.static Transition.SourceBuilderStarts building a transition from the given state.Gets the transition action.Gets the triggering event name.getGuard()Gets the guard condition.intGets the priority (higher = more preferred).Gets the source state.Gets the target state.static Transition.EventBuilderCreates an internal transition (self-loop).booleanReturns whether this is an automatic transition (no event required).booleanReturns whether this is a self-transition (internal).booleanmatchesEvent(Event event) Checks if this transition matches the given event.toString()
-
Method Details
-
from
Starts building a transition from the given state.- Parameters:
source- the source state- Returns:
- new builder
-
internal
Creates an internal transition (self-loop).- Parameters:
state- the state- Returns:
- builder for the internal transition
-
getSource
Gets the source state.- Returns:
- source state
-
getTarget
Gets the target state.- Returns:
- target state
-
getEventName
Gets the triggering event name.- Returns:
- event name, or null for automatic transitions
-
isAutomatic
public boolean isAutomatic()Returns whether this is an automatic transition (no event required).- Returns:
- true if automatic
-
getGuard
Gets the guard condition.- Returns:
- guard (never null, defaults to always())
-
getAction
Gets the transition action.- Returns:
- action or null
-
getPriority
public int getPriority()Gets the priority (higher = more preferred).- Returns:
- priority
-
matchesEvent
Checks if this transition matches the given event.- Parameters:
event- the event- Returns:
- true if matches
-
canFire
Evaluates the guard condition.- Parameters:
context- the context- Returns:
- true if guard passes
-
executeAction
Executes the transition action if present.- Parameters:
context- the context
-
isInternal
public boolean isInternal()Returns whether this is a self-transition (internal).- Returns:
- true if source equals target
-
toString
-