Search in sources :

Example 11 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class SingleInputStreamParser method initMetaStreamEvent.

/**
 * Method to generate MetaStreamEvent reagent to the given input stream. Empty definition will be created and
 * definition and reference is will be set accordingly in this method.
 *
 * @param inputStream              InputStream
 * @param streamDefinitionMap      StreamDefinition Map
 * @param tableDefinitionMap       TableDefinition Map
 * @param aggregationDefinitionMap AggregationDefinition Map
 * @param metaStreamEvent          MetaStreamEvent
 */
private static void initMetaStreamEvent(SingleInputStream inputStream, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, MetaStreamEvent metaStreamEvent) {
    String streamId = inputStream.getStreamId();
    if (!inputStream.isInnerStream() && windowDefinitionMap != null && windowDefinitionMap.containsKey(streamId)) {
        AbstractDefinition inputDefinition = windowDefinitionMap.get(streamId);
        if (!metaStreamEvent.getInputDefinitions().contains(inputDefinition)) {
            metaStreamEvent.addInputDefinition(inputDefinition);
        }
    } else if (streamDefinitionMap != null && streamDefinitionMap.containsKey(streamId)) {
        AbstractDefinition inputDefinition = streamDefinitionMap.get(streamId);
        metaStreamEvent.addInputDefinition(inputDefinition);
    } else if (!inputStream.isInnerStream() && tableDefinitionMap != null && tableDefinitionMap.containsKey(streamId)) {
        AbstractDefinition inputDefinition = tableDefinitionMap.get(streamId);
        metaStreamEvent.addInputDefinition(inputDefinition);
    } else if (!inputStream.isInnerStream() && aggregationDefinitionMap != null && aggregationDefinitionMap.containsKey(streamId)) {
        AbstractDefinition inputDefinition = aggregationDefinitionMap.get(streamId);
        metaStreamEvent.addInputDefinition(inputDefinition);
    } else {
        throw new SiddhiAppCreationException("Stream/table/window/aggregation definition with ID '" + inputStream.getStreamId() + "' has not been defined", inputStream.getQueryContextStartIndex(), inputStream.getQueryContextEndIndex());
    }
    if ((inputStream.getStreamReferenceId() != null) && !(inputStream.getStreamId()).equals(inputStream.getStreamReferenceId())) {
        // if ref id is provided
        metaStreamEvent.setInputReferenceId(inputStream.getStreamReferenceId());
    }
}
Also used : SiddhiAppCreationException(org.wso2.siddhi.core.exception.SiddhiAppCreationException) AbstractDefinition(org.wso2.siddhi.query.api.definition.AbstractDefinition)

Example 12 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class StoreQueryParser method parse.

/**
 * Parse a storeQuery and return corresponding StoreQueryRuntime.
 *
 * @param storeQuery       storeQuery to be parsed.
 * @param siddhiAppContext associated Siddhi app context.
 * @param tableMap         keyvalue containing tables.
 * @param windowMap        keyvalue containing windows.
 * @param aggregationMap   keyvalue containing aggregation runtimes.
 * @return StoreQueryRuntime
 */
public static StoreQueryRuntime parse(StoreQuery storeQuery, SiddhiAppContext siddhiAppContext, Map<String, Table> tableMap, Map<String, Window> windowMap, Map<String, AggregationRuntime> aggregationMap) {
    String queryName = "store_query_" + storeQuery.getInputStore().getStoreId();
    InputStore inputStore = storeQuery.getInputStore();
    int metaPosition = SiddhiConstants.UNKNOWN_STATE;
    Within within = null;
    Expression per = null;
    try {
        SnapshotService.getSkipSnapshotableThreadLocal().set(true);
        Expression onCondition = Expression.value(true);
        MetaStreamEvent metaStreamEvent = new MetaStreamEvent();
        metaStreamEvent.setInputReferenceId(inputStore.getStoreReferenceId());
        if (inputStore instanceof AggregationInputStore) {
            AggregationInputStore aggregationInputStore = (AggregationInputStore) inputStore;
            if (aggregationMap.get(inputStore.getStoreId()) == null) {
                throw new StoreQueryCreationException("Aggregation \"" + inputStore.getStoreId() + "\" has not been defined");
            }
            if (aggregationInputStore.getPer() != null && aggregationInputStore.getWithin() != null) {
                within = aggregationInputStore.getWithin();
                per = aggregationInputStore.getPer();
            } else if (aggregationInputStore.getPer() != null || aggregationInputStore.getWithin() != null) {
                throw new StoreQueryCreationException(inputStore.getStoreId() + " should either have both 'within' and 'per' defined or none.");
            }
            if (((AggregationInputStore) inputStore).getOnCondition() != null) {
                onCondition = ((AggregationInputStore) inputStore).getOnCondition();
            }
        } else if (inputStore instanceof ConditionInputStore) {
            if (((ConditionInputStore) inputStore).getOnCondition() != null) {
                onCondition = ((ConditionInputStore) inputStore).getOnCondition();
            }
        }
        List<VariableExpressionExecutor> variableExpressionExecutors = new ArrayList<>();
        Table table = tableMap.get(inputStore.getStoreId());
        if (table != null) {
            return constructStoreQueryRuntime(table, storeQuery, siddhiAppContext, tableMap, queryName, metaPosition, onCondition, metaStreamEvent, variableExpressionExecutors);
        } else {
            AggregationRuntime aggregation = aggregationMap.get(inputStore.getStoreId());
            if (aggregation != null) {
                return constructStoreQueryRuntime(aggregation, storeQuery, siddhiAppContext, tableMap, queryName, within, per, onCondition, metaStreamEvent, variableExpressionExecutors);
            } else {
                Window window = windowMap.get(inputStore.getStoreId());
                if (window != null) {
                    return constructStoreQueryRuntime(window, storeQuery, siddhiAppContext, tableMap, queryName, metaPosition, onCondition, metaStreamEvent, variableExpressionExecutors);
                } else {
                    throw new StoreQueryCreationException(inputStore.getStoreId() + " is neither a table, aggregation or window");
                }
            }
        }
    } finally {
        SnapshotService.getSkipSnapshotableThreadLocal().set(null);
    }
}
Also used : Window(org.wso2.siddhi.core.window.Window) Table(org.wso2.siddhi.core.table.Table) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ArrayList(java.util.ArrayList) AggregationInputStore(org.wso2.siddhi.query.api.execution.query.input.store.AggregationInputStore) Expression(org.wso2.siddhi.query.api.expression.Expression) ConditionInputStore(org.wso2.siddhi.query.api.execution.query.input.store.ConditionInputStore) AggregationInputStore(org.wso2.siddhi.query.api.execution.query.input.store.AggregationInputStore) InputStore(org.wso2.siddhi.query.api.execution.query.input.store.InputStore) ConditionInputStore(org.wso2.siddhi.query.api.execution.query.input.store.ConditionInputStore) Within(org.wso2.siddhi.query.api.aggregation.Within) StoreQueryCreationException(org.wso2.siddhi.core.exception.StoreQueryCreationException) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) AggregationRuntime(org.wso2.siddhi.core.aggregation.AggregationRuntime)

Example 13 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class StoreQueryParser method constructStoreQueryRuntime.

private static StoreQueryRuntime constructStoreQueryRuntime(Window window, StoreQuery storeQuery, SiddhiAppContext siddhiAppContext, Map<String, Table> tableMap, String queryName, int metaPosition, Expression onCondition, MetaStreamEvent metaStreamEvent, List<VariableExpressionExecutor> variableExpressionExecutors) {
    metaStreamEvent.setEventType(EventType.WINDOW);
    initMetaStreamEvent(metaStreamEvent, window.getWindowDefinition());
    MatchingMetaInfoHolder metaStreamInfoHolder = generateMatchingMetaInfoHolder(metaStreamEvent, window.getWindowDefinition());
    CompiledCondition compiledCondition = window.compileCondition(onCondition, generateMatchingMetaInfoHolder(metaStreamEvent, window.getWindowDefinition()), siddhiAppContext, variableExpressionExecutors, tableMap, queryName);
    FindStoreQueryRuntime findStoreQueryRuntime = new FindStoreQueryRuntime(window, compiledCondition, queryName, metaStreamEvent);
    populateFindStoreQueryRuntime(findStoreQueryRuntime, metaStreamInfoHolder, storeQuery.getSelector(), variableExpressionExecutors, siddhiAppContext, tableMap, queryName, metaPosition);
    return findStoreQueryRuntime;
}
Also used : CompiledCondition(org.wso2.siddhi.core.util.collection.operator.CompiledCondition) MatchingMetaInfoHolder(org.wso2.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) FindStoreQueryRuntime(org.wso2.siddhi.core.query.FindStoreQueryRuntime)

Example 14 with Window

use of org.wso2.siddhi.core.window.Window in project siddhi by wso2.

the class DefinitionParserHelper method validateDefinition.

public static void validateDefinition(AbstractDefinition definition, ConcurrentMap<String, AbstractDefinition> streamDefinitionMap, ConcurrentMap<String, AbstractDefinition> tableDefinitionMap, ConcurrentMap<String, AbstractDefinition> windowDefinitionMap, ConcurrentMap<String, AbstractDefinition> aggregationDefinitionMap) {
    AbstractDefinition existingTableDefinition = tableDefinitionMap.get(definition.getId());
    if (existingTableDefinition != null && (!existingTableDefinition.equals(definition) || definition instanceof StreamDefinition)) {
        throw new DuplicateDefinitionException("Table Definition with same Stream Id '" + definition.getId() + "' already exist : " + existingTableDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
    AbstractDefinition existingStreamDefinition = streamDefinitionMap.get(definition.getId());
    if (existingStreamDefinition != null && (!existingStreamDefinition.equals(definition) || definition instanceof TableDefinition)) {
        throw new DuplicateDefinitionException("Stream Definition with same Stream Id '" + definition.getId() + "' already exist : " + existingStreamDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
    AbstractDefinition existingWindowDefinition = windowDefinitionMap.get(definition.getId());
    if (existingWindowDefinition != null && (!existingWindowDefinition.equals(definition) || definition instanceof WindowDefinition)) {
        throw new DuplicateDefinitionException("Window Definition with same Window Id '" + definition.getId() + "' already exist : " + existingWindowDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
    AbstractDefinition existingAggregationDefinition = aggregationDefinitionMap.get(definition.getId());
    if (existingAggregationDefinition != null && (!existingAggregationDefinition.equals(definition) || definition instanceof AggregationDefinition)) {
        throw new DuplicateDefinitionException("Aggregation Definition with same Aggregation Id '" + definition.getId() + "' already exist : " + existingWindowDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
}
Also used : StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) DuplicateDefinitionException(org.wso2.siddhi.query.api.exception.DuplicateDefinitionException) AggregationDefinition(org.wso2.siddhi.query.api.definition.AggregationDefinition) AbstractDefinition(org.wso2.siddhi.query.api.definition.AbstractDefinition) TableDefinition(org.wso2.siddhi.query.api.definition.TableDefinition) WindowDefinition(org.wso2.siddhi.query.api.definition.WindowDefinition)

Example 15 with Window

use of org.wso2.siddhi.core.window.Window 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)

Aggregations

Test (org.testng.annotations.Test)161 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)130 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)130 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)118 Event (org.wso2.siddhi.core.event.Event)117 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)82 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)35 Query (org.wso2.siddhi.query.api.execution.query.Query)32 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)14 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)12 ArrayList (java.util.ArrayList)10 InMemoryPersistenceStore (org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore)10 PersistenceStore (org.wso2.siddhi.core.util.persistence.PersistenceStore)10 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)9 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)9 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)8 SiddhiAppValidationException (org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)8 Window (org.wso2.siddhi.core.window.Window)7 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)6 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)6