Search in sources :

Example 1 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

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.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 2 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

the class TimeWindowTestCase method timeWindowTest3.

@Test
public void timeWindowTest3() throws InterruptedException {
    SiddhiManager siddhiManager = new SiddhiManager();
    String queries = "define stream fireAlarmEventStream (deviceID string, sonar double);\n" + "@info(name = 'query1')\n" + "from fireAlarmEventStream#window.time(30 milliseconds)\n" + "select deviceID\n" + "insert expired events into analyzeStream;\n" + "" + "@info(name = 'query2')\n" + "from analyzeStream\n" + "select deviceID\n" + "insert into bulbOnStream;\n";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(queries);
    siddhiAppRuntime.addCallback("analyzeStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("fireAlarmEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "id1", 20d });
    inputHandler.send(new Object[] { "id2", 20d });
    Thread.sleep(2000);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 3 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

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.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 4 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

the class CustomJoinWindowTestCase method testJoinWindowWithWindow.

@Test
public void testJoinWindowWithWindow() throws InterruptedException {
    log.info("Test join window with another window");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream TempStream(deviceID long, roomNo int, temp double); " + "define stream RegulatorStream(deviceID long, roomNo int, isOn bool); " + "define window TempWindow(deviceID long, roomNo int, temp double) time(1 min); " + "define window RegulatorWindow(deviceID long, roomNo int, isOn bool) length(1); ";
    String query = "" + "@info(name = 'query1') " + "from TempStream[temp > 30.0] " + "insert into TempWindow; " + "" + "@info(name = 'query2') " + "from RegulatorStream[isOn == false] " + "insert into RegulatorWindow; " + "" + "@info(name = 'query3') " + "from TempWindow " + "join RegulatorWindow " + "on TempWindow.roomNo == RegulatorWindow.roomNo " + "select TempWindow.roomNo, RegulatorWindow.deviceID, 'start' as action " + "insert into RegulatorActionStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("RegulatorActionStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            inEventCount++;
        }
    });
    InputHandler tempStream = siddhiAppRuntime.getInputHandler("TempStream");
    InputHandler regulatorStream = siddhiAppRuntime.getInputHandler("RegulatorStream");
    siddhiAppRuntime.start();
    tempStream.send(new Object[] { 100L, 1, 20.0 });
    tempStream.send(new Object[] { 100L, 2, 25.0 });
    tempStream.send(new Object[] { 100L, 3, 30.0 });
    tempStream.send(new Object[] { 100L, 4, 35.0 });
    tempStream.send(new Object[] { 100L, 5, 40.0 });
    regulatorStream.send(new Object[] { 100L, 1, false });
    regulatorStream.send(new Object[] { 100L, 2, false });
    regulatorStream.send(new Object[] { 100L, 3, false });
    regulatorStream.send(new Object[] { 100L, 4, false });
    regulatorStream.send(new Object[] { 100L, 5, false });
    Thread.sleep(500);
    assertEquals("Number of success events", 2, inEventCount);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 5 with StreamCallback

use of org.ballerinalang.siddhi.core.stream.output.StreamCallback in project ballerina by ballerina-lang.

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.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) StreamCallback(org.ballerinalang.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)209 Test (org.testng.annotations.Test)204 Event (org.ballerinalang.siddhi.core.event.Event)203 SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)201 SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)201 InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)190 ComplexEvent (org.ballerinalang.siddhi.core.event.ComplexEvent)13 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PrintStream (java.io.PrintStream)6 SiddhiApp (org.ballerinalang.siddhi.query.api.SiddhiApp)6 Partition (org.ballerinalang.siddhi.query.api.execution.partition.Partition)6 Query (org.ballerinalang.siddhi.query.api.execution.query.Query)6 StreamEvent (org.ballerinalang.siddhi.core.event.stream.StreamEvent)5 QueryCallback (org.ballerinalang.siddhi.core.query.output.callback.QueryCallback)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CannotRestoreSiddhiAppStateException (org.ballerinalang.siddhi.core.exception.CannotRestoreSiddhiAppStateException)2 BlockingWaitStrategy (com.lmax.disruptor.BlockingWaitStrategy)1 ProducerType (com.lmax.disruptor.dsl.ProducerType)1 Constructor (java.lang.reflect.Constructor)1