Search in sources :

Example 11 with StateEvent

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

the class AbsentLogicalPreStateProcessor method process.

@Override
public void process(ComplexEventChunk complexEventChunk) {
    if (!this.active) {
        return;
    }
    this.lock.lock();
    boolean notProcessed = true;
    try {
        long currentTime = complexEventChunk.getFirst().getTimestamp();
        if (currentTime >= this.lastArrivalTime + waitingTime) {
            ComplexEventChunk<StateEvent> retEventChunk = new ComplexEventChunk<>(false);
            Iterator<StateEvent> iterator;
            if (isStartState && stateType == StateInputStream.Type.SEQUENCE && newAndEveryStateEventList.isEmpty() && pendingStateEventList.isEmpty()) {
                StateEvent stateEvent = stateEventPool.borrowEvent();
                addState(stateEvent);
            } else if (stateType == StateInputStream.Type.SEQUENCE && !newAndEveryStateEventList.isEmpty()) {
                this.resetState();
            }
            this.updateState();
            iterator = pendingStateEventList.iterator();
            while (iterator.hasNext()) {
                StateEvent stateEvent = iterator.next();
                // Remove expired events based on within
                if (withinStates.size() > 0) {
                    if (isExpired(stateEvent, currentTime)) {
                        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, streamEventPool.borrowEvent());
                        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, streamEventPool.borrowEvent());
                    }
                }
            }
            retEventChunk.reset();
            notProcessed = retEventChunk.getFirst() == null;
            while (retEventChunk.hasNext()) {
                StateEvent stateEvent = retEventChunk.next();
                retEventChunk.remove();
                sendEvent(stateEvent);
            }
            this.lastArrivalTime = 0;
        }
    } finally {
        this.lock.unlock();
    }
    if (thisStatePostProcessor.nextEveryStatePerProcessor != null || (notProcessed && isStartState)) {
        // If every or (notProcessed and startState), schedule again
        long nextBreak;
        if (lastArrivalTime == 0) {
            nextBreak = siddhiAppContext.getTimestampGenerator().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 12 with StateEvent

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

the class CountPreStateProcessor 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 (removeIfNextStateProcessed(stateEvent, iterator, stateId + 1)) {
            continue;
        }
        if (removeIfNextStateProcessed(stateEvent, iterator, stateId + 2)) {
            continue;
        }
        stateEvent.addEvent(stateId, streamEventCloner.copyStreamEvent(streamEvent));
        successCondition = false;
        process(stateEvent);
        if (this.thisLastProcessor.isEventReturned()) {
            this.thisLastProcessor.clearProcessedEvent();
            returnEventChunk.add(stateEvent);
        }
        if (stateChanged) {
            iterator.remove();
        }
        if (!successCondition) {
            switch(stateType) {
                case PATTERN:
                    stateEvent.removeLastEvent(stateId);
                    break;
                case SEQUENCE:
                    stateEvent.removeLastEvent(stateId);
                    iterator.remove();
                    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 13 with StateEvent

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

the class LogicalPreStateProcessor 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 (logicalType == LogicalStateElement.Type.OR && stateEvent.getStreamEvent(partnerStatePreProcessor.getStateId()) != null) {
            iterator.remove();
            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();
                    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 14 with StateEvent

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

the class LogicalPreStateProcessor method addEveryState.

@Override
public void addEveryState(StateEvent stateEvent) {
    StateEvent clonedEvent = stateEventCloner.copyStateEvent(stateEvent);
    clonedEvent.setEvent(stateId, null);
    newAndEveryStateEventList.add(clonedEvent);
    if (partnerStatePreProcessor != null) {
        clonedEvent.setEvent(partnerStatePreProcessor.stateId, null);
        partnerStatePreProcessor.newAndEveryStateEventList.add(clonedEvent);
    }
}
Also used : StateEvent(org.ballerinalang.siddhi.core.event.state.StateEvent)

Example 15 with StateEvent

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

the class FindStoreQueryRuntime method execute.

@Override
public Event[] execute() {
    try {
        StateEvent stateEvent = new StateEvent(1, 0);
        StreamEvent streamEvents = null;
        switch(eventType) {
            case TABLE:
                streamEvents = table.find(stateEvent, compiledCondition);
                break;
            case WINDOW:
                streamEvents = window.find(stateEvent, compiledCondition);
                break;
            case AGGREGATE:
                stateEvent = new StateEvent(2, 0);
                StreamEvent streamEvent = new StreamEvent(0, 2, 0);
                stateEvent.addEvent(0, streamEvent);
                streamEvents = aggregation.find(stateEvent, compiledCondition);
                break;
            case DEFAULT:
                break;
        }
        if (streamEvents == null) {
            return null;
        } else {
            if (selector != null) {
                return executeSelector(streamEvents, eventType);
            } else {
                List<Event> events = new ArrayList<Event>();
                while (streamEvents != null) {
                    events.add(new Event(streamEvents.getTimestamp(), streamEvents.getOutputData()));
                    streamEvents = streamEvents.getNext();
                }
                return events.toArray(new Event[0]);
            }
        }
    } catch (Throwable t) {
        throw new StoreQueryRuntimeException("Error executing '" + queryName + "', " + t.getMessage(), t);
    }
}
Also used : MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) StoreQueryRuntimeException(org.ballerinalang.siddhi.core.exception.StoreQueryRuntimeException) ArrayList(java.util.ArrayList) Event(org.ballerinalang.siddhi.core.event.Event) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) ComplexEvent(org.ballerinalang.siddhi.core.event.ComplexEvent) StreamEvent(org.ballerinalang.siddhi.core.event.stream.StreamEvent) StateEvent(org.ballerinalang.siddhi.core.event.state.StateEvent) StateEvent(org.ballerinalang.siddhi.core.event.state.StateEvent)

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