use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink-mirror by flink-ci.
the class CEPITCase method testSimpleOrFilterPatternCEP.
/**
* Checks that a certain event sequence is recognized with an OR filter.
*
* @throws Exception
*/
@Test
public void testSimpleOrFilterPatternCEP() throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(envConfiguration);
DataStream<Event> input = env.fromElements(new Event(1, "start", 1.0), new Event(2, "middle", 2.0), new Event(3, "end", 3.0), new Event(4, "start", 4.0), new Event(5, "middle", 5.0), new Event(6, "end", 6.0));
Pattern<Event, ?> pattern = Pattern.<Event>begin("start").where(new SimpleCondition<Event>() {
@Override
public boolean filter(Event value) throws Exception {
return value.getName().equals("start");
}
}).followedByAny("middle").where(new SimpleCondition<Event>() {
@Override
public boolean filter(Event value) throws Exception {
return value.getPrice() == 2.0;
}
}).or(new SimpleCondition<Event>() {
@Override
public boolean filter(Event value) throws Exception {
return value.getPrice() == 5.0;
}
}).followedByAny("end").where(new SimpleCondition<Event>() {
@Override
public boolean filter(Event value) throws Exception {
return value.getName().equals("end");
}
});
DataStream<String> result = CEP.pattern(input, pattern).inProcessingTime().select(new PatternSelectFunction<Event, String>() {
@Override
public String select(Map<String, List<Event>> pattern) {
StringBuilder builder = new StringBuilder();
builder.append(pattern.get("start").get(0).getId()).append(",").append(pattern.get("middle").get(0).getId()).append(",").append(pattern.get("end").get(0).getId());
return builder.toString();
}
});
List<String> resultList = new ArrayList<>();
DataStreamUtils.collect(result).forEachRemaining(resultList::add);
List<String> expected = Arrays.asList("1,5,6", "1,2,3", "4,5,6", "1,2,6");
expected.sort(String::compareTo);
resultList.sort(String::compareTo);
assertEquals(expected, resultList);
}
use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink-mirror by flink-ci.
the class AfterMatchSkipITCase method testSkipToLastWithOneOrMoreAtBeginning.
/**
* Example from docs.
*/
@Test
public void testSkipToLastWithOneOrMoreAtBeginning() throws Exception {
List<StreamRecord<Event>> streamEvents = new ArrayList<>();
Event a1 = new Event(1, "a1", 0.0);
Event a2 = new Event(2, "a2", 0.0);
Event a3 = new Event(3, "a3", 0.0);
Event b1 = new Event(4, "b1", 0.0);
streamEvents.add(new StreamRecord<>(a1));
streamEvents.add(new StreamRecord<>(a2));
streamEvents.add(new StreamRecord<>(a3));
streamEvents.add(new StreamRecord<>(b1));
Pattern<Event, ?> pattern = Pattern.<Event>begin("a", AfterMatchSkipStrategy.skipToLast("a")).where(new SimpleCondition<Event>() {
@Override
public boolean filter(Event value) throws Exception {
return value.getName().contains("a");
}
}).oneOrMore().consecutive().greedy().next("b").where(new SimpleCondition<Event>() {
@Override
public boolean filter(Event value) throws Exception {
return value.getName().contains("b");
}
});
NFATestHarness nfaTestHarness = NFATestHarness.forPattern(pattern).build();
List<List<Event>> resultingPatterns = nfaTestHarness.feedRecords(streamEvents);
comparePatterns(resultingPatterns, Lists.newArrayList(Lists.newArrayList(a1, a2, a3, b1), Lists.newArrayList(a3, b1)));
}
use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink-mirror by flink-ci.
the class GreedyITCase method testGreedyZeroOrMoreWithDummyEventsBeforeQuantifier.
@Test
public void testGreedyZeroOrMoreWithDummyEventsBeforeQuantifier() throws Exception {
List<StreamRecord<Event>> inputEvents = new ArrayList<>();
Event c = new Event(40, "c", 1.0);
Event d = new Event(44, "d", 3.0);
inputEvents.add(new StreamRecord<>(c, 1));
inputEvents.add(new StreamRecord<>(new Event(43, "dummy", 2.0), 2));
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, d)));
}
use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink-mirror by flink-ci.
the class GreedyITCase method testGreedyZeroOrMoreWithDummyEventsAfterQuantifier.
@Test
public void testGreedyZeroOrMoreWithDummyEventsAfterQuantifier() 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 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<>(new Event(43, "dummy", 2.0), 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, d)));
}
use of org.apache.flink.cep.pattern.conditions.SimpleCondition in project flink-mirror by flink-ci.
the class GreedyITCase method testGreedyUntilOneOrMoreWithDummyEventsBeforeQuantifier.
@Test
public void testGreedyUntilOneOrMoreWithDummyEventsBeforeQuantifier() 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", 3.0);
Event a3 = new Event(43, "a", 3.0);
Event d = new Event(45, "d", 3.0);
inputEvents.add(new StreamRecord<>(c, 1));
inputEvents.add(new StreamRecord<>(new Event(44, "a", 4.0), 2));
inputEvents.add(new StreamRecord<>(a1, 3));
inputEvents.add(new StreamRecord<>(a2, 4));
inputEvents.add(new StreamRecord<>(a3, 5));
inputEvents.add(new StreamRecord<>(d, 6));
// 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().greedy().until(new SimpleCondition<Event>() {
private static final long serialVersionUID = 5726188262756267490L;
@Override
public boolean filter(Event value) throws Exception {
return value.getPrice() > 3.0;
}
}).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());
}
Aggregations