Search in sources :

Example 41 with Processor

use of org.wso2.siddhi.core.query.processor.Processor in project siddhi by wso2.

the class ExternalTimeWindowProcessor method process.

@Override
protected synchronized void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner) {
    synchronized (this) {
        while (streamEventChunk.hasNext()) {
            StreamEvent streamEvent = streamEventChunk.next();
            long currentTime = (Long) timeStampVariableExpressionExecutor.execute(streamEvent);
            StreamEvent clonedEvent = streamEventCloner.copyStreamEvent(streamEvent);
            clonedEvent.setType(StreamEvent.Type.EXPIRED);
            // reset expiredEventChunk to make sure all of the expired events get removed,
            // otherwise lastReturned.next will always return null and here while check is always false
            expiredEventChunk.reset();
            while (expiredEventChunk.hasNext()) {
                StreamEvent expiredEvent = expiredEventChunk.next();
                long expiredEventTime = (Long) timeStampVariableExpressionExecutor.execute(expiredEvent);
                long timeDiff = expiredEventTime - currentTime + timeToKeep;
                if (timeDiff <= 0) {
                    expiredEventChunk.remove();
                    expiredEvent.setTimestamp(currentTime);
                    streamEventChunk.insertBeforeCurrent(expiredEvent);
                } else {
                    expiredEventChunk.reset();
                    break;
                }
            }
            if (streamEvent.getType() == StreamEvent.Type.CURRENT) {
                this.expiredEventChunk.add(clonedEvent);
            }
            expiredEventChunk.reset();
        }
    }
    nextProcessor.process(streamEventChunk);
}
Also used : StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent)

Example 42 with Processor

use of org.wso2.siddhi.core.query.processor.Processor in project siddhi by wso2.

the class LengthWindowProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner) {
    synchronized (this) {
        long currentTime = siddhiAppContext.getTimestampGenerator().currentTime();
        while (streamEventChunk.hasNext()) {
            StreamEvent streamEvent = streamEventChunk.next();
            StreamEvent clonedEvent = streamEventCloner.copyStreamEvent(streamEvent);
            clonedEvent.setType(StreamEvent.Type.EXPIRED);
            if (count < length) {
                count++;
                this.expiredEventChunk.add(clonedEvent);
            } else {
                StreamEvent firstEvent = this.expiredEventChunk.poll();
                if (firstEvent != null) {
                    firstEvent.setTimestamp(currentTime);
                    streamEventChunk.insertBeforeCurrent(firstEvent);
                    this.expiredEventChunk.add(clonedEvent);
                } else {
                    StreamEvent resetEvent = streamEventCloner.copyStreamEvent(streamEvent);
                    resetEvent.setType(ComplexEvent.Type.RESET);
                    // adding resetEvent and clonedEvent event to the streamEventChunk
                    // since we are using insertAfterCurrent(), the final order will be
                    // currentEvent > clonedEvent (or expiredEvent) > resetEvent
                    streamEventChunk.insertAfterCurrent(resetEvent);
                    streamEventChunk.insertAfterCurrent(clonedEvent);
                    // since we manually added resetEvent and clonedEvent in earlier step
                    // we have to skip those two events from getting processed in the next
                    // iteration. Hence, calling next() twice.
                    streamEventChunk.next();
                    streamEventChunk.next();
                }
            }
        }
    }
    nextProcessor.process(streamEventChunk);
}
Also used : StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent)

Example 43 with Processor

use of org.wso2.siddhi.core.query.processor.Processor in project siddhi by wso2.

the class LossyFrequentWindowProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner) {
    synchronized (this) {
        long currentTime = siddhiAppContext.getTimestampGenerator().currentTime();
        StreamEvent streamEvent = streamEventChunk.getFirst();
        streamEventChunk.clear();
        while (streamEvent != null) {
            StreamEvent next = streamEvent.getNext();
            streamEvent.setNext(null);
            StreamEvent clonedEvent = streamEventCloner.copyStreamEvent(streamEvent);
            clonedEvent.setType(StreamEvent.Type.EXPIRED);
            totalCount++;
            if (totalCount != 1) {
                currentBucketId = Math.ceil(totalCount / windowWidth);
            }
            String currentKey = generateKey(streamEvent);
            StreamEvent oldEvent = map.put(currentKey, clonedEvent);
            if (oldEvent != null) {
                // this event is already in the store
                countMap.put(currentKey, countMap.get(currentKey).incrementCount());
            } else {
                // This is a new event
                LossyCount lCount;
                lCount = new LossyCount(1, (int) currentBucketId - 1);
                countMap.put(currentKey, lCount);
            }
            // calculating all the events in the system which match the
            // requirement provided by the user
            List<String> keys = new ArrayList<String>();
            keys.addAll(countMap.keySet());
            for (String key : keys) {
                LossyCount lossyCount = countMap.get(key);
                if (lossyCount.getCount() >= ((support - error) * totalCount)) {
                    // among the selected events, if the newly arrive event is there we mark it as an inEvent
                    if (key.equals(currentKey)) {
                        streamEventChunk.add(streamEvent);
                    }
                }
            }
            if (totalCount % windowWidth == 0) {
                // its time to run the data-structure prune code
                keys = new ArrayList<String>();
                keys.addAll(countMap.keySet());
                for (String key : keys) {
                    LossyCount lossyCount = countMap.get(key);
                    if (lossyCount.getCount() + lossyCount.getBucketId() <= currentBucketId) {
                        log.info("Removing the Event: " + key + " from the window");
                        countMap.remove(key);
                        StreamEvent expirtedEvent = map.remove(key);
                        expirtedEvent.setTimestamp(currentTime);
                        streamEventChunk.add(expirtedEvent);
                    }
                }
            }
            streamEvent = next;
        }
    }
    nextProcessor.process(streamEventChunk);
}
Also used : StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList)

Example 44 with Processor

use of org.wso2.siddhi.core.query.processor.Processor in project siddhi by wso2.

the class TableWindowProcessor method cloneProcessor.

@Override
public Processor cloneProcessor(String key) {
    try {
        TableWindowProcessor streamProcessor = new TableWindowProcessor(table);
        streamProcessor.inputDefinition = inputDefinition;
        ExpressionExecutor[] innerExpressionExecutors = new ExpressionExecutor[attributeExpressionLength];
        ExpressionExecutor[] attributeExpressionExecutors1 = this.attributeExpressionExecutors;
        for (int i = 0; i < attributeExpressionLength; i++) {
            innerExpressionExecutors[i] = attributeExpressionExecutors1[i].cloneExecutor(key);
        }
        streamProcessor.attributeExpressionExecutors = innerExpressionExecutors;
        streamProcessor.attributeExpressionLength = attributeExpressionLength;
        streamProcessor.additionalAttributes = additionalAttributes;
        streamProcessor.complexEventPopulater = complexEventPopulater;
        streamProcessor.init(inputDefinition, attributeExpressionExecutors, configReader, siddhiAppContext, outputExpectsExpiredEvents);
        streamProcessor.start();
        return streamProcessor;
    } catch (Exception e) {
        throw new SiddhiAppRuntimeException("Exception in cloning " + this.getClass().getCanonicalName(), e);
    }
}
Also used : VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) SiddhiAppRuntimeException(org.wso2.siddhi.core.exception.SiddhiAppRuntimeException) SiddhiAppRuntimeException(org.wso2.siddhi.core.exception.SiddhiAppRuntimeException)

Example 45 with Processor

use of org.wso2.siddhi.core.query.processor.Processor in project siddhi by wso2.

the class TimeLengthWindowProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner) {
    synchronized (this) {
        long currentTime = siddhiAppContext.getTimestampGenerator().currentTime();
        while (streamEventChunk.hasNext()) {
            StreamEvent streamEvent = streamEventChunk.next();
            expiredEventChunk.reset();
            while (expiredEventChunk.hasNext()) {
                StreamEvent expiredEvent = expiredEventChunk.next();
                long timeDiff = expiredEvent.getTimestamp() - currentTime + timeInMilliSeconds;
                if (timeDiff <= 0) {
                    expiredEventChunk.remove();
                    count--;
                    expiredEvent.setTimestamp(currentTime);
                    streamEventChunk.insertBeforeCurrent(expiredEvent);
                } else {
                    break;
                }
            }
            expiredEventChunk.reset();
            if (streamEvent.getType() == StreamEvent.Type.CURRENT) {
                StreamEvent clonedEvent = streamEventCloner.copyStreamEvent(streamEvent);
                clonedEvent.setType(StreamEvent.Type.EXPIRED);
                if (count < length) {
                    count++;
                    this.expiredEventChunk.add(clonedEvent);
                } else {
                    StreamEvent firstEvent = this.expiredEventChunk.poll();
                    if (firstEvent != null) {
                        firstEvent.setTimestamp(currentTime);
                        streamEventChunk.insertBeforeCurrent(firstEvent);
                        this.expiredEventChunk.add(clonedEvent);
                    }
                }
                scheduler.notifyAt(clonedEvent.getTimestamp() + timeInMilliSeconds);
            } else {
                streamEventChunk.remove();
            }
        }
    }
    streamEventChunk.reset();
    if (streamEventChunk.hasNext()) {
        nextProcessor.process(streamEventChunk);
    }
}
Also used : StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent)

Aggregations

StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)15 WSDLProcessor (org.wso2.carbon.apimgt.core.api.WSDLProcessor)11 APIMgtWSDLException (org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException)8 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)6 Processor (org.wso2.siddhi.core.query.processor.Processor)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 ComplexEventChunk (org.wso2.siddhi.core.event.ComplexEventChunk)5 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)5 Test (org.testng.annotations.Test)4 API (org.wso2.carbon.apimgt.core.models.API)4 Label (org.wso2.carbon.apimgt.core.models.Label)4 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)4 SiddhiAppRuntimeException (org.wso2.siddhi.core.exception.SiddhiAppRuntimeException)4 WindowProcessor (org.wso2.siddhi.core.query.processor.stream.window.WindowProcessor)4 WSDLArchiveInfo (org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo)3 MetaStateEvent (org.wso2.siddhi.core.event.state.MetaStateEvent)3 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)3 SingleStreamRuntime (org.wso2.siddhi.core.query.input.stream.single.SingleStreamRuntime)3 SchedulingProcessor (org.wso2.siddhi.core.query.processor.SchedulingProcessor)3