io.siddhi.query.api.execution.query.input.stream.StateInputStream Java Examples

The following examples show how to use io.siddhi.query.api.execution.query.input.stream.StateInputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QueryRuntimeImpl.java    From siddhi with Apache License 2.0 6 votes vote down vote up
public boolean isFromLocalStream() {
    if (query.getInputStream() instanceof SingleInputStream) {
        return ((SingleInputStream) query.getInputStream()).isInnerStream();
    } else if (query.getInputStream() instanceof JoinInputStream) {
        return ((SingleInputStream) ((JoinInputStream) query.getInputStream()).getLeftInputStream())
                .isInnerStream() || ((SingleInputStream) ((JoinInputStream) query.getInputStream())
                .getRightInputStream()).isInnerStream();
    } else if (query.getInputStream() instanceof StateInputStream) {
        for (String streamId : query.getInputStream().getAllStreamIds()) {
            if (streamId.startsWith("#")) {
                return true;
            }
        }
    }
    return false;
}
 
Example #2
Source File: PartitionRuntimeImpl.java    From siddhi with Apache License 2.0 6 votes vote down vote up
public void addPartitionReceiver(QueryRuntimeImpl queryRuntime, List<VariableExpressionExecutor> executors,
                                 MetaStateEvent metaEvent) {
    Query query = queryRuntime.getQuery();
    List<List<PartitionExecutor>> partitionExecutors = new StreamPartitioner(query.getInputStream(),
            partition, metaEvent, executors, queryRuntime.getSiddhiQueryContext()).getPartitionExecutorLists();
    if (queryRuntime.getStreamRuntime() instanceof SingleStreamRuntime) {
        SingleInputStream singleInputStream = (SingleInputStream) query.getInputStream();
        addPartitionReceiver(singleInputStream.getStreamId(), singleInputStream.isInnerStream(), metaEvent
                .getMetaStreamEvent(0), partitionExecutors.get(0));
    } else if (queryRuntime.getStreamRuntime() instanceof JoinStreamRuntime) {
        SingleInputStream leftSingleInputStream = (SingleInputStream) ((JoinInputStream) query.getInputStream())
                .getLeftInputStream();
        addPartitionReceiver(leftSingleInputStream.getStreamId(), leftSingleInputStream.isInnerStream(),
                metaEvent.getMetaStreamEvent(0), partitionExecutors.get(0));
        SingleInputStream rightSingleInputStream = (SingleInputStream) ((JoinInputStream) query.getInputStream())
                .getRightInputStream();
        addPartitionReceiver(rightSingleInputStream.getStreamId(), rightSingleInputStream.isInnerStream(),
                metaEvent.getMetaStreamEvent(1), partitionExecutors.get(1));
    } else if (queryRuntime.getStreamRuntime() instanceof StateStreamRuntime) {
        StateElement stateElement = ((StateInputStream) query.getInputStream()).getStateElement();
        addPartitionReceiverForStateElement(stateElement, metaEvent, partitionExecutors, 0);
    }
}
 
Example #3
Source File: StreamPartitioner.java    From siddhi with Apache License 2.0 6 votes vote down vote up
private void createExecutors(InputStream inputStream, Partition partition, MetaComplexEvent metaEvent,
                             List<VariableExpressionExecutor> executors, SiddhiQueryContext siddhiQueryContext) {
    if (inputStream instanceof SingleInputStream) {
        if (metaEvent instanceof MetaStateEvent) {
            createSingleInputStreamExecutors((SingleInputStream) inputStream, partition,
                    ((MetaStateEvent) metaEvent).getMetaStreamEvent(0), executors, null,
                    siddhiQueryContext);
        } else {
            createSingleInputStreamExecutors((SingleInputStream) inputStream, partition, (MetaStreamEvent)
                    metaEvent, executors, null, siddhiQueryContext);
        }
    } else if (inputStream instanceof JoinInputStream) {
        createJoinInputStreamExecutors((JoinInputStream) inputStream, partition, (MetaStateEvent) metaEvent,
                executors, siddhiQueryContext);
    } else if (inputStream instanceof StateInputStream) {
        createStateInputStreamExecutors(((StateInputStream) inputStream).getStateElement(), partition,
                (MetaStateEvent) metaEvent, executors, 0, siddhiQueryContext);
    }
}
 
Example #4
Source File: CountPreStateProcessor.java    From siddhi with Apache License 2.0 6 votes vote down vote up
protected void addState(StateEvent stateEvent, StreamPreState state) {
    //        if (stateType == StateInputStream.Type.SEQUENCE) {
    //            newAndEveryStateEventList.clear();
    //            pendingStateEventList.clear();
    //        }
    lock.lock();
    try {
        if (stateType == StateInputStream.Type.SEQUENCE) {
            if (state.getNewAndEveryStateEventList().isEmpty()) {
                state.getNewAndEveryStateEventList().add(stateEvent);
            }
        } else {
            state.getNewAndEveryStateEventList().add(stateEvent);
        }
    } finally {
        lock.unlock();
    }
    if (minCount == 0 && stateEvent.getStreamEvent(stateId) == null) {
        ComplexEventChunk<StateEvent> eventChunk = state.getCurrentStateEventChunk();
        eventChunk.clear();
        eventChunk.add(stateEvent);
        countPostStateProcessor.processMinCountReached(stateEvent, eventChunk);
        eventChunk.clear();
    }
}
 
Example #5
Source File: LogicalPreStateProcessor.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Override
public void resetState() {
    StreamPreState state = stateHolder.getState();
    lock.lock();
    try {
        if (logicalType == LogicalStateElement.Type.OR || state.getPendingStateEventList().size() ==
                partnerStatePreProcessor.getPendingStateEventList().size()) {
            state.getPendingStateEventList().clear();
            partnerStatePreProcessor.getPendingStateEventList().clear();

            if (isStartState && state.getNewAndEveryStateEventList().isEmpty()) {
                if (stateType == StateInputStream.Type.SEQUENCE &&
                        thisStatePostProcessor.nextEveryStatePreProcessor == null &&
                        !((StreamPreStateProcessor) thisStatePostProcessor.nextStatePreProcessor)
                                .getPendingStateEventList().isEmpty()) {
                    return;
                }
                init();
            }
        }
    } finally {
        lock.unlock();
        stateHolder.returnState(state);
    }
}
 
Example #6
Source File: LogicalPreStateProcessor.java    From siddhi with Apache License 2.0 6 votes vote down vote up
protected void addState(StateEvent stateEvent, StreamPreState state) {
    lock.lock();
    try {
        if (isStartState || stateType == StateInputStream.Type.SEQUENCE) {
            if (state.getNewAndEveryStateEventList().isEmpty()) {
                state.getNewAndEveryStateEventList().add(stateEvent);
            }
            if (partnerStatePreProcessor != null && partnerStatePreProcessor.isNewAndEveryStateEventListEmpty()) {
                partnerStatePreProcessor.addEventToNewAndEveryStateEventList(stateEvent);
            }
        } else {
            state.getNewAndEveryStateEventList().add(stateEvent);
            if (partnerStatePreProcessor != null) {
                partnerStatePreProcessor.addEventToNewAndEveryStateEventList(stateEvent);
            }
        }
    } finally {
        lock.unlock();
    }
}
 
Example #7
Source File: StreamPreStateProcessor.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Override
public void resetState() {
    StreamPreState state = stateHolder.getState();
    lock.lock();
    try {
        state.pendingStateEventList.clear();
        if (isStartState && state.newAndEveryStateEventList.isEmpty()) {
            if (stateType == StateInputStream.Type.SEQUENCE && thisStatePostProcessor.nextEveryStatePreProcessor ==
                    null && !((StreamPreStateProcessor) thisStatePostProcessor.nextStatePreProcessor).
                    getPendingStateEventList().isEmpty()) {
                return;
            }
            init();
        }
    } finally {
        lock.unlock();
        stateHolder.returnState(state);
    }
}
 
Example #8
Source File: StreamPreStateProcessor.java    From siddhi with Apache License 2.0 6 votes vote down vote up
public void init() {
    StreamPreState state = stateHolder.getState();
    try {
        if (isStartState && (!state.initialized ||
                this.thisStatePostProcessor.nextEveryStatePreProcessor != null ||
                (stateType == StateInputStream.Type.SEQUENCE && this.thisStatePostProcessor.nextStatePreProcessor
                        instanceof AbsentPreStateProcessor))) {
            // For 'every' sequence, the 'thisStatePostProcessor.nextEveryStatePreProcessor != null'
            // check is not enough
            StateEvent stateEvent = stateEventFactory.newInstance();
            addState(stateEvent);
            state.initialized = true;
        }
    } finally {
        stateHolder.returnState(state);
    }
}
 
Example #9
Source File: AbsentStreamPreStateProcessor.java    From siddhi with Apache License 2.0 6 votes vote down vote up
@Override
public void resetState() {

    LogicalStreamPreState state = (LogicalStreamPreState) stateHolder.getState();
    lock.lock();
    try {
        // Clear the events added by the previous processor
        state.getPendingStateEventList().clear();
        if (isStartState) {
            if (stateType == StateInputStream.Type.SEQUENCE &&
                    thisStatePostProcessor.nextEveryStatePreProcessor == null &&
                    !((StreamPreStateProcessor) thisStatePostProcessor.nextStatePreProcessor)
                            .getPendingStateEventList().isEmpty()) {
                // Sequence without 'every' keyword and the next processor has pending events to be processed
                return;
            }
            // Start state needs a new event
            init();
        }
    } finally {
        lock.unlock();
        stateHolder.returnState(state);
    }
}
 
Example #10
Source File: SiddhiQLBaseVisitorImpl.java    From siddhi with Apache License 2.0 6 votes vote down vote up
/**
     * {@inheritDoc}
     * <p>The default implementation returns the result of calling
     * {@link #visitChildren} on {@code ctx}.</p>
     *
     * @param ctx
     */
    @Override
    public Object visitPattern_stream(@NotNull SiddhiQLParser.Pattern_streamContext ctx) {
//    pattern_stream
//    : every_pattern_source_chain within_time?
//    | every_absent_pattern_source_chain within_time?
//    ;

        StateElement stateElement;
        if (ctx.every_pattern_source_chain() != null) {
            stateElement = ((StateElement) visit(ctx.every_pattern_source_chain()));
        } else {
            stateElement = ((StateElement) visit(ctx.absent_pattern_source_chain()));
        }
        TimeConstant within = null;
        if (ctx.within_time() != null) {
            within = (TimeConstant) visit(ctx.within_time());
        }
        StateInputStream stateInputStream = new StateInputStream(StateInputStream.Type.PATTERN, stateElement, within);
        populateQueryContext(stateInputStream, ctx);
        return stateInputStream;
    }
 
Example #11
Source File: AbsentStreamPreStateProcessor.java    From siddhi with Apache License 2.0 6 votes vote down vote up
protected void addState(StateEvent stateEvent, StreamPreState preState) {
    LogicalStreamPreState state = (LogicalStreamPreState) preState;
    if (!state.active) {
        // 'every' keyword is not used and already a pattern is processed
        return;
    }
    lock.lock();
    try {
        if (stateType == StateInputStream.Type.SEQUENCE) {
            state.getNewAndEveryStateEventList().clear();
            state.getNewAndEveryStateEventList().add(stateEvent);
        } else {
            state.getNewAndEveryStateEventList().add(stateEvent);
        }
        // If this is the first processor, nothing to receive from previous patterns
        if (!isStartState) {
            // Start the scheduler
            state.lastScheduledTime = stateEvent.getTimestamp() + waitingTime;
            scheduler.notifyAt(state.lastScheduledTime);
        }
    } finally {
        lock.unlock();
    }
}
 
Example #12
Source File: InputStreamParser.java    From siddhi with Apache License 2.0 5 votes vote down vote up
/**
 * Parse an InputStream returning corresponding StreamRuntime
 *
 * @param inputStream                input stream to be parsed
 * @param streamDefinitionMap        map containing user given stream definitions
 * @param tableDefinitionMap         table definition map
 * @param windowDefinitionMap        window definition map
 * @param aggregationDefinitionMap   aggregation definition map
 * @param tableMap                   Table Map
 * @param windowMap                  event window map
 * @param aggregationMap             aggregator map
 * @param executors                  List to hold VariableExpressionExecutors to update after query parsing
 * @param outputExpectsExpiredEvents is expired events sent as output
 * @param siddhiQueryContext         Siddhi query context.
 * @return StreamRuntime
 */
public static StreamRuntime parse(InputStream inputStream, Query query,
                                  Map<String, AbstractDefinition> streamDefinitionMap,
                                  Map<String, AbstractDefinition> tableDefinitionMap,
                                  Map<String, AbstractDefinition> windowDefinitionMap,
                                  Map<String, AbstractDefinition> aggregationDefinitionMap,
                                  Map<String, Table> tableMap,
                                  Map<String, Window> windowMap,
                                  Map<String, AggregationRuntime> aggregationMap,
                                  List<VariableExpressionExecutor> executors,
                                  boolean outputExpectsExpiredEvents,
                                  SiddhiQueryContext siddhiQueryContext) {

    if (inputStream instanceof BasicSingleInputStream || inputStream instanceof SingleInputStream) {
        SingleInputStream singleInputStream = (SingleInputStream) inputStream;
        ProcessStreamReceiver processStreamReceiver = new ProcessStreamReceiver(singleInputStream.getStreamId(),
                siddhiQueryContext);
        return SingleInputStreamParser.parseInputStream((SingleInputStream) inputStream,
                executors, streamDefinitionMap,
                tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap,
                new MetaStreamEvent(), processStreamReceiver, true,
                outputExpectsExpiredEvents, false, false, siddhiQueryContext);
    } else if (inputStream instanceof JoinInputStream) {
        return JoinInputStreamParser.parseInputStream(((JoinInputStream) inputStream),
                query, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap,
                aggregationDefinitionMap, tableMap, windowMap, aggregationMap, executors,
                outputExpectsExpiredEvents, siddhiQueryContext);
    } else if (inputStream instanceof StateInputStream) {
        MetaStateEvent metaStateEvent = new MetaStateEvent(inputStream.getAllStreamIds().size());
        return StateInputStreamParser.parseInputStream(((StateInputStream) inputStream),
                metaStateEvent, streamDefinitionMap, tableDefinitionMap,
                windowDefinitionMap, aggregationDefinitionMap, tableMap, executors,
                siddhiQueryContext);
    } else {
        throw new OperationNotSupportedException();
    }
}
 
Example #13
Source File: AbsentStreamPreStateProcessor.java    From siddhi with Apache License 2.0 5 votes vote down vote up
/**
 * Construct an AbsentStreamPreStateProcessor object.
 *
 * @param stateType   PATTERN or SEQUENCE
 * @param waitingTime the waiting time defined by 'for' keyword
 */
public AbsentStreamPreStateProcessor(StateInputStream.Type stateType,
                                     long waitingTime) {
    super(stateType);
    // Not operator always has 'for' time
    this.waitingTime = waitingTime;
}
 
Example #14
Source File: StreamPreStateProcessor.java    From siddhi with Apache License 2.0 5 votes vote down vote up
protected void addState(StateEvent stateEvent, StreamPreState state) {
    lock.lock();
    try {
        if (stateType == StateInputStream.Type.SEQUENCE) {
            if (state.newAndEveryStateEventList.isEmpty()) {
                state.newAndEveryStateEventList.add(stateEvent);
            }
        } else {
            state.newAndEveryStateEventList.add(stateEvent);
        }
    } finally {
        lock.unlock();
    }
}
 
Example #15
Source File: AbsentLogicalPreStateProcessor.java    From siddhi with Apache License 2.0 5 votes vote down vote up
public boolean partnerCanProceed(StateEvent stateEvent) {
    StreamPreState state = stateHolder.getState();
    try {
        boolean process;
        if (stateType == StateInputStream.Type.SEQUENCE &&
                thisStatePostProcessor.nextEveryStatePreProcessor == null &&
                ((LogicalStreamPreState) state).lastArrivalTime > 0) {
            process = false;
        } else {
            if (this.waitingTime == -1) {
                // for time is not defined and event is not received by absent processor
                if (thisStatePostProcessor.nextEveryStatePreProcessor == null) {
                    process = stateEvent.getStreamEvent(this.stateId) == null;
                } else {
                    // Every
                    if (((LogicalStreamPreState) state).lastArrivalTime > 0) {
                        process = false;
                        ((LogicalStreamPreState) state).lastArrivalTime = 0;
                        init();
                    } else {
                        process = true;
                    }
                }
            } else if (stateEvent.getStreamEvent(this.stateId) != null) {
                // for time is defined
                process = true;
            } else {
                process = false;
            }
        }

        return process;
    } finally {
        stateHolder.returnState(state);
    }
}
 
Example #16
Source File: CountPostStateProcessor.java    From siddhi with Apache License 2.0 5 votes vote down vote up
protected void process(StateEvent stateEvent, ComplexEventChunk complexEventChunk) {
    StreamEvent streamEvent = stateEvent.getStreamEvent(stateId);
    int streamEvents = 1;
    while (streamEvent.getNext() != null) {
        streamEvents++;
        streamEvent = streamEvent.getNext();
    }
    ((CountPreStateProcessor) thisStatePreProcessor).successCondition();
    stateEvent.setTimestamp(streamEvent.getTimestamp());

    if (streamEvents >= minCount) {

        if (thisStatePreProcessor.stateType == StateInputStream.Type.SEQUENCE) {
            if (nextStatePreProcessor != null) {
                nextStatePreProcessor.addState(stateEvent);
            }
            if (streamEvents != maxCount) {
                thisStatePreProcessor.addState(stateEvent);
            }
        } else if (streamEvents == minCount) {
            processMinCountReached(stateEvent, complexEventChunk);
        }
        if (streamEvents == maxCount) {
            thisStatePreProcessor.stateChanged();
        }
    }
}
 
Example #17
Source File: CountPostStateProcessor.java    From siddhi with Apache License 2.0 5 votes vote down vote up
public void setNextStatePreProcessor(PreStateProcessor preStateProcessor) {
    this.nextStatePreProcessor = preStateProcessor;
    if (thisStatePreProcessor.isStartState &&
            thisStatePreProcessor.stateType == StateInputStream.Type.SEQUENCE &&
            minCount == 0) {
        preStateProcessor.getThisStatePostProcessor().setCallbackPreStateProcessor(
                (CountPreStateProcessor) thisStatePreProcessor);
    }
}
 
Example #18
Source File: AbsentLogicalPreStateProcessor.java    From siddhi with Apache License 2.0 5 votes vote down vote up
public AbsentLogicalPreStateProcessor(LogicalStateElement.Type type, StateInputStream.Type stateType,
                                      TimeConstant waitingTime) {
    super(type, stateType);
    if (waitingTime != null) {
        this.waitingTime = waitingTime.value();
    }
}
 
Example #19
Source File: EveryInnerStateRuntime.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public EveryInnerStateRuntime(InnerStateRuntime innerStateRuntime, StateInputStream.Type stateType) {
    super(stateType);
    this.innerStateRuntime = innerStateRuntime;
}
 
Example #20
Source File: CountPreStateProcessor.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public CountPreStateProcessor(int minCount, int maxCount, StateInputStream.Type stateType) {
    super(stateType);
    this.minCount = minCount;
    this.maxCount = maxCount;
}
 
Example #21
Source File: AbsentLogicalPreStateProcessor.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Override
public ComplexEventChunk<StateEvent> processAndReturn(ComplexEventChunk complexEventChunk) {

    ComplexEventChunk<StateEvent> returnEventChunk = new ComplexEventChunk<>();
    StreamPreState state = stateHolder.getState();
    try {

        if (!((LogicalStreamPreState) state).active) {
            return returnEventChunk;
        }
        complexEventChunk.reset();
        StreamEvent streamEvent = (StreamEvent) complexEventChunk.next(); //Sure only one will be sent
        this.lock.lock();
        try {
            for (Iterator<StateEvent> iterator = state.getPendingStateEventList().iterator();
                 iterator.hasNext(); ) {
                StateEvent stateEvent = iterator.next();
                if (logicalType == LogicalStateElement.Type.OR &&
                        stateEvent.getStreamEvent(partnerStatePreProcessor.getStateId()) != null) {
                    iterator.remove();
                    continue;
                }
                StreamEvent currentStreamEvent = stateEvent.getStreamEvent(stateId);
                stateEvent.setEvent(stateId, streamEventCloner.copyStreamEvent(streamEvent));
                process(stateEvent);
                if (waitingTime != -1 || (stateType == StateInputStream.Type.SEQUENCE &&
                        logicalType == LogicalStateElement.Type.AND && thisStatePostProcessor
                        .nextEveryStatePreProcessor != null)) {
                    // Reset to the original state after processing
                    stateEvent.setEvent(stateId, currentStreamEvent);
                }
                if (this.thisLastProcessor.isEventReturned()) {
                    this.thisLastProcessor.clearProcessedEvent();
                    // The event has passed the filter condition. So remove from being an absent candidate.
                    iterator.remove();
                    if (stateType == StateInputStream.Type.SEQUENCE) {
                        partnerStatePreProcessor.getPendingStateEventList().remove(stateEvent);
                    }
                }
                if (!state.isStateChanged()) {
                    switch (stateType) {
                        case PATTERN:
                            stateEvent.setEvent(stateId, currentStreamEvent);
                            break;
                        case SEQUENCE:
                            stateEvent.setEvent(stateId, currentStreamEvent);
                            iterator.remove();
                            break;
                    }
                }
            }
        } finally {
            this.lock.unlock();
        }
        return returnEventChunk;
    } finally {
        stateHolder.returnState(state);
    }
}
 
Example #22
Source File: LogicalPreStateProcessor.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public LogicalPreStateProcessor(LogicalStateElement.Type type, StateInputStream.Type stateType) {
    super(stateType);
    this.logicalType = type;
}
 
Example #23
Source File: LogicalInnerStateRuntime.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public LogicalInnerStateRuntime(InnerStateRuntime innerStateRuntime1, InnerStateRuntime innerStateRuntime2,
                                StateInputStream.Type stateType) {
    super(stateType);
    this.innerStateRuntime1 = innerStateRuntime1;
    this.innerStateRuntime2 = innerStateRuntime2;
}
 
Example #24
Source File: StreamInnerStateRuntime.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public StateInputStream.Type getStateType() {
    return stateType;
}
 
Example #25
Source File: StreamInnerStateRuntime.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public StreamInnerStateRuntime(StateInputStream.Type stateType) {
    this.stateType = stateType;
}
 
Example #26
Source File: NextInnerStateRuntime.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public NextInnerStateRuntime(InnerStateRuntime currentInnerStateRuntime, InnerStateRuntime nextInnerStateRuntime,
                             StateInputStream.Type stateType) {
    super(stateType);
    this.currentInnerStateRuntime = currentInnerStateRuntime;
    this.nextInnerStateRuntime = nextInnerStateRuntime;
}
 
Example #27
Source File: StreamPreStateProcessor.java    From siddhi with Apache License 2.0 4 votes vote down vote up
protected boolean removeOnNoStateChange(StateInputStream.Type stateType) {
    return stateType == StateInputStream.Type.SEQUENCE;
}
 
Example #28
Source File: StreamPreStateProcessor.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public StreamPreStateProcessor(StateInputStream.Type stateType) {
    this.stateType = stateType;
}
 
Example #29
Source File: AbsentStreamPreStateProcessor.java    From siddhi with Apache License 2.0 4 votes vote down vote up
protected boolean removeOnNoStateChange(StateInputStream.Type stateType) {
    return false;
}