Search in sources :

Example 26 with StateEvent

use of org.ballerinalang.siddhi.core.event.state.StateEvent in project ballerina by ballerina-lang.

the class AbsentStreamPreStateProcessor method process.

@Override
public void process(ComplexEventChunk complexEventChunk) {
    if (!this.active) {
        // Every keyword is not used and already a pattern is processed
        return;
    }
    boolean notProcessed = true;
    long currentTime = complexEventChunk.getFirst().getTimestamp();
    if (currentTime >= this.lastArrivalTime + waitingTime) {
        synchronized (this) {
            // If the process method is called, it is guaranteed that the waitingTime is passed
            boolean initialize;
            initialize = isStartState && newAndEveryStateEventList.isEmpty() && pendingStateEventList.isEmpty();
            if (initialize && stateType == StateInputStream.Type.SEQUENCE && thisStatePostProcessor.nextEveryStatePerProcessor == null && this.lastArrivalTime > 0) {
                // Sequence with no every but an event arrived
                initialize = false;
            }
            if (initialize) {
                // This is the first processor and no events received so far
                StateEvent stateEvent = stateEventPool.borrowEvent();
                addState(stateEvent);
            } else if (stateType == StateInputStream.Type.SEQUENCE && !newAndEveryStateEventList.isEmpty()) {
                this.resetState();
            }
            this.updateState();
            ComplexEventChunk<StateEvent> retEventChunk = new ComplexEventChunk<>(false);
            Iterator<StateEvent> iterator = pendingStateEventList.iterator();
            while (iterator.hasNext()) {
                StateEvent event = iterator.next();
                // Remove expired events based on within
                if (withinStates.size() > 0) {
                    if (isExpired(event, currentTime)) {
                        iterator.remove();
                        continue;
                    }
                }
                // Collect the events that came before the waiting time
                if (currentTime >= event.getTimestamp() + waitingTime) {
                    iterator.remove();
                    event.setTimestamp(currentTime);
                    retEventChunk.add(event);
                }
            }
            notProcessed = retEventChunk.getFirst() == null;
            while (retEventChunk.hasNext()) {
                StateEvent stateEvent = retEventChunk.next();
                retEventChunk.remove();
                sendEvent(stateEvent);
            }
        }
        this.lastArrivalTime = 0;
    }
    if (thisStatePostProcessor.nextEveryStatePerProcessor == this || (notProcessed && isStartState)) {
        // If every or (notProcessed and startState), schedule again
        long nextBreak;
        if (lastArrivalTime == 0) {
            nextBreak = currentTime + waitingTime;
        } else {
            nextBreak = lastArrivalTime + waitingTime;
        }
        this.scheduler.notifyAt(nextBreak);
    }
}
Also used : ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) StateEvent(org.ballerinalang.siddhi.core.event.state.StateEvent)

Example 27 with StateEvent

use of org.ballerinalang.siddhi.core.event.state.StateEvent in project ballerina by ballerina-lang.

the class StreamPostStateProcessor method process.

/**
 * Process the handed StreamEvent.
 *
 * @param complexEventChunk event chunk to be processed
 */
@Override
public void process(ComplexEventChunk complexEventChunk) {
    complexEventChunk.reset();
    if (complexEventChunk.hasNext()) {
        // one one event will be coming
        StateEvent stateEvent = (StateEvent) complexEventChunk.next();
        process(stateEvent, complexEventChunk);
    }
    complexEventChunk.clear();
}
Also used : StateEvent(org.ballerinalang.siddhi.core.event.state.StateEvent)

Example 28 with StateEvent

use of org.ballerinalang.siddhi.core.event.state.StateEvent in project ballerina by ballerina-lang.

the class StreamPreStateProcessor method init.

public void init() {
    if (isStartState && (!initialized || this.thisStatePostProcessor.nextEveryStatePerProcessor != null || (stateType == StateInputStream.Type.SEQUENCE && this.thisStatePostProcessor.nextStatePerProcessor instanceof AbsentPreStateProcessor))) {
        // For 'every' sequence, the 'thisStatePostProcessor.nextEveryStatePerProcessor != null' check is not enough
        StateEvent stateEvent = stateEventPool.borrowEvent();
        addState(stateEvent);
        initialized = true;
    }
}
Also used : StateEvent(org.ballerinalang.siddhi.core.event.state.StateEvent)

Example 29 with StateEvent

use of org.ballerinalang.siddhi.core.event.state.StateEvent in project ballerina by ballerina-lang.

the class StreamPreStateProcessor method processAndReturn.

@Override
public ComplexEventChunk<StateEvent> processAndReturn(ComplexEventChunk complexEventChunk) {
    ComplexEventChunk<StateEvent> returnEventChunk = new ComplexEventChunk<StateEvent>(false);
    complexEventChunk.reset();
    // Sure only one will be sent
    StreamEvent streamEvent = (StreamEvent) complexEventChunk.next();
    for (Iterator<StateEvent> iterator = pendingStateEventList.iterator(); iterator.hasNext(); ) {
        StateEvent stateEvent = iterator.next();
        if (withinStates.size() > 0) {
            if (isExpired(stateEvent, streamEvent.getTimestamp())) {
                iterator.remove();
                continue;
            }
        }
        // if (Math.abs(stateEvent.getTimestamp() - streamEvent.getTimestamp()) > withinStates) {
        // iterator.remove();
        // //                    switch (stateType) {
        // //                        case PATTERN:
        // //                            stateEvent.setEvent(stateId, null);
        // //                            break;
        // //                        case SEQUENCE:
        // //                            stateEvent.setEvent(stateId, null);
        // //                            iterator.remove();
        // //                            if (thisStatePostProcessor.callbackPreStateProcessor != null) {
        // //                                thisStatePostProcessor.callbackPreStateProcessor.startStateReset();
        // //                            }
        // //                            break;
        // //                    }
        // continue;
        // }
        // }
        stateEvent.setEvent(stateId, streamEventCloner.copyStreamEvent(streamEvent));
        process(stateEvent);
        if (this.thisLastProcessor.isEventReturned()) {
            this.thisLastProcessor.clearProcessedEvent();
            returnEventChunk.add(stateEvent);
        }
        if (stateChanged) {
            iterator.remove();
        } else {
            switch(stateType) {
                case PATTERN:
                    stateEvent.setEvent(stateId, null);
                    break;
                case SEQUENCE:
                    stateEvent.setEvent(stateId, null);
                    iterator.remove();
                    if (thisStatePostProcessor.callbackPreStateProcessor != null) {
                        thisStatePostProcessor.callbackPreStateProcessor.startStateReset();
                    }
                    break;
            }
        }
    }
    return returnEventChunk;
}
Also used : ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) StateEvent(org.ballerinalang.siddhi.core.event.state.StateEvent)

Example 30 with StateEvent

use of org.ballerinalang.siddhi.core.event.state.StateEvent in project ballerina by ballerina-lang.

the class CollectionOperator method tryUpdate.

@Override
public ComplexEventChunk<StreamEvent> tryUpdate(ComplexEventChunk<StateEvent> updatingOrAddingEventChunk, Object storeEvents, InMemoryCompiledUpdateSet compiledUpdateSet, AddingStreamEventExtractor addingStreamEventExtractor) {
    updatingOrAddingEventChunk.reset();
    ComplexEventChunk<StreamEvent> failedEventChunk = new ComplexEventChunk<StreamEvent>(updatingOrAddingEventChunk.isBatch());
    while (updatingOrAddingEventChunk.hasNext()) {
        StateEvent updateOrAddingEvent = updatingOrAddingEventChunk.next();
        try {
            boolean updated = false;
            if (((Collection<StreamEvent>) storeEvents).size() > 0) {
                for (StreamEvent storeEvent : ((Collection<StreamEvent>) storeEvents)) {
                    updateOrAddingEvent.setEvent(storeEventPosition, storeEvent);
                    if ((Boolean) expressionExecutor.execute(updateOrAddingEvent)) {
                        for (Map.Entry<Integer, ExpressionExecutor> entry : compiledUpdateSet.getExpressionExecutorMap().entrySet()) {
                            storeEvent.setOutputData(entry.getValue().execute(updateOrAddingEvent), entry.getKey());
                        }
                        updated = true;
                    }
                }
            }
            if (!updated) {
                failedEventChunk.add(addingStreamEventExtractor.getAddingStreamEvent(updateOrAddingEvent));
            }
        } finally {
            updateOrAddingEvent.setEvent(storeEventPosition, null);
        }
    }
    return failedEventChunk;
}
Also used : ComplexEventChunk(org.ballerinalang.siddhi.core.event.ComplexEventChunk) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) StateEvent(org.ballerinalang.siddhi.core.event.state.StateEvent) Map(java.util.Map)

Aggregations

StateEvent (org.ballerinalang.siddhi.core.event.state.StateEvent)34 ComplexEventChunk (org.ballerinalang.siddhi.core.event.ComplexEventChunk)19 StreamEvent (org.ballerinalang.siddhi.core.event.stream.StreamEvent)19 ExpressionExecutor (org.ballerinalang.siddhi.core.executor.ExpressionExecutor)7 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 ComplexEvent (org.ballerinalang.siddhi.core.event.ComplexEvent)5 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)4 VariableExpressionExecutor (org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor)4 HashMap (java.util.HashMap)3 Event (org.ballerinalang.siddhi.core.event.Event)3 StoreQueryRuntimeException (org.ballerinalang.siddhi.core.exception.StoreQueryRuntimeException)2 Collection (java.util.Collection)1 MetaStateEvent (org.ballerinalang.siddhi.core.event.state.MetaStateEvent)1 SiddhiAppCreationException (org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)1 ConstantExpressionExecutor (org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor)1 AndConditionExpressionExecutor (org.ballerinalang.siddhi.core.executor.condition.AndConditionExpressionExecutor)1 BoolConditionExpressionExecutor (org.ballerinalang.siddhi.core.executor.condition.BoolConditionExpressionExecutor)1 ConditionExpressionExecutor (org.ballerinalang.siddhi.core.executor.condition.ConditionExpressionExecutor)1 InConditionExpressionExecutor (org.ballerinalang.siddhi.core.executor.condition.InConditionExpressionExecutor)1