Search in sources :

Example 46 with SiddhiApp

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

the class TriggerTestCase method testQuery2.

@Test(expectedExceptions = SiddhiAppValidationException.class)
public void testQuery2() throws InterruptedException {
    log.info("testTrigger2 - OUT 0");
    SiddhiManager siddhiManager = new SiddhiManager();
    TriggerDefinition triggerDefinition = TriggerDefinition.id("cseEventStream").atEvery(Expression.Time.milliSec(500)).at("start");
    SiddhiApp siddhiApp = new SiddhiApp("ep1");
    siddhiApp.defineTrigger(triggerDefinition);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    siddhiAppRuntime.shutdown();
}
Also used : SiddhiApp(org.ballerinalang.siddhi.query.api.SiddhiApp) TriggerDefinition(org.ballerinalang.siddhi.query.api.definition.TriggerDefinition) SiddhiAppRuntime(org.ballerinalang.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.ballerinalang.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 47 with SiddhiApp

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

the class SiddhiQLBaseVisitorImpl method visitSiddhi_app.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public SiddhiApp visitSiddhi_app(@NotNull SiddhiQLParser.Siddhi_appContext ctx) {
    SiddhiApp siddhiApp = SiddhiApp.siddhiApp();
    for (SiddhiQLParser.App_annotationContext annotationContext : ctx.app_annotation()) {
        siddhiApp.annotation((Annotation) visit(annotationContext));
    }
    for (SiddhiQLParser.Definition_streamContext streamContext : ctx.definition_stream()) {
        siddhiApp.defineStream((StreamDefinition) visit(streamContext));
    }
    for (SiddhiQLParser.Definition_tableContext tableContext : ctx.definition_table()) {
        siddhiApp.defineTable((TableDefinition) visit(tableContext));
    }
    for (SiddhiQLParser.Definition_functionContext functionContext : ctx.definition_function()) {
        siddhiApp.defineFunction((FunctionDefinition) visit(functionContext));
    }
    for (SiddhiQLParser.Definition_windowContext windowContext : ctx.definition_window()) {
        siddhiApp.defineWindow((WindowDefinition) visit(windowContext));
    }
    for (SiddhiQLParser.Definition_aggregationContext aggregationContext : ctx.definition_aggregation()) {
        siddhiApp.defineAggregation((AggregationDefinition) visit(aggregationContext));
    }
    for (SiddhiQLParser.Execution_elementContext executionElementContext : ctx.execution_element()) {
        ExecutionElement executionElement = (ExecutionElement) visit(executionElementContext);
        if (executionElement instanceof Partition) {
            siddhiApp.addPartition((Partition) executionElement);
        } else if (executionElement instanceof Query) {
            siddhiApp.addQuery((Query) executionElement);
        } else {
            throw newSiddhiParserException(ctx);
        }
    }
    for (SiddhiQLParser.Definition_triggerContext triggerContext : ctx.definition_trigger()) {
        siddhiApp.defineTrigger((TriggerDefinition) visit(triggerContext));
    }
    populateQueryContext(siddhiApp, ctx);
    return siddhiApp;
}
Also used : SiddhiQLParser(org.ballerinalang.siddhi.query.compiler.SiddhiQLParser) Partition(org.ballerinalang.siddhi.query.api.execution.partition.Partition) SiddhiApp(org.ballerinalang.siddhi.query.api.SiddhiApp) StoreQuery(org.ballerinalang.siddhi.query.api.execution.query.StoreQuery) Query(org.ballerinalang.siddhi.query.api.execution.query.Query) ExecutionElement(org.ballerinalang.siddhi.query.api.execution.ExecutionElement)

Example 48 with SiddhiApp

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

the class FilterTestCase1 method testFilterQuery54.

@Test
public void testFilterQuery54() throws InterruptedException {
    log.info("Filter test54");
    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.DOUBLE);
    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("price"), Compare.Operator.EQUAL, Expression.value(50d))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")));
    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);
            count = count + inEvents.length;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 50f, 60d });
    inputHandler.send(new Object[] { "WSO2", 70f, 40d });
    inputHandler.send(new Object[] { "WSO2", 44f, 200d });
    Thread.sleep(100);
    AssertJUnit.assertEquals(1, count);
    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 49 with SiddhiApp

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

the class FilterTestCase1 method testFilterQuery74.

@Test
public void testFilterQuery74() throws InterruptedException {
    log.info("Filter test74");
    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);
    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("quantity"), Compare.Operator.LESS_THAN_EQUAL, Expression.value(5f))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")).select("quantity", Expression.variable("quantity")));
    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);
            count = count + inEvents.length;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 500f, 60d, 6 });
    inputHandler.send(new Object[] { "WSO2", 70f, 60d, 2 });
    inputHandler.send(new Object[] { "WSO2", 60f, 300d, 4 });
    Thread.sleep(100);
    AssertJUnit.assertEquals(2, count);
    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 50 with SiddhiApp

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

the class FilterTestCase1 method testFilterQuery82.

@Test
public void testFilterQuery82() throws InterruptedException {
    log.info("Filter test82");
    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.DOUBLE).attribute("volume", Attribute.Type.DOUBLE);
    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("price"), Compare.Operator.LESS_THAN, Expression.value(50))));
    query.annotation(Annotation.annotation("info").element("name", "query1"));
    query.select(Selector.selector().select("symbol", Expression.variable("symbol")).select("price", Expression.variable("price")));
    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);
            count = count + inEvents.length;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 50d, 60d });
    inputHandler.send(new Object[] { "WSO2", 70d, 40d });
    inputHandler.send(new Object[] { "WSO2", 44d, 200d });
    Thread.sleep(100);
    AssertJUnit.assertEquals(1, count);
    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