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.

@FunctionalInterface public interface Guard
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 Type
    Method
    Description
    static Guard
    Creates a guard that always returns true.
    default Guard
    and(Guard other)
    Combines this guard with another using AND.
    static Guard
    equals(String key, Object value)
    Creates a guard that checks if a context value equals expected.
    boolean
    Evaluates the guard condition.
    static Guard
    Creates a guard that checks event data.
    static Guard
    greaterThan(String key, double threshold)
    Creates a guard that checks if a numeric value is greater than threshold.
    static Guard
    Creates a guard that checks if a context key exists.
    static Guard
    lessThan(String key, double threshold)
    Creates a guard that checks if a numeric value is less than threshold.
    default Guard
    Negates this guard.
    static Guard
    Creates a guard that always returns false.
    default Guard
    or(Guard other)
    Combines this guard with another using OR.
  • Method Details

    • evaluate

      boolean evaluate(StateMachineContext context)
      Evaluates the guard condition.
      Parameters:
      context - the state machine context
      Returns:
      true if transition should proceed
    • always

      static Guard always()
      Creates a guard that always returns true.
      Returns:
      always-true guard
    • never

      static Guard never()
      Creates a guard that always returns false.
      Returns:
      always-false guard
    • hasKey

      static Guard hasKey(String key)
      Creates a guard that checks if a context key exists.
      Parameters:
      key - the key to check
      Returns:
      guard that checks key existence
    • equals

      static Guard equals(String key, Object value)
      Creates a guard that checks if a context value equals expected.
      Parameters:
      key - the key
      value - expected value
      Returns:
      guard that checks equality
    • greaterThan

      static Guard greaterThan(String key, double threshold)
      Creates a guard that checks if a numeric value is greater than threshold.
      Parameters:
      key - the key
      threshold - the threshold
      Returns:
      guard that checks greater than
    • lessThan

      static Guard lessThan(String key, double threshold)
      Creates a guard that checks if a numeric value is less than threshold.
      Parameters:
      key - the key
      threshold - the threshold
      Returns:
      guard that checks less than
    • eventDataEquals

      static Guard eventDataEquals(String key, Object value)
      Creates a guard that checks event data.
      Parameters:
      key - event data key
      value - expected value
      Returns:
      guard that checks event data
    • and

      default Guard and(Guard other)
      Combines this guard with another using AND.
      Parameters:
      other - the other guard
      Returns:
      combined guard
    • or

      default Guard or(Guard other)
      Combines this guard with another using OR.
      Parameters:
      other - the other guard
      Returns:
      combined guard
    • negate

      default Guard negate()
      Negates this guard.
      Returns:
      negated guard