Search in sources :

Example 26 with InputHandler

use of org.wso2.siddhi.core.stream.input.InputHandler in project siddhi by wso2.

the class PatternPartitionTestCase method testPatternPartitionQuery9.

@Test
public void testPatternPartitionQuery9() throws InterruptedException {
    log.info("testPatternCount1 - 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] <2:5> -> e2=Stream2[price>20] " + "select e1[0].price as price1_0, e1[1].price as price1_1, e1[2].price as price1_2, " + "   e1[3].price as price1_3, e2.price as price2 " + "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[] { 25.6f, 47.6f, 47.8f, null, 45.7f }, event.getData());
                }
                eventArrived = true;
            }
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 25.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 25.6f, 10 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 47.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 13.7f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 47.6f, 10 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 13.7f, 10 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 47.8f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 47.8f, 10 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 45.7f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 55.7f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 45.7f, 10 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 55.7f, 10 });
    SiddhiTestHelper.waitForEvents(100, 2, inEventCount, 60000);
    AssertJUnit.assertEquals("Number of success events", 2, inEventCount.get());
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount.get());
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 27 with InputHandler

use of org.wso2.siddhi.core.stream.input.InputHandler in project siddhi by wso2.

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

Example 28 with InputHandler

use of org.wso2.siddhi.core.stream.input.InputHandler in project siddhi by wso2.

the class PatternPartitionTestCase method testPatternPartitionQuery29.

@Test
public void testPatternPartitionQuery29() throws InterruptedException {
    log.info("testPatternComplex3 - OUT 3");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream Stream1 (symbol string, price float, volume int,quantity int); ";
    String partitionStart = "partition with (quantity of Stream1) begin ";
    String partitionEnd = "end";
    String query = "" + "@info(name = 'query1') " + "from every e1 = Stream1 [ price >= 50 and volume > 100 ] -> e2 = Stream1 [price <= 40 ] <2:> -> e3 =" + " Stream1 [volume <= 70 ] " + "select e1.symbol as symbol1, e2[last].symbol as symbol2, e3.symbol as symbol3 " + "insert into StockQuote;";
    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", "FB", "WSO2" }, event.getData());
                            break;
                        case 2:
                            AssertJUnit.assertArrayEquals(new Object[] { "ADP", "WSO2", "AMZN" }, event.getData());
                            break;
                        case 3:
                            AssertJUnit.assertArrayEquals(new Object[] { "WSO2", "QQQ", "CSCO" }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(3, inEventCount.get());
                    }
                }
                eventArrived = true;
            }
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "IBM", 75.6f, 105, 1 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 39.8f, 91, 1 });
    stream1.send(new Object[] { "FB", 35f, 81, 1 });
    stream1.send(new Object[] { "WSO2", 21f, 61, 1 });
    stream1.send(new Object[] { "ADP", 50f, 101, 1 });
    stream1.send(new Object[] { "GOOG", 41.2f, 90, 1 });
    stream1.send(new Object[] { "FB", 40f, 100, 1 });
    stream1.send(new Object[] { "WSO2", 33.6f, 85, 1 });
    stream1.send(new Object[] { "AMZN", 23.5f, 55, 1 });
    stream1.send(new Object[] { "WSO2", 51.7f, 180, 1 });
    stream1.send(new Object[] { "TXN", 34f, 61, 1 });
    stream1.send(new Object[] { "QQQ", 24.6f, 45, 1 });
    stream1.send(new Object[] { "CSCO", 181.6f, 40, 1 });
    stream1.send(new Object[] { "WSO2", 53.7f, 200, 1 });
    SiddhiTestHelper.waitForEvents(100, 3, inEventCount, 60000);
    AssertJUnit.assertEquals("Number of success events", 3, inEventCount.get());
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount.get());
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 29 with InputHandler

use of org.wso2.siddhi.core.stream.input.InputHandler in project siddhi by wso2.

the class PatternPartitionTestCase method testPatternPartitionQuery30.

@Test
public void testPatternPartitionQuery30() throws InterruptedException {
    log.info("testPatternComplex4 - OUT 2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream Stream1 (symbol string, price float, volume int,quantity int); " + "define stream Stream2 (symbol string, price float, volume int,quantity int); ";
    String partitionStart = "partition with (quantity of Stream1) begin ";
    String partitionEnd = "end";
    String query = "" + "@info(name = 'query1') " + "from every e1 = Stream1 [ price >= 50 and volume > 100 ] " + "   -> e2 = Stream2 [price <= 40 ] <1:> -> e3 = Stream2 [volume <= 70 ] " + "select e3.symbol as symbol1, e2[0].symbol as symbol2, e3.volume as symbol3 " + "insert into StockQuote;";
    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[] { "WSO2", "GOOG", 65 }, event.getData());
                            break;
                        case 2:
                            AssertJUnit.assertArrayEquals(new Object[] { "WSO2", "DDD", 60 }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(2, inEventCount.get());
                    }
                }
                eventArrived = true;
            }
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "IBM", 75.6f, 105, 1 });
    Thread.sleep(100);
    stream2.send(new Object[] { "GOOG", 21f, 81, 1 });
    stream2.send(new Object[] { "WSO2", 176.6f, 65, 1 });
    stream1.send(new Object[] { "AMBA", 126.6f, 165, 1 });
    stream2.send(new Object[] { "DDD", 23f, 181, 1 });
    stream2.send(new Object[] { "BIRT", 21f, 86, 1 });
    stream2.send(new Object[] { "BIRT", 21f, 82, 1 });
    stream2.send(new Object[] { "WSO2", 176.6f, 60, 1 });
    stream1.send(new Object[] { "AMBA", 126.6f, 165, 1 });
    stream2.send(new Object[] { "DOX", 16.2f, 25, 1 });
    SiddhiTestHelper.waitForEvents(100, 2, inEventCount, 60000);
    AssertJUnit.assertEquals("Number of success events", 2, inEventCount.get());
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount.get());
    AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 30 with InputHandler

use of org.wso2.siddhi.core.stream.input.InputHandler in project siddhi by wso2.

the class PatternPartitionTestCase method testPatternPartitionQuery25.

@Test
public void testPatternPartitionQuery25() throws InterruptedException {
    log.info("testPatternLogical5 - 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] and e3=Stream2['IBM' == symbol] " + "select e1.symbol as symbol1, 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.incrementAndGet();
                } else {
                    inEventCount.incrementAndGet();
                    AssertJUnit.assertArrayEquals(new Object[] { "WSO2", 72.7f, 72.7f }, 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[] { "IBM", 72.7f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 75.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.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Aggregations

InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)1155 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)1151 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)1149 Test (org.testng.annotations.Test)1136 Event (org.wso2.siddhi.core.event.Event)800 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)587 TestUtil (org.wso2.siddhi.core.TestUtil)300 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)201 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)80 Query (org.wso2.siddhi.query.api.execution.query.Query)79 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)77 ArrayList (java.util.ArrayList)25 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)13 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)13 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)13 InMemoryPersistenceStore (org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore)13 PersistenceStore (org.wso2.siddhi.core.util.persistence.PersistenceStore)13 InMemoryBroker (org.wso2.siddhi.core.util.transport.InMemoryBroker)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PrintStream (java.io.PrintStream)6