/** * Check pattern after match skip strategy. */ private void checkPatternSkipStrategy() { if (afterMatchSkipStrategy.getPatternName().isPresent()) { String patternName = afterMatchSkipStrategy.getPatternName().get(); Pattern<T, ?> pattern = currentPattern; while (pattern.getPrevious() != null && !pattern.getName().equals(patternName)) { pattern = pattern.getPrevious(); } // pattern name match check. if (!pattern.getName().equals(patternName)) { throw new MalformedPatternException("The pattern name specified in AfterMatchSkipStrategy " + "can not be found in the given Pattern"); } } }
/** * Check if the given pattern's name is already used or not. If yes, it * throws a {@link MalformedPatternException}. * * @param pattern The pattern to be checked */ private void checkPatternNameUniqueness(final Pattern pattern) { if (pattern instanceof GroupPattern) { Pattern patternToCheck = ((GroupPattern) pattern).getRawPattern(); while (patternToCheck != null) { checkPatternNameUniqueness(patternToCheck); patternToCheck = patternToCheck.getPrevious(); } } else { stateNameHandler.checkNameUniqueness(pattern.getName()); } }
private void addStopStateToLooping(final State<T> loopingState) { if (followingPattern != null && followingPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW) { final IterativeCondition<T> notCondition = getTakeCondition(followingPattern); final State<T> stopState = createStopState(notCondition, followingPattern.getName()); loopingState.addProceed(stopState, notCondition); } }
/** * Retrieves list of conditions resulting in Stop state and names of the corresponding NOT patterns. * * <p>A current not condition can be produced in two cases: * <ol> * <li>the previous pattern is a {@link Quantifier.ConsumingStrategy#NOT_FOLLOW}</li> * <li>exists a backward path of {@link Quantifier.QuantifierProperty#OPTIONAL} patterns to * {@link Quantifier.ConsumingStrategy#NOT_FOLLOW}</li> * </ol> * * <p><b>WARNING:</b> for more info on the second case see: {@link NFAFactoryCompiler#copyWithoutTransitiveNots(State)} * * @return list of not conditions with corresponding names */ private List<Tuple2<IterativeCondition<T>, String>> getCurrentNotCondition() { List<Tuple2<IterativeCondition<T>, String>> notConditions = new ArrayList<>(); Pattern<T, ? extends T> previousPattern = currentPattern; while (previousPattern.getPrevious() != null && ( previousPattern.getPrevious().getQuantifier().hasProperty(Quantifier.QuantifierProperty.OPTIONAL) || previousPattern.getPrevious().getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW)) { previousPattern = previousPattern.getPrevious(); if (previousPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW) { final IterativeCondition<T> notCondition = getTakeCondition(previousPattern); notConditions.add(Tuple2.of(notCondition, previousPattern.getName())); } } return notConditions; }
/** * Create the states for the group pattern as a looping one. * * @param groupPattern the group pattern to create the states for * @param sinkState the state that the group pattern being converted should point to * @return the first state of the states of the group pattern */ private State<T> createLoopingGroupPatternState( final GroupPattern<T, ?> groupPattern, final State<T> sinkState) { final IterativeCondition<T> proceedCondition = getTrueFunction(); Pattern<T, ?> oldCurrentPattern = currentPattern; Pattern<T, ?> oldFollowingPattern = followingPattern; GroupPattern<T, ?> oldGroupPattern = currentGroupPattern; final State<T> dummyState = createState(currentPattern.getName(), State.StateType.Normal); State<T> lastSink = dummyState; currentGroupPattern = groupPattern; currentPattern = groupPattern.getRawPattern(); lastSink = createMiddleStates(lastSink); lastSink = convertPattern(lastSink); lastSink.addProceed(sinkState, proceedCondition); dummyState.addProceed(lastSink, proceedCondition); currentPattern = oldCurrentPattern; followingPattern = oldFollowingPattern; currentGroupPattern = oldGroupPattern; return lastSink; }
/** * These test simply test that the pattern construction completes without failure. */ @Test public void testStrictContiguity() { Pattern<Object, ?> pattern = Pattern.begin("start").next("next").next("end"); Pattern<Object, ?> previous; Pattern<Object, ?> previous2; assertNotNull(previous = pattern.getPrevious()); assertNotNull(previous2 = previous.getPrevious()); assertNull(previous2.getPrevious()); assertEquals(pattern.getName(), "end"); assertEquals(previous.getName(), "next"); assertEquals(previous2.getName(), "start"); }
@Test public void testPatternWithSubtyping() { Pattern<Event, ?> pattern = Pattern.<Event>begin("start").next("subevent").subtype(SubEvent.class).followedBy("end"); Pattern<Event, ?> previous; Pattern<Event, ?> previous2; assertNotNull(previous = pattern.getPrevious()); assertNotNull(previous2 = previous.getPrevious()); assertNull(previous2.getPrevious()); assertNotNull(previous.getCondition()); assertTrue(previous.getCondition() instanceof SubtypeCondition); assertEquals(pattern.getName(), "end"); assertEquals(previous.getName(), "subevent"); assertEquals(previous2.getName(), "start"); }
@Test public void testNonStrictContiguity() { Pattern<Object, ?> pattern = Pattern.begin("start").followedBy("next").followedBy("end"); Pattern<Object, ?> previous; Pattern<Object, ?> previous2; assertNotNull(previous = pattern.getPrevious()); assertNotNull(previous2 = previous.getPrevious()); assertNull(previous2.getPrevious()); assertEquals(ConsumingStrategy.SKIP_TILL_NEXT, pattern.getQuantifier().getConsumingStrategy()); assertEquals(ConsumingStrategy.SKIP_TILL_NEXT, previous.getQuantifier().getConsumingStrategy()); assertEquals(pattern.getName(), "end"); assertEquals(previous.getName(), "next"); assertEquals(previous2.getName(), "start"); }
@Test public void testPatternWithSubtypingAndFilter() { Pattern<Event, Event> pattern = Pattern.<Event>begin("start").next("subevent").subtype(SubEvent.class).where(new SimpleCondition<SubEvent>() { private static final long serialVersionUID = -4118591291880230304L; @Override public boolean filter(SubEvent value) throws Exception { return false; } }).followedBy("end"); Pattern<Event, ?> previous; Pattern<Event, ?> previous2; assertNotNull(previous = pattern.getPrevious()); assertNotNull(previous2 = previous.getPrevious()); assertNull(previous2.getPrevious()); assertEquals(ConsumingStrategy.SKIP_TILL_NEXT, pattern.getQuantifier().getConsumingStrategy()); assertNotNull(previous.getCondition()); assertEquals(pattern.getName(), "end"); assertEquals(previous.getName(), "subevent"); assertEquals(previous2.getName(), "start"); }
@Test public void testStrictContiguityWithCondition() { Pattern<Event, ?> pattern = Pattern.<Event>begin("start").next("next").where(new SimpleCondition<Event>() { private static final long serialVersionUID = -7657256242101104925L; @Override public boolean filter(Event value) throws Exception { return value.getName().equals("foobar"); } }).next("end").where(new SimpleCondition<Event>() { private static final long serialVersionUID = -7597452389191504189L; @Override public boolean filter(Event value) throws Exception { return value.getId() == 42; } }); Pattern<Event, ?> previous; Pattern<Event, ?> previous2; assertNotNull(previous = pattern.getPrevious()); assertNotNull(previous2 = previous.getPrevious()); assertNull(previous2.getPrevious()); assertNotNull(pattern.getCondition()); assertNotNull(previous.getCondition()); assertNotNull(previous2.getCondition()); assertEquals(pattern.getName(), "end"); assertEquals(previous.getName(), "next"); assertEquals(previous2.getName(), "start"); }
/** * Check pattern after match skip strategy. */ private void checkPatternSkipStrategy() { if (afterMatchSkipStrategy.getPatternName().isPresent()) { String patternName = afterMatchSkipStrategy.getPatternName().get(); Pattern<T, ?> pattern = currentPattern; while (pattern.getPrevious() != null && !pattern.getName().equals(patternName)) { pattern = pattern.getPrevious(); } // pattern name match check. if (!pattern.getName().equals(patternName)) { throw new MalformedPatternException("The pattern name specified in AfterMatchSkipStrategy " + "can not be found in the given Pattern"); } } }
/** * Creates the Start {@link State} of the resulting NFA graph. * * @param sinkState the state that Start state should point to (always first state of middle states) * @return created state */ @SuppressWarnings("unchecked") private State<T> createStartState(State<T> sinkState) { stateNameHandler.checkNameUniqueness(currentPattern.getName()); final State<T> beginningState = convertPattern(sinkState); beginningState.makeStart(); return beginningState; }
/** * Check if the given pattern's name is already used or not. If yes, it * throws a {@link MalformedPatternException}. * * @param pattern The pattern to be checked */ private void checkPatternNameUniqueness(final Pattern pattern) { if (pattern instanceof GroupPattern) { Pattern patternToCheck = ((GroupPattern) pattern).getRawPattern(); while (patternToCheck != null) { checkPatternNameUniqueness(patternToCheck); patternToCheck = patternToCheck.getPrevious(); } } else { stateNameHandler.checkNameUniqueness(pattern.getName()); } }
private void addStopStateToLooping(final State<T> loopingState) { if (followingPattern != null && followingPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW) { final IterativeCondition<T> notCondition = getTakeCondition(followingPattern); final State<T> stopState = createStopState(notCondition, followingPattern.getName()); loopingState.addProceed(stopState, notCondition); } }
private void addStopStateToLooping(final State<T> loopingState) { if (followingPattern != null && followingPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW) { final IterativeCondition<T> notCondition = (IterativeCondition<T>) followingPattern.getCondition(); final State<T> stopState = createStopState(notCondition, followingPattern.getName()); loopingState.addProceed(stopState, notCondition); } }
private void addStopStateToLooping(final State<T> loopingState) { if (followingPattern != null && followingPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW) { final IterativeCondition<T> notCondition = getTakeCondition(followingPattern); final State<T> stopState = createStopState(notCondition, followingPattern.getName()); loopingState.addProceed(stopState, notCondition); } }