Package com.tnsai.fsm

Class StateMachineContext

java.lang.Object
com.tnsai.fsm.StateMachineContext

public class StateMachineContext extends Object
Context object passed to state actions and guards.

Provides access to state machine state, event data, and a shared key-value store for passing data between states.

Usage


 // In a state action
 StateActionSpec action = ctx -> {
     String userId = ctx.get("userId");
     ctx.set("processed", true);
     ctx.setResult("Success");
 };

 // In a guard condition
 Guard guard = ctx -> {
     double confidence = ctx.getOrDefault("confidence", 0.0);
     return confidence > 0.9;
 };
 
  • Constructor Details

    • StateMachineContext

      public StateMachineContext()
      Creates a new context.
    • StateMachineContext

      public StateMachineContext(Map<String,Object> initialData)
      Creates a context with initial data.
      Parameters:
      initialData - initial data map
  • Method Details

    • getCurrentState

      public State getCurrentState()
      Gets the current state.
      Returns:
      current state
    • getCurrentEvent

      public Event getCurrentEvent()
      Gets the current event being processed.
      Returns:
      current event
    • get

      public <T> T get(String key)
      Gets a value from the context.
      Type Parameters:
      T - the value type
      Parameters:
      key - the key
      Returns:
      value or null
    • getOrDefault

      public <T> T getOrDefault(String key, T defaultValue)
      Gets a value with a default.
      Type Parameters:
      T - the value type
      Parameters:
      key - the key
      defaultValue - default value if not found
      Returns:
      value or default
    • set

      public void set(String key, Object value)
      Sets a value in the context.
      Parameters:
      key - the key
      value - the value
    • remove

      public void remove(String key)
      Removes a value from the context.
      Parameters:
      key - the key
    • has

      public boolean has(String key)
      Checks if a key exists.
      Parameters:
      key - the key
      Returns:
      true if exists
    • getResult

      public <T> T getResult()
      Gets the result of the state machine execution.
      Type Parameters:
      T - the result type
      Returns:
      result or null
    • setResult

      public void setResult(Object result)
      Sets the result.
      Parameters:
      result - the result
    • getError

      public String getError()
      Gets the error message if any.
      Returns:
      error message or null
    • setError

      public void setError(String error)
      Sets an error message.
      Parameters:
      error - the error message
    • hasError

      public boolean hasError()
      Returns whether there's an error.
      Returns:
      true if error exists
    • getHistory

      Gets the transition history.
      Returns:
      list of transition records
    • getTransitionCount

      public int getTransitionCount()
      Gets the number of transitions that occurred.
      Returns:
      transition count
    • getData

      public Map<String,Object> getData()
      Gets all data as an unmodifiable map.
      Returns:
      data map
    • clear

      public void clear()
      Clears all data.