Search in sources :

Example 56 with StreamCallback

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

the class PatternPartitionTestCase method testPatternPartitionQuery17.

@Test
public void testPatternPartitionQuery17() throws InterruptedException {
    log.info("testPatternCount9 - OUT 1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream EventStream (symbol string, price float, volume int , quantity int); ";
    String partitionStart = "partition with (quantity of EventStream) begin ";
    String query = "" + "@info(name = 'query1') " + "from e1 = EventStream [price >= 50 and volume > 100] -> e2 = EventStream [price <= 40] <0:5> " + "   -> e3 = EventStream [volume <= 70] " + "select e1.symbol as symbol1, e2[0].symbol as symbol2, e3.symbol as symbol3 " + "insert into StockQuote;";
    String partitionEnd = "end";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + partitionStart + query + partitionEnd);
    siddhiAppRuntime.addCallback("StockQuote", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                if (event.isExpired()) {
                    removeEventCount.incrementAndGet();
                } else {
                    inEventCount.incrementAndGet();
                    switch(inEventCount.get()) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[] { "IBM", "GOOG", "WSO2" }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(1, inEventCount.get());
                    }
                }
                eventArrived = true;
            }
        }
    });
    InputHandler eventStream = siddhiAppRuntime.getInputHandler("EventStream");
    siddhiAppRuntime.start();
    eventStream.send(new Object[] { "IBM", 75.6f, 105, 5 });
    Thread.sleep(100);
    eventStream.send(new Object[] { "GOOG", 21f, 81, 5 });
    eventStream.send(new Object[] { "WSO2", 176.6f, 65, 5 });
    SiddhiTestHelper.waitForEvents(100, 1, inEventCount, 60000);
    AssertJUnit.assertEquals("Number of success events", 1, inEventCount.get());
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount.get());
    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 57 with StreamCallback

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

the class PatternPartitionTestCase method testPatternPartitionQuery1.

@Test
public void testPatternPartitionQuery1() throws InterruptedException {
    log.info("Partition - testPatternEvery1 - 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 e1=Stream1[price>20] -> e2=Stream2[price>e1.price] " + "select e1.symbol as symbol1, e2.symbol as symbol2 " + "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.incrementAndGet();
                } else {
                    inEventCount.incrementAndGet();
                    AssertJUnit.assertArrayEquals(new Object[] { "WSO2", "IBM" }, event.getData());
                    eventArrived = true;
                }
                eventArrived = true;
            }
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 55.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 55.7f, 100 });
    SiddhiTestHelper.waitForEvents(100, 1, inEventCount, 60000);
    AssertJUnit.assertEquals("Number of success events", 1, inEventCount.get());
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount.get());
    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 58 with StreamCallback

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

the class PatternPartitionTestCase method testPatternPartitionQuery3.

@Test
public void testPatternPartitionQuery3() throws InterruptedException {
    log.info("Partition - testPatternEvery3 - OUT 1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream Stream1 (symbol string, price float, volume int); " + "define stream Stream2 (symbol string, price1 float, volume int); ";
    String partitionStart = "partition with (volume of Stream1,volume of Stream2) begin ";
    String query = "" + "@info(name = 'query1') " + "from every e1=Stream1[price>20] -> e2=Stream2[price1>e1.price] " + "select e1.symbol as symbol1, e2.symbol as symbol2 " + "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.incrementAndGet();
                } else {
                    inEventCount.incrementAndGet();
                    switch(inEventCount.get()) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[] { "WSO2", "IBM" }, event.getData());
                            break;
                        case 2:
                            AssertJUnit.assertArrayEquals(new Object[] { "GOOG", "IBM" }, event.getData());
                            break;
                        case 3:
                            AssertJUnit.assertArrayEquals(new Object[] { "WSO2", "IBM" }, event.getData());
                            break;
                        case 4:
                            AssertJUnit.assertArrayEquals(new Object[] { "GOOG", "IBM" }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(4, inEventCount.get());
                    }
                    eventArrived = true;
                }
                eventArrived = true;
            }
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 55.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 55.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 55.7f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 55.6f, 150 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 55.6f, 150 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 55.7f, 150 });
    SiddhiTestHelper.waitForEvents(100, 4, inEventCount, 60000);
    AssertJUnit.assertEquals("Number of success events", 4, inEventCount.get());
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount.get());
    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 59 with StreamCallback

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

the class PatternPartitionTestCase method testPatternPartitionQuery21.

@Test
public void testPatternPartitionQuery21() throws InterruptedException {
    log.info("testPatternLogical1 - 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 e1=Stream1[price > 20] -> e2=Stream2[price > e1.price] or e3=Stream2['IBM' == symbol] " + "select e1.symbol as symbol1, e2.symbol as symbol2 " + "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.incrementAndGet();
                } else {
                    inEventCount.incrementAndGet();
                    AssertJUnit.assertArrayEquals(new Object[] { "WSO2", "GOOG" }, event.getData());
                }
                eventArrived = true;
            }
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 55.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "GOOG", 59.6f, 200 });
    Thread.sleep(100);
    stream2.send(new Object[] { "GOOG", 59.6f, 100 });
    SiddhiTestHelper.waitForEvents(100, 1, inEventCount, 60000);
    AssertJUnit.assertEquals("Number of success events", 1, inEventCount.get());
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount.get());
    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 60 with StreamCallback

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

the class PatternPartitionTestCase method testPatternPartitionQuery2.

@Test
public void testPatternPartitionQuery2() throws InterruptedException {
    log.info("Partition - testPatternEvery2 - OUT 1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream Stream1 (symbol string, price float, volume int); " + "define stream Stream2 (symbol string, price1 float, volume int); ";
    String partitionStart = "partition with (volume of Stream1,volume of Stream2) begin ";
    String query = "" + "@info(name = 'query1') " + "from e1=Stream1[price>20] -> e2=Stream2[price1>e1.price] " + "select e1.symbol as symbol1, e2.symbol as symbol2 " + "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.incrementAndGet();
                } else {
                    inEventCount.incrementAndGet();
                    AssertJUnit.assertArrayEquals(new Object[] { "WSO2", "IBM" }, event.getData());
                    eventArrived = true;
                }
                eventArrived = true;
            }
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 55.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 55.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 55.7f, 100 });
    SiddhiTestHelper.waitForEvents(100, 1, inEventCount, 60000);
    AssertJUnit.assertEquals("Number of success events", 1, inEventCount.get());
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount.get());
    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)

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