Search in sources :

Example 26 with Extension

use of org.wso2.siddhi.query.api.extension.Extension in project siddhi by wso2.

the class DefinitionParserHelper method addFunction.

public static void addFunction(SiddhiAppContext siddhiAppContext, final FunctionDefinition functionDefinition) {
    Extension extension = new Extension() {

        @Override
        public String getNamespace() {
            return "script";
        }

        @Override
        public String getName() {
            return functionDefinition.getLanguage().toLowerCase();
        }
    };
    try {
        Script script = (Script) SiddhiClassLoader.loadExtensionImplementation(extension, ScriptExtensionHolder.getInstance(siddhiAppContext));
        ConfigReader configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(extension.getNamespace(), extension.getName());
        script.setReturnType(functionDefinition.getReturnType());
        script.init(functionDefinition.getId(), functionDefinition.getBody(), configReader);
        siddhiAppContext.getScriptFunctionMap().put(functionDefinition.getId(), script);
    } catch (Throwable t) {
        ExceptionUtil.populateQueryContext(t, functionDefinition, siddhiAppContext);
        throw t;
    }
}
Also used : Extension(org.wso2.siddhi.query.api.extension.Extension) Script(org.wso2.siddhi.core.function.Script) ConfigReader(org.wso2.siddhi.core.util.config.ConfigReader)

Example 27 with Extension

use of org.wso2.siddhi.query.api.extension.Extension in project siddhi by wso2.

the class MultiClientDistributedSink method initTransport.

@Override
public void initTransport(OptionHolder sinkOptionHolder, List<OptionHolder> destinationOptionHolders, Annotation sinkAnnotation, ConfigReader sinkConfigReader, SiddhiAppContext siddhiAppContext) {
    String transportType = sinkOptionHolder.validateAndGetStaticValue(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
    Extension sinkExtension = DefinitionParserHelper.constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_SINK, transportType, sinkAnnotation, SiddhiConstants.NAMESPACE_SINK);
    destinationOptionHolders.forEach(destinationOption -> {
        Sink sink = (Sink) SiddhiClassLoader.loadExtensionImplementation(sinkExtension, SinkExecutorExtensionHolder.getInstance(siddhiAppContext));
        destinationOption.merge(sinkOptionHolder);
        sink.initOnlyTransport(streamDefinition, destinationOption, sinkConfigReader, siddhiAppContext);
        transports.add(sink);
    });
}
Also used : Extension(org.wso2.siddhi.query.api.extension.Extension) Sink(org.wso2.siddhi.core.stream.output.sink.Sink)

Example 28 with Extension

use of org.wso2.siddhi.query.api.extension.Extension in project siddhi by wso2.

the class ExtensionTestCase method extensionTest5.

@Test
public void extensionTest5() throws InterruptedException, ClassNotFoundException {
    log.info("extension test5");
    SiddhiManager siddhiManager = new SiddhiManager();
    Map<String, String> configMap = new HashMap<>();
    configMap.put("email.getAllNew.append.abc", "true");
    siddhiManager.setConfigManager(new InMemoryConfigManager(configMap, null));
    siddhiManager.setExtension("custom:plus", CustomFunctionExtension.class);
    siddhiManager.setExtension("email:getAllNew", StringConcatAggregatorString.class);
    String cseEventStream = "" + "" + "define stream cseEventStream (symbol string, price float, volume long);";
    String query = ("" + "@info(name = 'query1') " + "from cseEventStream " + "select price , email:getAllNew(symbol,'') as toConcat " + "group by volume " + "insert into mailOutput;");
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timeStamp, inEvents, removeEvents);
            count = count + inEvents.length;
            if (count == 3) {
                AssertJUnit.assertEquals("WSO2ABC-abc", inEvents[inEvents.length - 1].getData(1));
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 700f, 100L });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 60.5f, 200L });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "ABC", 60.5f, 200L });
    Thread.sleep(100);
    AssertJUnit.assertEquals(3, count);
    AssertJUnit.assertTrue(eventArrived);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) InMemoryConfigManager(org.wso2.siddhi.core.util.config.InMemoryConfigManager) HashMap(java.util.HashMap) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) StringConcatAggregatorString(org.wso2.siddhi.core.query.extension.util.StringConcatAggregatorString) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 29 with Extension

use of org.wso2.siddhi.query.api.extension.Extension in project siddhi by wso2.

the class ExtensionTestCase method extensionTest2.

@Test
public void extensionTest2() throws InterruptedException, ClassNotFoundException {
    log.info("extension test2");
    SiddhiManager siddhiManager = new SiddhiManager();
    siddhiManager.setExtension("custom:plus", CustomFunctionExtension.class);
    siddhiManager.setExtension("email:getAll", StringConcatAggregatorString.class);
    String cseEventStream = "define stream cseEventStream (symbol string, price long, volume long);";
    String query = ("@info(name = 'query1') from cseEventStream select symbol , custom:plus(price,volume) as " + "totalCount " + "insert into mailOutput;");
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            for (Event inEvent : inEvents) {
                count++;
                if (count == 1) {
                    AssertJUnit.assertEquals(800L, inEvent.getData(1));
                } else if (count == 2) {
                    AssertJUnit.assertEquals(805L, inEvent.getData(1));
                } else if (count == 3) {
                    AssertJUnit.assertEquals(260L, inEvent.getData(1));
                }
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 700L, 100L });
    inputHandler.send(new Object[] { "WSO2", 605L, 200L });
    inputHandler.send(new Object[] { "ABC", 60L, 200L });
    Thread.sleep(100);
    AssertJUnit.assertEquals(3, 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) StringConcatAggregatorString(org.wso2.siddhi.core.query.extension.util.StringConcatAggregatorString) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Example 30 with Extension

use of org.wso2.siddhi.query.api.extension.Extension in project siddhi by wso2.

the class ExtensionTestCase method extensionTest1.

@Test
public void extensionTest1() throws InterruptedException {
    log.info("extension test1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream cseEventStream (symbol string, price float, volume long);";
    String query = ("@info(name = 'query1') from cseEventStream select price , custom:getAll(symbol) as toConcat " + "group by volume insert into mailOutput;");
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
    siddhiAppRuntime.addCallback("query1", new QueryCallback() {

        @Override
        public void receive(long timestamp, Event[] inEvents, Event[] removeEvents) {
            EventPrinter.print(timestamp, inEvents, removeEvents);
            count = count + inEvents.length;
            if (count == 3) {
                AssertJUnit.assertEquals("WSO2ABC", inEvents[inEvents.length - 1].getData(1));
            }
            eventArrived = true;
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("cseEventStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "IBM", 700f, 100L });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "WSO2", 60.5f, 200L });
    Thread.sleep(100);
    inputHandler.send(new Object[] { "ABC", 60.5f, 200L });
    Thread.sleep(100);
    AssertJUnit.assertEquals(3, 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) StringConcatAggregatorString(org.wso2.siddhi.core.query.extension.util.StringConcatAggregatorString) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) Test(org.testng.annotations.Test)

Aggregations

ArrayList (java.util.ArrayList)12 Attribute (org.wso2.charon3.core.attributes.Attribute)10 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)10 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)10 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)10 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)7 Extension (org.wso2.siddhi.query.api.extension.Extension)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 List (java.util.List)6 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)6 ConfigReader (org.wso2.siddhi.core.util.config.ConfigReader)6 Test (org.testng.annotations.Test)5 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)5 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)5 Event (org.wso2.siddhi.core.event.Event)5 StringConcatAggregatorString (org.wso2.siddhi.core.query.extension.util.StringConcatAggregatorString)5 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)5 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)5 Map (java.util.Map)4