Search in sources :

Example 86 with SiddhiAppRuntime

use of org.ballerinalang.siddhi.core.SiddhiAppRuntime 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 87 with SiddhiAppRuntime

use of org.ballerinalang.siddhi.core.SiddhiAppRuntime 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 88 with SiddhiAppRuntime

use of org.ballerinalang.siddhi.core.SiddhiAppRuntime 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)

Example 89 with SiddhiAppRuntime

use of org.ballerinalang.siddhi.core.SiddhiAppRuntime in project ballerina by ballerina-lang.

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.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 90 with SiddhiAppRuntime

use of org.ballerinalang.siddhi.core.SiddhiAppRuntime in project ballerina by ballerina-lang.

the class CustomJoinWindowTestCase method testJoinWindowWithSameWindow.

/**
 * Behaviour of traditional Window and Window are different in join  with themselves.
 * Traditional Windows joins always maintain two windows and the event is passed to them one after
 * the other. Therefore, when left window receives a current event right window will bbe empty and
 * if an event is expired from left window it can join with right window since it will not expire at the same
 * time. Since Window maintains only one instance, when an event arrives to the window it will
 * be compared to the internal event chunk, where the current event will match with itself.
 * When an event expires, it will be immediately removed from the internal event chunk so that,
 * when joined with the window, the expired event will not match with itself.
 *
 * @throws InterruptedException throw exception if interrupted the input handler sender.
 */
@Test
public void testJoinWindowWithSameWindow() throws InterruptedException {
    log.info("Test join window with the same window");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream cseEventStream (symbol string, price float, volume int); " + "define window cseEventWindow (symbol string, price float, volume int) length(2); ";
    String query = "" + "@info(name = 'query0') " + "from cseEventStream " + "insert into " + "cseEventWindow; " + "" + "@info(name = 'query1') " + "from cseEventWindow as a " + "join cseEventWindow as b " + "on a.symbol== b.symbol " + "select a.symbol as symbol, a.price as priceA, b.price as priceB " + "insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("query1", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                EventPrinter.print(timestamp, inEvents, removeEvents);
                if (inEvents != null) {
                    inEventCount += inEvents.length;
                }
                if (removeEvents != null) {
                    removeEventCount += removeEvents.length;
                }
                eventArrived = true;
            }
        });
        InputHandler cseEventStreamHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
        siddhiAppRuntime.start();
        // Match with itself
        cseEventStreamHandler.send(new Object[] { "IBM", 75.6f, 100 });
        // Match with itself
        cseEventStreamHandler.send(new Object[] { "WSO2", 57.6f, 100 });
        // When the next event enters, the expired {"IBM", 75.6f, 100} will come out and joined
        // with the window which contains [{"WSO2", 57.6f, 100}, {"IBM", 59.6f, 100}] and latter the
        // current event {"IBM", 59.6f, 100} will match with the window.
        cseEventStreamHandler.send(new Object[] { "IBM", 59.6f, 100 });
        Thread.sleep(1000);
        AssertJUnit.assertEquals(3, inEventCount);
        AssertJUnit.assertEquals(1, removeEventCount);
        AssertJUnit.assertTrue(eventArrived);
    } finally {
        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) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)1235 SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)1231 Test (org.testng.annotations.Test)1231 InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)1123 Event (org.ballerinalang.siddhi.core.event.Event)798 QueryCallback (org.ballerinalang.siddhi.core.query.output.callback.QueryCallback)574 TestUtil (org.ballerinalang.siddhi.core.TestUtil)300 StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)201 SiddhiApp (org.ballerinalang.siddhi.query.api.SiddhiApp)85 Query (org.ballerinalang.siddhi.query.api.execution.query.Query)82 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)80 ArrayList (java.util.ArrayList)25 InMemoryBroker (org.ballerinalang.siddhi.core.util.transport.InMemoryBroker)16 ComplexEvent (org.ballerinalang.siddhi.core.event.ComplexEvent)12 CannotRestoreSiddhiAppStateException (org.ballerinalang.siddhi.core.exception.CannotRestoreSiddhiAppStateException)12 InMemoryPersistenceStore (org.ballerinalang.siddhi.core.util.persistence.InMemoryPersistenceStore)12 PersistenceStore (org.ballerinalang.siddhi.core.util.persistence.PersistenceStore)12 HashMap (java.util.HashMap)11 InMemoryConfigManager (org.ballerinalang.siddhi.core.util.config.InMemoryConfigManager)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6