Package com.tnsai.fsm
Interface Guard
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Functional interface for transition guard conditions.
Guards determine whether a transition can occur based on the current context. If a guard returns false, the transition is not taken even if the event matches.
Usage
// Simple guard
Guard isAuthenticated = ctx -> ctx.has("userId");
// Confidence threshold guard
Guard highConfidence = ctx -> {
double confidence = ctx.getOrDefault("confidence", 0.0);
return confidence > 0.9;
};
// Event data guard
Guard approvedByReviewer = ctx -> {
Event event = ctx.getCurrentEvent();
return "APPROVED".equals(event.getData("status"));
};
// Combining guards
Guard combined = highConfidence.and(isAuthenticated);
-
Method Summary
Modifier and TypeMethodDescriptionstatic Guardalways()Creates a guard that always returns true.default GuardCombines this guard with another using AND.static GuardCreates a guard that checks if a context value equals expected.booleanevaluate(StateMachineContext context) Evaluates the guard condition.static GuardeventDataEquals(String key, Object value) Creates a guard that checks event data.static GuardgreaterThan(String key, double threshold) Creates a guard that checks if a numeric value is greater than threshold.static GuardCreates a guard that checks if a context key exists.static GuardCreates a guard that checks if a numeric value is less than threshold.default Guardnegate()Negates this guard.static Guardnever()Creates a guard that always returns false.default GuardCombines this guard with another using OR.
-
Method Details
-
evaluate
Evaluates the guard condition.- Parameters:
context- the state machine context- Returns:
- true if transition should proceed
-
always
Creates a guard that always returns true.- Returns:
- always-true guard
-
never
Creates a guard that always returns false.- Returns:
- always-false guard
-
hasKey
Creates a guard that checks if a context key exists.- Parameters:
key- the key to check- Returns:
- guard that checks key existence
-
equals
Creates a guard that checks if a context value equals expected.- Parameters:
key- the keyvalue- expected value- Returns:
- guard that checks equality
-
greaterThan
Creates a guard that checks if a numeric value is greater than threshold.- Parameters:
key- the keythreshold- the threshold- Returns:
- guard that checks greater than
-
lessThan
Creates a guard that checks if a numeric value is less than threshold.- Parameters:
key- the keythreshold- the threshold- Returns:
- guard that checks less than
-
eventDataEquals
Creates a guard that checks event data.- Parameters:
key- event data keyvalue- expected value- Returns:
- guard that checks event data
-
and
Combines this guard with another using AND.- Parameters:
other- the other guard- Returns:
- combined guard
-
or
Combines this guard with another using OR.- Parameters:
other- the other guard- Returns:
- combined guard
-
negate
Negates this guard.- Returns:
- negated guard
-