Search in sources :

Example 71 with And

use of org.wso2.siddhi.query.api.expression.condition.And in project siddhi by wso2.

the class StdDevAttributeAggregatorTestCase method stdDevAggregatorTest5.

@Test
public void stdDevAggregatorTest5() throws InterruptedException {
    log.info("stdDevAggregator Test #5: stdDev of large and small numbers");
    SiddhiManager siddhiManager = new SiddhiManager();
    String execPlan = "" + "@app:name('stdDevAggregatorTests') " + "" + "define stream cseEventStream (symbol string, price double);" + "" + "@info(name = 'query1') " + "from cseEventStream#window.lengthBatch(6) " + "select stdDev(price) as deviation " + "group by symbol " + "insert into outputStream;";
    SiddhiAppRuntime execPlanRunTime = siddhiManager.createSiddhiAppRuntime(execPlan);
    execPlanRunTime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            AssertJUnit.assertTrue(Math.abs((Double) inEvents[0].getData(0) - 400.13025) < epsilon);
            AssertJUnit.assertTrue(Math.abs((Double) inEvents[1].getData(0) - 0.00103) < epsilon);
        }
    });
    InputHandler inputHandler = execPlanRunTime.getInputHandler("cseEventStream");
    execPlanRunTime.start();
    inputHandler.send(new Object[] { "WSO2", 23.0 });
    inputHandler.send(new Object[] { "WSO2", 1003.0 });
    inputHandler.send(new Object[] { "WSO2", 500.0 });
    inputHandler.send(new Object[] { "IBM", 0.002 });
    inputHandler.send(new Object[] { "IBM", 0.0034 });
    inputHandler.send(new Object[] { "IBM", 0.00454 });
    Thread.sleep(600);
    execPlanRunTime.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) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 72 with And

use of org.wso2.siddhi.query.api.expression.condition.And in project siddhi by wso2.

the class SequenceTestCase method testQuery19.

@Test
public void testQuery19() throws InterruptedException {
    log.info("testSequence19 - 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 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 ;";
    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) {
                for (Event event : inEvents) {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[] { 25.0f, 40.0f, null, 35.0f }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(1, inEventCount);
                    }
                }
                eventArrived = true;
            }
            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);
    stream1.send(new Object[] { "WSO2", 40.0f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 35.0f, 100 });
    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) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 73 with And

use of org.wso2.siddhi.query.api.expression.condition.And in project siddhi by wso2.

the class SequenceTestCase method testTimeBatchAndSequence.

@Test
public void testTimeBatchAndSequence() throws Exception {
    log.info("testTimeBatchAndSequence  OUT 1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream received_reclamations (timestamp long, product_id string, defect_category string);";
    String query = "" + "@info(name = 'query1') " + "from received_reclamations#window.timeBatch(1 sec) " + "select product_id, defect_category, count(product_id) as num " + "group by product_id, defect_category " + "insert into reclamation_averages;" + "" + "@info(name = 'query2') " + "from a=reclamation_averages[num > 1], b=reclamation_averages[num > a.num and product_id == a" + ".product_id and defect_category == a.defect_category] " + "select a.product_id, a.defect_category, a.num as oldNum, b.num as newNum " + "insert into increased_reclamations;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("increased_reclamations", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            if (events != null) {
                for (Event event : events) {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            String product = (String) event.getData()[0];
                            String defectCategory = (String) event.getData()[1];
                            long oldNum = (Long) event.getData()[2];
                            long newNum = (Long) event.getData()[3];
                            AssertJUnit.assertTrue(product.equals("abc"));
                            AssertJUnit.assertTrue(defectCategory.equals("123"));
                            AssertJUnit.assertTrue(oldNum < newNum);
                            break;
                        default:
                            AssertJUnit.assertSame(1, inEventCount);
                    }
                }
            }
            eventArrived = true;
        }
    });
    InputHandler i1 = siddhiAppRuntime.getInputHandler("received_reclamations");
    siddhiAppRuntime.start();
    for (int i = 0; i < 5; i++) {
        i1.send(new Object[] { System.currentTimeMillis(), "abc", "123" });
        Thread.sleep(100);
    }
    Thread.sleep(500);
    for (int i = 0; i < 8; i++) {
        i1.send(new Object[] { System.currentTimeMillis(), "abc", "123" });
        Thread.sleep(100);
    }
    Thread.sleep(1000);
    siddhiAppRuntime.shutdown();
    AssertJUnit.assertEquals("Event arrived", 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)

Example 74 with And

use of org.wso2.siddhi.query.api.expression.condition.And in project siddhi by wso2.

the class SequenceTestCase method testQuery25.

@Test
public void testQuery25() throws InterruptedException {
    log.info("testQuery25 - 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); " + "define stream Stream3 (symbol string, price float, volume int); ";
    String query = "" + "@info(name = 'query1') " + "from 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;
                    default:
                        AssertJUnit.assertEquals("Number of success events", 1, 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);
    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", 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) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 75 with And

use of org.wso2.siddhi.query.api.expression.condition.And in project siddhi by wso2.

the class SequenceTestCase method testQuery18.

@Test
public void testQuery18() throws InterruptedException {
    log.info("testSequence18 - 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 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 ;";
    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) {
                for (Event event : inEvents) {
                    inEventCount++;
                    switch(inEventCount) {
                        case 1:
                            AssertJUnit.assertArrayEquals(new Object[] { 25.0f, 35.6f, 57.6f, 47.6f }, event.getData());
                            break;
                        default:
                            AssertJUnit.assertSame(1, inEventCount);
                    }
                }
                eventArrived = true;
            }
            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", 29.6f, 100 });
    Thread.sleep(100);
    stream1.send(new Object[] { "WSO2", 25.0f, 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);
    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) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)409 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)337 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)336 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)327 TestUtil (org.wso2.siddhi.core.TestUtil)204 Event (org.wso2.siddhi.core.event.Event)119 ArrayList (java.util.ArrayList)110 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)86 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)77 HashMap (java.util.HashMap)61 PreparedStatement (java.sql.PreparedStatement)55 SQLException (java.sql.SQLException)51 IOException (java.io.IOException)45 Connection (java.sql.Connection)45 ActivityInterface (org.wso2.carbon.bpel.ui.bpel2svg.ActivityInterface)44 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)43 API (org.wso2.carbon.apimgt.core.models.API)39 ResultSet (java.sql.ResultSet)35 Map (java.util.Map)33 CharonException (org.wso2.charon3.core.exceptions.CharonException)33