Package com.tnsai.fsm
Class StateMachineContext
java.lang.Object
com.tnsai.fsm.StateMachineContext
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;
};
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordRecord of a state transition. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new context.StateMachineContext(Map<String, Object> initialData) Creates a context with initial data. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all data.<T> TGets a value from the context.Gets the current event being processed.Gets the current state.getData()Gets all data as an unmodifiable map.getError()Gets the error message if any.Gets the transition history.<T> TgetOrDefault(String key, T defaultValue) Gets a value with a default.<T> TGets the result of the state machine execution.intGets the number of transitions that occurred.booleanChecks if a key exists.booleanhasError()Returns whether there's an error.voidRemoves a value from the context.voidSets a value in the context.voidSets an error message.voidSets the result.
-
Constructor Details
-
StateMachineContext
public StateMachineContext()Creates a new context. -
StateMachineContext
Creates a context with initial data.- Parameters:
initialData- initial data map
-
-
Method Details
-
getCurrentState
Gets the current state.- Returns:
- current state
-
getCurrentEvent
Gets the current event being processed.- Returns:
- current event
-
get
Gets a value from the context.- Type Parameters:
T- the value type- Parameters:
key- the key- Returns:
- value or null
-
getOrDefault
Gets a value with a default.- Type Parameters:
T- the value type- Parameters:
key- the keydefaultValue- default value if not found- Returns:
- value or default
-
set
Sets a value in the context.- Parameters:
key- the keyvalue- the value
-
remove
Removes a value from the context.- Parameters:
key- the key
-
has
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
Sets the result.- Parameters:
result- the result
-
getError
Gets the error message if any.- Returns:
- error message or null
-
setError
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
Gets all data as an unmodifiable map.- Returns:
- data map
-
clear
public void clear()Clears all data.
-