use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink by apache.
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());
}
use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink by apache.
the class UntilConditionITCase method testIterativeUntilConditionOneOrMore.
@Test
public void testIterativeUntilConditionOneOrMore() 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 middleEvent3 = new Event(40, "d", 1.0);
Event breaking = new Event(44, "a", 5.0);
Event ignored = new Event(45, "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<>(middleEvent3, 5));
inputEvents.add(new StreamRecord<>(breaking, 6));
inputEvents.add(new StreamRecord<>(ignored, 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");
}
}).followedBy("middle").oneOrMore().until(new IterativeCondition<Event>() {
@Override
public boolean filter(Event value, Context<Event> ctx) throws Exception {
double sum = 0;
for (Event middle : ctx.getEventsForPattern("middle")) {
sum += middle.getPrice();
}
return sum == 6.0;
}
});
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, middleEvent3), Lists.newArrayList(startEvent, middleEvent1, middleEvent2), Lists.newArrayList(startEvent, middleEvent1)));
assertEquals(1, nfaState.getPartialMatches().size());
assertEquals("start", nfaState.getPartialMatches().peek().getCurrentStateName());
}
use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink by apache.
the class NFAStateAccessTest method testComplexBranchingAfterZeroOrMore.
@Test
public void testComplexBranchingAfterZeroOrMore() 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 middleEvent3 = new Event(43, "a", 4.0);
Event end1 = new Event(44, "b", 5.0);
Event end2 = new Event(45, "d", 6.0);
Event end3 = new Event(46, "d", 7.0);
Event end4 = new Event(47, "e", 8.0);
inputEvents.add(new StreamRecord<>(startEvent, 1));
inputEvents.add(new StreamRecord<>(middleEvent1, 3));
inputEvents.add(new StreamRecord<>(middleEvent2, 4));
inputEvents.add(new StreamRecord<>(middleEvent3, 5));
inputEvents.add(new StreamRecord<>(end1, 6));
inputEvents.add(new StreamRecord<>(end2, 7));
inputEvents.add(new StreamRecord<>(end3, 8));
inputEvents.add(new StreamRecord<>(end4, 9));
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().allowCombinations().optional().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");
}
}).followedByAny("end2").where(new SimpleCondition<Event>() {
private static final long serialVersionUID = 5726188262756267490L;
@Override
public boolean filter(Event value) throws Exception {
return value.getName().equals("d");
}
}).followedByAny("end3").where(new SimpleCondition<Event>() {
private static final long serialVersionUID = 5726188262756267490L;
@Override
public boolean filter(Event value) throws Exception {
return value.getName().equals("e");
}
});
TestSharedBuffer<Event> sharedBuffer = TestSharedBuffer.createTestBuffer(Event.createTypeSerializer());
NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).withSharedBuffer(sharedBuffer).build();
nfaTestHarness.consumeRecords(inputEvents);
assertEquals(58, sharedBuffer.getStateReads());
assertEquals(33, sharedBuffer.getStateWrites());
assertEquals(91, sharedBuffer.getStateAccesses());
}
use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink by apache.
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 by apache.
the class SameElementITCase method testRepeatingPatternWSameElement.
@Test
public void testRepeatingPatternWSameElement() throws Exception {
List<StreamRecord<Event>> inputEvents = new ArrayList<>();
Event startEvent = new Event(40, "c", 1.0);
Event middle1Event1 = new Event(40, "a", 2.0);
Event middle1Event2 = new Event(40, "a", 3.0);
Event middle1Event3 = new Event(40, "a", 4.0);
Event middle2Event1 = new Event(40, "b", 5.0);
inputEvents.add(new StreamRecord<>(startEvent, 1));
inputEvents.add(new StreamRecord<>(middle1Event1, 3));
inputEvents.add(new StreamRecord<>(middle1Event1, 3));
inputEvents.add(new StreamRecord<>(middle1Event2, 3));
inputEvents.add(new StreamRecord<>(new Event(40, "d", 6.0), 5));
inputEvents.add(new StreamRecord<>(middle2Event1, 6));
inputEvents.add(new StreamRecord<>(middle1Event3, 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");
}
}).followedBy("middle1").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().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");
}
}).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("a");
}
});
NFA<Event> nfa = compile(pattern, false);
final List<List<Event>> resultingPatterns = feedNFA(inputEvents, nfa);
comparePatterns(resultingPatterns, Lists.<List<Event>>newArrayList(Lists.newArrayList(startEvent, middle1Event1), Lists.newArrayList(startEvent, middle1Event1, middle1Event1), Lists.newArrayList(startEvent, middle2Event1, middle1Event3), Lists.newArrayList(startEvent, middle1Event1, middle1Event1, middle1Event2), Lists.newArrayList(startEvent, middle1Event1, middle2Event1, middle1Event3), Lists.newArrayList(startEvent, middle1Event1, middle1Event1, middle1Event2, middle1Event3), Lists.newArrayList(startEvent, middle1Event1, middle1Event1, middle2Event1, middle1Event3), Lists.newArrayList(startEvent, middle1Event1, middle1Event1, middle1Event2, middle2Event1, middle1Event3)));
}
Aggregations