Search in sources :

Example 31 with Window

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

the class TimeLengthWindowTestCase method timeLengthWindowTest4.

/*
           Time Period > Window time
           Number of Events > Window length
    */
@Test
public void timeLengthWindowTest4() throws InterruptedException {
    log.info("Testing timeLength window with no of events greater than window length and time period greater than" + " window time");
    SiddhiManager siddhiManager = new SiddhiManager();
    String sensorStream = "define stream sensorStream (id string, sensorValue float);";
    String query = "@info(name = 'query1') from sensorStream#window.timeLength(2 sec,4)" + " select id,sensorValue" + " insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(sensorStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
            }
            if (removeEvents != null) {
                AssertJUnit.assertTrue("InEvents arrived before RemoveEvents", inEventCount > removeEventCount);
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("sensorStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "id1", 10d });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "id2", 20d });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "id3", 30d });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "id4", 40d });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "id5", 50d });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "id6", 60d });
    Thread.sleep(2100);
    AssertJUnit.assertEquals(6, inEventCount);
    AssertJUnit.assertEquals(6, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 32 with Window

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

the class TimeLengthWindowTestCase method timeLengthWindowTest3.

/*
        Time Period < Window time
        Number of Events > Window length
    */
@Test
public void timeLengthWindowTest3() throws InterruptedException {
    log.info("Testing timeLength window with no of events greater than window length and time period less than " + "window time");
    SiddhiManager siddhiManager = new SiddhiManager();
    String sensorStream = "define stream sensorStream (id string, sensorValue float);";
    String query = "@info(name = 'query1') from sensorStream#window.timeLength(10 sec,4)" + " select id,sensorValue" + " insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(sensorStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
            }
            if (removeEvents != null) {
                AssertJUnit.assertTrue("InEvents arrived before RemoveEvents", inEventCount > removeEventCount);
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("sensorStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "id1", 10d });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "id2", 20d });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "id3", 30d });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "id4", 40d });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "id5", 50d });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "id6", 60d });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "id7", 70d });
    Thread.sleep(500);
    inputHandler.send(new Object[] { "id8", 80d });
    Thread.sleep(2000);
    AssertJUnit.assertEquals(8, inEventCount);
    AssertJUnit.assertEquals(4, removeEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 33 with Window

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

the class CronWindowTestCase method testCronWindow1.

@Test
public void testCronWindow1() throws InterruptedException {
    log.info("Testing cron window for current events");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int); " + "define window cseEventWindow (symbol string, price float, volume int) cron('*/5 * * * * ?'); ";
    String query = "@info(name = 'query0') " + "from cseEventStream " + "insert into cseEventWindow; " + "" + "@info(name = 'query1') from cseEventWindow " + "select symbol,price,volume " + "insert 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) {
                if (event.isExpired()) {
                    removeEventCount++;
                } else {
                    inEventCount++;
                }
            }
            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 });
    Thread.sleep(7000);
    AssertJUnit.assertEquals(6, inEventCount);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 34 with Window

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

the class CustomJoinWindowTestCase method testMultipleStreamsToWindow.

@Test
public void testMultipleStreamsToWindow() throws InterruptedException {
    log.info("Test sending events from multiple streams into a single window");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream Stream1 (symbol string, price float, volume long); " + "define stream Stream2 (symbol string, price float, volume long); " + "define stream Stream3 (symbol string, price float, volume long); " + "define stream Stream4 (symbol string, price float, volume long); " + "define stream Stream5 (symbol string, price float, volume long); " + "define stream Stream6 (symbol string, price float, volume long); " + "define window StockWindow (symbol string, price float, volume long) lengthBatch(5); ";
    String query = "" + "@info(name = 'query0') " + "from Stream1 " + "insert into StockWindow; " + "from Stream2 " + "insert into StockWindow; " + "from Stream3 " + "insert into StockWindow; " + "from Stream4 " + "insert into StockWindow; " + "from Stream5 " + "insert into StockWindow; " + "from Stream6 " + "insert into StockWindow; " + "" + "@info(name = 'query1') " + "from StockWindow " + "select symbol, sum(price) as totalPrice, sum(volume) as volumes " + "insert into OutputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            assertEquals("Invalid number of output events", 1, events.length);
            assertArrayEquals(new Object[] { "WSO2", 150.0, 5L }, events[0].getData());
        }
    });
    siddhiAppRuntime.start();
    for (int i = 1; i <= 6; i++) {
        siddhiAppRuntime.getInputHandler("Stream" + i).send(new Object[] { "WSO2", (i * 10.0f), 1L });
    }
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 35 with Window

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

the class CustomJoinWindowTestCase method testMultipleStreamsFromWindow.

@Test
public void testMultipleStreamsFromWindow() throws InterruptedException {
    log.info("Test joining a single window by multiple streams");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockInputStream (symbol string, price float, volume long); " + "define stream SummaryOfCompanyTriggerStream (symbol string); " + "define stream VolumeGreaterThanTriggerStream (volume long); " + "define window StockWindow (symbol string, price float, volume long) lengthBatch(10); ";
    String query = "" + "@info(name = 'query0') " + "from StockInputStream " + "insert into StockWindow; " + "@info(name = 'query1') " + "from StockWindow join SummaryOfCompanyTriggerStream#window.length(1) " + "on StockWindow.symbol == SummaryOfCompanyTriggerStream.symbol " + "select StockWindow.symbol, sum(StockWindow.price) as totalPrice, sum(StockWindow.volume) as volumes " + "insert into SummaryOfCompanyOutputStream; " + "" + "@info(name = 'query2') " + "from StockWindow join VolumeGreaterThanTriggerStream#window.length(1) " + "on StockWindow.volume > VolumeGreaterThanTriggerStream.volume " + "select StockWindow.symbol, StockWindow.price, StockWindow.volume as volume " + "insert into VolumeGreaterThanOutputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("SummaryOfCompanyOutputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            Object[] data = events[0].getData();
            if ("WSO2".equals(data[0])) {
                assertArrayEquals(new Object[] { "WSO2", 430.0, 57L }, data);
            } else if ("IBM".equals(data[0])) {
                assertArrayEquals(new Object[] { "IBM", 222.0, 16L }, data);
            }
        }
    });
    siddhiAppRuntime.addCallback("VolumeGreaterThanOutputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            assertEquals("Error in number of events with volume > 6", 6, events.length);
        }
    });
    siddhiAppRuntime.start();
    InputHandler stockInputStreamInputHandler = siddhiAppRuntime.getInputHandler("StockInputStream");
    InputHandler summaryOfCompanyTriggerStreamInputHandler = siddhiAppRuntime.getInputHandler("SummaryOfCompanyTriggerStream");
    InputHandler volumeGreaterThanTriggerStreamInputHandler = siddhiAppRuntime.getInputHandler("VolumeGreaterThanTriggerStream");
    // 10 inputs
    stockInputStreamInputHandler.send(new Object[] { "WSO2", 84.0f, 20L });
    stockInputStreamInputHandler.send(new Object[] { "IBM", 90.0f, 1L });
    stockInputStreamInputHandler.send(new Object[] { "WSO2", 55.0f, 5L });
    stockInputStreamInputHandler.send(new Object[] { "WSO2", 77.0f, 10L });
    stockInputStreamInputHandler.send(new Object[] { "IBM", 46.0f, 5L });
    stockInputStreamInputHandler.send(new Object[] { "WSO2", 36.0f, 2L });
    stockInputStreamInputHandler.send(new Object[] { "WSO2", 56.0f, 6L });
    stockInputStreamInputHandler.send(new Object[] { "IBM", 86.0f, 10L });
    stockInputStreamInputHandler.send(new Object[] { "WSO2", 26.0f, 6L });
    stockInputStreamInputHandler.send(new Object[] { "WSO2", 96.0f, 8L });
    // out of 10
    stockInputStreamInputHandler.send(new Object[] { "WSO2", 56.0f, 10L });
    stockInputStreamInputHandler.send(new Object[] { "IBM", 36.0f, 15L });
    Thread.sleep(100);
    summaryOfCompanyTriggerStreamInputHandler.send(new Object[] { "WSO2" });
    summaryOfCompanyTriggerStreamInputHandler.send(new Object[] { "IBM" });
    volumeGreaterThanTriggerStreamInputHandler.send(new Object[] { 5L });
    Thread.sleep(500);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) 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