Class IterativeCondition<T>

java.lang.Object
org.apache.flink.cep.pattern.conditions.IterativeCondition<T>
All Implemented Interfaces:
Serializable, org.apache.flink.api.common.functions.Function
Direct Known Subclasses:
RichIterativeCondition, SimpleCondition

@PublicEvolving public abstract class IterativeCondition<T> extends Object implements org.apache.flink.api.common.functions.Function, Serializable
A user-defined condition that decides if an element should be accepted in the pattern or not. Accepting an element also signals a state transition for the corresponding NFA.

A condition can be a simple filter or a more complex condition that iterates over the previously accepted elements in the pattern and decides to accept a new element or not based on some statistic over these elements. In the former case, the condition should extend the SimpleCondition class. In the later, the condition should extend this class, which gives you also access to the previously accepted elements through a IterativeCondition.Context.

An iterative condition that accepts an element if i) its name is middle, and ii) the sum of the prices of all accepted elements is less than 5 would look like:


 private class MyCondition extends IterativeCondition<Event> {

 		@Override
     	public boolean filter(Event value, Context<Event> ctx) throws Exception {
     		if (!value.getName().equals("middle")) {
     			return false;
     		}

     		double sum = 0.0;
     		for (Event e: ctx.getEventsForPattern("middle")) {
     			sum += e.getPrice();
     		}
     		sum += value.getPrice();
     		return Double.compare(sum, 5.0) <= 0;
     	}
    }
 
ATTENTION: The call to getEventsForPattern(...) has to find the elements that belong to the pattern among the elements stored by the NFA. The cost of this operation can vary, so when implementing your condition, try to minimize the times the method is called.
See Also:
  • Constructor Details

    • IterativeCondition

      public IterativeCondition()
  • Method Details

    • filter

      public abstract boolean filter(T value, IterativeCondition.Context<T> ctx) throws Exception
      The filter function that evaluates the predicate.

      IMPORTANT: The system assumes that the function does not modify the elements on which the predicate is applied. Violating this assumption can lead to incorrect results.

      Parameters:
      value - The value to be tested.
      ctx - The IterativeCondition.Context used for the evaluation of the function and provides access to the already accepted events in the pattern (see IterativeCondition.Context.getEventsForPattern(String)).
      Returns:
      true for values that should be retained, false for values to be filtered out.
      Throws:
      Exception - This method may throw exceptions. Throwing an exception will cause the operation to fail and may trigger recovery.