Search in sources :

Example 11 with StateEvent

use of io.siddhi.core.event.state.StateEvent in project siddhi by wso2.

the class SelectOnDemandQueryRuntime method generateResetComplexEventChunk.

private ComplexEventChunk<ComplexEvent> generateResetComplexEventChunk(MetaStreamEvent metaStreamEvent) {
    StreamEvent streamEvent = new StreamEvent(metaStreamEvent.getOutputData().size(), metaStreamEvent.getOnAfterWindowData().size(), metaStreamEvent.getOutputData().size());
    streamEvent.setType(ComplexEvent.Type.RESET);
    StateEvent stateEvent = stateEventFactory.newInstance();
    if (eventType == MetaStreamEvent.EventType.AGGREGATE) {
        stateEvent.addEvent(1, streamEvent);
    } else {
        stateEvent.addEvent(0, streamEvent);
    }
    stateEvent.setType(ComplexEvent.Type.RESET);
    ComplexEventChunk<ComplexEvent> complexEventChunk = new ComplexEventChunk<>();
    complexEventChunk.add(stateEvent);
    return complexEventChunk;
}
Also used : ComplexEvent(io.siddhi.core.event.ComplexEvent) ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) StateEvent(io.siddhi.core.event.state.StateEvent)

Example 12 with StateEvent

use of io.siddhi.core.event.state.StateEvent in project siddhi by wso2.

the class UpdateOrInsertOnDemandQueryRuntime method generateResetComplexEventChunk.

private ComplexEventChunk<ComplexEvent> generateResetComplexEventChunk(MetaStreamEvent metaStreamEvent) {
    StreamEvent streamEvent = new StreamEvent(metaStreamEvent.getBeforeWindowData().size(), metaStreamEvent.getOnAfterWindowData().size(), metaStreamEvent.getOutputData().size());
    streamEvent.setType(ComplexEvent.Type.RESET);
    StateEvent stateEvent = stateEventFactory.newInstance();
    stateEvent.addEvent(0, streamEvent);
    stateEvent.setType(ComplexEvent.Type.RESET);
    ComplexEventChunk<ComplexEvent> complexEventChunk = new ComplexEventChunk<>();
    complexEventChunk.add(stateEvent);
    return complexEventChunk;
}
Also used : ComplexEvent(io.siddhi.core.event.ComplexEvent) ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(io.siddhi.core.event.stream.StreamEvent) StateEvent(io.siddhi.core.event.state.StateEvent)

Example 13 with StateEvent

use of io.siddhi.core.event.state.StateEvent in project siddhi by wso2.

the class JoinProcessor method execute.

private void execute(ComplexEventChunk complexEventChunk, List<ComplexEventChunk> returnEventChunkList) {
    StateEvent joinStateEvent = new StateEvent(2, 0);
    StreamEvent nextEvent = (StreamEvent) complexEventChunk.getFirst();
    complexEventChunk.clear();
    while (nextEvent != null) {
        StreamEvent streamEvent = nextEvent;
        nextEvent = streamEvent.getNext();
        streamEvent.setNext(null);
        ComplexEvent.Type eventType = streamEvent.getType();
        if (eventType == ComplexEvent.Type.TIMER) {
            continue;
        } else if (eventType == ComplexEvent.Type.RESET) {
            if (!leftJoinProcessor) {
                StateEvent outputStateEvent = joinEventBuilder(null, streamEvent, eventType);
                returnEventChunkList.add(new SelectorTypeComplexEventChunk(new ComplexEventChunk<>(outputStateEvent, outputStateEvent), false));
            } else {
                StateEvent outputStateEvent = joinEventBuilder(streamEvent, null, eventType);
                returnEventChunkList.add(new SelectorTypeComplexEventChunk(new ComplexEventChunk<>(outputStateEvent, outputStateEvent), false));
            }
        } else {
            joinStateEvent.setEvent(matchingStreamIndex, streamEvent);
            StreamEvent foundStreamEvent;
            if (this.isOptimisedQuery) {
                try {
                    foundStreamEvent = query(joinStateEvent);
                } catch (SiddhiAppRuntimeException e) {
                    log.warn("Performing select clause in databases failed due to '" + e.getMessage() + " in query '" + queryName + "' within Siddhi app '" + siddhiAppName + "' hence reverting back to querying only with where clause.", e);
                    this.isOptimisedQuery = false;
                    foundStreamEvent = findableProcessor.find(joinStateEvent, compiledCondition);
                }
            } else {
                foundStreamEvent = findableProcessor.find(joinStateEvent, compiledCondition);
            }
            joinStateEvent.setEvent(matchingStreamIndex, null);
            if (foundStreamEvent == null) {
                if (outerJoinProcessor && !leftJoinProcessor) {
                    StateEvent outputStateEvent = joinEventBuilder(null, streamEvent, eventType);
                    returnEventChunkList.add(new SelectorTypeComplexEventChunk(new ComplexEventChunk<>(outputStateEvent, outputStateEvent), false));
                } else if (outerJoinProcessor && leftJoinProcessor) {
                    StateEvent outputStateEvent = joinEventBuilder(streamEvent, null, eventType);
                    returnEventChunkList.add(new SelectorTypeComplexEventChunk(new ComplexEventChunk<>(outputStateEvent, outputStateEvent), false));
                }
            } else if (!isOptimisedQuery) {
                ComplexEventChunk<ComplexEvent> returnEventChunk = new ComplexEventChunk<>();
                while (foundStreamEvent != null) {
                    StreamEvent nextFoundStreamEvent = foundStreamEvent.getNext();
                    foundStreamEvent.setNext(null);
                    if (!leftJoinProcessor) {
                        returnEventChunk.add(joinEventBuilder(foundStreamEvent, streamEvent, eventType));
                    } else {
                        returnEventChunk.add(joinEventBuilder(streamEvent, foundStreamEvent, eventType));
                    }
                    foundStreamEvent = nextFoundStreamEvent;
                }
                returnEventChunkList.add(new SelectorTypeComplexEventChunk(returnEventChunk, false));
            } else {
                ComplexEventChunk<ComplexEvent> returnEventChunk = new ComplexEventChunk<>();
                while (foundStreamEvent != null) {
                    StreamEvent nextFoundStreamEvent = foundStreamEvent.getNext();
                    StateEvent returnEvent = stateEventFactory.newInstance();
                    returnEvent.setType(eventType);
                    returnEvent.setTimestamp(foundStreamEvent.getTimestamp());
                    Object[] outputData = foundStreamEvent.getOutputData();
                    for (int i = 0; i < outputData.length; i++) {
                        Object data = outputData[i];
                        returnEvent.setOutputData(data, i);
                    }
                    returnEventChunk.add(returnEvent);
                    foundStreamEvent = nextFoundStreamEvent;
                }
                returnEventChunkList.add(new SelectorTypeComplexEventChunk(returnEventChunk, true));
            }
        }
    }
}
Also used : ComplexEvent(io.siddhi.core.event.ComplexEvent) ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) SelectorTypeComplexEventChunk(io.siddhi.core.query.selector.SelectorTypeComplexEventChunk) StreamEvent(io.siddhi.core.event.stream.StreamEvent) SiddhiAppRuntimeException(io.siddhi.core.exception.SiddhiAppRuntimeException) StateEvent(io.siddhi.core.event.state.StateEvent) SelectorTypeComplexEventChunk(io.siddhi.core.query.selector.SelectorTypeComplexEventChunk)

Example 14 with StateEvent

use of io.siddhi.core.event.state.StateEvent in project siddhi by wso2.

the class AbsentLogicalPreStateProcessor method process.

@Override
public void process(ComplexEventChunk complexEventChunk) {
    StreamPreState state = stateHolder.getState();
    try {
        if (!((LogicalStreamPreState) state).active) {
            return;
        }
        boolean notProcessed = true;
        ComplexEventChunk<StateEvent> retEventChunk = new ComplexEventChunk<>();
        this.lock.lock();
        try {
            long currentTime = complexEventChunk.getFirst().getTimestamp();
            if (currentTime >= ((LogicalStreamPreState) state).lastArrivalTime + waitingTime) {
                Iterator<StateEvent> iterator;
                if (isStartState && stateType == StateInputStream.Type.SEQUENCE && state.getNewAndEveryStateEventList().isEmpty() && state.getPendingStateEventList().isEmpty()) {
                    StateEvent stateEvent = stateEventFactory.newInstance();
                    addState(stateEvent);
                } else if (stateType == StateInputStream.Type.SEQUENCE && !state.getNewAndEveryStateEventList().isEmpty()) {
                    this.resetState();
                }
                this.updateState();
                StateEvent expiredStateEvent = null;
                iterator = state.getPendingStateEventList().iterator();
                while (iterator.hasNext()) {
                    StateEvent stateEvent = iterator.next();
                    // Remove expired events based on within
                    if (isExpired(stateEvent, currentTime)) {
                        expiredStateEvent = stateEvent;
                        iterator.remove();
                        continue;
                    }
                    // Collect the events that came before the waiting time
                    if (waitingTimePassed(currentTime, stateEvent)) {
                        iterator.remove();
                        if (logicalType == LogicalStateElement.Type.OR && stateEvent.getStreamEvent(partnerStatePreProcessor.getStateId()) == null) {
                            // OR Partner not received
                            stateEvent.addEvent(stateId, streamEventFactory.newInstance());
                            retEventChunk.add(stateEvent);
                        } else if (logicalType == LogicalStateElement.Type.AND && stateEvent.getStreamEvent(partnerStatePreProcessor.getStateId()) != null) {
                            // AND partner received but didn't send out
                            retEventChunk.add(stateEvent);
                        } else if (logicalType == LogicalStateElement.Type.AND && stateEvent.getStreamEvent(partnerStatePreProcessor.getStateId()) == null) {
                            // AND partner didn't receive
                            // Let the partner to process or not
                            stateEvent.addEvent(stateId, streamEventFactory.newInstance());
                        }
                    }
                }
                if (expiredStateEvent != null && withinEveryPreStateProcessor != null) {
                    withinEveryPreStateProcessor.addEveryState(expiredStateEvent);
                    withinEveryPreStateProcessor.updateState();
                }
                retEventChunk.reset();
                notProcessed = retEventChunk.getFirst() == null;
                while (retEventChunk.hasNext()) {
                    StateEvent stateEvent = retEventChunk.next();
                    retEventChunk.remove();
                    stateEvent.setTimestamp(currentTime);
                    sendEvent(stateEvent, state);
                }
                ((LogicalStreamPreState) state).lastArrivalTime = 0;
            }
        } finally {
            this.lock.unlock();
        }
        if (thisStatePostProcessor.nextEveryStatePreProcessor != null || (notProcessed && isStartState)) {
            // If every or (notProcessed and startState), schedule again
            long nextBreak;
            if (((LogicalStreamPreState) state).lastArrivalTime == 0) {
                nextBreak = siddhiQueryContext.getSiddhiAppContext().getTimestampGenerator().currentTime() + waitingTime;
            } else {
                nextBreak = ((LogicalStreamPreState) state).lastArrivalTime + waitingTime;
            }
            this.scheduler.notifyAt(nextBreak);
        }
    } finally {
        stateHolder.returnState(state);
    }
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) StateEvent(io.siddhi.core.event.state.StateEvent)

Example 15 with StateEvent

use of io.siddhi.core.event.state.StateEvent in project siddhi by wso2.

the class CountPreStateProcessor method processAndReturn.

@Override
public ComplexEventChunk<StateEvent> processAndReturn(ComplexEventChunk complexEventChunk) {
    ComplexEventChunk<StateEvent> returnEventChunk = new ComplexEventChunk<StateEvent>();
    complexEventChunk.reset();
    // Sure only one will be sent
    StreamEvent streamEvent = (StreamEvent) complexEventChunk.next();
    CountStreamPreState state = (CountStreamPreState) stateHolder.getState();
    lock.lock();
    try {
        for (Iterator<StateEvent> iterator = state.getPendingStateEventList().iterator(); iterator.hasNext(); ) {
            StateEvent stateEvent = iterator.next();
            if (removeIfNextStateProcessed(stateEvent, iterator, stateId + 1)) {
                continue;
            }
            if (removeIfNextStateProcessed(stateEvent, iterator, stateId + 2)) {
                continue;
            }
            stateEvent.addEvent(stateId, streamEventCloner.copyStreamEvent(streamEvent));
            state.successCondition = false;
            process(stateEvent);
            if (this.thisLastProcessor.isEventReturned()) {
                this.thisLastProcessor.clearProcessedEvent();
                returnEventChunk.add(stateEvent);
            }
            if (state.isStateChanged()) {
                iterator.remove();
            }
            if (!state.successCondition) {
                switch(stateType) {
                    case PATTERN:
                        stateEvent.removeLastEvent(stateId);
                        break;
                    case SEQUENCE:
                        stateEvent.removeLastEvent(stateId);
                        iterator.remove();
                        break;
                }
            }
        }
    } finally {
        lock.unlock();
        stateHolder.returnState(state);
    }
    return returnEventChunk;
}
Also used : ComplexEventChunk(io.siddhi.core.event.ComplexEventChunk) StreamEvent(io.siddhi.core.event.stream.StreamEvent) StateEvent(io.siddhi.core.event.state.StateEvent)

Aggregations

StateEvent (io.siddhi.core.event.state.StateEvent)70 StreamEvent (io.siddhi.core.event.stream.StreamEvent)48 ComplexEventChunk (io.siddhi.core.event.ComplexEventChunk)36 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)26 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)19 ComplexEvent (io.siddhi.core.event.ComplexEvent)13 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)11 Map (java.util.Map)10 ArrayList (java.util.ArrayList)7 LinkedList (java.util.LinkedList)6 ConnectionUnavailableException (io.siddhi.core.exception.ConnectionUnavailableException)5 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)5 HashMap (java.util.HashMap)5 Attribute (io.siddhi.query.api.definition.Attribute)4 Event (io.siddhi.core.event.Event)3 SnapshotableStreamEventQueue (io.siddhi.core.event.stream.holder.SnapshotableStreamEventQueue)3 DatabaseRuntimeException (io.siddhi.core.exception.DatabaseRuntimeException)3 OnDemandQueryRuntimeException (io.siddhi.core.exception.OnDemandQueryRuntimeException)3 OnDemandQueryRuntimeUtil.executeSelectorAndReturnStreamEvent (io.siddhi.core.util.OnDemandQueryRuntimeUtil.executeSelectorAndReturnStreamEvent)3 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)2