Java Code Examples for io.siddhi.query.compiler.SiddhiCompiler#parse()

The following examples show how to use io.siddhi.query.compiler.SiddhiCompiler#parse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PolicyExecutionPlannerImpl.java    From eagle with Apache License 2.0 6 votes vote down vote up
private String validateAndGet(String executionPlan) {
    try {
        SiddhiCompiler.parse(executionPlan);
        return executionPlan;
    } catch (SiddhiParserException e) {
        // There should be at least 1 stream definition for compiler to parse the execution plan.
        // Therefore, try prepending a IgnoreStream definition to the execution plan.
        String ignoreStreamDef = "define stream IgnoreStream (ignored bool); ";
        try {
            String epWithStreamDef = ignoreStreamDef + executionPlan;
            SiddhiCompiler.parse(epWithStreamDef);
            return epWithStreamDef;
        } catch (Exception ex) {
            return executionPlan;
        }
    }
}
 
Example 2
Source File: SimpleQueryTestCase.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompilerParse() {
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume long, available " +
            "bool);";

    SiddhiCompiler.parse(cseEventStream);
}
 
Example 3
Source File: SandboxTestCase.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Test
public void sandboxTest2() throws InterruptedException {
    log.info("sandbox test2");

    SiddhiManager siddhiManager = new SiddhiManager();

    String app = "" +
            "@source(type='foo')" +
            "@source(type='foo1')" +
            "@sink(type='foo1')" +
            "@source(type='inMemory', topic='myTopic')" +
            "define stream StockStream (symbol string, price float, vol long);\n" +
            "" +
            "@sink(type='foo1')" +
            "@sink(type='inMemory', topic='myTopic1')" +
            "define stream DeleteStockStream (symbol string, price float, vol long);\n" +
            "" +
            "@store(type='rdbms')" +
            "define table StockTable (symbol string, price float, volume long);\n" +
            "" +
            "define stream CountStockStream (symbol string);\n" +
            "" +
            "@info(name = 'query1') " +
            "from StockStream " +
            "select symbol, price, vol as volume " +
            "insert into StockTable ;" +
            "" +
            "@info(name = 'query2') " +
            "from DeleteStockStream[vol>=100] " +
            "delete StockTable " +
            "   on StockTable.symbol==symbol ;" +
            "" +
            "@info(name = 'query3') " +
            "from CountStockStream#window.length(0) join StockTable" +
            " on CountStockStream.symbol==StockTable.symbol " +
            "select CountStockStream.symbol as symbol " +
            "insert into CountResultsStream ;";

    SiddhiApp siddhiApp = SiddhiCompiler.parse(app);
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSandboxSiddhiAppRuntime(siddhiApp);

    Assert.assertEquals(siddhiAppRuntime.getSources().size(), 1);
    Assert.assertEquals(siddhiAppRuntime.getSinks().size(), 1);
    Assert.assertEquals(siddhiAppRuntime.getTables().size(), 1);

    for (List<Source> sources : siddhiAppRuntime.getSources()) {
        for (Source source : sources) {
            Assert.assertTrue(source.getType().equalsIgnoreCase("inMemory"));
        }
    }

    for (List<Sink> sinks : siddhiAppRuntime.getSinks()) {
        for (Sink sink : sinks) {
            Assert.assertTrue(sink.getType().equalsIgnoreCase("inMemory"));
        }
    }

    for (Table table : siddhiAppRuntime.getTables()) {
        Assert.assertTrue(table instanceof InMemoryTable);
    }

    InputHandler stockStream = siddhiAppRuntime.getInputHandler("StockStream");
    InputHandler deleteStockStream = siddhiAppRuntime.getInputHandler("DeleteStockStream");
    InputHandler countStockStream = siddhiAppRuntime.getInputHandler("CountStockStream");

    siddhiAppRuntime.addCallback("CountResultsStream", new StreamCallback() {
        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            count.addAndGet(events.length);
        }
    });
    siddhiAppRuntime.start();

    stockStream.send(new Object[]{"WSO2", 55.6f, 100L});
    stockStream.send(new Object[]{"IBM", 75.6f, 100L});
    stockStream.send(new Object[]{"WSO2", 57.6f, 100L});
    deleteStockStream.send(new Object[]{"IBM", 57.6f, 100L});
    countStockStream.send(new Object[]{"WSO2"});

    Thread.sleep(500);
    Assert.assertEquals(count.get(), 2);
    siddhiAppRuntime.shutdown();

}
 
Example 4
Source File: SiddhiApiServiceImpl.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@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();
}