use of com.github.fhuss.kafka.streams.cep.pattern.QueryBuilder in project kafkastreams-cep by fhussonnois.
the class NFATest method testComplexPatternWithState.
/**
* PATTERN SEQ(Stock+ a[ ], Stock b)
* WHERE skip_till_next_match(a[ ], b) {
* [symbol]
* and
* a[1].volume > 1000
* and
* a[i].price > avg(a[..i-1].price)
* and
* b.volume < 80%*a[a.LEN].volume }
* WITHIN 1 hour
*/
@Test
public void testComplexPatternWithState() {
StockEvent e1 = new StockEvent("e1", 100, 1010);
StockEvent e2 = new StockEvent("e2", 120, 990);
StockEvent e3 = new StockEvent("e3", 120, 1005);
StockEvent e4 = new StockEvent("e4", 121, 999);
StockEvent e5 = new StockEvent("e5", 120, 999);
StockEvent e6 = new StockEvent("e6", 125, 750);
StockEvent e7 = new StockEvent("e7", 120, 950);
StockEvent e8 = new StockEvent("e8", 120, 700);
Pattern<String, StockEvent> pattern = new QueryBuilder<String, StockEvent>().select().where((k, v, ts, store) -> v.volume > 1000).<Long>fold("avg", (k, v, curr) -> v.price).then().select().zeroOrMore().skipTillNextMatch().where((k, v, ts, state) -> v.price > (long) state.get("avg")).<Long>fold("avg", (k, v, curr) -> (curr + v.price) / 2).<Long>fold("volume", (k, v, curr) -> v.volume).then().select().skipTillNextMatch().where((k, v, ts, state) -> v.volume < 0.8 * (long) state.getOrElse("volume", 0L)).within(1, TimeUnit.HOURS).build();
final NFA<String, StockEvent> nfa = newNFA(pattern, Serdes.String(), new StockEventSerde());
AtomicLong offset = new AtomicLong(0);
List<Event<Object, StockEvent>> collect = Arrays.asList(new StockEvent[] { e1, e2, e3, e4, e5, e6, e7, e8 }).stream().map(e -> new Event<>(null, e, System.currentTimeMillis(), "test", 0, offset.getAndIncrement())).collect(Collectors.toList());
List<Sequence<String, StockEvent>> s = simulate(nfa, collect.toArray(new Event[collect.size()]));
assertEquals(4, s.size());
}
Aggregations