Search in sources :

Example 1 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class ExternalTimeBatchWindowProcessor method process.

/**
 * Here an assumption is taken:
 * Parameter: timestamp: The time which the window determines as current time and will act upon,
 * the value of this parameter should be monotonically increasing.
 * from https://docs.wso2.com/display/CEP400/Inbuilt+Windows#InbuiltWindows-externalTime
 */
@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner) {
    // event incoming trigger process. No events means no action
    if (streamEventChunk.getFirst() == null) {
        return;
    }
    List<ComplexEventChunk<StreamEvent>> complexEventChunks = new ArrayList<ComplexEventChunk<StreamEvent>>();
    synchronized (this) {
        initTiming(streamEventChunk.getFirst());
        StreamEvent nextStreamEvent = streamEventChunk.getFirst();
        while (nextStreamEvent != null) {
            StreamEvent currStreamEvent = nextStreamEvent;
            nextStreamEvent = nextStreamEvent.getNext();
            if (currStreamEvent.getType() == ComplexEvent.Type.TIMER) {
                if (lastScheduledTime <= currStreamEvent.getTimestamp()) {
                    // implies that there have not been any more events after this schedule has been done.
                    if (!flushed) {
                        flushToOutputChunk(streamEventCloner, complexEventChunks, lastCurrentEventTime, true);
                        flushed = true;
                    } else {
                        if (currentEventChunk.getFirst() != null) {
                            appendToOutputChunk(streamEventCloner, complexEventChunks, lastCurrentEventTime, true);
                        }
                    }
                    // rescheduling to emit the current batch after expiring it if no further events arrive.
                    lastScheduledTime = siddhiAppContext.getTimestampGenerator().currentTime() + schedulerTimeout;
                    scheduler.notifyAt(lastScheduledTime);
                }
                continue;
            } else if (currStreamEvent.getType() != ComplexEvent.Type.CURRENT) {
                continue;
            }
            long currentEventTime = (Long) timestampExpressionExecutor.execute(currStreamEvent);
            if (lastCurrentEventTime < currentEventTime) {
                lastCurrentEventTime = currentEventTime;
            }
            if (currentEventTime < endTime) {
                cloneAppend(streamEventCloner, currStreamEvent);
            } else {
                if (flushed) {
                    appendToOutputChunk(streamEventCloner, complexEventChunks, lastCurrentEventTime, false);
                    flushed = false;
                } else {
                    flushToOutputChunk(streamEventCloner, complexEventChunks, lastCurrentEventTime, false);
                }
                // update timestamp, call next processor
                endTime = findEndTime(lastCurrentEventTime, startTime, timeToKeep);
                cloneAppend(streamEventCloner, currStreamEvent);
                // triggering the last batch expiration.
                if (schedulerTimeout > 0) {
                    lastScheduledTime = siddhiAppContext.getTimestampGenerator().currentTime() + schedulerTimeout;
                    scheduler.notifyAt(lastScheduledTime);
                }
            }
        }
    }
    for (ComplexEventChunk<StreamEvent> complexEventChunk : complexEventChunks) {
        nextProcessor.process(complexEventChunk);
    }
}
Also used : ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList)

Example 2 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class InsertIntoWindowCallback method send.

/**
 * Add the event into the {@link Window}
 *
 * @param complexEventChunk the event to add
 * @param noOfEvents number of events
 */
@Override
public void send(ComplexEventChunk complexEventChunk, int noOfEvents) {
    if (getSiddhiDebugger() != null) {
        getSiddhiDebugger().checkBreakPoint(getQueryName(), SiddhiDebugger.QueryTerminal.OUT, complexEventChunk.getFirst());
    }
    // If events are inserted directly from another window, expired events can arrive
    complexEventChunk.reset();
    while (complexEventChunk.hasNext()) {
        ComplexEvent complexEvent = complexEventChunk.next();
        if (complexEvent.getType() == ComplexEvent.Type.EXPIRED) {
            complexEvent.setType(ComplexEvent.Type.CURRENT);
        }
    }
    window.add(complexEventChunk);
}
Also used : ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent)

Example 3 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class TimeBatchWindowProcessor method init.

@Override
protected void init(ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, boolean outputExpectsExpiredEvents, SiddhiAppContext siddhiAppContext) {
    this.outputExpectsExpiredEvents = outputExpectsExpiredEvents;
    this.siddhiAppContext = siddhiAppContext;
    if (outputExpectsExpiredEvents) {
        this.expiredEventChunk = new ComplexEventChunk<StreamEvent>(false);
    }
    if (attributeExpressionExecutors.length == 1) {
        if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
            if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.INT) {
                timeInMilliSeconds = (Integer) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
            } else if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.LONG) {
                timeInMilliSeconds = (Long) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
            } else {
                throw new SiddhiAppValidationException("Time window's parameter attribute should be either " + "int or long, but found " + attributeExpressionExecutors[0].getReturnType());
            }
        } else {
            throw new SiddhiAppValidationException("Time window should have constant parameter attribute but " + "found a dynamic attribute " + attributeExpressionExecutors[0].getClass().getCanonicalName());
        }
    } else if (attributeExpressionExecutors.length == 2) {
        if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
            if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.INT) {
                timeInMilliSeconds = (Integer) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
            } else if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.LONG) {
                timeInMilliSeconds = (Long) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
            } else {
                throw new SiddhiAppValidationException("Time window's parameter attribute should be either " + "int or long, but found " + attributeExpressionExecutors[0].getReturnType());
            }
        } else {
            throw new SiddhiAppValidationException("Time window should have constant parameter attribute but " + "found a dynamic attribute " + attributeExpressionExecutors[0].getClass().getCanonicalName());
        }
        // start time
        isStartTimeEnabled = true;
        if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.INT) {
            startTime = Integer.parseInt(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue()));
        } else {
            startTime = Long.parseLong(String.valueOf(((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue()));
        }
    } else {
        throw new SiddhiAppValidationException("Time window should only have one or two parameters. " + "(<int|long|time> windowTime), but found " + attributeExpressionExecutors.length + " input " + "attributes");
    }
}
Also used : StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) SiddhiAppValidationException(org.wso2.siddhi.query.api.exception.SiddhiAppValidationException) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor)

Example 4 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class TimeLengthWindowProcessor method init.

@Override
protected void init(ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, boolean outputExpectsExpiredEvents, SiddhiAppContext siddhiAppContext) {
    this.siddhiAppContext = siddhiAppContext;
    expiredEventChunk = new ComplexEventChunk<StreamEvent>(false);
    if (attributeExpressionExecutors.length == 2) {
        length = (Integer) ((ConstantExpressionExecutor) attributeExpressionExecutors[1]).getValue();
        if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
            if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.INT) {
                timeInMilliSeconds = (Integer) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
            } else if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.LONG) {
                timeInMilliSeconds = (Long) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
            } else {
                throw new SiddhiAppValidationException("TimeLength window's first parameter attribute should " + "be either int or long, but found " + attributeExpressionExecutors[0].getReturnType());
            }
        } else {
            throw new SiddhiAppValidationException("TimeLength window should have constant parameter " + "attributes but found a dynamic attribute " + attributeExpressionExecutors[0].getClass().getCanonicalName());
        }
    } else {
        throw new SiddhiAppValidationException("TimeLength window should only have two parameters (<int> " + "windowTime,<int> windowLength), but found " + attributeExpressionExecutors.length + " input " + "attributes");
    }
}
Also used : StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) SiddhiAppValidationException(org.wso2.siddhi.query.api.exception.SiddhiAppValidationException) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor)

Example 5 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class TimeWindowProcessor method init.

@Override
protected void init(ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, boolean outputExpectsExpiredEvents, SiddhiAppContext siddhiAppContext) {
    this.siddhiAppContext = siddhiAppContext;
    this.expiredEventChunk = new ComplexEventChunk<StreamEvent>(false);
    if (attributeExpressionExecutors.length == 1) {
        if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
            if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.INT) {
                timeInMilliSeconds = (Integer) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
            } else if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.LONG) {
                timeInMilliSeconds = (Long) ((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue();
            } else {
                throw new SiddhiAppValidationException("Time window's parameter attribute should be either " + "int or long, but found " + attributeExpressionExecutors[0].getReturnType());
            }
        } else {
            throw new SiddhiAppValidationException("Time window should have constant parameter attribute but " + "found a dynamic attribute " + attributeExpressionExecutors[0].getClass().getCanonicalName());
        }
    } else {
        throw new SiddhiAppValidationException("Time window should only have one parameter (<int|long|time> " + "windowTime), but found " + attributeExpressionExecutors.length + " input attributes");
    }
}
Also used : StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) SiddhiAppValidationException(org.wso2.siddhi.query.api.exception.SiddhiAppValidationException) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor)

Aggregations

Test (org.testng.annotations.Test)161 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)130 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)130 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)118 Event (org.wso2.siddhi.core.event.Event)117 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)82 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)35 Query (org.wso2.siddhi.query.api.execution.query.Query)32 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)14 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)12 ArrayList (java.util.ArrayList)10 InMemoryPersistenceStore (org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore)10 PersistenceStore (org.wso2.siddhi.core.util.persistence.PersistenceStore)10 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)9 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)9 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)8 SiddhiAppValidationException (org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)8 Window (org.wso2.siddhi.core.window.Window)7 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)6 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)6