Search in sources :

Example 31 with InputHandler

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

the class PatternPartitionTestCase method testPatternPartitionQuery28.

@Test
public void testPatternPartitionQuery28() throws InterruptedException {
    log.info("testPatternComplex2 - 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=Stream1[price > 20] -> e2=Stream1[price > 20]<1:2>) -> e3=Stream1[price > e1.price] " + "select e1.price as price1, e2[0].price as price2_0, e2[1].price as price2_1, 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();
                    switch(inEventCount.get()) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[] { 55.6f, 54.0f, 53.6f, 57.0f }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(1, inEventCount.get());
                    }
                }
                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", 54f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 53.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 57f, 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 32 with InputHandler

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

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.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 33 with InputHandler

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

the class SequencePartitionTestCase method testSequencePartitionQuery9.

@Test
public void testSequencePartitionQuery9() throws InterruptedException {
    log.info("Pattern -testSequence9 - 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, 57.6f, null }, event.getData());
                            break;
                        case 2:
                            AssertJUnit.assertArrayEquals(new Object[] { 57.6f, null, 55.7f }, event.getData());
                            break;
                        case 3:
                            AssertJUnit.assertArrayEquals(new Object[] { 155.6f, 207.6f, null }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(3, 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", 155.6f, 200 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 55.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 57.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 55.7f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 207.6f, 200 });
    Thread.sleep(100);
    AssertJUnit.assertEquals("Number of success events", 3, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    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 34 with InputHandler

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

the class SequencePartitionTestCase method testSequencePartitionQuery11.

@Test
public void testSequencePartitionQuery11() throws InterruptedException {
    log.info("Pattern -testSequence11 - 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=Stream1[price>20], " + "   e2=Stream1[((e2[last].price is null) and price>=e1.price) or ((not (e2[last].price is null)) and " + "price>=e2[last].price)]+, " + "   e3=Stream1[price<e2[last].price] " + "select e1.price as price1, e2[0].price as price2, e2[1].price as price3, e3.price as price4 " + "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[] { 29.6f, 35.6f, 57.6f, 47.6f }, event.getData());
                            break;
                        case 2:
                            AssertJUnit.assertArrayEquals(new Object[] { 129.6f, 135.6f, 157.6f, 147.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", 29.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 35.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 57.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "IBM", 47.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 129.6f, 10 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 135.6f, 10 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 157.6f, 10 });
    Thread.sleep(100);
    stream1.send(new Object[] { "IBM", 147.6f, 10 });
    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.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 35 with InputHandler

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

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.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