use of org.apache.flink.cep.SubEvent in project flink by apache.
the class NFAITCase method testBranchingPattern.
@Test
public void testBranchingPattern() throws Exception {
List<StreamRecord<Event>> inputEvents = new ArrayList<>();
Event startEvent = new Event(40, "start", 1.0);
SubEvent middleEvent1 = new SubEvent(41, "foo1", 1.0, 10.0);
SubEvent middleEvent2 = new SubEvent(42, "foo2", 1.0, 10.0);
SubEvent middleEvent3 = new SubEvent(43, "foo3", 1.0, 10.0);
SubEvent nextOne1 = new SubEvent(44, "next-one", 1.0, 2.0);
SubEvent nextOne2 = new SubEvent(45, "next-one", 1.0, 2.0);
Event endEvent = new Event(46, "end", 1.0);
inputEvents.add(new StreamRecord<>(startEvent, 1));
inputEvents.add(new StreamRecord<Event>(middleEvent1, 3));
inputEvents.add(new StreamRecord<Event>(middleEvent2, 4));
inputEvents.add(new StreamRecord<Event>(middleEvent3, 5));
inputEvents.add(new StreamRecord<Event>(nextOne1, 6));
inputEvents.add(new StreamRecord<Event>(nextOne2, 7));
inputEvents.add(new StreamRecord<>(endEvent, 8));
Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {
private static final long serialVersionUID = 5726188262756267490L;
@Override
public boolean filter(Event value) throws Exception {
return value.getName().equals("start");
}
}).followedByAny("middle-first").subtype(SubEvent.class).where(new SimpleCondition<SubEvent>() {
private static final long serialVersionUID = 6215754202506583964L;
@Override
public boolean filter(SubEvent value) throws Exception {
return value.getVolume() > 5.0;
}
}).followedByAny("middle-second").subtype(SubEvent.class).where(new SimpleCondition<SubEvent>() {
private static final long serialVersionUID = 6215754202506583964L;
@Override
public boolean filter(SubEvent value) throws Exception {
return value.getName().equals("next-one");
}
}).followedByAny("end").where(new SimpleCondition<Event>() {
private static final long serialVersionUID = 7056763917392056548L;
@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(startEvent, middleEvent1, nextOne1, endEvent), Lists.newArrayList(startEvent, middleEvent2, nextOne1, endEvent), Lists.newArrayList(startEvent, middleEvent3, nextOne1, endEvent), Lists.newArrayList(startEvent, middleEvent1, nextOne2, endEvent), Lists.newArrayList(startEvent, middleEvent2, nextOne2, endEvent), Lists.newArrayList(startEvent, middleEvent3, nextOne2, endEvent)));
}
use of org.apache.flink.cep.SubEvent 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)));
}
Aggregations