Search in sources :

Example 86 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.

the class LogicalPatternTestCase method testQuery12.

@Test
public void testQuery12() throws InterruptedException {
    log.info("testPatternLogical12 - 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); " + "define stream Stream3 (symbol string, price float, volume int); ";
    String query = "" + "@info(name = 'query1') " + "from every e1=Stream1[price >20] -> e2=Stream2['IBM' == symbol] or " + "e3=Stream3['WSO2' == symbol]" + "select e1.price as price1, e2.price as price2, e3.price as price3 " + "insert into OutputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
                switch(inEventCount) {
                    case 1:
                        AssertJUnit.assertArrayEquals(new Object[] { 25.5f, 45.5f, null }, inEvents[0].getData());
                        break;
                    case 2:
                        AssertJUnit.assertArrayEquals(new Object[] { 59.65f, 45.5f, null }, inEvents[0].getData());
                        break;
                    default:
                        AssertJUnit.assertEquals("Number of success events", 2, inEventCount);
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    InputHandler stream3 = siddhiAppRuntime.getInputHandler("Stream3");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "IBM", 25.5f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "IBM", 59.65f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 45.5f, 100 });
    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(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 87 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.

the class LogicalPatternTestCase method testQuery16.

@Test
public void testQuery16() throws InterruptedException {
    log.info("testQuery16 - OUT 3");
    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 query = "" + "@info(name = 'query1') " + "from every (e1=Stream1[price > 20] or e2=Stream2[price >30]) " + "select e1.symbol as symbol1, e2.price as price2 " + "insert into OutputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
                eventArrived = true;
                switch(inEventCount) {
                    case 1:
                        AssertJUnit.assertArrayEquals(new Object[] { "WSO2", null }, inEvents[0].getData());
                        break;
                    case 2:
                        AssertJUnit.assertArrayEquals(new Object[] { null, 35.0f }, inEvents[0].getData());
                        break;
                    case 3:
                        AssertJUnit.assertArrayEquals(new Object[] { null, 45.0f }, inEvents[0].getData());
                }
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "WSO2", 25.0f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 35.0f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "ORACLE", 45.0f, 100 });
    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(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 88 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.

the class LogicalPatternTestCase method testQuery19.

@Test
public void testQuery19() throws InterruptedException {
    log.info("testQuery19 - 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); " + "define stream Stream3 (symbol string, price float, volume int); ";
    String query = "" + "@info(name = 'query1') " + "from every (e1=Stream1[price>10] and e2=Stream2[price>20]) -> e3=Stream3[price>30] " + "select e1.symbol as symbol1, e2.symbol as symbol2, e3.symbol as symbol3 " + "insert into OutputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
                eventArrived = true;
                switch(inEventCount) {
                    case 1:
                        AssertJUnit.assertArrayEquals(new Object[] { "ORACLE", "MICROSOFT", "GOOGLE" }, inEvents[0].getData());
                        break;
                    case 2:
                        AssertJUnit.assertArrayEquals(new Object[] { "IBM", "WSO2", "GOOGLE" }, inEvents[0].getData());
                }
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    InputHandler stream3 = siddhiAppRuntime.getInputHandler("Stream3");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "ORACLE", 15.0f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "MICROSOFT", 45.0f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "IBM", 55.0f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 65.0f, 100 });
    Thread.sleep(100);
    stream3.send(new Object[] { "GOOGLE", 75.0f, 100 });
    Thread.sleep(100);
    AssertJUnit.assertEquals("Number of success events", 2, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertTrue("Event arrived", eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 89 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.

the class LogicalPatternTestCase method testQuery11.

@Test
public void testQuery11() throws InterruptedException {
    log.info("testPatternLogical11 - 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); " + "define stream Stream3 (symbol string, price float, volume int); ";
    String query = "" + "@info(name = 'query1') " + "from every e1=Stream1[price >20] -> e2=Stream2['IBM' == symbol] and " + "e3=Stream3['WSO2' == symbol]" + "select e1.price as price1, e2.price as price2, e3.price as price3 " + "insert into OutputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
                switch(inEventCount) {
                    case 1:
                        AssertJUnit.assertArrayEquals(new Object[] { 25.5f, 45.5f, 46.56f }, inEvents[0].getData());
                        break;
                    case 2:
                        AssertJUnit.assertArrayEquals(new Object[] { 59.65f, 45.5f, 46.56f }, inEvents[0].getData());
                        break;
                    default:
                        AssertJUnit.assertEquals("Number of success events", 2, inEventCount);
                }
                eventArrived = true;
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    InputHandler stream3 = siddhiAppRuntime.getInputHandler("Stream3");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "IBM", 25.5f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "IBM", 59.65f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "IBM", 45.5f, 100 });
    Thread.sleep(100);
    stream3.send(new Object[] { "WSO2", 46.56f, 100 });
    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(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 90 with SiddhiAppRuntime

use of io.siddhi.core.SiddhiAppRuntime in project siddhi by wso2.

the class LogicalPatternTestCase method testQuery20.

@Test
public void testQuery20() throws InterruptedException {
    log.info("testQuery20 - 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); " + "define stream Stream3 (symbol string, price float, volume int); ";
    String query = "" + "@info(name = 'query1') " + "from every (e1=Stream1[price>10] and e2=Stream2[price>20] -> e3=Stream3[price>30]) " + "select e1.symbol as symbol1, e2.symbol as symbol2, e3.symbol as symbol3 " + "insert into OutputStream ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            if (inEvents != null) {
                inEventCount = inEventCount + inEvents.length;
                eventArrived = true;
                switch(inEventCount) {
                    case 1:
                        AssertJUnit.assertArrayEquals(new Object[] { "ORACLE", "MICROSOFT", "GOOGLE" }, inEvents[0].getData());
                        break;
                    case 2:
                        AssertJUnit.assertArrayEquals(new Object[] { "IBM1", "WSO21", "GOOGLE1" }, inEvents[0].getData());
                }
            }
            if (removeEvents != null) {
                removeEventCount = removeEventCount + removeEvents.length;
            }
            eventArrived = true;
        }
    });
    InputHandler stream1 = siddhiAppRuntime.getInputHandler("Stream1");
    InputHandler stream2 = siddhiAppRuntime.getInputHandler("Stream2");
    InputHandler stream3 = siddhiAppRuntime.getInputHandler("Stream3");
    siddhiAppRuntime.start();
    stream1.send(new Object[] { "ORACLE", 15.0f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "MICROSOFT", 45.0f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "IBM", 55.0f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO2", 65.0f, 100 });
    Thread.sleep(100);
    stream3.send(new Object[] { "GOOGLE", 75.0f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "IBM1", 55.0f, 100 });
    Thread.sleep(100);
    stream2.send(new Object[] { "WSO21", 65.0f, 100 });
    Thread.sleep(100);
    stream3.send(new Object[] { "GOOGLE1", 75.0f, 100 });
    Thread.sleep(100);
    AssertJUnit.assertEquals("Number of success events", 2, inEventCount);
    AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
    AssertJUnit.assertTrue("Event arrived", eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)1761 SiddhiManager (io.siddhi.core.SiddhiManager)1756 Test (org.testng.annotations.Test)1744 InputHandler (io.siddhi.core.stream.input.InputHandler)1590 Event (io.siddhi.core.event.Event)1257 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)823 StreamCallback (io.siddhi.core.stream.output.StreamCallback)305 TestUtil (io.siddhi.core.TestUtil)304 SiddhiApp (io.siddhi.query.api.SiddhiApp)90 Query (io.siddhi.query.api.execution.query.Query)85 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)84 ArrayList (java.util.ArrayList)73 Logger (org.apache.logging.log4j.core.Logger)57 TestAppenderToValidateLogsForCachingTests (io.siddhi.core.query.table.util.TestAppenderToValidateLogsForCachingTests)34 InMemoryBroker (io.siddhi.core.util.transport.InMemoryBroker)32 CannotRestoreSiddhiAppStateException (io.siddhi.core.exception.CannotRestoreSiddhiAppStateException)30 HashMap (java.util.HashMap)25 UnitTestAppender (io.siddhi.core.UnitTestAppender)23 InMemoryConfigManager (io.siddhi.core.util.config.InMemoryConfigManager)21 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)15