Search in sources :

Example 1 with Processor

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

the class SiddhiAnnotationProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    // Iterate over all @Extension annotated elements.
    for (Element element : roundEnv.getElementsAnnotatedWith(Extension.class)) {
        // Check if a class has been annotated with @Extension.
        if (element.getKind() == ElementKind.CLASS) {
            String superClass = getMatchingSuperClass(element, new String[] { AnnotationConstants.SINK_MAPPER_SUPER_CLASS, AnnotationConstants.SINK_SUPER_CLASS, AnnotationConstants.FUNCTION_EXECUTOR_SUPER_CLASS, AnnotationConstants.AGGREGATION_ATTRIBUTE_SUPER_CLASS, AnnotationConstants.DISTRIBUTION_STRATEGY_SUPER_CLASS, AnnotationConstants.STREAM_PROCESSOR_SUPER_CLASS, AnnotationConstants.STREAM_FUNCTION_PROCESSOR_SUPER_CLASS, AnnotationConstants.STORE_SUPER_CLASS, AnnotationConstants.SOURCE_SUPER_CLASS, AnnotationConstants.SOURCE_MAPPER_SUPER_CLASS, AnnotationConstants.WINDOW_PROCESSOR_CLASS, AnnotationConstants.SCRIPT_SUPER_CLASS, AnnotationConstants.INCREMENTAL_ATTRIBUTE_AGGREGATOR_SUPER_CLASS });
            AbstractAnnotationProcessor abstractAnnotationProcessor = null;
            Extension annotation = element.getAnnotation(Extension.class);
            String name = annotation.name();
            String description = annotation.description();
            String namespace = annotation.namespace();
            Parameter[] parameters = annotation.parameters();
            ReturnAttribute[] returnAttributes = annotation.returnAttributes();
            SystemParameter[] systemParameters = annotation.systemParameter();
            Example[] examples = annotation.examples();
            String extensionClassFullName = element.asType().toString();
            if (superClass != null) {
                switch(superClass) {
                    case AnnotationConstants.DISTRIBUTION_STRATEGY_SUPER_CLASS:
                        abstractAnnotationProcessor = new DistributionStrategyValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.SINK_MAPPER_SUPER_CLASS:
                        abstractAnnotationProcessor = new SinkMapperValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.SINK_SUPER_CLASS:
                        abstractAnnotationProcessor = new SinkValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.FUNCTION_EXECUTOR_SUPER_CLASS:
                        abstractAnnotationProcessor = new FunctionExecutorValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.AGGREGATION_ATTRIBUTE_SUPER_CLASS:
                        abstractAnnotationProcessor = new AggregationAttributeValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.STREAM_PROCESSOR_SUPER_CLASS:
                        abstractAnnotationProcessor = new StreamProcessorValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.SOURCE_SUPER_CLASS:
                        abstractAnnotationProcessor = new SourceValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.SOURCE_MAPPER_SUPER_CLASS:
                        abstractAnnotationProcessor = new SourceMapperValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.STORE_SUPER_CLASS:
                        abstractAnnotationProcessor = new StoreValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.STREAM_FUNCTION_PROCESSOR_SUPER_CLASS:
                        abstractAnnotationProcessor = new StreamFunctionProcessorValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.WINDOW_PROCESSOR_CLASS:
                        abstractAnnotationProcessor = new WindowProcessorValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.SCRIPT_SUPER_CLASS:
                        abstractAnnotationProcessor = new ScriptValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    case AnnotationConstants.INCREMENTAL_ATTRIBUTE_AGGREGATOR_SUPER_CLASS:
                        abstractAnnotationProcessor = new IncrementalAggregationAttributeValidationAnnotationProcessor(extensionClassFullName);
                        break;
                    default:
                        // Throw error if no matching super class.
                        showBuildError(MessageFormat.format("Default switch case executed as there is no " + "matching super class option for @{0}.", superClass), element);
                        break;
                }
                if (abstractAnnotationProcessor != null) {
                    try {
                        abstractAnnotationProcessor.basicParameterValidation(name, description, namespace);
                        abstractAnnotationProcessor.parameterValidation(parameters);
                        abstractAnnotationProcessor.returnAttributesValidation(returnAttributes);
                        abstractAnnotationProcessor.systemParametersValidation(systemParameters);
                        abstractAnnotationProcessor.examplesValidation(examples);
                    } catch (AnnotationValidationException e) {
                        showBuildError(e.getMessage(), element);
                    }
                } else {
                    showBuildError(MessageFormat.format("Error while validation, " + "abstractAnnotationProcessor cannot be null.", superClass), element);
                }
            } else {
                // Throw error if no matching super class.
                showBuildError("Class does not have a matching Siddhi Extension super class.", element);
            }
        } else {
            // Throw error if the element returned is method or package.
            showBuildError(MessageFormat.format("Only classes can be annotated with @{0}.", Extension.class.getCanonicalName()), element);
        }
    }
    // Returning false since this processor only validates.
    return false;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) AnnotationValidationException(org.wso2.siddhi.annotation.util.AnnotationValidationException) ReturnAttribute(org.wso2.siddhi.annotation.ReturnAttribute) Example(org.wso2.siddhi.annotation.Example) SystemParameter(org.wso2.siddhi.annotation.SystemParameter) Extension(org.wso2.siddhi.annotation.Extension) Parameter(org.wso2.siddhi.annotation.Parameter) SystemParameter(org.wso2.siddhi.annotation.SystemParameter)

Example 2 with Processor

use of org.wso2.siddhi.core.query.processor.Processor 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 3 with Processor

use of org.wso2.siddhi.core.query.processor.Processor 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 4 with Processor

use of org.wso2.siddhi.core.query.processor.Processor 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 5 with Processor

use of org.wso2.siddhi.core.query.processor.Processor 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)

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