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)));
}
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);
}
}
}
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);
}
}
}
Aggregations