Search in sources :

Example 6 with IterativeCondition

use of org.apache.flink.cep.pattern.conditions.IterativeCondition in project flink by apache.

the class IterativeConditionsITCase method testIterativeWithABACPattern.

@Test
public void testIterativeWithABACPattern() throws Exception {
    List<StreamRecord<Event>> inputEvents = new ArrayList<>();
    // 1
    inputEvents.add(new StreamRecord<>(startEvent1, 1L));
    // 1
    inputEvents.add(new StreamRecord<Event>(middleEvent1, 2L));
    // 2
    inputEvents.add(new StreamRecord<>(startEvent2, 2L));
    // 3
    inputEvents.add(new StreamRecord<>(startEvent3, 2L));
    // 2
    inputEvents.add(new StreamRecord<Event>(middleEvent2, 2L));
    // 4
    inputEvents.add(new StreamRecord<>(startEvent4, 2L));
    // 3
    inputEvents.add(new StreamRecord<Event>(middleEvent3, 2L));
    // 1
    inputEvents.add(new StreamRecord<Event>(middleEvent4, 2L));
    inputEvents.add(new StreamRecord<>(endEvent, 4L));
    Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 6215754202506583964L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("start");
        }
    }).followedByAny("middle1").subtype(SubEvent.class).where(new SimpleCondition<SubEvent>() {

        private static final long serialVersionUID = 2178338526904474690L;

        @Override
        public boolean filter(SubEvent value) throws Exception {
            return value.getName().startsWith("foo");
        }
    }).followedBy("middle2").where(new IterativeCondition<Event>() {

        private static final long serialVersionUID = -1223388426808292695L;

        @Override
        public boolean filter(Event value, Context<Event> ctx) throws Exception {
            if (!value.getName().equals("start")) {
                return false;
            }
            double sum = 0.0;
            for (Event e : ctx.getEventsForPattern("middle2")) {
                sum += e.getPrice();
            }
            sum += value.getPrice();
            return Double.compare(sum, 5.0) <= 0;
        }
    }).oneOrMore().followedBy("end").where(new SimpleCondition<Event>() {

        private static final long serialVersionUID = 562590474115118323L;

        @Override
        public boolean filter(Event value) throws Exception {
            return value.getName().equals("end");
        }
    });
    NFA<Event> nfa = compile(pattern, false);
    List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
    comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(startEvent1, startEvent2, startEvent3, middleEvent1, endEvent), Lists.newArrayList(startEvent1, middleEvent1, startEvent2, endEvent), Lists.newArrayList(startEvent1, middleEvent2, startEvent4, endEvent), Lists.newArrayList(startEvent2, middleEvent2, startEvent4, endEvent), Lists.newArrayList(startEvent3, middleEvent2, startEvent4, endEvent)));
}
Also used : StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) SubEvent(org.apache.flink.cep.SubEvent) SimpleCondition(org.apache.flink.cep.pattern.conditions.SimpleCondition) ArrayList(java.util.ArrayList) IterativeCondition(org.apache.flink.cep.pattern.conditions.IterativeCondition) Event(org.apache.flink.cep.Event) SubEvent(org.apache.flink.cep.SubEvent) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 7 with IterativeCondition

use of org.apache.flink.cep.pattern.conditions.IterativeCondition in project flink by apache.

the class NFA method open.

/**
 * Initialization method for the NFA. It is called before any element is passed and thus
 * suitable for one time setup work.
 *
 * @param cepRuntimeContext runtime context of the enclosing operator
 * @param conf The configuration containing the parameters attached to the contract.
 */
public void open(RuntimeContext cepRuntimeContext, Configuration conf) throws Exception {
    for (State<T> state : getStates()) {
        for (StateTransition<T> transition : state.getStateTransitions()) {
            IterativeCondition condition = transition.getCondition();
            FunctionUtils.setFunctionRuntimeContext(condition, cepRuntimeContext);
            FunctionUtils.openFunction(condition, conf);
        }
    }
}
Also used : IterativeCondition(org.apache.flink.cep.pattern.conditions.IterativeCondition)

Example 8 with IterativeCondition

use of org.apache.flink.cep.pattern.conditions.IterativeCondition in project flink by apache.

the class NFA method close.

/**
 * Tear-down method for the NFA.
 */
public void close() throws Exception {
    for (State<T> state : getStates()) {
        for (StateTransition<T> transition : state.getStateTransitions()) {
            IterativeCondition condition = transition.getCondition();
            FunctionUtils.closeFunction(condition);
        }
    }
}
Also used : IterativeCondition(org.apache.flink.cep.pattern.conditions.IterativeCondition)

Aggregations

IterativeCondition (org.apache.flink.cep.pattern.conditions.IterativeCondition)8 Event (org.apache.flink.cep.Event)6 Test (org.junit.Test)6 SimpleCondition (org.apache.flink.cep.pattern.conditions.SimpleCondition)4 ArrayList (java.util.ArrayList)3 List (java.util.List)2 SubEvent (org.apache.flink.cep.SubEvent)2 NFATestHarness (org.apache.flink.cep.utils.NFATestHarness)2 StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)1 Pattern (org.apache.flink.cep.pattern.Pattern)1 DataInputViewStreamWrapper (org.apache.flink.core.memory.DataInputViewStreamWrapper)1 DataOutputViewStreamWrapper (org.apache.flink.core.memory.DataOutputViewStreamWrapper)1