use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink-mirror by flink-ci.
the class GreedyITCase method testGreedyZeroOrMoreInBetween.
@Test
public void testGreedyZeroOrMoreInBetween() 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<>(new Event(1, "dummy", 1111), 2));
inputEvents.add(new StreamRecord<>(a1, 3));
inputEvents.add(new StreamRecord<>(new Event(1, "dummy", 1111), 4));
inputEvents.add(new StreamRecord<>(a2, 5));
inputEvents.add(new StreamRecord<>(new Event(1, "dummy", 1111), 6));
inputEvents.add(new StreamRecord<>(a3, 7));
inputEvents.add(new StreamRecord<>(d, 8));
// 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-mirror by flink-ci.
the class CEPOperatorTest method testCEPOperatorSerializationWRocksDB.
@Test
public void testCEPOperatorSerializationWRocksDB() throws Exception {
String rocksDbPath = tempFolder.newFolder().getAbsolutePath();
RocksDBStateBackend rocksDBStateBackend = new RocksDBStateBackend(new MemoryStateBackend());
rocksDBStateBackend.setDbStoragePath(rocksDbPath);
final Event startEvent1 = new Event(40, "start", 1.0);
final Event startEvent2 = new Event(40, "start", 2.0);
final SubEvent middleEvent1 = new SubEvent(40, "foo1", 1.0, 10);
final SubEvent middleEvent2 = new SubEvent(40, "foo2", 2.0, 10);
final SubEvent middleEvent3 = new SubEvent(40, "foo3", 3.0, 10);
final SubEvent middleEvent4 = new SubEvent(40, "foo4", 1.0, 10);
final Event nextOne = new Event(40, "next-one", 1.0);
final Event endEvent = new Event(40, "end", 1.0);
final 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");
}
}).followedBy("middle").subtype(SubEvent.class).where(new IterativeCondition<SubEvent>() {
private static final long serialVersionUID = 6215754202506583964L;
@Override
public boolean filter(SubEvent value, Context<SubEvent> ctx) throws Exception {
if (!value.getName().startsWith("foo")) {
return false;
}
double sum = 0.0;
for (Event event : ctx.getEventsForPattern("middle")) {
sum += event.getPrice();
}
sum += value.getPrice();
return Double.compare(sum, 5.0) < 0;
}
}).oneOrMore().allowCombinations().followedBy("end").where(new SimpleCondition<Event>() {
private static final long serialVersionUID = 7056763917392056548L;
@Override
public boolean filter(Event value) throws Exception {
return value.getName().equals("end");
}
});
CepOperator<Event, Integer, Map<String, List<Event>>> operator = CepOperatorTestUtilities.getKeyedCepOperator(false, new NFACompiler.NFAFactory<Event>() {
private static final long serialVersionUID = 477082663248051994L;
@Override
public NFA<Event> createNFA() {
return NFACompiler.compileFactory(pattern, false).createNFA();
}
});
OneInputStreamOperatorTestHarness<Event, Map<String, List<Event>>> harness = CepOperatorTestUtilities.getCepTestHarness(operator);
try {
harness.setStateBackend(rocksDBStateBackend);
harness.open();
harness.processWatermark(0L);
harness.processElement(new StreamRecord<>(startEvent1, 1));
harness.processElement(new StreamRecord<Event>(middleEvent1, 2));
harness.processWatermark(2L);
harness.processElement(new StreamRecord<Event>(middleEvent3, 5));
harness.processElement(new StreamRecord<Event>(middleEvent2, 3));
harness.processElement(new StreamRecord<>(startEvent2, 4));
harness.processWatermark(5L);
harness.processElement(new StreamRecord<>(nextOne, 7));
harness.processElement(new StreamRecord<>(endEvent, 8));
harness.processElement(new StreamRecord<Event>(middleEvent4, 6));
harness.processWatermark(100L);
List<List<Event>> resultingPatterns = new ArrayList<>();
while (!harness.getOutput().isEmpty()) {
Object o = harness.getOutput().poll();
if (!(o instanceof Watermark)) {
StreamRecord<Map<String, List<Event>>> el = (StreamRecord<Map<String, List<Event>>>) o;
List<Event> res = new ArrayList<>();
for (List<Event> le : el.getValue().values()) {
res.addAll(le);
}
resultingPatterns.add(res);
}
}
compareMaps(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(startEvent1, endEvent, middleEvent1, middleEvent2, middleEvent4), Lists.newArrayList(startEvent1, endEvent, middleEvent2, middleEvent1), Lists.newArrayList(startEvent1, endEvent, middleEvent3, middleEvent1), Lists.newArrayList(startEvent2, endEvent, middleEvent3, middleEvent4), Lists.newArrayList(startEvent1, endEvent, middleEvent4, middleEvent1), Lists.newArrayList(startEvent1, endEvent, middleEvent1), Lists.newArrayList(startEvent2, endEvent, middleEvent3)));
} finally {
harness.close();
}
}
use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink-mirror by flink-ci.
the class SameElementITCase method testZeroOrMoreSameElement.
@Test
public void testZeroOrMoreSameElement() throws Exception {
List<StreamRecord<Event>> inputEvents = new ArrayList<>();
Event startEvent = new Event(40, "c", 1.0);
Event middleEvent1 = new Event(41, "a", 2.0);
Event middleEvent1a = new Event(41, "a", 2.0);
Event middleEvent2 = new Event(42, "a", 3.0);
Event middleEvent3 = new Event(43, "a", 4.0);
Event middleEvent3a = new Event(43, "a", 4.0);
Event end1 = new Event(44, "b", 5.0);
inputEvents.add(new StreamRecord<>(startEvent, 1));
inputEvents.add(new StreamRecord<>(middleEvent1, 3));
inputEvents.add(new StreamRecord<>(middleEvent1a, 3));
inputEvents.add(new StreamRecord<>(middleEvent2, 4));
inputEvents.add(new StreamRecord<>(new Event(50, "d", 6.0), 5));
inputEvents.add(new StreamRecord<>(middleEvent3, 6));
inputEvents.add(new StreamRecord<>(middleEvent3a, 6));
inputEvents.add(new StreamRecord<>(end1, 7));
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("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().allowCombinations().followedByAny("end1").where(new SimpleCondition<Event>() {
private static final long serialVersionUID = 5726188262756267490L;
@Override
public boolean filter(Event value) throws Exception {
return value.getName().equals("b");
}
});
NFA<Event> nfa = compile(pattern, false);
final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent2, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent2, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent2, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent2, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent2, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent2, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent2, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent2, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent2, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent2, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent2, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent1, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent2, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent1, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent2, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent1a, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent2, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent2, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent3, middleEvent3a, end1), Lists.newArrayList(startEvent, middleEvent1, end1), Lists.newArrayList(startEvent, middleEvent1a, end1), Lists.newArrayList(startEvent, middleEvent2, end1), Lists.newArrayList(startEvent, middleEvent3, end1), Lists.newArrayList(startEvent, middleEvent3a, end1), Lists.newArrayList(startEvent, end1)));
}
use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink-mirror by flink-ci.
the class SameElementITCase method testIterativeConditionWSameElement.
@Test
public void testIterativeConditionWSameElement() throws Exception {
List<StreamRecord<Event>> inputEvents = new ArrayList<>();
Event startEvent = new Event(40, "c", 1.0);
Event middleEvent1 = new Event(41, "a", 2.0);
Event middleEvent1a = new Event(41, "a", 2.0);
Event middleEvent1b = new Event(41, "a", 2.0);
final Event end = new Event(44, "b", 5.0);
inputEvents.add(new StreamRecord<>(startEvent, 1));
inputEvents.add(new StreamRecord<>(middleEvent1, 3));
inputEvents.add(new StreamRecord<>(middleEvent1a, 3));
inputEvents.add(new StreamRecord<>(middleEvent1b, 3));
inputEvents.add(new StreamRecord<>(end, 7));
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("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().allowCombinations().followedBy("end").where(new IterativeCondition<Event>() {
private static final long serialVersionUID = -5566639743229703237L;
@Override
public boolean filter(Event value, Context<Event> ctx) throws Exception {
double sum = 0.0;
for (Event event : ctx.getEventsForPattern("middle")) {
sum += event.getPrice();
}
return Double.compare(sum, 4.0) == 0;
}
});
NFA<Event> nfa = compile(pattern, false);
final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, end), Lists.newArrayList(startEvent, middleEvent1, middleEvent1a, middleEvent1b), Lists.newArrayList(startEvent, middleEvent1a, middleEvent1b, end)));
}
use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink-mirror by flink-ci.
the class UntilConditionITCase method testUntilConditionFollowedByOneOrMoreConsecutive.
@Test
public void testUntilConditionFollowedByOneOrMoreConsecutive() throws Exception {
List<StreamRecord<Event>> inputEvents = new ArrayList<>();
Event startEvent = new Event(40, "c", 1.0);
Event middleEvent1 = new Event(41, "a", 2.0);
Event middleEvent2 = new Event(42, "a", 3.0);
Event breaking = new Event(45, "a", 5.0);
Event ignored = new Event(46, "a", 6.0);
inputEvents.add(new StreamRecord<>(startEvent, 1));
inputEvents.add(new StreamRecord<>(middleEvent1, 3));
inputEvents.add(new StreamRecord<>(middleEvent2, 4));
inputEvents.add(new StreamRecord<>(breaking, 7));
inputEvents.add(new StreamRecord<>(ignored, 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("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().consecutive().until(UNTIL_CONDITION).followedBy("end").where(UNTIL_CONDITION);
NFA<Event> nfa = compile(pattern, false);
NFAState nfaState = nfa.createInitialNFAState();
NFATestHarness nfaTestHarness = NFATestHarness.forNFA(nfa).withNFAState(nfaState).build();
final List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(inputEvents);
comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(startEvent, middleEvent1, middleEvent2, breaking), Lists.newArrayList(startEvent, middleEvent1, breaking)));
assertEquals(1, nfaState.getPartialMatches().size());
assertEquals("start", nfaState.getPartialMatches().peek().getCurrentStateName());
}
Aggregations