Search in sources :

Example 86 with SiddhiApp

use of org.ballerinalang.siddhi.query.api.SiddhiApp in project ballerina by ballerina-lang.

the class PassThroughTestCase method passThroughTest1.

@Test
public void passThroughTest1() throws InterruptedException {
    log.info("pass through test1");
    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").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.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);
            AssertJUnit.assertTrue("IBM".equals(inEvents[0].getData(0)) || "WSO2".equals(inEvents[0].getData(0)));
            count += inEvents.length;
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 100 });
    inputHandler.send(new Object[] { "WSO2", 100 });
    Thread.sleep(100);
    AssertJUnit.assertEquals(2, count);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiApp(org.ballerinalang.siddhi.query.api.SiddhiApp) StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) Query(org.ballerinalang.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 87 with SiddhiApp

use of org.ballerinalang.siddhi.query.api.SiddhiApp in project ballerina by ballerina-lang.

the class FunctionTestCase method functionTest1.

// Coalesce
@Test
public void functionTest1() throws InterruptedException {
    log.info("function test 1");
    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price1", Attribute.Type.FLOAT).attribute("price2", Attribute.Type.FLOAT);
    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.function("coalesce", Expression.variable("price1"), Expression.variable("price2"))));
    query.insertInto("StockQuote");
    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);
            eventArrived = true;
            for (Event inEvent : inEvents) {
                count++;
                if (count == 1) {
                    AssertJUnit.assertEquals(55.6f, inEvent.getData()[1]);
                } else if (count == 2) {
                    AssertJUnit.assertEquals(65.7f, inEvent.getData()[1]);
                } else if (count == 3) {
                    AssertJUnit.assertEquals(23.6f, inEvent.getData()[1]);
                } else if (count == 4) {
                    AssertJUnit.assertEquals(34.6f, inEvent.getData()[1]);
                } else if (count == 5) {
                    AssertJUnit.assertNull(inEvent.getData()[1]);
                }
            }
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 55.6f, 70.6f });
    inputHandler.send(new Object[] { "WSO2", 65.7f, 12.8f });
    inputHandler.send(new Object[] { "WSO2", 23.6f, null });
    inputHandler.send(new Object[] { "WSO2", null, 34.6f });
    inputHandler.send(new Object[] { "WSO2", null, null });
    Thread.sleep(100);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.ballerinalang.siddhi.core.stream.input.InputHandler) SiddhiApp(org.ballerinalang.siddhi.query.api.SiddhiApp) StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) Query(org.ballerinalang.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) Event(org.ballerinalang.siddhi.core.event.Event) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) QueryCallback(org.ballerinalang.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

SiddhiApp (org.ballerinalang.siddhi.query.api.SiddhiApp)87 SiddhiAppRuntime (org.ballerinalang.siddhi.core.SiddhiAppRuntime)85 SiddhiManager (org.ballerinalang.siddhi.core.SiddhiManager)85 Test (org.testng.annotations.Test)85 Query (org.ballerinalang.siddhi.query.api.execution.query.Query)83 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)80 InputHandler (org.ballerinalang.siddhi.core.stream.input.InputHandler)79 Event (org.ballerinalang.siddhi.core.event.Event)78 QueryCallback (org.ballerinalang.siddhi.core.query.output.callback.QueryCallback)73 Partition (org.ballerinalang.siddhi.query.api.execution.partition.Partition)7 StreamCallback (org.ballerinalang.siddhi.core.stream.output.StreamCallback)6 TriggerDefinition (org.ballerinalang.siddhi.query.api.definition.TriggerDefinition)2 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 TableDefinition (org.ballerinalang.siddhi.query.api.definition.TableDefinition)1 ExecutionElement (org.ballerinalang.siddhi.query.api.execution.ExecutionElement)1 StoreQuery (org.ballerinalang.siddhi.query.api.execution.query.StoreQuery)1 SiddhiQLParser (org.ballerinalang.siddhi.query.compiler.SiddhiQLParser)1 SiddhiQLBaseVisitorImpl (org.ballerinalang.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl)1