Search in sources :

Example 56 with SiddhiApp

use of org.wso2.siddhi.query.api.SiddhiApp in project siddhi by wso2.

the class FilterTestCase2 method testFilterQuery109.

// *****************************************************************************************************************
// Expression-Add
@Test
public void testFilterQuery109() throws InterruptedException {
    log.info("Filter test109");
    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE).attribute("quantity", Attribute.Type.INT).attribute("awards", Attribute.Type.LONG);
    Query query = new Query();
    query.from(InputStream.stream("cseEventStream"));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("increasedPrice", Expression.add(Expression.value(100), Expression.variable("price"))).select("increasedVolume", Expression.add(Expression.value(50), Expression.variable("volume"))).select("increasedQuantity", Expression.add(Expression.value(4), Expression.variable("quantity"))).select("increasedAwards", Expression.add(Expression.value(10), Expression.variable("awards"))));
    query.insertInto("outputStream");
    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            AssertJUnit.assertTrue("155.5".equals(inEvents[0].getData()[1].toString()));
            AssertJUnit.assertTrue("150.0".equals(inEvents[0].getData()[2].toString()));
            AssertJUnit.assertTrue("9".equals(inEvents[0].getData()[3].toString()));
            AssertJUnit.assertTrue("20".equals(inEvents[0].getData()[4].toString()));
            count = count + inEvents.length;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 55.5f, 100d, 5, 10L });
    Thread.sleep(100);
    AssertJUnit.assertEquals(1, count);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiApp(org.wso2.siddhi.query.api.SiddhiApp) StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) Query(org.wso2.siddhi.query.api.execution.query.Query) 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 SiddhiApp

use of org.wso2.siddhi.query.api.SiddhiApp in project siddhi by wso2.

the class FilterTestCase2 method testFilterQuery111.

// ***************************************************************************************************************
// Expression Divide
@Test
public void testFilterQuery111() throws InterruptedException {
    log.info("Filter test111");
    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE).attribute("quantity", Attribute.Type.INT).attribute("awards", Attribute.Type.LONG);
    Query query = new Query();
    query.from(InputStream.stream("cseEventStream"));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("dividedPrice", Expression.divide(Expression.variable("price"), Expression.value(2))).select("dividedVolume", Expression.divide(Expression.variable("volume"), Expression.value(2))).select("dividedQuantity", Expression.divide(Expression.variable("quantity"), Expression.value(5))).select("dividedAwards", Expression.divide(Expression.variable("awards"), Expression.value(10))));
    query.insertInto("OutputStream");
    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            AssertJUnit.assertTrue("30.0".equals(inEvents[0].getData()[1].toString()));
            AssertJUnit.assertTrue("50.0".equals(inEvents[0].getData()[2].toString()));
            AssertJUnit.assertTrue("20".equals(inEvents[0].getData()[3].toString()));
            AssertJUnit.assertTrue("7".equals(inEvents[0].getData()[4].toString()));
            count = count + inEvents.length;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 60f, 100d, 100, 70L });
    Thread.sleep(100);
    AssertJUnit.assertEquals(1, count);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiApp(org.wso2.siddhi.query.api.SiddhiApp) StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) Query(org.wso2.siddhi.query.api.execution.query.Query) 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 SiddhiApp

use of org.wso2.siddhi.query.api.SiddhiApp in project siddhi by wso2.

the class GroupByTestCase method testGroupByQuery1.

@Test
public void testGroupByQuery1() throws InterruptedException {
    log.info("GroupBy test1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('GroupByTest1') " + "" + "define stream cseEventStream (symbol string, price float, volume long);" + "" + "@info(name = 'query1') " + "from cseEventStream#window.time(1 sec) " + "select symbol, sum(volume) as totalVolume, avg(price) as avgPrice " + "   group by symbol " + "insert into outputStream;" + "";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    log.info("Running : " + siddhiAppRuntime.getName());
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count = count + inEvents.length;
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 50f, 200L });
    inputHandler.send(new Object[] { "WSO2", 50f, 200L });
    Thread.sleep(200);
    inputHandler.send(new Object[] { "WSO2", 50f, 200L });
    inputHandler.send(new Object[] { "IBM", 50f, 200L });
    Thread.sleep(4200);
    inputHandler.send(new Object[] { "WSO2", 50f, 200L });
    inputHandler.send(new Object[] { "WSO2", 50f, 200L });
    Thread.sleep(100);
    AssertJUnit.assertEquals(6, count);
    AssertJUnit.assertTrue(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 59 with SiddhiApp

use of org.wso2.siddhi.query.api.SiddhiApp in project siddhi by wso2.

the class PassThroughTestCase method passThroughTest4.

@Test
public void passThroughTest4() throws InterruptedException {
    log.info("pass through test4");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@app:name('passThroughTest4') " + "" + "define stream cseEventStream (symbol string, price float, volume long);" + "" + "@info(name = 'query1') " + "from cseEventStream " + "insert into outputStream;" + "" + "@info(name = 'query2') " + "from outputStream " + "select * " + "insert into outputStream2 ;";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    log.info("Running : " + siddhiAppRuntime.getName());
    QueryCallback queryCallback = new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            AssertJUnit.assertTrue("WSO2".equals(inEvents[0].getData(0)));
            count = count + inEvents.length;
            eventArrived = true;
        }
    };
    siddhiAppRuntime.addCallback("query2", queryCallback);
    queryCallback.startProcessing();
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 700f, 100L });
    inputHandler.send(new Object[] { "WSO2", 60.5f, 200L });
    Thread.sleep(100);
    AssertJUnit.assertEquals(2, count);
    AssertJUnit.assertTrue(eventArrived);
    queryCallback.stopProcessing();
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 60 with SiddhiApp

use of org.wso2.siddhi.query.api.SiddhiApp in project siddhi by wso2.

the class PassThroughTestCase method passThroughTest2.

@Test
public void passThroughTest2() throws InterruptedException {
    log.info("pass through test2");
    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT);
    StreamDefinition cseEventStream1 = StreamDefinition.id("cseEventStream1").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.INT);
    Query query = new Query();
    query.from(InputStream.stream("cseEventStream"));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")));
    query.insertInto("StockQuote");
    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineStream(cseEventStream);
    siddhiApp.defineStream(cseEventStream1);
    siddhiApp.addQuery(query);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(inEvents);
            count += inEvents.length;
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream1");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 100 });
    inputHandler.send(new Object[] { "WSO2", 100 });
    Thread.sleep(100);
    AssertJUnit.assertEquals(0, count);
    AssertJUnit.assertFalse(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) SiddhiApp(org.wso2.siddhi.query.api.SiddhiApp) StreamDefinition(org.wso2.siddhi.query.api.definition.StreamDefinition) Query(org.wso2.siddhi.query.api.execution.query.Query) 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

SiddhiManager (org.wso2.siddhi.core.SiddhiManager)273 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)272 Test (org.testng.annotations.Test)261 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)245 Event (org.wso2.siddhi.core.event.Event)241 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)138 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)109 SiddhiApp (org.wso2.siddhi.query.api.SiddhiApp)88 Query (org.wso2.siddhi.query.api.execution.query.Query)84 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)81 CannotRestoreSiddhiAppStateException (org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException)13 InMemoryPersistenceStore (org.wso2.siddhi.core.util.persistence.InMemoryPersistenceStore)13 PersistenceStore (org.wso2.siddhi.core.util.persistence.PersistenceStore)13 Partition (org.wso2.siddhi.query.api.execution.partition.Partition)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 PrintStream (java.io.PrintStream)6 HashMap (java.util.HashMap)6 SiddhiAppValidationException (org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)6 InMemoryConfigManager (org.wso2.siddhi.core.util.config.InMemoryConfigManager)5 ExecutionElement (org.wso2.siddhi.query.api.execution.ExecutionElement)4