Search in sources :

Example 21 with Partition

use of org.wso2.siddhi.query.api.execution.partition.Partition in project siddhi by wso2.

the class PatternPartitionTestCase method testPatternPartitionQuery15.

@Test
public void testPatternPartitionQuery15() throws InterruptedException {
    log.info("testPatternCount7 - 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] <0:5> -> e2=Stream2[price>20] " + "select e1[0].price as price1_0, e1[1].price as price1_1, 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[] { null, null, 45.7f }, event.getData());
                }
                eventArrived = true;
            }
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    stream2.send(new Object[] { "IBM", 45.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 22 with Partition

use of org.wso2.siddhi.query.api.execution.partition.Partition in project siddhi by wso2.

the class PatternPartitionTestCase method testPatternPartitionQuery19.

@Test
public void testPatternPartitionQuery19() throws InterruptedException {
    log.info("testPatternCount11 - 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] <:5> " + "   -> e3 = EventStream [volume <= 70] " + "select e1.symbol as symbol1, e2[last].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", null, "GOOG" }, 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, 4 });
    Thread.sleep(100);
    eventStream.send(new Object[] { "GOOG", 21f, 61, 4 });
    eventStream.send(new Object[] { "WSO2", 21f, 61, 4 });
    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 23 with Partition

use of org.wso2.siddhi.query.api.execution.partition.Partition in project siddhi by wso2.

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.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 24 with Partition

use of org.wso2.siddhi.query.api.execution.partition.Partition in project siddhi by wso2.

the class PatternPartitionTestCase method testPatternPartitionQuery13.

@Test
public void testPatternPartitionQuery13() throws InterruptedException {
    log.info("testPatternCount5 - 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, 23.7f, 24.7f, 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[] { "GOOG", 47.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 23.7f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 24.7f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 25.7f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 27.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 45.7f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "GOOG", 47.8f, 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 25 with Partition

use of org.wso2.siddhi.query.api.execution.partition.Partition in project siddhi by wso2.

the class PatternPartitionTestCase method testPatternPartitionQuery24.

@Test
public void testPatternPartitionQuery24() throws InterruptedException {
    log.info("testPatternLogical4 - 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();
                    switch(inEventCount.get()) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[] { "WSO2", 72.7f, 4.7f }, event.getData());
                            break;
                        case 2:
                            AssertJUnit.assertArrayEquals(new Object[] { "IBM", 72.7f, 4.7f }, 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[] { "WSO2", 55.6f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "GOOG", 72.7f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 4.7f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "IBM", 55.6f, 10 });
    Thread.sleep(100);
    stream2.send(new Object[] { "GOOG", 72.7f, 10 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 4.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)

Aggregations

Test (org.testng.annotations.Test)132 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)131 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)131 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)130 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)127 Event (org.wso2.siddhi.core.event.Event)125 Partition (org.wso2.siddhi.query.api.execution.partition.Partition)13 Query (org.wso2.siddhi.query.api.execution.query.Query)11 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)7 ArrayList (java.util.ArrayList)5 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)4 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)4 SiddhiAppValidationException (org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)4 ExecutionElement (org.wso2.siddhi.query.api.execution.ExecutionElement)4 QueryRuntime (org.wso2.siddhi.core.query.QueryRuntime)3 Element (org.wso2.siddhi.query.api.annotation.Element)3 List (java.util.List)2 TestUtil (org.wso2.siddhi.core.TestUtil)2 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)2 PartitionRuntime (org.wso2.siddhi.core.partition.PartitionRuntime)2