Search in sources :

Example 1 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project siddhi by wso2.

the class LogStreamProcessor method init.

/**
 * The init method of the StreamFunction
 *
 * @param inputDefinition              the incoming stream definition
 * @param attributeExpressionExecutors the executors for the function parameters
 * @param siddhiAppContext         siddhi app context
 * @param configReader this hold the {@link LogStreamProcessor} configuration reader.
 * @return the additional output attributes introduced by the function
 */
@Override
protected List<Attribute> init(AbstractDefinition inputDefinition, ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, SiddhiAppContext siddhiAppContext) {
    int inputExecutorLength = attributeExpressionExecutors.length;
    if (inputExecutorLength == 1) {
        if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.STRING) {
            logMessageExpressionExecutor = attributeExpressionExecutors[0];
        } else if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.BOOL) {
            isLogEventExpressionExecutor = attributeExpressionExecutors[0];
        } else {
            throw new SiddhiAppValidationException("Input attribute is expected to be 'isEventLogged (Bool)' " + "or 'logMessage (String)' or 'isEventLogged (Bool), logMessage (String)' or 'priority " + "(String), isEventLogged (Bool), logMessage (String)', but its 1st attribute is " + attributeExpressionExecutors[0].getReturnType());
        }
    } else if (inputExecutorLength == 2) {
        if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.STRING && attributeExpressionExecutors[1].getReturnType() == Attribute.Type.BOOL) {
            logMessageExpressionExecutor = attributeExpressionExecutors[0];
            isLogEventExpressionExecutor = attributeExpressionExecutors[1];
        } else if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.STRING && attributeExpressionExecutors[1].getReturnType() == Attribute.Type.STRING) {
            if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
                logPriority = LogPriority.valueOf(((String) attributeExpressionExecutors[0].execute(null)).toUpperCase());
            } else {
                logPriorityExpressionExecutor = attributeExpressionExecutors[0];
            }
            logMessageExpressionExecutor = attributeExpressionExecutors[1];
        } else {
            throw new SiddhiAppValidationException("Input attribute is expected to be 'logMessage (String), " + "isEventLogged (Bool)' or 'priority (String), logMessage (String)', but its returning are '" + attributeExpressionExecutors[0].getReturnType() + ", " + attributeExpressionExecutors[1].getReturnType() + "'");
        }
    } else if (inputExecutorLength == 3) {
        if (attributeExpressionExecutors[0].getReturnType() == Attribute.Type.STRING) {
            if (attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor) {
                logPriority = LogPriority.valueOf(((String) attributeExpressionExecutors[0].execute(null)).toUpperCase());
            } else {
                logPriorityExpressionExecutor = attributeExpressionExecutors[0];
            }
        } else {
            throw new SiddhiAppValidationException("Input attribute is expected to be 'priority (String), " + "logMessage (String), isEventLogged (Bool)', but its 1st attribute is returning " + attributeExpressionExecutors[0].getReturnType());
        }
        if (attributeExpressionExecutors[1].getReturnType() == Attribute.Type.STRING) {
            logMessageExpressionExecutor = attributeExpressionExecutors[1];
        } else {
            throw new SiddhiAppValidationException("Input attribute is expected to be 'priority (String), " + "logMessage (String), isEventLogged (Bool)', but its 2nd attribute is returning " + attributeExpressionExecutors[1].getReturnType());
        }
        if (attributeExpressionExecutors[2].getReturnType() == Attribute.Type.BOOL) {
            isLogEventExpressionExecutor = attributeExpressionExecutors[2];
        } else {
            throw new SiddhiAppValidationException("Input attribute is expected to be 'priority (String), " + "logMessage (String), isEventLogged (Bool)', but its 3rd attribute is returning " + attributeExpressionExecutors[2].getReturnType());
        }
    } else if (inputExecutorLength > 3) {
        throw new SiddhiAppValidationException("Input parameters for Log can be logMessage (String), " + "isEventLogged (Bool), but there are " + attributeExpressionExecutors.length + " in the input!");
    }
    logPrefix = siddhiAppContext.getName() + ": ";
    return new ArrayList<Attribute>();
}
Also used : ArrayList(java.util.ArrayList) SiddhiAppValidationException(org.wso2.siddhi.query.api.exception.SiddhiAppValidationException) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor)

Example 2 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project siddhi by wso2.

the class QueryParserHelper method createThroughputTracker.

public static ThroughputTracker createThroughputTracker(SiddhiAppContext siddhiAppContext, String name, String type, String function) {
    ThroughputTracker throughputTracker = null;
    if (siddhiAppContext.getStatisticsManager() != null) {
        String metricName = siddhiAppContext.getSiddhiContext().getStatisticsConfiguration().getMetricPrefix() + SiddhiConstants.METRIC_DELIMITER + SiddhiConstants.METRIC_INFIX_SIDDHI_APPS + SiddhiConstants.METRIC_DELIMITER + siddhiAppContext.getName() + SiddhiConstants.METRIC_DELIMITER + SiddhiConstants.METRIC_INFIX_SIDDHI + SiddhiConstants.METRIC_DELIMITER + type + SiddhiConstants.METRIC_DELIMITER + name;
        if (function != null) {
            metricName += SiddhiConstants.METRIC_DELIMITER + function;
        }
        metricName += SiddhiConstants.METRIC_DELIMITER + "throughput";
        boolean matchExist = false;
        for (String regex : siddhiAppContext.getIncludedMetrics()) {
            if (metricName.matches(regex)) {
                matchExist = true;
                break;
            }
        }
        if (matchExist) {
            throughputTracker = siddhiAppContext.getSiddhiContext().getStatisticsConfiguration().getFactory().createThroughputTracker(metricName, siddhiAppContext.getStatisticsManager());
        }
    }
    return throughputTracker;
}
Also used : ThroughputTracker(org.wso2.siddhi.core.util.statistics.ThroughputTracker)

Example 3 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project siddhi by wso2.

the class ConvertFunctionTestCase method convertFunctionTest2.

@Test
public void convertFunctionTest2() throws InterruptedException {
    log.info("convert function test 2");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "" + "" + "define stream typeStream (typeS string, typeF float, typeD double, typeI int, typeL long, typeB " + "bool) ;";
    String query = "" + "@info(name = 'query1') " + "from typeStream " + "select convert(typeS,'string') as valueS1, convert(typeS,'float') as valueF1, convert(typeS," + "'double') as valueD1, convert(typeS,'int') as valueI1 , convert(typeS,'long') as valueL1 , convert" + "(typeS,'bool') as valueB1, " + "        convert(typeF,'string') as valueS2, convert(typeF,'float') as valueF2, convert(typeF," + "'double') as valueD2, convert(typeF,'int') as valueI2 , convert(typeF,'long') as valueL2 , convert" + "(typeF,'bool') as valueB2, " + "        convert(typeD,'string') as valueS3, convert(typeD,'float') as valueF3, convert(typeD," + "'double') as valueD3, convert(typeD,'int') as valueI3 , convert(typeD,'long') as valueL3 , convert" + "(typeD,'bool') as valueB3, " + "        convert(typeI,'string') as valueS4, convert(typeI,'float') as valueF4, convert(typeI," + "'double') as valueD4, convert(typeI,'int') as valueI4 , convert(typeI,'long') as valueL4 , convert" + "(typeI,'bool') as valueB4, " + "        convert(typeL,'string') as valueS5, convert(typeL,'float') as valueF5, convert(typeL," + "'double') as valueD5, convert(typeL,'int') as valueI5 , convert(typeL,'long') as valueL5 , convert" + "(typeL,'bool') as valueB5, " + "        convert(typeB,'string') as valueS6, convert(typeB,'float') as valueF6, convert(typeB," + "'double') as valueD6, convert(typeB,'int') as valueI6 , convert(typeB,'long') as valueL6 , convert" + "(typeB,'bool') as valueB6  " + "insert into outputStream ;";
    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++;
            AssertJUnit.assertTrue(inEvents[0].getData(0) instanceof String);
            AssertJUnit.assertTrue(inEvents[0].getData(1) == null);
            AssertJUnit.assertTrue(inEvents[0].getData(2) == null);
            AssertJUnit.assertTrue(inEvents[0].getData(3) == null);
            AssertJUnit.assertTrue(inEvents[0].getData(4) == null);
            AssertJUnit.assertTrue(inEvents[0].getData(5) instanceof Boolean && !((Boolean) inEvents[0].getData(5)));
            AssertJUnit.assertTrue(inEvents[0].getData(6) instanceof String);
            AssertJUnit.assertTrue(inEvents[0].getData(7) instanceof Float);
            AssertJUnit.assertTrue(inEvents[0].getData(8) instanceof Double);
            AssertJUnit.assertTrue(inEvents[0].getData(9) instanceof Integer);
            AssertJUnit.assertTrue(inEvents[0].getData(10) instanceof Long);
            AssertJUnit.assertTrue(inEvents[0].getData(11) instanceof Boolean && !((Boolean) inEvents[0].getData(11)));
            AssertJUnit.assertTrue(inEvents[0].getData(12) instanceof String);
            AssertJUnit.assertTrue(inEvents[0].getData(13) instanceof Float);
            AssertJUnit.assertTrue(inEvents[0].getData(14) instanceof Double);
            AssertJUnit.assertTrue(inEvents[0].getData(15) instanceof Integer);
            AssertJUnit.assertTrue(inEvents[0].getData(16) instanceof Long);
            AssertJUnit.assertTrue(inEvents[0].getData(17) instanceof Boolean && !((Boolean) inEvents[0].getData(17)));
            AssertJUnit.assertTrue(inEvents[0].getData(18) instanceof String);
            AssertJUnit.assertTrue(inEvents[0].getData(19) instanceof Float);
            AssertJUnit.assertTrue(inEvents[0].getData(20) instanceof Double);
            AssertJUnit.assertTrue(inEvents[0].getData(21) instanceof Integer);
            AssertJUnit.assertTrue(inEvents[0].getData(22) instanceof Long);
            AssertJUnit.assertTrue(inEvents[0].getData(23) instanceof Boolean && !((Boolean) inEvents[0].getData(23)));
            AssertJUnit.assertTrue(inEvents[0].getData(24) instanceof String);
            AssertJUnit.assertTrue(inEvents[0].getData(25) instanceof Float);
            AssertJUnit.assertTrue(inEvents[0].getData(26) instanceof Double);
            AssertJUnit.assertTrue(inEvents[0].getData(27) instanceof Integer);
            AssertJUnit.assertTrue(inEvents[0].getData(28) instanceof Long);
            AssertJUnit.assertTrue(inEvents[0].getData(29) instanceof Boolean && !((Boolean) inEvents[0].getData(29)));
            AssertJUnit.assertTrue(inEvents[0].getData(30) instanceof String);
            AssertJUnit.assertTrue(inEvents[0].getData(31) instanceof Float);
            AssertJUnit.assertTrue(inEvents[0].getData(32) instanceof Double);
            AssertJUnit.assertTrue(inEvents[0].getData(33) instanceof Integer);
            AssertJUnit.assertTrue(inEvents[0].getData(34) instanceof Long);
            AssertJUnit.assertTrue(inEvents[0].getData(35) instanceof Boolean && ((Boolean) inEvents[0].getData(35)));
        }
    });
    InputHandler inputHandler = siddhiAppRuntime.getInputHandler("typeStream");
    siddhiAppRuntime.start();
    inputHandler.send(new Object[] { "WSO2", 2f, 3d, 4, 5L, true });
    Thread.sleep(100);
    AssertJUnit.assertEquals(1, count);
    siddhiAppRuntime.shutdown();
}
Also used : InputHandler(org.wso2.siddhi.core.stream.input.InputHandler) QueryCallback(org.wso2.siddhi.core.query.output.callback.QueryCallback) SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 4 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project siddhi by wso2.

the class ConvertFunctionTestCase method convertFunctionExceptionTest7.

@Test(expectedExceptions = SiddhiAppCreationException.class)
public void convertFunctionExceptionTest7() throws InterruptedException {
    log.info("convert function test 7");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream typeStream (typeS string, typeF float, typeD double, typeI int, typeL " + "long, typeB bool, typeN double) ;";
    String query = "" + "@info(name = 'query1') " + "from typeStream " + "select convert(typeS,'234') as valueS, convert(typeF,'float') as valueF, convert(typeD," + "'double') as valueD , convert(typeI,'int') as valueI , convert(typeL,'long') as valueL , " + "convert(typeB,'bool') as valueB, convert(typeN,'string') as valueN " + "insert into outputStream ;";
    siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
}
Also used : SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 5 with Function

use of org.wso2.carbon.apimgt.core.models.Function in project siddhi by wso2.

the class ConvertFunctionTestCase method convertFunctionExceptionTest8.

@Test(expectedExceptions = SiddhiAppCreationException.class)
public void convertFunctionExceptionTest8() throws InterruptedException {
    log.info("convert function test 8");
    SiddhiManager siddhiManager = new SiddhiManager();
    String cseEventStream = "define stream typeStream (typeS string, typeF float, typeD double, typeI int, typeL " + "long, typeB bool, typeN double) ;";
    String query = "" + "@info(name = 'query1') " + "from typeStream " + "select convert(typeS,123) as valueS, convert(typeF,'float') as valueF, convert(typeD," + "'double') as valueD , convert(typeI,'int') as valueI , convert(typeL,'long') as valueL , " + "convert(typeB,'bool') as valueB, convert(typeN,'string') as valueN " + "insert into outputStream ;";
    siddhiManager.createSiddhiAppRuntime(cseEventStream + query);
}
Also used : SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)94 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)49 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)44 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)39 ArrayList (java.util.ArrayList)37 BLangFunction (org.wso2.ballerinalang.compiler.tree.BLangFunction)35 BString (org.ballerinalang.model.values.BString)30 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)29 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)26 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)25 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)20 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)20 BJSON (org.ballerinalang.model.values.BJSON)19 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)19 List (java.util.List)18 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)18 BLangStruct (org.wso2.ballerinalang.compiler.tree.BLangStruct)16 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)15 BPackageSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BPackageSymbol)14 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)14