@Test
public void testNFAGivenSequenceCondition() {

    Pattern<String, Integer> pattern = new QueryBuilder<String, Integer>()
            .select("first")
                .where(TestMatcher.isGreaterThan(0))
                .then()
            .select("second")
                .oneOrMore()
                .where((event, sequence, states) ->  {
                    double average = StreamSupport.stream(sequence.spliterator(), false)
                            .mapToInt(Event::value)
                            .summaryStatistics()
                            .getAverage();
                    return average >= event.value();
                })
                .then()
            .select("latest")
                .where((event, sequence, states) ->  {
                    double average = StreamSupport.stream(sequence.spliterator(), false)
                            .mapToInt(Event::value)
                            .summaryStatistics()
                            .getAverage();
                    return average < event.value();
                })
            .build();
    final NFA<String, Integer> nfa = newNFA(pattern);
    Event<String, Integer> e1 = nextEvent("t1", "key", 5);
    Event<String, Integer> e2 = nextEvent("t1", "key", 3);
    Event<String, Integer> e3 = nextEvent("t1", "key", 4);
    Event<String, Integer> e4 = nextEvent("t1", "key", 10);
    List<Sequence<String, Integer>> s = simulate(nfa, e1, e2, e3, e4);
    assertEquals(1, s.size());
    assertNFA(nfa, 5, 2);
    Sequence<String, Integer> expected =  Sequence.<String, Integer>newBuilder()
            .add("latest", e4)
            .add("second", e3)
            .add("second", e2)
            .add("first", e1)
            .build(true);
    assertEquals(expected, s.get(0));
}