@Test
public void testNFAGivenStatefulCondition() {

    Pattern<String, Integer> pattern = new QueryBuilder<String, Integer>()
            .select("first")
                .where(TestMatcher.isGreaterThan(0))
                .fold("sum", (key, value, state) -> value)
                .fold("count", (key, value, state) -> 1)
            .then()
                .select("second")
                .oneOrMore()
                .where((event, states) -> {
                    double average =(int)states.get("sum") / (int)states.get("count");
                    return average >= event.value();
                })
                .<Integer>fold("sum", (key, value, state) -> state + value)
                .<Integer>fold("count", (key, value, state) -> state + 1)
            .then()
                .select("latest")
                .where((event, states) -> {
                    double average = (int)states.get("sum") / (int)states.get("count");
                    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));
}