Search in sources :

Example 6 with In

use of org.wso2.siddhi.query.api.expression.condition.In in project siddhi by wso2.

the class AbstractStreamProcessor method cloneProcessor.

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

Example 7 with In

use of org.wso2.siddhi.query.api.expression.condition.In in project siddhi by wso2.

the class LogStreamProcessor method init.

/**
 * The init method of the StreamFunction
 *
 * @param inputDefinition              the incoming stream definition
 * @param attributeExpressionExecutors the executors for the function parameters
 * @param siddhiAppContext         siddhi app context
 * @param configReader this hold the {@link LogStreamProcessor} configuration reader.
 * @return the additional output attributes introduced by the function
 */
@Override
protected List<Attribute> init(AbstractDefinition inputDefinition, ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, SiddhiAppContext siddhiAppContext) {
    int inputExecutorLength = attributeExpressionExecutors.length;
    if (inputExecutorLength == 1) {
        if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.STRING) {
            logMessageExpressionExecutor = attributeExpressionExecutors[0];
        } else if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.BOOL) {
            isLogEventExpressionExecutor = attributeExpressionExecutors[0];
        } else {
            throw new SiddhiAppValidationException("Input attribute is expected to be 'isEventLogged (Bool)' " + "or 'logMessage (String)' or 'isEventLogged (Bool), logMessage (String)' or 'priority " + "(String), isEventLogged (Bool), logMessage (String)', but its 1st attribute is " + attributeExpressionExecutors[0].getReturnType());
        }
    } else if (inputExecutorLength == 2) {
        if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.STRING && attributeExpressionExecutors[1].getReturnType() == Attribute.Type.BOOL) {
            logMessageExpressionExecutor = attributeExpressionExecutors[0];
            isLogEventExpressionExecutor = attributeExpressionExecutors[1];
        } else if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.STRING && attributeExpressionExecutors[1].getReturnType() == Attribute.Type.STRING) {
            if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
                logPriority = LogPriority.valueOf(((String) attributeExpressionExecutors[0].execute(null)).toUpperCase());
            } else {
                logPriorityExpressionExecutor = attributeExpressionExecutors[0];
            }
            logMessageExpressionExecutor = attributeExpressionExecutors[1];
        } else {
            throw new SiddhiAppValidationException("Input attribute is expected to be 'logMessage (String), " + "isEventLogged (Bool)' or 'priority (String), logMessage (String)', but its returning are '" + attributeExpressionExecutors[0].getReturnType() + ", " + attributeExpressionExecutors[1].getReturnType() + "'");
        }
    } else if (inputExecutorLength == 3) {
        if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.STRING) {
            if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
                logPriority = LogPriority.valueOf(((String) attributeExpressionExecutors[0].execute(null)).toUpperCase());
            } else {
                logPriorityExpressionExecutor = attributeExpressionExecutors[0];
            }
        } else {
            throw new SiddhiAppValidationException("Input attribute is expected to be 'priority (String), " + "logMessage (String), isEventLogged (Bool)', but its 1st attribute is returning " + attributeExpressionExecutors[0].getReturnType());
        }
        if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.STRING) {
            logMessageExpressionExecutor = attributeExpressionExecutors[1];
        } else {
            throw new SiddhiAppValidationException("Input attribute is expected to be 'priority (String), " + "logMessage (String), isEventLogged (Bool)', but its 2nd attribute is returning " + attributeExpressionExecutors[1].getReturnType());
        }
        if (attributeExpressionExecutors[2].getReturnType() == Attribute.Type.BOOL) {
            isLogEventExpressionExecutor = attributeExpressionExecutors[2];
        } else {
            throw new SiddhiAppValidationException("Input attribute is expected to be 'priority (String), " + "logMessage (String), isEventLogged (Bool)', but its 3rd attribute is returning " + attributeExpressionExecutors[2].getReturnType());
        }
    } else if (inputExecutorLength > 3) {
        throw new SiddhiAppValidationException("Input parameters for Log can be logMessage (String), " + "isEventLogged (Bool), but there are " + attributeExpressionExecutors.length + " in the input!");
    }
    logPrefix = siddhiAppContext.getName() + ": ";
    return new ArrayList<Attribute>();
}
Also used : ArrayList(java.util.ArrayList) SiddhiAppValidationException(org.wso2.siddhi.query.api.exception.SiddhiAppValidationException) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor)

Example 8 with In

use of org.wso2.siddhi.query.api.expression.condition.In in project siddhi by wso2.

the class AggregateWindowProcessor method cloneProcessor.

@Override
public Processor cloneProcessor(String key) {
    try {
        AggregateWindowProcessor streamProcessor = new AggregateWindowProcessor(aggregationRuntime, within, per);
        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 9 with In

use of org.wso2.siddhi.query.api.expression.condition.In in project siddhi by wso2.

the class ExternalTimeBatchWindowProcessor method appendToOutputChunk.

private void appendToOutputChunk(StreamEventCloner streamEventCloner, List<ComplexEventChunk<StreamEvent>> complexEventChunks, long currentTime, boolean preserveCurrentEvents) {
    ComplexEventChunk<StreamEvent> newEventChunk = new ComplexEventChunk<StreamEvent>(true);
    ComplexEventChunk<StreamEvent> sentEventChunk = new ComplexEventChunk<StreamEvent>(true);
    if (currentEventChunk.getFirst() != null) {
        if (expiredEventChunk.getFirst() != null) {
            // mark the timestamp for the expiredType event
            expiredEventChunk.reset();
            while (expiredEventChunk.hasNext()) {
                StreamEvent expiredEvent = expiredEventChunk.next();
                if (outputExpectsExpiredEvents) {
                    // add expired event to newEventChunk.
                    StreamEvent toExpireEvent = streamEventCloner.copyStreamEvent(expiredEvent);
                    toExpireEvent.setTimestamp(currentTime);
                    newEventChunk.add(toExpireEvent);
                }
                StreamEvent toSendEvent = streamEventCloner.copyStreamEvent(expiredEvent);
                toSendEvent.setType(ComplexEvent.Type.CURRENT);
                sentEventChunk.add(toSendEvent);
            }
        }
        // add reset event in front of current events
        StreamEvent toResetEvent = streamEventCloner.copyStreamEvent(resetEvent);
        toResetEvent.setTimestamp(currentTime);
        newEventChunk.add(toResetEvent);
        // add old events
        newEventChunk.add(sentEventChunk.getFirst());
        // move to expired events
        if (preserveCurrentEvents || storeExpiredEvents) {
            currentEventChunk.reset();
            while (currentEventChunk.hasNext()) {
                StreamEvent currentEvent = currentEventChunk.next();
                StreamEvent toExpireEvent = streamEventCloner.copyStreamEvent(currentEvent);
                toExpireEvent.setType(StreamEvent.Type.EXPIRED);
                expiredEventChunk.add(toExpireEvent);
            }
        }
        // add current event chunk to next processor
        newEventChunk.add(currentEventChunk.getFirst());
    }
    currentEventChunk.clear();
    if (newEventChunk.getFirst() != null) {
        complexEventChunks.add(newEventChunk);
    }
}
Also used : ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent)

Example 10 with In

use of org.wso2.siddhi.query.api.expression.condition.In in project siddhi by wso2.

the class FrequentWindowProcessor method process.

@Override
protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor, StreamEventCloner streamEventCloner) {
    synchronized (this) {
        StreamEvent streamEvent = streamEventChunk.getFirst();
        streamEventChunk.clear();
        long currentTime = siddhiAppContext.getTimestampGenerator().currentTime();
        while (streamEvent != null) {
            StreamEvent next = streamEvent.getNext();
            streamEvent.setNext(null);
            StreamEvent clonedEvent = streamEventCloner.copyStreamEvent(streamEvent);
            clonedEvent.setType(StreamEvent.Type.EXPIRED);
            String key = generateKey(streamEvent);
            StreamEvent oldEvent = map.put(key, clonedEvent);
            if (oldEvent != null) {
                countMap.put(key, countMap.get(key) + 1);
                streamEventChunk.add(streamEvent);
            } else {
                // This is a new event
                if (map.size() > mostFrequentCount) {
                    List<String> keys = new ArrayList<String>(countMap.keySet());
                    for (int i = 0; i < mostFrequentCount; i++) {
                        int count = countMap.get(keys.get(i)) - 1;
                        if (count == 0) {
                            countMap.remove(keys.get(i));
                            StreamEvent expiredEvent = map.remove(keys.get(i));
                            expiredEvent.setTimestamp(currentTime);
                            streamEventChunk.add(expiredEvent);
                        } else {
                            countMap.put(keys.get(i), count);
                        }
                    }
                    // now we have tried to remove one for newly added item
                    if (map.size() > mostFrequentCount) {
                        // nothing happend by the attempt to remove one from the
                        // map so we are ignoring this event
                        map.remove(key);
                    // Here we do nothing just drop the message
                    } else {
                        // we got some space, event is already there in map object
                        // we just have to add it to the countMap
                        countMap.put(key, 1);
                        streamEventChunk.add(streamEvent);
                    }
                } else {
                    countMap.put(generateKey(streamEvent), 1);
                    streamEventChunk.add(streamEvent);
                }
            }
            streamEvent = next;
        }
    }
    nextProcessor.process(streamEventChunk);
}
Also used : StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ArrayList(java.util.ArrayList)

Aggregations

Test (org.testng.annotations.Test)281 Event (org.wso2.siddhi.core.event.Event)187 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)186 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)186 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)182 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)160 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)153 ArrayList (java.util.ArrayList)148 HashMap (java.util.HashMap)118 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)103 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)81 IOException (java.io.IOException)57 Map (java.util.Map)54 API (org.wso2.carbon.apimgt.core.models.API)52 SQLException (java.sql.SQLException)48 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)46 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)39 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)37 File (java.io.File)35 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)34