- Add the Codota plugin to your IDE and get smart completions
private void myMethod () {}
NFAFactoryCompiler<?> compiler = new NFAFactoryCompiler<>(checkNotNull(pattern)); compiler.compileFactory(); State<?> startState = compiler.getStates().stream().filter(State::isStart).findFirst().orElseThrow( () -> new IllegalStateException("Compiler produced no start state. It is a bug. File a jira."));
private State<T> createLooping(final State<T> sinkState) { if (currentPattern instanceof GroupPattern) { return createLoopingGroupPatternState((GroupPattern) currentPattern, sinkState); final IterativeCondition<T> ignoreCondition = extendWithUntilCondition( getInnerIgnoreCondition(currentPattern), untilCondition, false); final IterativeCondition<T> takeCondition = extendWithUntilCondition( getTakeCondition(currentPattern), untilCondition, true); IterativeCondition<T> proceedCondition = getTrueFunction(); final State<T> loopingState = createState(currentPattern.getName(), State.StateType.Normal); State<T> sinkStateCopy = copy(sinkState); loopingState.addProceed(sinkStateCopy, new RichAndCondition<>(proceedCondition, untilCondition)); originalStateMap.put(sinkState.getName(), sinkStateCopy); ? new RichAndCondition<>(proceedCondition, new RichNotCondition<>(untilCondition)) : proceedCondition); updateWithGreedyCondition(sinkState, getTakeCondition(currentPattern)); } else { loopingState.addProceed(sinkState, proceedCondition); addStopStateToLooping(loopingState); final State<T> ignoreState = createState(currentPattern.getName(), State.StateType.Normal);
final boolean isOptional) { if (currentPattern instanceof GroupPattern) { return createGroupPatternState((GroupPattern) currentPattern, sinkState, proceedState, isOptional); final State<T> singletonState = createState(currentPattern.getName(), State.StateType.Normal); final State<T> sink = copyWithoutTransitiveNots(sinkState); singletonState.addTake(sink, takeCondition); final IterativeCondition<T> proceedCondition = getTrueFunction(); if (isOptional && !headOfGroup(currentPattern)) { if (currentPattern.getQuantifier().hasProperty(Quantifier.QuantifierProperty.GREEDY)) { final IterativeCondition<T> untilCondition = final State<T> ignoreState; if (isOptional) { ignoreState = createState(currentPattern.getName(), State.StateType.Normal); ignoreState.addTake(sink, takeCondition); ignoreState.addIgnore(ignoreCondition); addStopStates(ignoreState); } else { ignoreState = singletonState;
/** * 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) { final State<T> beginningState = convertPattern(sinkState); beginningState.makeStart(); return beginningState; }
private State<T> convertPattern(final State<T> sinkState) { final State<T> lastSink; final Quantifier quantifier = currentPattern.getQuantifier(); if (quantifier.hasProperty(Quantifier.QuantifierProperty.LOOPING)) { // if loop has started then all notPatterns previous to the optional states are no longer valid setCurrentGroupPatternFirstOfLoop(false); final State<T> sink = copyWithoutTransitiveNots(sinkState); final State<T> looping = createLooping(sink); setCurrentGroupPatternFirstOfLoop(true); lastSink = createTimesState(looping, sinkState, currentPattern.getTimes()); } else if (quantifier.hasProperty(Quantifier.QuantifierProperty.TIMES)) { lastSink = createTimesState(sinkState, sinkState, currentPattern.getTimes()); } else { lastSink = createSingletonState(sinkState); } addStopStates(lastSink); return lastSink; }
/** * Compiles the given pattern into a {@link NFAFactory}. The NFA factory can be used to create * multiple NFAs. * * @param pattern Definition of sequence pattern * @param timeoutHandling True if the NFA shall return timed out event patterns * @param <T> Type of the input events * @return Factory for NFAs corresponding to the given pattern */ @SuppressWarnings("unchecked") public static <T> NFAFactory<T> compileFactory( final Pattern<T, ?> pattern, boolean timeoutHandling) { if (pattern == null) { // return a factory for empty NFAs return new NFAFactoryImpl<>(0, Collections.<State<T>>emptyList(), timeoutHandling); } else { final NFAFactoryCompiler<T> nfaFactoryCompiler = new NFAFactoryCompiler<>(pattern); nfaFactoryCompiler.compileFactory(); return new NFAFactoryImpl<>(nfaFactoryCompiler.getWindowTime(), nfaFactoryCompiler.getStates(), timeoutHandling); } }
private State<T> convertPattern(final State<T> sinkState) { final State<T> lastSink; final Quantifier quantifier = currentPattern.getQuantifier(); if (quantifier.hasProperty(Quantifier.QuantifierProperty.LOOPING)) { // if loop has started then all notPatterns previous to the optional states are no longer valid final State<T> sink = copyWithoutTransitiveNots(sinkState); final State<T> looping = createLooping(sink); if (!quantifier.hasProperty(Quantifier.QuantifierProperty.OPTIONAL)) { lastSink = createInitMandatoryStateOfOneOrMore(looping); } else { lastSink = createInitOptionalStateOfZeroOrMore(looping, sinkState); } } else if (quantifier.hasProperty(Quantifier.QuantifierProperty.TIMES)) { lastSink = createTimesState(sinkState, currentPattern.getTimes()); } else { lastSink = createSingletonState(sinkState); } addStopStates(lastSink); return lastSink; }
/** * Compiles the given pattern into a {@link NFAFactory}. The NFA factory can be used to create * multiple NFAs. */ void compileFactory() { if (currentPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW) { throw new MalformedPatternException("NotFollowedBy is not supported as a last part of a Pattern!"); } checkPatternNameUniqueness(); checkPatternSkipStrategy(); // we're traversing the pattern from the end to the beginning --> the first state is the final state State<T> sinkState = createEndingState(); // add all the normal states sinkState = createMiddleStates(sinkState); // add the beginning state createStartState(sinkState); }
/** * Compiles the given pattern into a {@link NFAFactory}. The NFA factory can be used to create * multiple NFAs. * * @param pattern Definition of sequence pattern * @param timeoutHandling True if the NFA shall return timed out event patterns * @param <T> Type of the input events * @return Factory for NFAs corresponding to the given pattern */ @SuppressWarnings("unchecked") public static <T> NFAFactory<T> compileFactory( final Pattern<T, ?> pattern, boolean timeoutHandling) { if (pattern == null) { // return a factory for empty NFAs return new NFAFactoryImpl<>(0, Collections.<State<T>>emptyList(), timeoutHandling); } else { final NFAFactoryCompiler<T> nfaFactoryCompiler = new NFAFactoryCompiler<>(pattern); nfaFactoryCompiler.compileFactory(); return new NFAFactoryImpl<>(nfaFactoryCompiler.getWindowTime(), nfaFactoryCompiler.getStates(), timeoutHandling); } }
/** * 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; }
/** * 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; }
/** * Compiles the given pattern into a {@link NFAFactory}. The NFA factory can be used to create * multiple NFAs. * * @param pattern Definition of sequence pattern * @param inputTypeSerializer Serializer for the input type * @param timeoutHandling True if the NFA shall return timed out event patterns * @param <T> Type of the input events * @return Factory for NFAs corresponding to the given pattern */ @SuppressWarnings("unchecked") public static <T> NFAFactory<T> compileFactory( final Pattern<T, ?> pattern, final TypeSerializer<T> inputTypeSerializer, boolean timeoutHandling) { if (pattern == null) { // return a factory for empty NFAs return new NFAFactoryImpl<>(inputTypeSerializer, 0, Collections.<State<T>>emptyList(), timeoutHandling); } else { final NFAFactoryCompiler<T> nfaFactoryCompiler = new NFAFactoryCompiler<>(pattern); nfaFactoryCompiler.compileFactory(); return new NFAFactoryImpl<>(inputTypeSerializer, nfaFactoryCompiler.getWindowTime(), nfaFactoryCompiler.getStates(), timeoutHandling); } }
State<T> lastSink = copyWithoutTransitiveNots(sinkState); for (int i = 0; i < times - 1; i++) { lastSink = createSingletonState(lastSink, getInnerIgnoreCondition(currentPattern), false); addStopStateToLooping(lastSink); final IterativeCondition<T> ignoreCondition = getIgnoreCondition(currentPattern); return createSingletonState(lastSink, ignoreCondition, false); final State<T> singletonState = createState(currentPattern.getName(), State.StateType.Normal); singletonState.addTake(lastSink, currentCondition); singletonState.addProceed(sinkState, BooleanConditions.<T>trueFunction()); State<T> ignoreState = createState(currentPattern.getName(), State.StateType.Normal); ignoreState.addTake(lastSink, currentCondition); ignoreState.addIgnore(ignoreCondition); singletonState.addIgnore(ignoreState, ignoreCondition); addStopStates(ignoreState);
private State<T> convertPattern(final State<T> sinkState) { final State<T> lastSink; final Quantifier quantifier = currentPattern.getQuantifier(); if (quantifier.hasProperty(Quantifier.QuantifierProperty.LOOPING)) { // if loop has started then all notPatterns previous to the optional states are no longer valid setCurrentGroupPatternFirstOfLoop(false); final State<T> sink = copyWithoutTransitiveNots(sinkState); final State<T> looping = createLooping(sink); setCurrentGroupPatternFirstOfLoop(true); lastSink = createTimesState(looping, sinkState, currentPattern.getTimes()); } else if (quantifier.hasProperty(Quantifier.QuantifierProperty.TIMES)) { lastSink = createTimesState(sinkState, sinkState, currentPattern.getTimes()); } else { lastSink = createSingletonState(sinkState); } addStopStates(lastSink); return lastSink; }
/** * Creates the given state as a looping one. Looping state is one with TAKE edge to itself and * PROCEED edge to the sinkState. It also consists of a similar state without the PROCEED edge, so that * for each PROCEED transition branches in computation state graph can be created only once. * * @param sinkState the state that the converted state should point to * @return the first state of the created complex state */ @SuppressWarnings("unchecked") private State<T> createLooping(final State<T> sinkState) { final IterativeCondition<T> currentCondition = (IterativeCondition<T>) currentPattern.getCondition(); final IterativeCondition<T> ignoreCondition = getInnerIgnoreCondition(currentPattern); final IterativeCondition<T> trueFunction = BooleanConditions.trueFunction(); final State<T> loopingState = createState(currentPattern.getName(), State.StateType.Normal); loopingState.addProceed(sinkState, trueFunction); loopingState.addTake(currentCondition); addStopStateToLooping(loopingState); if (ignoreCondition != null) { final State<T> ignoreState = createState(currentPattern.getName(), State.StateType.Normal); ignoreState.addTake(loopingState, currentCondition); ignoreState.addIgnore(ignoreCondition); loopingState.addIgnore(ignoreState, ignoreCondition); addStopStateToLooping(ignoreState); } return loopingState; }
/** * Compiles the given pattern into a {@link NFAFactory}. The NFA factory can be used to create * multiple NFAs. */ void compileFactory() { if (currentPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW) { throw new MalformedPatternException("NotFollowedBy is not supported as a last part of a Pattern!"); } // we're traversing the pattern from the end to the beginning --> the first state is the final state State<T> sinkState = createEndingState(); // add all the normal states sinkState = createMiddleStates(sinkState); // add the beginning state createStartState(sinkState); }
final State<T> proceedState, final boolean isOptional) { final IterativeCondition<T> proceedCondition = getTrueFunction(); currentGroupPattern = groupPattern; currentPattern = groupPattern.getRawPattern(); lastSink = createMiddleStates(lastSink); lastSink = convertPattern(lastSink); if (isOptional) {
/** * Compiles the given pattern into a {@link NFAFactory}. The NFA factory can be used to create * multiple NFAs. */ void compileFactory() { if (currentPattern.getQuantifier().getConsumingStrategy() == Quantifier.ConsumingStrategy.NOT_FOLLOW) { throw new MalformedPatternException("NotFollowedBy is not supported as a last part of a Pattern!"); } checkPatternNameUniqueness(); checkPatternSkipStrategy(); // we're traversing the pattern from the end to the beginning --> the first state is the final state State<T> sinkState = createEndingState(); // add all the normal states sinkState = createMiddleStates(sinkState); // add the beginning state createStartState(sinkState); }
final IterativeCondition<T> trueFunction = BooleanConditions.trueFunction(); final State<T> singletonState = createState(currentPattern.getName(), State.StateType.Normal); final State<T> sink = copyWithoutTransitiveNots(sinkState); singletonState.addTake(sink, currentCondition); final State<T> ignoreState; if (isOptional) { ignoreState = createState(currentPattern.getName(), State.StateType.Normal); ignoreState.addTake(sink, currentCondition); ignoreState.addIgnore(ignoreCondition); addStopStates(ignoreState); } else { ignoreState = singletonState;
/** * 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; }