Search in sources :

Example 26 with ComplexEvent

use of org.wso2.siddhi.core.event.ComplexEvent in project siddhi by wso2.

the class QuerySelector method processGroupBy.

private ComplexEventChunk<ComplexEvent> processGroupBy(ComplexEventChunk complexEventChunk) {
    complexEventChunk.reset();
    ComplexEventChunk<ComplexEvent> currentComplexEventChunk = new ComplexEventChunk<ComplexEvent>(complexEventChunk.isBatch());
    synchronized (this) {
        int limitCount = 0;
        while (complexEventChunk.hasNext()) {
            ComplexEvent event = complexEventChunk.next();
            switch(event.getType()) {
                case CURRENT:
                case EXPIRED:
                    eventPopulator.populateStateEvent(event);
                    String groupedByKey = groupByKeyGenerator.constructEventKey(event);
                    GroupByAggregationAttributeExecutor.getKeyThreadLocal().set(groupedByKey);
                    for (AttributeProcessor attributeProcessor : attributeProcessorList) {
                        attributeProcessor.process(event);
                    }
                    if ((event.getType() == StreamEvent.Type.CURRENT && currentOn) || (event.getType() == StreamEvent.Type.EXPIRED && expiredOn)) {
                        if (!(havingConditionExecutor != null && !havingConditionExecutor.execute(event))) {
                            complexEventChunk.remove();
                            if (limit == SiddhiConstants.UNKNOWN_STATE) {
                                currentComplexEventChunk.add(new GroupedComplexEvent(groupedByKey, event));
                            } else {
                                if (limitCount < limit) {
                                    currentComplexEventChunk.add(new GroupedComplexEvent(groupedByKey, event));
                                    limitCount++;
                                }
                            }
                        }
                    }
                    GroupByAggregationAttributeExecutor.getKeyThreadLocal().remove();
                    break;
                case TIMER:
                    break;
                case RESET:
                    for (AttributeProcessor attributeProcessor : attributeProcessorList) {
                        attributeProcessor.process(event);
                    }
                    break;
            }
        }
    }
    if (isOrderBy) {
        orderEventChunk(complexEventChunk);
    }
    if (limit != SiddhiConstants.UNKNOWN_STATE) {
        limitEventChunk(complexEventChunk);
    }
    currentComplexEventChunk.reset();
    if (currentComplexEventChunk.hasNext()) {
        return currentComplexEventChunk;
    }
    return null;
}
Also used : GroupedComplexEvent(org.wso2.siddhi.core.event.GroupedComplexEvent) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) GroupedComplexEvent(org.wso2.siddhi.core.event.GroupedComplexEvent) AttributeProcessor(org.wso2.siddhi.core.query.selector.attribute.processor.AttributeProcessor)

Example 27 with ComplexEvent

use of org.wso2.siddhi.core.event.ComplexEvent in project siddhi by wso2.

the class QuerySelector method processNoGroupBy.

private ComplexEventChunk processNoGroupBy(ComplexEventChunk complexEventChunk) {
    complexEventChunk.reset();
    synchronized (this) {
        while (complexEventChunk.hasNext()) {
            ComplexEvent event = complexEventChunk.next();
            switch(event.getType()) {
                case CURRENT:
                case EXPIRED:
                    eventPopulator.populateStateEvent(event);
                    for (AttributeProcessor attributeProcessor : attributeProcessorList) {
                        attributeProcessor.process(event);
                    }
                    if (((event.getType() != StreamEvent.Type.CURRENT || !currentOn) && (event.getType() != StreamEvent.Type.EXPIRED || !expiredOn)) || ((havingConditionExecutor != null && !havingConditionExecutor.execute(event)))) {
                        complexEventChunk.remove();
                    }
                    break;
                case RESET:
                    for (AttributeProcessor attributeProcessor : attributeProcessorList) {
                        attributeProcessor.process(event);
                    }
                    break;
                case TIMER:
                    complexEventChunk.remove();
                    break;
            }
        }
    }
    if (isOrderBy) {
        orderEventChunk(complexEventChunk);
    }
    if (limit != SiddhiConstants.UNKNOWN_STATE) {
        limitEventChunk(complexEventChunk);
    }
    complexEventChunk.reset();
    if (complexEventChunk.hasNext()) {
        return complexEventChunk;
    }
    return null;
}
Also used : GroupedComplexEvent(org.wso2.siddhi.core.event.GroupedComplexEvent) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) AttributeProcessor(org.wso2.siddhi.core.query.selector.attribute.processor.AttributeProcessor)

Example 28 with ComplexEvent

use of org.wso2.siddhi.core.event.ComplexEvent in project siddhi by wso2.

the class SelectorParser method getAttributeProcessors.

/**
 * Method to construct AttributeProcessor list for the selector.
 *
 * @param selector                    Selector
 * @param id                          stream id
 * @param siddhiAppContext            siddhi app context
 * @param metaComplexEvent            meta ComplexEvent
 * @param tableMap                    Table Map
 * @param variableExpressionExecutors list of VariableExpressionExecutors
 * @param outputStream
 * @return list of AttributeProcessors
 */
private static List<AttributeProcessor> getAttributeProcessors(Selector selector, String id, SiddhiAppContext siddhiAppContext, MetaComplexEvent metaComplexEvent, Map<String, Table> tableMap, List<VariableExpressionExecutor> variableExpressionExecutors, OutputStream outputStream, String queryName, int metaPosition) {
    List<AttributeProcessor> attributeProcessorList = new ArrayList<>();
    StreamDefinition outputDefinition = StreamDefinition.id(id);
    outputDefinition.setQueryContextStartIndex(outputStream.getQueryContextStartIndex());
    outputDefinition.setQueryContextEndIndex(outputStream.getQueryContextEndIndex());
    List<OutputAttribute> outputAttributes = selector.getSelectionList();
    if (selector.getSelectionList().size() == 0) {
        if (metaComplexEvent instanceof MetaStreamEvent) {
            List<Attribute> attributeList = ((MetaStreamEvent) metaComplexEvent).getLastInputDefinition().getAttributeList();
            for (Attribute attribute : attributeList) {
                outputAttributes.add(new OutputAttribute(new Variable(attribute.getName())));
            }
        } else {
            int position = 0;
            for (MetaStreamEvent metaStreamEvent : ((MetaStateEvent) metaComplexEvent).getMetaStreamEvents()) {
                if (metaPosition == SiddhiConstants.UNKNOWN_STATE || metaPosition == position) {
                    List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
                    for (Attribute attribute : attributeList) {
                        OutputAttribute outputAttribute = new OutputAttribute(new Variable(attribute.getName()));
                        if (!outputAttributes.contains(outputAttribute)) {
                            outputAttributes.add(outputAttribute);
                        } else {
                            List<AbstractDefinition> definitions = new ArrayList<>();
                            for (MetaStreamEvent aMetaStreamEvent : ((MetaStateEvent) metaComplexEvent).getMetaStreamEvents()) {
                                definitions.add(aMetaStreamEvent.getLastInputDefinition());
                            }
                            throw new DuplicateAttributeException("Duplicate attribute exist in streams " + definitions, outputStream.getQueryContextStartIndex(), outputStream.getQueryContextEndIndex());
                        }
                    }
                }
                ++position;
            }
        }
    }
    int i = 0;
    for (OutputAttribute outputAttribute : outputAttributes) {
        ExpressionExecutor expressionExecutor = ExpressionParser.parseExpression(outputAttribute.getExpression(), metaComplexEvent, SiddhiConstants.UNKNOWN_STATE, tableMap, variableExpressionExecutors, siddhiAppContext, !(selector.getGroupByList().isEmpty()), 0, queryName);
        if (expressionExecutor instanceof VariableExpressionExecutor) {
            // for variables we will directly put
            // value at conversion stage
            VariableExpressionExecutor executor = ((VariableExpressionExecutor) expressionExecutor);
            if (metaComplexEvent instanceof MetaStateEvent) {
                ((MetaStateEvent) metaComplexEvent).addOutputDataAllowingDuplicate(new MetaStateEventAttribute(executor.getAttribute(), executor.getPosition()));
            } else {
                ((MetaStreamEvent) metaComplexEvent).addOutputDataAllowingDuplicate(executor.getAttribute());
            }
            outputDefinition.attribute(outputAttribute.getRename(), ((VariableExpressionExecutor) expressionExecutor).getAttribute().getType());
        } else {
            // To maintain output variable positions
            if (metaComplexEvent instanceof MetaStateEvent) {
                ((MetaStateEvent) metaComplexEvent).addOutputDataAllowingDuplicate(null);
            } else {
                ((MetaStreamEvent) metaComplexEvent).addOutputDataAllowingDuplicate(null);
            }
            AttributeProcessor attributeProcessor = new AttributeProcessor(expressionExecutor);
            attributeProcessor.setOutputPosition(i);
            attributeProcessorList.add(attributeProcessor);
            outputDefinition.attribute(outputAttribute.getRename(), attributeProcessor.getOutputType());
        }
        i++;
    }
    metaComplexEvent.setOutputDefinition(outputDefinition);
    return attributeProcessorList;
}
Also used : Variable(org.wso2.siddhi.query.api.expression.Variable) StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) ConditionExpressionExecutor(org.wso2.siddhi.core.executor.condition.ConditionExpressionExecutor) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor) Attribute(org.wso2.siddhi.query.api.definition.Attribute) MetaStateEventAttribute(org.wso2.siddhi.core.event.state.MetaStateEventAttribute) OutputAttribute(org.wso2.siddhi.query.api.execution.query.selection.OutputAttribute) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ArrayList(java.util.ArrayList) AbstractDefinition(org.wso2.siddhi.query.api.definition.AbstractDefinition) OutputAttribute(org.wso2.siddhi.query.api.execution.query.selection.OutputAttribute) DuplicateAttributeException(org.wso2.siddhi.query.api.exception.DuplicateAttributeException) MetaStateEvent(org.wso2.siddhi.core.event.state.MetaStateEvent) MetaStateEventAttribute(org.wso2.siddhi.core.event.state.MetaStateEventAttribute) AttributeProcessor(org.wso2.siddhi.core.query.selector.attribute.processor.AttributeProcessor) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent)

Example 29 with ComplexEvent

use of org.wso2.siddhi.core.event.ComplexEvent in project siddhi by wso2.

the class TestDebugger method testDebugger4.

@Test
public void testDebugger4() throws InterruptedException {
    log.info("Siddi Debugger Test 4: Test next traversal in a query with time batch window where next call delays" + " 1 sec");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int);";
    final String query = "@info(name = 'query1')" + "from cseEventStream#window.timeBatch(1 sec) " + "select symbol, price, volume " + "insert into OutputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            inEventCount.addAndGet(events.length);
            AssertJUnit.assertEquals("Cannot emit all three in one time", 1, events.length);
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    SiddhiDebugger siddhiDebugger = siddhiAppRuntime.debug();
    siddhiDebugger.acquireBreakPoint("query1", SiddhiDebugger.QueryTerminal.IN);
    siddhiDebugger.setDebuggerCallback(new SiddhiDebuggerCallback() {

        @Override
        public void debugEvent(ComplexEvent event, String queryName, SiddhiDebugger.QueryTerminal queryTerminal, SiddhiDebugger debugger) {
            log.info(event);
            int count = debugEventCount.addAndGet(getCount(event));
            if (count != 1 && queryTerminal == SiddhiDebugger.QueryTerminal.IN) {
                try {
                    Thread.sleep(1100);
                } catch (InterruptedException e) {
                }
            }
            // next call will not reach OUT since there is a window
            debugger.next();
        }
    });
    inputHandler.send(new Object[] { "WSO2", 50f, 60 });
    inputHandler.send(new Object[] { "WSO2", 70f, 40 });
    inputHandler.send(new Object[] { "WSO2", 60f, 50 });
    Thread.sleep(1500);
    AssertJUnit.assertEquals("Invalid number of output events", 3, inEventCount.get());
    AssertJUnit.assertEquals("Invalid number of debug events", 3, debugEventCount.get());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 30 with ComplexEvent

use of org.wso2.siddhi.core.event.ComplexEvent in project siddhi by wso2.

the class TestDebugger method testDebugger9.

@Test
public void testDebugger9() throws InterruptedException {
    log.info("Siddi Debugger Test 9: Test state traversal in a simple query");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "@config(async = 'true') define stream cseEventStream (symbol string, price float, " + "volume int);";
    final String query = "@info(name = 'query1')" + "from cseEventStream#window.length(3) " + "select symbol, price, sum(volume) as volume " + "insert into OutputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            inEventCount.addAndGet(events.length);
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    SiddhiDebugger siddhiDebugger = siddhiAppRuntime.debug();
    siddhiDebugger.acquireBreakPoint("query1", SiddhiDebugger.QueryTerminal.IN);
    siddhiDebugger.setDebuggerCallback(new SiddhiDebuggerCallback() {

        @Override
        public void debugEvent(ComplexEvent event, String queryName, SiddhiDebugger.QueryTerminal queryTerminal, SiddhiDebugger debugger) {
            log.info("Query: " + queryName + ":" + queryTerminal);
            log.info(event);
            int count = debugEventCount.addAndGet(getCount(event));
            if (count == 2) {
                Map<String, Object> queryState = debugger.getQueryState(queryName);
                log.info(queryState);
                log.info(queryState.values().toArray()[0]);
                StreamEvent streamEvent = null;
                // Order of the query state items is unpredictable
                for (Map.Entry<String, Object> entry : queryState.entrySet()) {
                    if (entry.getKey().startsWith("AbstractStreamProcessor")) {
                        streamEvent = (StreamEvent) ((Map<String, Object>) entry.getValue()).get("ExpiredEventChunk");
                        break;
                    }
                }
                AssertJUnit.assertArrayEquals(streamEvent.getOutputData(), new Object[] { "WSO2", 50.0f, null });
            }
            debugger.next();
        }
    });
    inputHandler.send(new Object[] { "WSO2", 50f, 60 });
    inputHandler.send(new Object[] { "WSO2", 70f, 40 });
    Thread.sleep(100);
    AssertJUnit.assertEquals("Invalid number of output events", 2, inEventCount.get());
    AssertJUnit.assertEquals("Invalid number of debug events", 4, debugEventCount.get());
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) ComplexEvent(org.wso2.siddhi.core.event.ComplexEvent) Event(org.wso2.siddhi.core.event.Event) Map(java.util.Map) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Aggregations

ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)59 ComplexEventChunk (org.wso2.siddhi.core.event.ComplexEventChunk)29 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)26 ArrayList (java.util.ArrayList)21 GroupedComplexEvent (org.wso2.siddhi.core.event.GroupedComplexEvent)17 Event (org.wso2.siddhi.core.event.Event)16 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)13 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)13 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)13 Test (org.testng.annotations.Test)12 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)12 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)10 AttributeProcessor (org.wso2.siddhi.core.query.selector.attribute.processor.AttributeProcessor)5 Map (java.util.Map)4 StateEvent (org.wso2.siddhi.core.event.state.StateEvent)4 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)3 Iterator (java.util.Iterator)2 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)2 Attribute (org.wso2.siddhi.query.api.definition.Attribute)2 HashMap (java.util.HashMap)1