Package com.tnsai.fsm

Class Transition

java.lang.Object
com.tnsai.fsm.Transition

public class Transition extends Object
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"));
 
  • Method Details

    • from

      public static Transition.SourceBuilder from(State source)
      Starts building a transition from the given state.
      Parameters:
      source - the source state
      Returns:
      new builder
    • internal

      public static Transition.EventBuilder internal(State state)
      Creates an internal transition (self-loop).
      Parameters:
      state - the state
      Returns:
      builder for the internal transition
    • getSource

      public State getSource()
      Gets the source state.
      Returns:
      source state
    • getTarget

      public State getTarget()
      Gets the target state.
      Returns:
      target state
    • getEventName

      public String 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

      public Guard getGuard()
      Gets the guard condition.
      Returns:
      guard (never null, defaults to always())
    • getAction

      public TransitionAction getAction()
      Gets the transition action.
      Returns:
      action or null
    • getPriority

      public int getPriority()
      Gets the priority (higher = more preferred).
      Returns:
      priority
    • matchesEvent

      public boolean matchesEvent(Event event)
      Checks if this transition matches the given event.
      Parameters:
      event - the event
      Returns:
      true if matches
    • canFire

      public boolean canFire(StateMachineContext context)
      Evaluates the guard condition.
      Parameters:
      context - the context
      Returns:
      true if guard passes
    • executeAction

      public void executeAction(StateMachineContext context)
      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

      public String toString()
      Overrides:
      toString in class Object