NFACompiler { public static <T> NFA<T> compile( Pattern<T, ?> pattern, TypeSerializer<T> inputTypeSerializer, boolean timeoutHandling) { NFAFactory<T> factory = compileFactory(pattern, inputTypeSerializer, timeoutHandling); return factory.createNFA(); }  static NFA<T> compile(
		Pattern<T, ?> pattern,
		TypeSerializer<T> inputTypeSerializer,
		boolean timeoutHandling); @SuppressWarnings("unchecked") static NFAFactory<T> compileFactory(
		final Pattern<T, ?> pattern,
		final TypeSerializer<T> inputTypeSerializer,
		boolean timeoutHandling);  }
@Test public void testNFACompilerUniquePatternName() { expectedException.expect(MalformedPatternException.class); expectedException.expectMessage("Duplicate pattern name: start. Names must be unique."); Pattern<Event, ?> invalidPattern = Pattern.<Event>begin("start").where(new TestFilter()) .followedBy("middle").where(new TestFilter()) .followedBy("start").where(new TestFilter()); NFACompiler.compile(invalidPattern, Event.createTypeSerializer(), false); }
@Test public void testNFACompilerPatternEndsWithNotFollowedBy() { expectedException.expect(MalformedPatternException.class); expectedException.expectMessage("NotFollowedBy is not supported as a last part of a Pattern!"); Pattern<Event, ?> invalidPattern = Pattern.<Event>begin("start").where(new TestFilter()) .followedBy("middle").where(new TestFilter()) .notFollowedBy("end").where(new TestFilter()); NFACompiler.compile(invalidPattern, Event.createTypeSerializer(), false); }
@Test public void testNFACompilerWithSimplePattern() { Pattern<Event, Event> pattern = Pattern.<Event>begin("start").where(startFilter) .followedBy("middle").subtype(SubEvent.class) .next("end").where(endFilter); NFA<Event> nfa = NFACompiler.compile(pattern, serializer, false); Set<State<Event>> states = nfa.getStates(); assertEquals(4, states.size()); Map<String, State<Event>> stateMap = new HashMap<>(); for (State<Event> state : states) { stateMap.put(state.getName(), state); } assertTrue(stateMap.containsKey("start")); State<Event> startState = stateMap.get("start"); assertTrue(startState.isStart()); final Set<Tuple2<String, StateTransitionAction>> startTransitions = unfoldTransitions(startState); assertEquals(Sets.newHashSet( Tuple2.of("middle", StateTransitionAction.TAKE) ), startTransitions); assertTrue(stateMap.containsKey("middle")); State<Event> middleState = stateMap.get("middle"); final Set<Tuple2<String, StateTransitionAction>> middleTransitions = unfoldTransitions(middleState); assertEquals(Sets.newHashSet( Tuple2.of("middle", StateTransitionAction.IGNORE), Tuple2.of("end", StateTransitionAction.TAKE) ), middleTransitions); assertTrue(stateMap.containsKey("end")); State<Event> endState = stateMap.get("end"); final Set<Tuple2<String, StateTransitionAction>> endTransitions = unfoldTransitions(endState); assertEquals(Sets.newHashSet( Tuple2.of(NFACompiler.ENDING_STATE_NAME, StateTransitionAction.TAKE) ), endTransitions); assertTrue(stateMap.containsKey(NFACompiler.ENDING_STATE_NAME)); State<Event> endingState = stateMap.get(NFACompiler.ENDING_STATE_NAME); assertTrue(endingState.isFinal()); assertEquals(0, endingState.getStateTransitions().size()); }
@Test public void testSkipToNotExistsMatchingPattern() { expectedException.expect(MalformedPatternException.class); expectedException.expectMessage("The pattern name specified in AfterMatchSkipStrategy can not be found in the given Pattern"); Pattern<Event, ?> invalidPattern = Pattern.<Event>begin("start", AfterMatchSkipStrategy.skipToLast("midd")).where(new SimpleCondition<Event>() { @Override public boolean filter(Event value) throws Exception { return value.getName().contains("a"); } }).next("middle").where( new SimpleCondition<Event>() { @Override public boolean filter(Event value) throws Exception { return value.getName().contains("d"); } } ).oneOrMore().optional().next("end").where(new SimpleCondition<Event>() { @Override public boolean filter(Event value) throws Exception { return value.getName().contains("c"); } }); NFACompiler.compile(invalidPattern, Event.createTypeSerializer(), false); }