Search in sources :

Example 1 with Window

use of io.siddhi.query.api.execution.query.input.handler.Window in project siddhi by wso2.

the class SingleInputStreamParser method generateProcessor.

public static Processor generateProcessor(StreamHandler streamHandler, MetaComplexEvent metaEvent, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, Table> tableMap, boolean supportsBatchProcessing, boolean outputExpectsExpiredEvents, boolean findToBeExecuted, SiddhiQueryContext siddhiQueryContext) {
    Expression[] parameters = streamHandler.getParameters();
    MetaStreamEvent metaStreamEvent;
    int stateIndex = SiddhiConstants.UNKNOWN_STATE;
    if (metaEvent instanceof MetaStateEvent) {
        stateIndex = ((MetaStateEvent) metaEvent).getStreamEventCount() - 1;
        metaStreamEvent = ((MetaStateEvent) metaEvent).getMetaStreamEvent(stateIndex);
    } else {
        metaStreamEvent = (MetaStreamEvent) metaEvent;
    }
    if (streamHandler instanceof Window) {
        metaStreamEvent.initializeOnAfterWindowData();
    }
    ExpressionExecutor[] attributeExpressionExecutors;
    if (parameters != null) {
        if (parameters.length > 0) {
            attributeExpressionExecutors = new ExpressionExecutor[parameters.length];
            for (int i = 0, parametersLength = parameters.length; i < parametersLength; i++) {
                attributeExpressionExecutors[i] = ExpressionParser.parseExpression(parameters[i], metaEvent, stateIndex, tableMap, variableExpressionExecutors, false, SiddhiConstants.CURRENT, ProcessingMode.BATCH, false, siddhiQueryContext);
            }
        } else {
            List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
            int parameterSize = attributeList.size();
            attributeExpressionExecutors = new ExpressionExecutor[parameterSize];
            for (int i = 0; i < parameterSize; i++) {
                attributeExpressionExecutors[i] = ExpressionParser.parseExpression(new Variable(attributeList.get(i).getName()), metaEvent, stateIndex, tableMap, variableExpressionExecutors, false, SiddhiConstants.CURRENT, ProcessingMode.BATCH, false, siddhiQueryContext);
            }
        }
    } else {
        attributeExpressionExecutors = new ExpressionExecutor[0];
    }
    ConfigReader configReader;
    if (streamHandler instanceof Filter) {
        return new FilterProcessor(attributeExpressionExecutors[0]);
    } else if (streamHandler instanceof Window) {
        WindowProcessor windowProcessor = (WindowProcessor) SiddhiClassLoader.loadExtensionImplementation((Extension) streamHandler, WindowProcessorExtensionHolder.getInstance(siddhiQueryContext.getSiddhiAppContext()));
        configReader = siddhiQueryContext.getSiddhiContext().getConfigManager().generateConfigReader(((Window) streamHandler).getNamespace(), ((Window) streamHandler).getName());
        windowProcessor.initProcessor(metaStreamEvent, attributeExpressionExecutors, configReader, outputExpectsExpiredEvents, findToBeExecuted, false, streamHandler, siddhiQueryContext);
        return windowProcessor;
    } else if (streamHandler instanceof StreamFunction) {
        AbstractStreamProcessor abstractStreamProcessor;
        configReader = siddhiQueryContext.getSiddhiContext().getConfigManager().generateConfigReader(((StreamFunction) streamHandler).getNamespace(), ((StreamFunction) streamHandler).getName());
        if (supportsBatchProcessing) {
            try {
                abstractStreamProcessor = (StreamProcessor) SiddhiClassLoader.loadExtensionImplementation((Extension) streamHandler, StreamProcessorExtensionHolder.getInstance(siddhiQueryContext.getSiddhiAppContext()));
                abstractStreamProcessor.initProcessor(metaStreamEvent, attributeExpressionExecutors, configReader, outputExpectsExpiredEvents, false, false, streamHandler, siddhiQueryContext);
                return abstractStreamProcessor;
            } catch (SiddhiAppCreationException e) {
                if (!e.isClassLoadingIssue()) {
                    ExceptionUtil.populateQueryContext(e, streamHandler, siddhiQueryContext.getSiddhiAppContext(), siddhiQueryContext);
                    throw e;
                }
            }
        }
        abstractStreamProcessor = (StreamFunctionProcessor) SiddhiClassLoader.loadExtensionImplementation((Extension) streamHandler, StreamFunctionProcessorExtensionHolder.getInstance(siddhiQueryContext.getSiddhiAppContext()));
        abstractStreamProcessor.initProcessor(metaStreamEvent, attributeExpressionExecutors, configReader, outputExpectsExpiredEvents, false, false, streamHandler, siddhiQueryContext);
        return abstractStreamProcessor;
    } else {
        throw new SiddhiAppCreationException(streamHandler.getClass().getName() + " is not supported", streamHandler.getQueryContextStartIndex(), streamHandler.getQueryContextEndIndex());
    }
}
Also used : Window(io.siddhi.query.api.execution.query.input.handler.Window) Variable(io.siddhi.query.api.expression.Variable) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) AbstractStreamProcessor(io.siddhi.core.query.processor.stream.AbstractStreamProcessor) Attribute(io.siddhi.query.api.definition.Attribute) FilterProcessor(io.siddhi.core.query.processor.filter.FilterProcessor) StreamFunction(io.siddhi.query.api.execution.query.input.handler.StreamFunction) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) ConfigReader(io.siddhi.core.util.config.ConfigReader) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) Expression(io.siddhi.query.api.expression.Expression) Filter(io.siddhi.query.api.execution.query.input.handler.Filter) WindowProcessor(io.siddhi.core.query.processor.stream.window.WindowProcessor) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Example 2 with Window

use of io.siddhi.query.api.execution.query.input.handler.Window in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitStandard_stream.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public SingleInputStream visitStandard_stream(@NotNull SiddhiQLParser.Standard_streamContext ctx) {
    // standard_stream
    // : io (basic_source_stream_handler)* window? (basic_source_stream_handler)*
    // ;
    Source source = (Source) visit(ctx.source());
    BasicSingleInputStream basicSingleInputStream = new BasicSingleInputStream(null, source.streamId, source.isInnerStream, source.isFaultStream);
    if (ctx.pre_window_handlers != null) {
        basicSingleInputStream.addStreamHandlers((List<StreamHandler>) visit(ctx.pre_window_handlers));
    }
    if (ctx.window() == null && ctx.post_window_handlers == null) {
        populateQueryContext(basicSingleInputStream, ctx);
        return basicSingleInputStream;
    } else if (ctx.window() != null) {
        SingleInputStream singleInputStream = new SingleInputStream(basicSingleInputStream, (Window) visit(ctx.window()));
        if (ctx.post_window_handlers != null) {
            singleInputStream.addStreamHandlers((List<StreamHandler>) visit(ctx.post_window_handlers));
        }
        populateQueryContext(singleInputStream, ctx);
        return singleInputStream;
    } else {
        throw newSiddhiParserException(ctx);
    }
}
Also used : Window(io.siddhi.query.api.execution.query.input.handler.Window) SingleInputStream(io.siddhi.query.api.execution.query.input.stream.SingleInputStream) BasicSingleInputStream(io.siddhi.query.api.execution.query.input.stream.BasicSingleInputStream) BasicSingleInputStream(io.siddhi.query.api.execution.query.input.stream.BasicSingleInputStream) StreamHandler(io.siddhi.query.api.execution.query.input.handler.StreamHandler) List(java.util.List) ArrayList(java.util.ArrayList)

Example 3 with Window

use of io.siddhi.query.api.execution.query.input.handler.Window in project siddhi by wso2.

the class SimpleQueryTestCase method testCreatingReturnFilterQueryWithExtension3.

@Test
public void testCreatingReturnFilterQueryWithExtension3() {
    Query query = Query.query();
    Window window1 = new Window("Foo");
    AssertJUnit.assertFalse(window1.equals("falsewindow"));
    query.from(InputStream.stream("StockStream").filter(Expression.and(Expression.compare(Expression.function("ext", "FooBarCond", Expression.value(7), Expression.value(9.5)), Compare.Operator.GREATER_THAN, Expression.variable("price")), Expression.function("ext", "BarCond", Expression.value(100), Expression.variable("volume")))).function("ext", "Foo", Expression.value(67), Expression.value(89)).window(window1));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("avgPrice", Expression.function("ext", "avg", Expression.variable("price"))));
}
Also used : Window(io.siddhi.query.api.execution.query.input.handler.Window) Query(io.siddhi.query.api.execution.query.Query) Test(org.testng.annotations.Test)

Example 4 with Window

use of io.siddhi.query.api.execution.query.input.handler.Window in project siddhi by wso2.

the class SimpleQueryTestCase method testCreatingReturnFilterQueryWithExtension2.

@Test
public void testCreatingReturnFilterQueryWithExtension2() {
    Query query = Query.query();
    Window window1 = new Window("ext", "Foo");
    query.from(InputStream.stream("StockStream").filter(Expression.and(Expression.compare(Expression.function("ext", "FooBarCond", Expression.value(7), Expression.value(9.5)), Compare.Operator.GREATER_THAN, Expression.variable("price")), Expression.function("ext", "BarCond", Expression.value(100), Expression.variable("volume")))).function("ext", "Foo", Expression.value(67), Expression.value(89)).window(window1));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("avgPrice", Expression.function("ext", "avg", Expression.variable("price"))));
}
Also used : Window(io.siddhi.query.api.execution.query.input.handler.Window) Query(io.siddhi.query.api.execution.query.Query) Test(org.testng.annotations.Test)

Example 5 with Window

use of io.siddhi.query.api.execution.query.input.handler.Window in project siddhi by wso2.

the class SingleInputStreamParser method parseInputStream.

/**
 * Parse single InputStream and return SingleStreamRuntime
 *
 * @param inputStream                 single input stream to be parsed
 * @param variableExpressionExecutors List to hold VariableExpressionExecutors to update after query parsing
 * @param streamDefinitionMap         Stream Definition Map
 * @param tableDefinitionMap          Table Definition Map
 * @param windowDefinitionMap         window definition map
 * @param aggregationDefinitionMap    aggregation definition map
 * @param tableMap                    Table Map
 * @param metaComplexEvent            MetaComplexEvent
 * @param processStreamReceiver       ProcessStreamReceiver
 * @param supportsBatchProcessing     supports batch processing
 * @param outputExpectsExpiredEvents  is expired events sent as output
 * @param findToBeExecuted            find will be executed in the steam stores
 * @param multiValue                  event has the possibility to produce multiple values
 * @param siddhiQueryContext          @return SingleStreamRuntime
 */
public static SingleStreamRuntime parseInputStream(SingleInputStream inputStream, List<VariableExpressionExecutor> variableExpressionExecutors, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, Map<String, Table> tableMap, MetaComplexEvent metaComplexEvent, ProcessStreamReceiver processStreamReceiver, boolean supportsBatchProcessing, boolean outputExpectsExpiredEvents, boolean findToBeExecuted, boolean multiValue, SiddhiQueryContext siddhiQueryContext) {
    Processor processor = null;
    EntryValveProcessor entryValveProcessor = null;
    ProcessingMode processingMode = ProcessingMode.BATCH;
    boolean first = true;
    MetaStreamEvent metaStreamEvent;
    if (metaComplexEvent instanceof MetaStateEvent) {
        metaStreamEvent = new MetaStreamEvent();
        ((MetaStateEvent) metaComplexEvent).addEvent(metaStreamEvent);
        initMetaStreamEvent(inputStream, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, multiValue, metaStreamEvent);
    } else {
        metaStreamEvent = (MetaStreamEvent) metaComplexEvent;
        initMetaStreamEvent(inputStream, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, multiValue, metaStreamEvent);
    }
    // A window cannot be defined for a window stream
    if (!inputStream.getStreamHandlers().isEmpty() && windowDefinitionMap != null && windowDefinitionMap.containsKey(inputStream.getStreamId())) {
        for (StreamHandler handler : inputStream.getStreamHandlers()) {
            if (handler instanceof Window) {
                throw new OperationNotSupportedException("Cannot create " + ((Window) handler).getName() + " " + "window for the window stream " + inputStream.getStreamId());
            }
        }
    }
    if (!inputStream.getStreamHandlers().isEmpty()) {
        for (StreamHandler handler : inputStream.getStreamHandlers()) {
            Processor currentProcessor = generateProcessor(handler, metaComplexEvent, variableExpressionExecutors, tableMap, supportsBatchProcessing, outputExpectsExpiredEvents, findToBeExecuted, siddhiQueryContext);
            if (currentProcessor instanceof SchedulingProcessor) {
                if (entryValveProcessor == null) {
                    entryValveProcessor = new EntryValveProcessor(siddhiQueryContext.getSiddhiAppContext());
                    if (first) {
                        processor = entryValveProcessor;
                        first = false;
                    } else {
                        processor.setToLast(entryValveProcessor);
                    }
                }
                Scheduler scheduler = SchedulerParser.parse(entryValveProcessor, siddhiQueryContext);
                ((SchedulingProcessor) currentProcessor).setScheduler(scheduler);
            }
            if (currentProcessor instanceof AbstractStreamProcessor) {
                processingMode = ProcessingMode.findUpdatedProcessingMode(processingMode, ((AbstractStreamProcessor) currentProcessor).getProcessingMode());
            }
            if (first) {
                processor = currentProcessor;
                first = false;
            } else {
                processor.setToLast(currentProcessor);
            }
        }
    }
    metaStreamEvent.initializeOnAfterWindowData();
    return new SingleStreamRuntime(processStreamReceiver, processor, processingMode, metaComplexEvent);
}
Also used : Window(io.siddhi.query.api.execution.query.input.handler.Window) OperationNotSupportedException(io.siddhi.core.exception.OperationNotSupportedException) FilterProcessor(io.siddhi.core.query.processor.filter.FilterProcessor) WindowProcessor(io.siddhi.core.query.processor.stream.window.WindowProcessor) EntryValveProcessor(io.siddhi.core.query.input.stream.single.EntryValveProcessor) AbstractStreamProcessor(io.siddhi.core.query.processor.stream.AbstractStreamProcessor) StreamProcessor(io.siddhi.core.query.processor.stream.StreamProcessor) StreamFunctionProcessor(io.siddhi.core.query.processor.stream.function.StreamFunctionProcessor) Processor(io.siddhi.core.query.processor.Processor) SchedulingProcessor(io.siddhi.core.query.processor.SchedulingProcessor) AbstractStreamProcessor(io.siddhi.core.query.processor.stream.AbstractStreamProcessor) Scheduler(io.siddhi.core.util.Scheduler) SingleStreamRuntime(io.siddhi.core.query.input.stream.single.SingleStreamRuntime) ProcessingMode(io.siddhi.core.query.processor.ProcessingMode) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) SchedulingProcessor(io.siddhi.core.query.processor.SchedulingProcessor) StreamHandler(io.siddhi.query.api.execution.query.input.handler.StreamHandler) EntryValveProcessor(io.siddhi.core.query.input.stream.single.EntryValveProcessor) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Aggregations

Window (io.siddhi.query.api.execution.query.input.handler.Window)7 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)2 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)2 FilterProcessor (io.siddhi.core.query.processor.filter.FilterProcessor)2 AbstractStreamProcessor (io.siddhi.core.query.processor.stream.AbstractStreamProcessor)2 WindowProcessor (io.siddhi.core.query.processor.stream.window.WindowProcessor)2 Attribute (io.siddhi.query.api.definition.Attribute)2 Query (io.siddhi.query.api.execution.query.Query)2 StreamHandler (io.siddhi.query.api.execution.query.input.handler.StreamHandler)2 AttributeFunction (io.siddhi.query.api.expression.AttributeFunction)2 Test (org.testng.annotations.Test)2 OperationNotSupportedException (io.siddhi.core.exception.OperationNotSupportedException)1 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)1 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)1 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)1 EntryValveProcessor (io.siddhi.core.query.input.stream.single.EntryValveProcessor)1 SingleStreamRuntime (io.siddhi.core.query.input.stream.single.SingleStreamRuntime)1 ProcessingMode (io.siddhi.core.query.processor.ProcessingMode)1 Processor (io.siddhi.core.query.processor.Processor)1 SchedulingProcessor (io.siddhi.core.query.processor.SchedulingProcessor)1