Search in sources :

Example 56 with Table

use of org.wso2.siddhi.core.table.Table in project siddhi by wso2.

the class PrimaryKeyTableTestCase method primaryKeyTableTest2.

@Test
public void primaryKeyTableTest2() throws InterruptedException {
    log.info("primaryKeyTableTest2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream CheckStockStream (symbol string, volume long); " + "define stream UpdateStockStream (symbol string, price float, volume long);" + "@PrimaryKey('symbol') " + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from CheckStockStream join StockTable " + " on CheckStockStream.symbol!=StockTable.symbol " + "select CheckStockStream.symbol, StockTable.symbol as tableSymbol, StockTable.volume " + "insert into OutStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("query2", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                EventPrinter.print(timestamp, inEvents, removeEvents);
                if (inEvents != null) {
                    for (Event event : inEvents) {
                        inEventsList.add(event.getData());
                        inEventCount.incrementAndGet();
                    }
                    eventArrived = true;
                }
                if (removeEvents != null) {
                    removeEventCount = removeEventCount + removeEvents.length;
                }
                eventArrived = true;
            }
        });
        InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
        InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
        siddhiAppRuntime.start();
        stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
        stockStream.send(new Object[] { "IBM", 55.6f, 100L });
        checkStockStream.send(new Object[] { "GOOG", 100L });
        List<Object[]> expected = Arrays.asList(new Object[] { "GOOG", "IBM", 100L }, new Object[] { "GOOG", "WSO2", 100L });
        SiddhiTestHelper.waitForEvents(100, 2, inEventCount, 60000);
        AssertJUnit.assertEquals("In events matched", true, SiddhiTestHelper.isUnsortedEventsMatch(inEventsList, expected));
        AssertJUnit.assertEquals("Number of success events", 2, inEventCount.get());
        AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
        AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    } finally {
        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 57 with Table

use of org.wso2.siddhi.core.table.Table in project siddhi by wso2.

the class PrimaryKeyTableTestCase method primaryKeyTableTest22.

@Test
public void primaryKeyTableTest22() throws InterruptedException {
    log.info("primaryKeyTableTest22");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream CheckStockStream (symbol string, volume long); " + "@PrimaryKey('symbol') " + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from CheckStockStream[(symbol!=StockTable.symbol) in StockTable] " + "insert into OutStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("query2", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                EventPrinter.print(timestamp, inEvents, removeEvents);
                if (inEvents != null) {
                    for (Event event : inEvents) {
                        inEventsList.add(event.getData());
                        inEventCount.incrementAndGet();
                    }
                    eventArrived = true;
                }
                if (removeEvents != null) {
                    removeEventCount = removeEventCount + removeEvents.length;
                }
                eventArrived = true;
            }
        });
        InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
        InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
        siddhiAppRuntime.start();
        stockStream.send(new Object[] { "WSO2", 55.6f, 200L });
        stockStream.send(new Object[] { "BAR", 55.6f, 150L });
        stockStream.send(new Object[] { "IBM", 55.6f, 100L });
        checkStockStream.send(new Object[] { "FOO", 100L });
        checkStockStream.send(new Object[] { "WSO2", 100L });
        List<Object[]> expected = Arrays.asList(new Object[] { "FOO", 100L }, new Object[] { "WSO2", 100L });
        SiddhiTestHelper.waitForEvents(100, 2, inEventCount, 60000);
        AssertJUnit.assertEquals("In events matched", true, SiddhiTestHelper.isUnsortedEventsMatch(inEventsList, expected));
        AssertJUnit.assertEquals("Number of success events", 2, inEventCount.get());
        AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
        AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    } finally {
        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 58 with Table

use of org.wso2.siddhi.core.table.Table in project siddhi by wso2.

the class PrimaryKeyTableTestCase method primaryKeyTableTest27.

@Test
public void primaryKeyTableTest27() throws InterruptedException {
    log.info("primaryKeyTableTest27");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream CheckStockStream (symbol string, volume long, price float); " + "define stream UpdateStockStream (comp string, vol long); " + "@PrimaryKey('symbol') " + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from UpdateStockStream left outer join StockTable " + "   on UpdateStockStream.comp == StockTable.symbol " + "select comp as symbol, ifThenElse(price is null,0f,price) as price, vol as volume " + "update or insert into StockTable " + "   on StockTable.symbol==symbol;" + "" + "@info(name = 'query3') " + "from CheckStockStream[(symbol==StockTable.symbol and volume==StockTable.volume" + " and price==StockTable.price) in StockTable] " + "insert into OutStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("query3", 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.incrementAndGet();
                        switch(inEventCount.get()) {
                            case 1:
                                AssertJUnit.assertArrayEquals(new Object[] { "IBM", 200L, 0f }, event.getData());
                                break;
                            case 2:
                                AssertJUnit.assertArrayEquals(new Object[] { "WSO2", 300L, 55.6f }, event.getData());
                                break;
                            default:
                                AssertJUnit.assertSame(2, inEventCount.get());
                        }
                    }
                    eventArrived = true;
                }
                if (removeEvents != null) {
                    removeEventCount += removeEvents.length;
                }
                eventArrived = true;
            }
        });
        InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
        InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
        InputHandler updateStockStream = siddhiAppRuntime.getInputHandler("UpdateStockStream");
        siddhiAppRuntime.start();
        stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
        checkStockStream.send(new Object[] { "IBM", 100L, 155.6f });
        checkStockStream.send(new Object[] { "WSO2", 100L, 155.6f });
        updateStockStream.send(new Object[] { "IBM", 200L });
        updateStockStream.send(new Object[] { "WSO2", 300L });
        checkStockStream.send(new Object[] { "IBM", 200L, 0f });
        checkStockStream.send(new Object[] { "WSO2", 300L, 55.6f });
        SiddhiTestHelper.waitForEvents(100, 2, inEventCount, 60000);
        AssertJUnit.assertEquals("Number of success events", 2, inEventCount.get());
        AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
        AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    } finally {
        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 59 with Table

use of org.wso2.siddhi.core.table.Table in project siddhi by wso2.

the class PrimaryKeyTableTestCase method primaryKeyTableTest10.

@Test
public void primaryKeyTableTest10() throws InterruptedException {
    log.info("primaryKeyTableTest10");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream CheckStockStream (symbol string, volume long); " + "define stream UpdateStockStream (symbol string, price float, volume long);" + "@PrimaryKey('symbol') " + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from UpdateStockStream " + "update StockTable " + "   on StockTable.symbol!=symbol;" + "" + "@info(name = 'query3') " + "from CheckStockStream join StockTable " + " on CheckStockStream.symbol!=StockTable.symbol " + "select StockTable.symbol, StockTable.volume " + "insert into OutStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("query3", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                EventPrinter.print(timestamp, inEvents, removeEvents);
                if (inEvents != null) {
                    for (Event event : inEvents) {
                        inEventsList.add(event.getData());
                        inEventCount.incrementAndGet();
                    }
                    eventArrived = true;
                }
                if (removeEvents != null) {
                    removeEventCount = removeEventCount + removeEvents.length;
                }
                eventArrived = true;
            }
        });
        InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
        InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
        InputHandler updateStockStream = siddhiAppRuntime.getInputHandler("UpdateStockStream");
        siddhiAppRuntime.start();
        stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
        stockStream.send(new Object[] { "IBM", 55.6f, 100L });
        checkStockStream.send(new Object[] { "IBM", 100L });
        checkStockStream.send(new Object[] { "WSO2", 100L });
        updateStockStream.send(new Object[] { "IBM", 77.6f, 200L });
        checkStockStream.send(new Object[] { "WSO2", 100L });
        List<Object[]> expected = Arrays.asList(new Object[] { "WSO2", 100L }, new Object[] { "IBM", 100L }, new Object[] { "IBM", 100L });
        SiddhiTestHelper.waitForEvents(100, 3, inEventCount, 60000);
        AssertJUnit.assertEquals("In events matched", true, SiddhiTestHelper.isEventsMatch(inEventsList, expected));
        AssertJUnit.assertEquals("Number of success events", 3, inEventCount.get());
        AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
        AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    } finally {
        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 60 with Table

use of org.wso2.siddhi.core.table.Table in project siddhi by wso2.

the class PrimaryKeyTableTestCase method primaryKeyTableTest38.

@Test
public void primaryKeyTableTest38() throws InterruptedException {
    log.info("primaryKeyTableTest38");
    SiddhiManager siddhiManager = new SiddhiManager();
    String streams = "" + "define stream StockStream (symbol string, price float, volume long); " + "define stream CheckStockStream (symbol string, volume long); " + "define stream UpdateStockStream (symbol string, price float, volume long);" + "@PrimaryKey('symbol','volume') " + "define table StockTable (symbol string, price float, volume long); ";
    String query = "" + "@info(name = 'query1') " + "from StockStream " + "insert into StockTable ;" + "" + "@info(name = 'query2') " + "from CheckStockStream join StockTable " + " on (CheckStockStream.symbol==StockTable.symbol and CheckStockStream.volume==StockTable.volume) and " + " 55.6f == StockTable.price " + "select CheckStockStream.symbol, StockTable.volume " + "insert into OutStream;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    try {
        siddhiAppRuntime.addCallback("query2", new QueryCallback() {

            @Override
            public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
                EventPrinter.print(timestamp, inEvents, removeEvents);
                if (inEvents != null) {
                    for (Event event : inEvents) {
                        inEventsList.add(event.getData());
                        inEventCount.incrementAndGet();
                    }
                    eventArrived = true;
                }
                if (removeEvents != null) {
                    removeEventCount = removeEventCount + removeEvents.length;
                }
                eventArrived = true;
            }
        });
        InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
        InputHandler checkStockStream = siddhiAppRuntime.getInputHandler("CheckStockStream");
        siddhiAppRuntime.start();
        stockStream.send(new Object[] { "WSO2", 55.6f, 100L });
        stockStream.send(new Object[] { "IBM", 55.6f, 100L });
        stockStream.send(new Object[] { "IBM", 55.6f, 101L });
        stockStream.send(new Object[] { "IBM", 55.6f, 102L });
        stockStream.send(new Object[] { "IBM", 55.6f, 200L });
        checkStockStream.send(new Object[] { "IBM", 200L });
        checkStockStream.send(new Object[] { "WSO2", 100L });
        List<Object[]> expected = Arrays.asList(new Object[] { "IBM", 200L }, new Object[] { "WSO2", 100L });
        SiddhiTestHelper.waitForEvents(100, 2, inEventCount, 60000);
        AssertJUnit.assertEquals("In events matched", true, SiddhiTestHelper.isEventsMatch(inEventsList, expected));
        AssertJUnit.assertEquals("Number of success events", 2, inEventCount.get());
        AssertJUnit.assertEquals("Number of remove events", 0, removeEventCount);
        AssertJUnit.assertEquals("Event arrived", true, eventArrived);
    } finally {
        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)183 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)176 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)176 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)140 Event (org.wso2.siddhi.core.event.Event)119 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)102 ArrayList (java.util.ArrayList)47 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)23 HashMap (java.util.HashMap)22 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)20 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)19 Attribute (org.wso2.siddhi.query.api.definition.Attribute)19 Table (org.wso2.siddhi.core.table.Table)11 Expression (org.wso2.siddhi.query.api.expression.Expression)11 Variable (org.wso2.siddhi.query.api.expression.Variable)11 MetaStateEvent (org.wso2.siddhi.core.event.state.MetaStateEvent)10 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)10 TableDefinition (org.wso2.siddhi.query.api.definition.TableDefinition)9 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)8 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)8