Search in sources :

Example 1 with Event

use of io.siddhi.core.event.Event in project siddhi by wso2.

the class UpdateOrInsertTestStoreTestCase method updateOrInsertTableTest10.

@Test
public void updateOrInsertTableTest10() throws InterruptedException, SQLException {
    log.info("updateOrInsertTableTest10");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream CheckStockStream (symbol string, volume long, price float); " + "define stream UpdateStockStream (comp string, vol long); " + "@Store(type=\"testStoreContainingInMemoryTable\")\n" + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from UpdateStockStream left outer join StockTable " + "   on UpdateStockStream.comp == StockTable.symbol " + "select comp as symbol, ifThenElse(price is nulL,5F,price) as price, vol as volume " + "update or insert into StockTable " + "   on StockTable.symbol==symbol;" + "" + "@info(name = 'query3') " + "from CheckStockStream[(symbol==StockTable.symbol and volume == StockTable.volume and " + "price < StockTable.price) in StockTable] " + "insert into OutStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query3", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
                for (Event event : inEvents) {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[] { "IBM", 200L, 0f }, event.getData());
                            break;
                        case 2:
                            AssertJUnit.assertArrayEquals(new Object[] { "WSO2", 300L, 4.6f }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(2, inEventCount);
                    }
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
    InputHandler updateStockStream = siddhiAppRuntime.getInputHandler("UpdateStockStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6F, 100L });
    checkStockStream.send(new Object[] { "IBM", 100L, 155.6f });
    checkStockStream.send(new Object[] { "WSO2", 100L, 155.6f });
    updateStockStream.send(new Object[] { "IBM", 200L });
    updateStockStream.send(new Object[] { "WSO2", 300L });
    checkStockStream.send(new Object[] { "IBM", 200L, 0f });
    checkStockStream.send(new Object[] { "WSO2", 300L, 4.6f });
    Thread.sleep(1000);
    AssertJUnit.assertEquals("Number of success events", 2, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 2 with Event

use of io.siddhi.core.event.Event in project siddhi by wso2.

the class UpdateOrInsertTestStoreTestCase method updateOrInsertTableTest7.

@Test
public void updateOrInsertTableTest7() throws InterruptedException, SQLException {
    log.info("updateOrInsertTableTest7");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream CheckStockStream (symbol string, volume long, price float); " + "define stream UpdateStockStream (comp string, vol long); " + "@Store(type=\"testStoreContainingInMemoryTable\")\n" + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from UpdateStockStream " + "select comp as symbol,  5f as price, vol as volume " + "update or insert into StockTable " + "   on StockTable.symbol==symbol;" + "" + "@info(name = 'query3') " + "from CheckStockStream[(symbol==StockTable.symbol and volume==StockTable.volume and price " + "< StockTable.price) in StockTable] " + "insert into OutStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query3", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
                for (Event event : inEvents) {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[] { "IBM", 100L, 56.6f }, event.getData());
                            break;
                        case 2:
                            AssertJUnit.assertArrayEquals(new Object[] { "IBM", 200L, 0f }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(2, inEventCount);
                    }
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
    InputHandler updateStockStream = siddhiAppRuntime.getInputHandler("UpdateStockStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "WSO2", 55.6F, 100L });
    stockStream.send(new Object[] { "IBM", 155.6F, 100L });
    checkStockStream.send(new Object[] { "IBM", 100L, 56.6f });
    checkStockStream.send(new Object[] { "WSO2", 100L, 155.6f });
    updateStockStream.send(new Object[] { "IBM", 200L });
    checkStockStream.send(new Object[] { "IBM", 200L, 0f });
    checkStockStream.send(new Object[] { "WSO2", 100L, 155.6f });
    Thread.sleep(2000);
    AssertJUnit.assertEquals("Number of success events", 2, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 3 with Event

use of io.siddhi.core.event.Event in project siddhi by wso2.

the class TestStoreContainingInMemoryTable method query.

@Override
protected RecordIterator<Object[]> query(Map<String, Object> parameterMap, CompiledCondition compiledCondition, CompiledSelection compiledSelection, Attribute[] outputAttributes) {
    StreamEvent outEvent = inMemoryTable.find(compiledCondition, findMatchingEvent);
    List<Object[]> objects = new LinkedList<>();
    CompiledSelectionWithCache compiledSelectionWithCache = null;
    if (outEvent != null) {
        compiledSelectionWithCache = (CompiledSelectionWithCache) compiledSelection;
        StateEventFactory stateEventFactory = new StateEventFactory(compiledSelectionWithCache.getMetaStateEvent());
        Event[] cacheResultsAfterSelection = executeSelector(stateEventFactory, null, outEvent, compiledSelectionWithCache.getStoreEventIndex(), compiledSelectionWithCache.getQuerySelector());
        if (compiledSelectionWithCache.getQuerySelector() != null & compiledSelectionWithCache.getQuerySelector().getAttributeProcessorList().size() != 0) {
            compiledSelectionWithCache.getQuerySelector().process(generateResetComplexEventChunk(outEvent.getOutputData().length, stateEventFactory));
        }
        if (cacheResultsAfterSelection != null) {
            for (Event event : cacheResultsAfterSelection) {
                objects.add(event.getData());
            }
        }
    }
    return new TestStoreWithCacheIterator(objects.iterator());
}
Also used : StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) StateEventFactory(io.siddhi.core.event.state.StateEventFactory) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) Event(io.siddhi.core.event.Event) ComplexEvent(io.siddhi.core.event.ComplexEvent) StateEvent(io.siddhi.core.event.state.StateEvent) LinkedList(java.util.LinkedList)

Example 4 with Event

use of io.siddhi.core.event.Event in project siddhi by wso2.

the class TestStoreForCacheMiss method query.

@Override
protected RecordIterator<Object[]> query(Map<String, Object> parameterMap, CompiledCondition compiledCondition, CompiledSelection compiledSelection, Attribute[] outputAttributes) {
    StreamEvent outEvent = inMemoryTable.find(compiledCondition, findMatchingEvent);
    List<Object[]> objects = new LinkedList<>();
    CompiledSelectionWithCache compiledSelectionWithCache = null;
    if (outEvent != null) {
        compiledSelectionWithCache = (CompiledSelectionWithCache) compiledSelection;
        StateEventFactory stateEventFactory = new StateEventFactory(compiledSelectionWithCache.getMetaStateEvent());
        Event[] cacheResultsAfterSelection = executeSelector(stateEventFactory, null, outEvent, compiledSelectionWithCache.getStoreEventIndex(), compiledSelectionWithCache.getQuerySelector());
        if (compiledSelectionWithCache.getQuerySelector() != null & compiledSelectionWithCache.getQuerySelector().getAttributeProcessorList().size() != 0) {
            compiledSelectionWithCache.getQuerySelector().process(generateResetComplexEventChunk(outEvent.getOutputData().length, stateEventFactory));
        }
        if (cacheResultsAfterSelection != null) {
            for (Event event : cacheResultsAfterSelection) {
                objects.add(event.getData());
            }
        }
    }
    return new TestStoreWithCacheIterator(objects.iterator());
}
Also used : StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) StateEventFactory(io.siddhi.core.event.state.StateEventFactory) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) Event(io.siddhi.core.event.Event) ComplexEvent(io.siddhi.core.event.ComplexEvent) StateEvent(io.siddhi.core.event.state.StateEvent) LinkedList(java.util.LinkedList)

Example 5 with Event

use of io.siddhi.core.event.Event in project siddhi by wso2.

the class CronWindowTestCase method cronWindowTest2.

@Test
public void cronWindowTest2() throws InterruptedException {
    log.info("Testing cron window for expired events");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "@info(name = 'query1') " + "from cseEventStream#window.cron('*/5 * * * * ?') " + "select symbol,price,volume " + "insert expired events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                removeEventCount++;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 700f, 0 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 1 });
    Thread.sleep(7000);
    inputHandler.send(new Object[] { "IBM1", 700f, 0 });
    inputHandler.send(new Object[] { "WSO22", 60.5f, 1 });
    Thread.sleep(7000);
    inputHandler.send(new Object[] { "IBM43", 700f, 0 });
    inputHandler.send(new Object[] { "WSO4343", 60.5f, 1 });
    SiddhiTestHelper.waitForEvents(7000, 4, removeEventCount, 20000);
    AssertJUnit.assertEquals(4, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

Event (io.siddhi.core.event.Event)1298 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)1257 SiddhiManager (io.siddhi.core.SiddhiManager)1255 Test (org.testng.annotations.Test)1251 InputHandler (io.siddhi.core.stream.input.InputHandler)1216 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)818 StreamCallback (io.siddhi.core.stream.output.StreamCallback)303 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)86 SiddhiApp (io.siddhi.query.api.SiddhiApp)82 Query (io.siddhi.query.api.execution.query.Query)81 ArrayList (java.util.ArrayList)80 Logger (org.apache.logging.log4j.core.Logger)52 StreamEvent (io.siddhi.core.event.stream.StreamEvent)38 TestAppenderToValidateLogsForCachingTests (io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests)34 ComplexEvent (io.siddhi.core.event.ComplexEvent)29 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)29 UnitTestAppender (io.siddhi.core.UnitTestAppender)18 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)17 HashMap (java.util.HashMap)15 InMemoryPersistenceStore (io.siddhi.core.util.persistence.InMemoryPersistenceStore)14