Package com.tnsai.fsm
Class StateMachine
java.lang.Object
com.tnsai.fsm.StateMachine
A Finite State Machine implementation for bounded agent autonomy.
Provides deterministic state transitions with guard conditions, entry/exit actions, and transition actions. Suitable for production AI agent workflows where predictable behavior is required.
Thread safety: This class is not thread-safe.
All calls to start(), fire(Event), and reset()
must be externally synchronized if accessed from multiple threads.
Usage
// Define states
State idle = State.initial("IDLE");
State processing = State.of("PROCESSING");
State review = State.of("REVIEW");
State approved = State.terminal("APPROVED");
State rejected = State.terminal("REJECTED");
// Build state machine
StateMachine sm = StateMachine.builder("ReviewWorkflow")
.states(idle, processing, review, approved, rejected)
.transition(Transition.from(idle).to(processing).on("START"))
.transition(Transition.from(processing).to(review).on("SUBMIT"))
.transition(Transition.from(review).to(approved)
.on("REVIEW_COMPLETE")
.when(Guard.greaterThan("confidence", 0.9)))
.transition(Transition.from(review).to(rejected)
.on("REVIEW_COMPLETE")
.when(Guard.lessThan("confidence", 0.5)))
.maxTransitions(100)
.build();
// Execute
sm.start();
sm.fire(Event.of("START"));
sm.fire(Event.of("SUBMIT"));
sm.getContext().set("confidence", 0.95);
sm.fire(Event.of("REVIEW_COMPLETE"));
// Check result
if (sm.isInTerminalState()) {
System.out.println("Final state: " + sm.getCurrentState().getName());
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for StateMachine.static enumState machine status. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(StateMachineListener listener) Adds a listener.static StateMachine.BuilderCreates a builder with the given name.booleanFires an event to trigger transitions.Gets available events from current state.Gets the context.Gets the current state.getName()Gets the state machine name.Gets all states.Gets the status.intGets the transition count.Gets all transitions.booleanReturns whether the state machine is in a terminal state.booleanReturns whether the state machine is running.voidremoveListener(StateMachineListener listener) Removes a listener.voidreset()Resets the state machine.start()Starts the state machine.start(StateMachineContext context) Starts the state machine with the given context.toString()
-
Method Details
-
builder
Creates a builder with the given name.- Parameters:
name- the state machine name- Returns:
- new builder
-
start
Starts the state machine.- Returns:
- this state machine
- Throws:
StateMachineException- if already started or no initial state
-
start
Starts the state machine with the given context.- Parameters:
context- the context- Returns:
- this state machine
- Throws:
StateMachineException- if already started or no initial state
-
fire
Fires an event to trigger transitions.- Parameters:
event- the event- Returns:
- true if a transition occurred
- Throws:
StateMachineException- if not running or max transitions exceeded
-
getCurrentState
Gets the current state.- Returns:
- current state
-
getContext
Gets the context.- Returns:
- context
-
getStatus
Gets the status.- Returns:
- status
-
isRunning
public boolean isRunning()Returns whether the state machine is running.- Returns:
- true if running
-
isInTerminalState
public boolean isInTerminalState()Returns whether the state machine is in a terminal state.- Returns:
- true if completed
-
getName
Gets the state machine name.- Returns:
- name
-
getTransitionCount
public int getTransitionCount()Gets the transition count.- Returns:
- number of transitions executed
-
getStates
Gets all states.- Returns:
- unmodifiable map of states
-
getTransitions
Gets all transitions.- Returns:
- unmodifiable list of transitions
-
getAvailableEvents
Gets available events from current state.- Returns:
- list of available event names
-
reset
public void reset()Resets the state machine. -
addListener
Adds a listener.- Parameters:
listener- the listener
-
removeListener
Removes a listener.- Parameters:
listener- the listener
-
toString
-