Search in sources :

Example 66 with StreamCallback

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

the class SequencePartitionTestCase method testSequencePartitionQuery5.

@Test
public void testSequencePartitionQuery5() throws InterruptedException {
    log.info("Pattern -testSequence5 - OUT 1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream Stream1 (symbol string, price float, volume int); " + "define stream Stream2 (symbol string, price float, volume int); ";
    String partitionStart = "partition with (volume of Stream1 , volume of Stream2) begin ";
    String query = "" + "@info(name = 'query1') " + "from every e1=Stream2[price>20]*, e2=Stream1[price>e1[0].price] " + "select e1[0].price as price1, e1[1].price as price2, e2.price as price3 " + "insert into OutputStream ;";
    String partitionEnd = "end";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + partitionStart + query + partitionEnd);
    siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                if (event.isExpired()) {
                    removeEventCount++;
                } else {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[] { 55.6f, 55.0f, 57.6f }, event.getData());
                            break;
                        case 2:
                            AssertJUnit.assertArrayEquals(new Object[] { 85.6f, 85.0f, 87.6f }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(2, inEventCount);
                    }
                }
                eventArrived = true;
            }
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 59.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 55.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 55.0f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 57.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 85.6f, 1000 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 85.0f, 1000 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 87.6f, 1000 });
    Thread.sleep(100);
    AssertJUnit.assertEquals("Number of success events", 2, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertEquals("Event arrived", true, 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 67 with StreamCallback

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

the class SequencePartitionTestCase method testSequencePartitionQuery7.

@Test
public void testSequencePartitionQuery7() throws InterruptedException {
    log.info("Pattern -testSequence7 - OUT 2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream Stream1 (symbol string, price float, volume int); " + "define stream Stream2 (symbol string, price float, volume int); ";
    String partitionStart = "partition with (volume of Stream1 , volume of Stream2) begin ";
    String query = "" + "@info(name = 'query1') " + "from every e1=Stream2[price>20], e2=Stream2[price>e1.price] or e3=Stream2[symbol=='IBM'] " + "select e1.price as price1, e2.price as price2, e3.price as price3 " + "insert into OutputStream ;";
    String partitionEnd = "end";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + partitionStart + query + partitionEnd);
    siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                if (event.isExpired()) {
                    removeEventCount++;
                } else {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[] { 55.6f, 55.7f, null }, event.getData());
                            break;
                        case 2:
                            AssertJUnit.assertArrayEquals(new Object[] { 55.7f, 57.6f, null }, event.getData());
                            break;
                        case 3:
                            AssertJUnit.assertArrayEquals(new Object[] { 55.6f, 155.7f, null }, event.getData());
                            break;
                        case 4:
                            AssertJUnit.assertArrayEquals(new Object[] { 155.7f, 457.6f, null }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(4, inEventCount);
                    }
                }
                eventArrived = true;
            }
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    stream2.send(new Object[] { "WSO2", 59.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 55.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 55.7f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 57.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 599.6f, 4100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 55.6f, 4100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 155.7f, 4100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 457.6f, 4100 });
    Thread.sleep(100);
    AssertJUnit.assertEquals("Number of success events", 4, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertEquals("Event arrived", true, 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 68 with StreamCallback

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

the class SequencePartitionTestCase method testSequencePartitionQuery16.

@Test
public void testSequencePartitionQuery16() throws InterruptedException {
    log.info("Pattern -testSequence16 - OUT 2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream1 (symbol string, price float, volume int, quantity int); " + "define stream StockStream2 (symbol string, price float, volume int, quantity int); ";
    String partitionStart = "partition with (quantity of StockStream1 , quantity of StockStream2) begin ";
    String query = "" + "@info(name = 'query1') " + "from every e1=StockStream1, e2=StockStream2[e1.symbol != 'AMBA']*, e3=StockStream2[volume <= 70] " + "select e3.symbol as symbol1, e2[0].symbol as symbol2, e3.volume as volume, e1.quantity as quantity " + "insert into OutputStream ;";
    String partitionEnd = "end";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + partitionStart + query + partitionEnd);
    siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                if (event.isExpired()) {
                    removeEventCount++;
                } else {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[] { "WSO2", "GOOG", 65, 5 }, event.getData());
                            break;
                        case 2:
                            AssertJUnit.assertArrayEquals(new Object[] { "WSO2", "GOOG", 65, 155 }, event.getData());
                            break;
                        case 3:
                            AssertJUnit.assertArrayEquals(new Object[] { "DOX", null, 25, 5 }, event.getData());
                            break;
                        case 4:
                            AssertJUnit.assertArrayEquals(new Object[] { "WSO2", "GOOG", 65, 55 }, event.getData());
                            break;
                        case 5:
                            AssertJUnit.assertArrayEquals(new Object[] { "DOX", null, 25, 55 }, event.getData());
                            break;
                        case 6:
                            AssertJUnit.assertArrayEquals(new Object[] { "DOX", null, 25, 155 }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(6, inEventCount);
                    }
                }
                eventArrived = true;
            }
        }
    });
    InputHandler stockStream1 = siddhiAppRuntime.getInputHandler("StockStream1");
    InputHandler stockStream2 = siddhiAppRuntime.getInputHandler("StockStream2");
    siddhiAppRuntime.start();
    stockStream1.send(new Object[] { "IBM", 75.6f, 105, 5 });
    Thread.sleep(100);
    stockStream2.send(new Object[] { "GOOG", 21f, 81, 5 });
    stockStream2.send(new Object[] { "WSO2", 176.6f, 65, 5 });
    stockStream1.send(new Object[] { "BIRT", 21f, 81, 5 });
    stockStream1.send(new Object[] { "AMBA", 126.6f, 165, 5 });
    stockStream1.send(new Object[] { "IBM", 75.6f, 105, 155 });
    Thread.sleep(100);
    stockStream2.send(new Object[] { "GOOG", 21f, 81, 155 });
    stockStream2.send(new Object[] { "WSO2", 176.6f, 65, 155 });
    stockStream1.send(new Object[] { "BIRT", 21f, 81, 155 });
    stockStream2.send(new Object[] { "DDD", 23f, 181, 5 });
    stockStream2.send(new Object[] { "BIRT", 21f, 86, 5 });
    stockStream2.send(new Object[] { "BIRT", 21f, 82, 5 });
    stockStream2.send(new Object[] { "WSO2", 176.6f, 60, 5 });
    stockStream1.send(new Object[] { "AMBA", 126.6f, 165, 5 });
    stockStream2.send(new Object[] { "DOX", 16.2f, 25, 5 });
    Thread.sleep(100);
    stockStream1.send(new Object[] { "AMBA", 126.6f, 165, 155 });
    stockStream2.send(new Object[] { "DDD", 23f, 181, 155 });
    stockStream2.send(new Object[] { "BIRT", 21f, 86, 155 });
    stockStream2.send(new Object[] { "BIRT", 21f, 82, 155 });
    stockStream2.send(new Object[] { "WSO2", 176.6f, 60, 155 });
    stockStream1.send(new Object[] { "IBM", 75.6f, 105, 55 });
    Thread.sleep(100);
    stockStream2.send(new Object[] { "GOOG", 21f, 81, 55 });
    stockStream2.send(new Object[] { "WSO2", 176.6f, 65, 55 });
    stockStream1.send(new Object[] { "BIRT", 21f, 81, 55 });
    stockStream1.send(new Object[] { "AMBA", 126.6f, 165, 55 });
    stockStream2.send(new Object[] { "DDD", 23f, 181, 55 });
    stockStream2.send(new Object[] { "BIRT", 21f, 86, 55 });
    stockStream2.send(new Object[] { "BIRT", 21f, 82, 55 });
    stockStream2.send(new Object[] { "WSO2", 176.6f, 60, 55 });
    stockStream1.send(new Object[] { "AMBA", 126.6f, 165, 55 });
    stockStream2.send(new Object[] { "DOX", 16.2f, 25, 55 });
    Thread.sleep(100);
    stockStream1.send(new Object[] { "AMBA", 126.6f, 165, 155 });
    stockStream2.send(new Object[] { "DOX", 16.2f, 25, 155 });
    Thread.sleep(100);
    AssertJUnit.assertEquals("Number of success events", 6, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertEquals("Event arrived", true, 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 69 with StreamCallback

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

the class SequencePartitionTestCase method testSequencePartitionQuery10.

@Test
public void testSequencePartitionQuery10() throws InterruptedException {
    log.info("Pattern -testSequence10 - OUT 1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream Stream1 (symbol string, price float, volume int); " + "define stream Stream2 (symbol string, price float, volume int); ";
    String partitionStart = "partition with (volume of Stream1 , volume of Stream2) begin ";
    String query = "" + "@info(name = 'query1') " + "from every e1=Stream2[price>20]+, e2=Stream1[price>e1[0].price] " + "select e1[0].price as price1, e1[1].price as price2, e2.price as price3 " + "insert into OutputStream ;";
    String partitionEnd = "end";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + partitionStart + query + partitionEnd);
    siddhiAppRuntime.addCallback("OutputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                if (event.isExpired()) {
                    removeEventCount++;
                } else {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[] { 55.6f, null, 57.6f }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(1, inEventCount);
                    }
                }
                eventArrived = true;
            }
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 59.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 55.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 57.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 55.6f, 120 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 57.6f, 150 });
    Thread.sleep(100);
    AssertJUnit.assertEquals("Number of success events", 1, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertEquals("Event arrived", true, 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 70 with StreamCallback

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

the class TablePartitionTestCase method testPartitionQuery2.

@Test
public void testPartitionQuery2() throws InterruptedException {
    log.info("Table Partition test 2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('PartitionTest') " + "define stream streamA (symbol string, price int);" + "define stream streamB (symbol string);" + "define table tableA (symbol string, price int);" + "partition with (symbol of streamA, symbol of streamB) " + "begin " + "   @info(name = 'query1') " + "   from streamA " + "   select symbol, price " + "   insert into tableA;  " + "" + "end ;" + "" + "@info(name = 'query2') " + "from streamB[(symbol==tableA.symbol) in tableA] " + "select symbol " + "insert into outputStream;  " + "";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("outputStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                count.incrementAndGet();
            }
            eventArrived = true;
        }
    });
    InputHandler streamAInputHandler = siddhiAppRuntime.getInputHandler("streamA");
    InputHandler streamBInputHandler = siddhiAppRuntime.getInputHandler("streamB");
    siddhiAppRuntime.start();
    streamAInputHandler.send(new Object[] { "IBM", 700 });
    streamAInputHandler.send(new Object[] { "WSO2", 60 });
    streamAInputHandler.send(new Object[] { "WSO2", 60 });
    streamBInputHandler.send(new Object[] { "WSO2" });
    streamBInputHandler.send(new Object[] { "FB" });
    streamBInputHandler.send(new Object[] { "IBM" });
    SiddhiTestHelper.waitForEvents(100, 2, count, 60000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals(2, count.get());
    AssertJUnit.assertEquals(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)

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