Search in sources :

Example 81 with InputHandler

use of org.ballerinalang.siddhi.core.stream.input.InputHandler in project ballerina by ballerina-lang.

the class TimeWindowTestCase method timeWindowTest2.

/**
 * Commenting out intermittent failing test case until fix this properly.
 *
 * @throws InterruptedException throw exception if interrupted the input handler sender.
 */
@Test
public void timeWindowTest2() throws InterruptedException {
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume int);";
    String query = "@info(name = 'query1') from cseEventStream#window.time(1 sec) select symbol,price," + "volume insert all events into outputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + 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("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 700f, 1 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 2 });
    Thread.sleep(1100);
    inputHandler.send(new Object[] { "IBM", 700f, 3 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 4 });
    Thread.sleep(1100);
    inputHandler.send(new Object[] { "IBM", 700f, 5 });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 6 });
    Thread.sleep(4000);
    AssertJUnit.assertEquals(6, inEventCount);
    AssertJUnit.assertEquals(6, removeEventCount);
    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) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 82 with InputHandler

use of org.ballerinalang.siddhi.core.stream.input.InputHandler 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 83 with InputHandler

use of org.ballerinalang.siddhi.core.stream.input.InputHandler 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 84 with InputHandler

use of org.ballerinalang.siddhi.core.stream.input.InputHandler 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 85 with InputHandler

use of org.ballerinalang.siddhi.core.stream.input.InputHandler 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

InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)1124 SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)1123 Test (org.testng.annotations.Test)1122 SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)1120 Event (org.ballerinalang.siddhi.core.event.Event)771 QueryCallback (org.ballerinalang.siddhi.core.query.output.callback.QueryCallback)572 TestUtil (org.ballerinalang.siddhi.core.TestUtil)300 StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)190 SiddhiApp (org.ballerinalang.siddhi.query.api.SiddhiApp)79 Query (org.ballerinalang.siddhi.query.api.execution.query.Query)79 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)77 ArrayList (java.util.ArrayList)25 InMemoryBroker (org.ballerinalang.siddhi.core.util.transport.InMemoryBroker)13 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 ComplexEvent (org.ballerinalang.siddhi.core.event.ComplexEvent)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PrintStream (java.io.PrintStream)6 Partition (org.ballerinalang.siddhi.query.api.execution.partition.Partition)6