Search in sources :

Example 91 with SiddhiApp

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

the class FilterTestCase1 method testFilterQuery77.

@Test
public void testFilterQuery77() throws InterruptedException {
    log.info("Filter test77");
    SiddhiManager siddhiManager = new SiddhiManager();
    StreamDefinition cseEventStream = StreamDefinition.id("cseEventStream").attribute("symbol", Attribute.Type.STRING).attribute("price", Attribute.Type.FLOAT).attribute("volume", Attribute.Type.LONG);
    Query query = new Query();
    query.from(InputStream.stream("cseEventStream").filter(Expression.compare(Expression.variable("volume"), Compare.Operator.LESS_THAN_EQUAL, Expression.value(50f))));
    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.addAndGet(inEvents.length);
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 50f, 60L });
    inputHandler.send(new Object[] { "WSO2", 70f, 40L });
    inputHandler.send(new Object[] { "WSO2", 44f, 200L });
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiApp(io.siddhi.query.api.SiddhiApp) StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) Query(io.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 92 with SiddhiApp

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

the class FilterTestCase1 method testFilterQuery61.

@Test
public void testFilterQuery61() throws InterruptedException {
    log.info("Filter test61");
    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.EQUAL, Expression.value(4L))));
    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.addAndGet(inEvents.length);
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 50f, 60d, 5 });
    inputHandler.send(new Object[] { "WSO2", 70f, 60d, 2 });
    inputHandler.send(new Object[] { "WSO2", 60f, 200d, 4 });
    SiddhiTestHelper.waitForEvents(10, 1, count, 100);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiApp(io.siddhi.query.api.SiddhiApp) StreamDefinition(io.siddhi.query.api.definition.StreamDefinition) Query(io.siddhi.query.api.execution.query.Query) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) SiddhiManager(io.siddhi.core.SiddhiManager) QueryCallback(io.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 93 with SiddhiApp

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

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(io.siddhi.query.compiler.SiddhiQLParser) Partition(io.siddhi.query.api.execution.partition.Partition) SiddhiApp(io.siddhi.query.api.SiddhiApp) Query(io.siddhi.query.api.execution.query.Query) OnDemandQuery(io.siddhi.query.api.execution.query.OnDemandQuery) ExecutionElement(io.siddhi.query.api.execution.ExecutionElement)

Example 94 with SiddhiApp

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

the class SiddhiCompiler method parse.

public static SiddhiApp parse(String siddhiApp) {
    CharStream input = CharStreams.fromString(siddhiApp);
    SiddhiQLLexer lexer = new SiddhiQLLexer(input);
    lexer.removeErrorListeners();
    lexer.addErrorListener(SiddhiErrorListener.INSTANCE);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SiddhiQLParser parser = new SiddhiQLParser(tokens);
    // parser.setErrorHandler(new BailErrorStrategy());
    parser.removeErrorListeners();
    parser.addErrorListener(SiddhiErrorListener.INSTANCE);
    ParseTree tree = parser.parse();
    SiddhiQLVisitor eval = new SiddhiQLBaseVisitorImpl();
    return (SiddhiApp) eval.visit(tree);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) SiddhiApp(io.siddhi.query.api.SiddhiApp) SiddhiQLBaseVisitorImpl(io.siddhi.query.compiler.internal.SiddhiQLBaseVisitorImpl) CharStream(org.antlr.v4.runtime.CharStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 95 with SiddhiApp

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

the class SiddhiApiServiceImpl method siddhiArtifactDeployPost.

@Override
public Response siddhiArtifactDeployPost(String siddhiApp) throws NotFoundException {
    log.info("SiddhiApp = " + siddhiApp);
    String jsonString = new Gson().toString();
    try {
        SiddhiApp parsedSiddhiApp = SiddhiCompiler.parse(siddhiApp);
        String siddhiAppName = AnnotationHelper.getAnnotationElement(SiddhiServiceConstants.ANNOTATION_NAME_NAME, null, parsedSiddhiApp.getAnnotations()).getValue();
        if (!siddhiAppRunTimeMap.containsKey(siddhiApp)) {
            SiddhiAppConfiguration siddhiAppConfiguration = new SiddhiAppConfiguration();
            siddhiAppConfiguration.setName(siddhiAppName);
            siddhiAppConfigurationMap.put(siddhiAppName, siddhiAppConfiguration);
            SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
            if (siddhiAppRuntime != null) {
                Set<String> streamNames = siddhiAppRuntime.getStreamDefinitionMap().keySet();
                Map<String, InputHandler> inputHandlerMap = new ConcurrentHashMap<>(streamNames.size());
                for (String streamName : streamNames) {
                    inputHandlerMap.put(streamName, siddhiAppRuntime.getInputHandler(streamName));
                }
                siddhiAppSpecificInputHandlerMap.put(siddhiAppName, inputHandlerMap);
                siddhiAppRunTimeMap.put(siddhiAppName, siddhiAppRuntime);
                siddhiAppRuntime.start();
                jsonString = new Gson().toJson(new ApiResponseMessage(ApiResponseMessage.OK, "Siddhi app is deployed " + "and runtime is created"));
            }
        } else {
            jsonString = new Gson().toJson(new ApiResponseMessage(ApiResponseMessage.ERROR, "There is a Siddhi app already " + "exists with same name"));
        }
    } catch (Exception e) {
        jsonString = new Gson().toJson(new ApiResponseMessage(ApiResponseMessage.ERROR, e.getMessage()));
    }
    return Response.ok().entity(jsonString).build();
}
Also used : ApiResponseMessage(io.siddhi.service.api.ApiResponseMessage) InputHandler(io.siddhi.core.stream.input.InputHandler) SiddhiApp(io.siddhi.query.api.SiddhiApp) Gson(com.google.gson.Gson) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) SiddhiAppConfiguration(io.siddhi.service.util.SiddhiAppConfiguration) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NotFoundException(io.siddhi.service.api.NotFoundException)

Aggregations

SiddhiApp (io.siddhi.query.api.SiddhiApp)97 Test (org.testng.annotations.Test)93 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)90 SiddhiManager (io.siddhi.core.SiddhiManager)89 StreamDefinition (io.siddhi.query.api.definition.StreamDefinition)88 Query (io.siddhi.query.api.execution.query.Query)86 InputHandler (io.siddhi.core.stream.input.InputHandler)84 Event (io.siddhi.core.event.Event)82 QueryCallback (io.siddhi.core.query.output.callback.QueryCallback)74 Partition (io.siddhi.query.api.execution.partition.Partition)10 StreamCallback (io.siddhi.core.stream.output.StreamCallback)9 FunctionDefinition (io.siddhi.query.api.definition.FunctionDefinition)4 TriggerDefinition (io.siddhi.query.api.definition.TriggerDefinition)2 Gson (com.google.gson.Gson)1 Source (io.siddhi.core.stream.input.source.Source)1 Sink (io.siddhi.core.stream.output.sink.Sink)1 InMemoryTable (io.siddhi.core.table.InMemoryTable)1 Table (io.siddhi.core.table.Table)1 TableDefinition (io.siddhi.query.api.definition.TableDefinition)1 ExecutionElement (io.siddhi.query.api.execution.ExecutionElement)1