Search in sources :

Example 81 with Event

use of org.wso2.siddhi.core.event.Event in project siddhi by wso2.

the class SequencePartitionTestCase method testSequencePartitionQuery2.

@Test
public void testSequencePartitionQuery2() throws InterruptedException {
    log.info("Pattern -testSequence2 - 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=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++;
                } else {
                    inEventCount++;
                    AssertJUnit.assertArrayEquals(new Object[] { "GOOG", "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", 57.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 65.7f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 55.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 57.6f, 200 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 65.7f, 300 });
    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.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 82 with Event

use of org.wso2.siddhi.core.event.Event in project siddhi by wso2.

the class SequencePartitionTestCase method testSequencePartitionQuery8.

@Test
public void testSequencePartitionQuery8() throws InterruptedException {
    log.info("Pattern -testSequence8 - 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, null, 55.0f }, event.getData());
                            break;
                        case 2:
                            AssertJUnit.assertArrayEquals(new Object[] { 155.6f, null, 95.0f }, event.getData());
                            break;
                        case 3:
                            AssertJUnit.assertArrayEquals(new Object[] { 55.0f, 57.6f, null }, event.getData());
                            break;
                        case 4:
                            AssertJUnit.assertArrayEquals(new Object[] { 95.0f, 207.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", 259.6f, 200 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 55.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 155.6f, 200 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 55.0f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 95.0f, 200 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 57.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 207.6f, 200 });
    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.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 83 with Event

use of org.wso2.siddhi.core.event.Event in project siddhi by wso2.

the class SequencePartitionTestCase method testSequencePartitionQuery12.

@Test
public void testSequencePartitionQuery12() throws InterruptedException {
    log.info("Pattern -testSequence12 - OUT 1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume int,name String); " + "define stream TwitterStream (symbol string, count int,user String); ";
    String partitionStart = "partition with (name of StockStream , user of TwitterStream) begin ";
    String query = "" + "@info(name = 'query1') " + "from every e1=StockStream[ price >= 50 and volume > 100 ], e2=TwitterStream[count > 10] " + "select e1.price as price, e1.symbol as symbol, e2.count as count " + "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[] { 76.6f, "IBM", 20 }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(1, inEventCount);
                    }
                }
                eventArrived = true;
            }
        }
    });
    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler twitterStream = siddhiAppRuntime.getInputHandler("TwitterStream");
    siddhiAppRuntime.start();
    stockStream.send(new Object[] { "IBM", 75.6f, 105, "user" });
    stockStream.send(new Object[] { "GOOG", 51f, 101, "user" });
    stockStream.send(new Object[] { "IBM", 76.6f, 111, "user" });
    stockStream.send(new Object[] { "IBM", 76.6f, 111, "user2" });
    Thread.sleep(100);
    twitterStream.send(new Object[] { "IBM", 20, "user" });
    stockStream.send(new Object[] { "WSO2", 45.6f, 100, "user" });
    Thread.sleep(100);
    twitterStream.send(new Object[] { "GOOG", 20, "user" });
    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.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 84 with Event

use of org.wso2.siddhi.core.event.Event in project siddhi by wso2.

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.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 85 with Event

use of org.wso2.siddhi.core.event.Event in project siddhi by wso2.

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

Test (org.testng.annotations.Test)1150 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)1146 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)1145 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)1119 Event (org.wso2.siddhi.core.event.Event)855 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)218 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)86 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)79 Query (org.wso2.siddhi.query.api.execution.query.Query)79 ArrayList (java.util.ArrayList)68 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)63 ComplexEvent (org.wso2.siddhi.core.event.ComplexEvent)58 ComplexEventChunk (org.wso2.siddhi.core.event.ComplexEventChunk)38 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)35 GroupedComplexEvent (org.wso2.siddhi.core.event.GroupedComplexEvent)16 InMemoryBroker (org.wso2.siddhi.core.util.transport.InMemoryBroker)16 HashMap (java.util.HashMap)14 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)13