Search in sources :

Example 76 with StreamCallback

use of org.wso2.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class SequenceTestCase method testTimeBatchAndSequence.

@Test
public void testTimeBatchAndSequence() throws Exception {
    log.info("testTimeBatchAndSequence  OUT 1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream received_reclamations (timestamp long, product_id string, defect_category string);";
    String query = "" + "@info(name = 'query1') " + "from received_reclamations#window.timeBatch(1 sec) " + "select product_id, defect_category, count(product_id) as num " + "group by product_id, defect_category " + "insert into reclamation_averages;" + "" + "@info(name = 'query2') " + "from a=reclamation_averages[num > 1], b=reclamation_averages[num > a.num and product_id == a" + ".product_id and defect_category == a.defect_category] " + "select a.product_id, a.defect_category, a.num as oldNum, b.num as newNum " + "insert into increased_reclamations;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("increased_reclamations", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            if (events != null) {
                for (Event event : events) {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            String product = (String) event.getData()[0];
                            String defectCategory = (String) event.getData()[1];
                            long oldNum = (Long) event.getData()[2];
                            long newNum = (Long) event.getData()[3];
                            AssertJUnit.assertTrue(product.equals("abc"));
                            AssertJUnit.assertTrue(defectCategory.equals("123"));
                            AssertJUnit.assertTrue(oldNum < newNum);
                            break;
                        default:
                            AssertJUnit.assertSame(1, inEventCount);
                    }
                }
            }
            eventArrived = true;
        }
    });
    InputHandler i1 = siddhiAppRuntime.getInputHandler("received_reclamations");
    siddhiAppRuntime.start();
    for (int i = 0; i < 5; i++) {
        i1.send(new Object[] { System.currentTimeMillis(), "abc", "123" });
        Thread.sleep(100);
    }
    Thread.sleep(500);
    for (int i = 0; i < 8; i++) {
        i1.send(new Object[] { System.currentTimeMillis(), "abc", "123" });
        Thread.sleep(100);
    }
    Thread.sleep(1000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
}
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 77 with StreamCallback

use of org.wso2.siddhi.core.stream.output.StreamCallback 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 78 with StreamCallback

use of org.wso2.siddhi.core.stream.output.StreamCallback 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 79 with StreamCallback

use of org.wso2.siddhi.core.stream.output.StreamCallback 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)

Example 80 with StreamCallback

use of org.wso2.siddhi.core.stream.output.StreamCallback in project siddhi by wso2.

the class CustomJoinWindowTestCase method testGroupByUseCase.

@Test
public void testGroupByUseCase() throws InterruptedException {
    log.info("Test joining a single window by multiple streams");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream SensorStream (name string, value float, roomNo int, deviceID string); " + "define window SensorWindow (name string, value float, roomNo int, deviceID string) timeBatch(1 " + "second); ";
    String query = "" + "@info(name = 'query0') " + "from SensorStream " + "insert into SensorWindow; " + "@info(name = 'query1') " + "from SensorWindow  " + "select name, max(value) as maxValue, roomNo " + "group by name, roomNo " + "insert into MaxSensorReadingPerRoomStream; " + "@info(name = 'query2') " + "from SensorWindow  " + "select name, max(value) as maxValue " + "group by name " + "insert into OverallMaxSensorReadingStream; " + "@info(name = 'query3') " + "from SensorWindow  " + "select name, avg(value) as avgValue " + "group by name " + "insert into OverallAverageSensorReadingStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("MaxSensorReadingPerRoomStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            System.out.print("MaxSensorReadingPerRoomStream: ");
            EventPrinter.print(events);
        }
    });
    siddhiAppRuntime.addCallback("OverallMaxSensorReadingStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            System.out.print("OverallMaxSensorReadingStream: ");
            EventPrinter.print(events);
        }
    });
    siddhiAppRuntime.addCallback("OverallAverageSensorReadingStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            System.out.print("OverallAverageSensorReadingStream: ");
            EventPrinter.print(events);
        }
    });
    siddhiAppRuntime.start();
    InputHandler sensorStreamInputHandler = siddhiAppRuntime.getInputHandler("SensorStream");
    sensorStreamInputHandler.send(new Object[] { "Temperature", 23.0f, 1, "T001A" });
    sensorStreamInputHandler.send(new Object[] { "Pressure", 20.0f, 1, "P001" });
    sensorStreamInputHandler.send(new Object[] { "Temperature", 25.0f, 2, "T002" });
    sensorStreamInputHandler.send(new Object[] { "Temperature", 24.0f, 3, "T003" });
    sensorStreamInputHandler.send(new Object[] { "Pressure", 45.0f, 3, "P003" });
    sensorStreamInputHandler.send(new Object[] { "Temperature", 23.5f, 1, "T001B" });
    sensorStreamInputHandler.send(new Object[] { "Humidity", 15.0f, 2, "H002" });
    sensorStreamInputHandler.send(new Object[] { "Humidity", 10.0f, 3, "H003" });
    Thread.sleep(1000);
    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

StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)218 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)213 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)213 Event (org.wso2.siddhi.core.event.Event)213 Test (org.testng.annotations.Test)207 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)202 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)17 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)14 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PrintStream (java.io.PrintStream)6 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)6 Partition (org.wso2.siddhi.query.api.execution.partition.Partition)6 Query (org.wso2.siddhi.query.api.execution.query.Query)6 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)5 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)3 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 InMemoryPersistenceStore (org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore)2 PersistenceStore (org.wso2.siddhi.core.util.persistence.PersistenceStore)2