use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink by splunk.
the class GroupITCase method testGroupFollowedByAnyTimesOptional.
@Test
public void testGroupFollowedByAnyTimesOptional() throws Exception {
List<StreamRecord<Event>> inputEvents = new ArrayList<>();
Event c = new Event(40, "c", 1.0);
Event a1 = new Event(41, "a", 2.0);
Event b1 = new Event(42, "b", 3.0);
Event a2 = new Event(43, "a", 4.0);
Event b2 = new Event(44, "b", 5.0);
Event a3 = new Event(45, "a", 4.0);
Event b3 = new Event(46, "b", 5.0);
Event d = new Event(47, "d", 6.0);
inputEvents.add(new StreamRecord<>(c, 1));
inputEvents.add(new StreamRecord<>(a1, 2));
inputEvents.add(new StreamRecord<>(b1, 3));
inputEvents.add(new StreamRecord<>(a2, 4));
inputEvents.add(new StreamRecord<>(b2, 5));
inputEvents.add(new StreamRecord<>(a3, 6));
inputEvents.add(new StreamRecord<>(b3, 7));
inputEvents.add(new StreamRecord<>(d, 8));
// c any (a b){2}? d
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("c");
}
}).followedByAny(Pattern.<Event>begin("middle1").where(new SimpleCondition<Event>() {
private static final long serialVersionUID = 5726188262756267490L;
@Override
public boolean filter(Event value) throws Exception {
return value.getName().equals("a");
}
}).followedBy("middle2").where(new SimpleCondition<Event>() {
private static final long serialVersionUID = 5726188262756267490L;
@Override
public boolean filter(Event value) throws Exception {
return value.getName().equals("b");
}
})).times(2).optional().followedBy("end").where(new SimpleCondition<Event>() {
private static final long serialVersionUID = 5726188262756267490L;
@Override
public boolean filter(Event value) throws Exception {
return value.getName().equals("d");
}
});
NFA<Event> nfa = compile(pattern, false);
final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(c, d), Lists.newArrayList(c, a1, b1, a2, b2, d), Lists.newArrayList(c, a2, b2, a3, b3, d)));
}
use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink by splunk.
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.SimpleCondition in project flink by splunk.
the class GreedyITCase method testEndWithZeroOrMoreGreedy.
@Test
public void testEndWithZeroOrMoreGreedy() throws Exception {
List<StreamRecord<Event>> inputEvents = new ArrayList<>();
Event c = new Event(40, "c", 1.0);
Event a1 = new Event(41, "a", 2.0);
Event a2 = new Event(42, "a", 2.0);
Event a3 = new Event(43, "a", 2.0);
inputEvents.add(new StreamRecord<>(c, 1));
inputEvents.add(new StreamRecord<>(a1, 2));
inputEvents.add(new StreamRecord<>(a2, 3));
inputEvents.add(new StreamRecord<>(new Event(44, "dummy", 2.0), 4));
inputEvents.add(new StreamRecord<>(a3, 5));
// c a*
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("c");
}
}).followedBy("end").where(new SimpleCondition<Event>() {
private static final long serialVersionUID = 5726188262756267490L;
@Override
public boolean filter(Event value) throws Exception {
return value.getName().equals("a");
}
}).oneOrMore().optional().greedy();
NFA<Event> nfa = compile(pattern, false);
final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(c), Lists.newArrayList(c, a1), Lists.newArrayList(c, a1, a2), Lists.newArrayList(c, a1, a2, a3)));
}
use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink by splunk.
the class GreedyITCase method testGreedyZeroOrMore.
@Test
public void testGreedyZeroOrMore() throws Exception {
List<StreamRecord<Event>> inputEvents = new ArrayList<>();
Event c = new Event(40, "c", 1.0);
Event a1 = new Event(41, "a", 2.0);
Event a2 = new Event(42, "a", 2.0);
Event a3 = new Event(43, "a", 2.0);
Event d = new Event(44, "d", 3.0);
inputEvents.add(new StreamRecord<>(c, 1));
inputEvents.add(new StreamRecord<>(a1, 2));
inputEvents.add(new StreamRecord<>(a2, 3));
inputEvents.add(new StreamRecord<>(a3, 4));
inputEvents.add(new StreamRecord<>(d, 5));
// c a* d
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("c");
}
}).followedBy("middle").where(new SimpleCondition<Event>() {
private static final long serialVersionUID = 5726188262756267490L;
@Override
public boolean filter(Event value) throws Exception {
return value.getName().equals("a");
}
}).oneOrMore().optional().greedy().followedBy("end").where(new SimpleCondition<Event>() {
private static final long serialVersionUID = 5726188262756267490L;
@Override
public boolean filter(Event value) throws Exception {
return value.getName().equals("d");
}
});
NFA<Event> nfa = compile(pattern, false);
final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(c, a1, a2, a3, d)));
}
use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink by splunk.
the class GreedyITCase method testGreedyZeroOrMoreBeforeGroupPattern.
@Test
public void testGreedyZeroOrMoreBeforeGroupPattern() throws Exception {
List<StreamRecord<Event>> inputEvents = new ArrayList<>();
Event c = new Event(40, "c", 1.0);
Event a1 = new Event(40, "a", 1.0);
Event a2 = new Event(40, "a", 1.0);
Event a3 = new Event(40, "a", 1.0);
Event d1 = new Event(40, "d", 1.0);
Event e1 = new Event(40, "e", 1.0);
Event d2 = new Event(40, "d", 1.0);
Event e2 = new Event(40, "e", 1.0);
Event f = new Event(44, "f", 3.0);
inputEvents.add(new StreamRecord<>(c, 1));
inputEvents.add(new StreamRecord<>(a1, 2));
inputEvents.add(new StreamRecord<>(a2, 3));
inputEvents.add(new StreamRecord<>(new Event(43, "dummy", 2.0), 4));
inputEvents.add(new StreamRecord<>(a3, 5));
inputEvents.add(new StreamRecord<>(d1, 6));
inputEvents.add(new StreamRecord<>(e1, 7));
inputEvents.add(new StreamRecord<>(d2, 8));
inputEvents.add(new StreamRecord<>(e2, 9));
inputEvents.add(new StreamRecord<>(f, 10));
// c a* (d e){2} f
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("c");
}
}).followedBy("middle").where(new SimpleCondition<Event>() {
private static final long serialVersionUID = 5726188262756267490L;
@Override
public boolean filter(Event value) throws Exception {
return value.getName().equals("a");
}
}).oneOrMore().optional().greedy().followedBy(Pattern.<Event>begin("middle1").where(new SimpleCondition<Event>() {
private static final long serialVersionUID = 5726188262756267490L;
@Override
public boolean filter(Event value) throws Exception {
return value.getName().equals("d");
}
}).followedBy("middle2").where(new SimpleCondition<Event>() {
private static final long serialVersionUID = 5726188262756267490L;
@Override
public boolean filter(Event value) throws Exception {
return value.getName().equals("e");
}
})).times(2).followedBy("end").where(new SimpleCondition<Event>() {
private static final long serialVersionUID = 5726188262756267490L;
@Override
public boolean filter(Event value) throws Exception {
return value.getName().equals("f");
}
});
NFA<Event> nfa = compile(pattern, false);
final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(c, a1, a2, a3, d1, e1, d2, e2, f)));
}
Aggregations