Search in sources :

Example 1 with StreamFunction

use of org.wso2.siddhi.query.api.execution.query.input.handler.StreamFunction 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 StreamFunction

use of org.wso2.siddhi.query.api.execution.query.input.handler.StreamFunction in project siddhi by wso2.

the class SingleInputStreamParser method generateProcessor.

public static Processor generateProcessor(StreamHandler streamHandler, MetaComplexEvent metaEvent, List<VariableExpressionExecutor> variableExpressionExecutors, SiddhiAppContext siddhiAppContext, Map<String, Table> tableMap, boolean supportsBatchProcessing, boolean outputExpectsExpiredEvents, String queryName) {
    Expression[] parameters = streamHandler.getParameters();
    MetaStreamEvent metaStreamEvent;
    int stateIndex = SiddhiConstants.UNKNOWN_STATE;
    if (metaEvent instanceof MetaStateEvent) {
        stateIndex = ((MetaStateEvent) metaEvent).getStreamEventCount() - 1;
        metaStreamEvent = ((MetaStateEvent) metaEvent).getMetaStreamEvent(stateIndex);
    } else {
        metaStreamEvent = (MetaStreamEvent) metaEvent;
    }
    ExpressionExecutor[] attributeExpressionExecutors;
    if (parameters != null) {
        if (parameters.length > 0) {
            attributeExpressionExecutors = new ExpressionExecutor[parameters.length];
            for (int i = 0, parametersLength = parameters.length; i < parametersLength; i++) {
                attributeExpressionExecutors[i] = ExpressionParser.parseExpression(parameters[i], metaEvent, stateIndex, tableMap, variableExpressionExecutors, siddhiAppContext, false, SiddhiConstants.CURRENT, queryName);
            }
        } else {
            List<Attribute> attributeList = metaStreamEvent.getLastInputDefinition().getAttributeList();
            int parameterSize = attributeList.size();
            attributeExpressionExecutors = new ExpressionExecutor[parameterSize];
            for (int i = 0; i < parameterSize; i++) {
                attributeExpressionExecutors[i] = ExpressionParser.parseExpression(new Variable(attributeList.get(i).getName()), metaEvent, stateIndex, tableMap, variableExpressionExecutors, siddhiAppContext, false, SiddhiConstants.CURRENT, queryName);
            }
        }
    } else {
        attributeExpressionExecutors = new ExpressionExecutor[0];
    }
    ConfigReader configReader;
    if (streamHandler instanceof Filter) {
        return new FilterProcessor(attributeExpressionExecutors[0]);
    } else if (streamHandler instanceof Window) {
        WindowProcessor windowProcessor = (WindowProcessor) SiddhiClassLoader.loadExtensionImplementation((Extension) streamHandler, WindowProcessorExtensionHolder.getInstance(siddhiAppContext));
        configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(((Window) streamHandler).getNamespace(), ((Window) streamHandler).getName());
        windowProcessor.initProcessor(metaStreamEvent.getLastInputDefinition(), attributeExpressionExecutors, configReader, siddhiAppContext, outputExpectsExpiredEvents, queryName, streamHandler);
        return windowProcessor;
    } else if (streamHandler instanceof StreamFunction) {
        AbstractStreamProcessor abstractStreamProcessor;
        configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(((StreamFunction) streamHandler).getNamespace(), ((StreamFunction) streamHandler).getName());
        if (supportsBatchProcessing) {
            try {
                abstractStreamProcessor = (StreamProcessor) SiddhiClassLoader.loadExtensionImplementation((Extension) streamHandler, StreamProcessorExtensionHolder.getInstance(siddhiAppContext));
                metaStreamEvent.addInputDefinition(abstractStreamProcessor.initProcessor(metaStreamEvent.getLastInputDefinition(), attributeExpressionExecutors, configReader, siddhiAppContext, outputExpectsExpiredEvents, queryName, streamHandler));
                return abstractStreamProcessor;
            } catch (SiddhiAppCreationException e) {
                if (!e.isClassLoadingIssue()) {
                    ExceptionUtil.populateQueryContext(e, streamHandler, siddhiAppContext);
                    throw e;
                }
            }
        }
        abstractStreamProcessor = (StreamFunctionProcessor) SiddhiClassLoader.loadExtensionImplementation((Extension) streamHandler, StreamFunctionProcessorExtensionHolder.getInstance(siddhiAppContext));
        metaStreamEvent.addInputDefinition(abstractStreamProcessor.initProcessor(metaStreamEvent.getLastInputDefinition(), attributeExpressionExecutors, configReader, siddhiAppContext, outputExpectsExpiredEvents, queryName, streamHandler));
        return abstractStreamProcessor;
    } else {
        throw new SiddhiAppCreationException(streamHandler.getClass().getName() + " is not supported", streamHandler.getQueryContextStartIndex(), streamHandler.getQueryContextEndIndex());
    }
}
Also used : Window(org.wso2.siddhi.query.api.execution.query.input.handler.Window) Variable(org.wso2.siddhi.query.api.expression.Variable) VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) AbstractStreamProcessor(org.wso2.siddhi.core.query.processor.stream.AbstractStreamProcessor) Attribute(org.wso2.siddhi.query.api.definition.Attribute) FilterProcessor(org.wso2.siddhi.core.query.processor.filter.FilterProcessor) StreamFunction(org.wso2.siddhi.query.api.execution.query.input.handler.StreamFunction) SiddhiAppCreationException(org.wso2.siddhi.core.exception.SiddhiAppCreationException) ConfigReader(org.wso2.siddhi.core.util.config.ConfigReader) MetaStateEvent(org.wso2.siddhi.core.event.state.MetaStateEvent) Expression(org.wso2.siddhi.query.api.expression.Expression) Filter(org.wso2.siddhi.query.api.execution.query.input.handler.Filter) WindowProcessor(org.wso2.siddhi.core.query.processor.stream.window.WindowProcessor) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent)

Example 3 with StreamFunction

use of org.wso2.siddhi.query.api.execution.query.input.handler.StreamFunction in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitStream_function.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public StreamFunction visitStream_function(@NotNull SiddhiQLParser.Stream_functionContext ctx) {
    AttributeFunction attributeFunction = (AttributeFunction) visit(ctx.function_operation());
    StreamFunction streamFunction = new StreamFunction(attributeFunction.getNamespace(), attributeFunction.getName(), attributeFunction.getParameters());
    populateQueryContext(streamFunction, ctx);
    return streamFunction;
}
Also used : StreamFunction(org.wso2.siddhi.query.api.execution.query.input.handler.StreamFunction) AttributeFunction(org.wso2.siddhi.query.api.expression.AttributeFunction)

Aggregations

StreamFunction (org.wso2.siddhi.query.api.execution.query.input.handler.StreamFunction)2 ArrayList (java.util.ArrayList)1 MetaStateEvent (org.wso2.siddhi.core.event.state.MetaStateEvent)1 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)1 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)1 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)1 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)1 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)1 FilterProcessor (org.wso2.siddhi.core.query.processor.filter.FilterProcessor)1 AbstractStreamProcessor (org.wso2.siddhi.core.query.processor.stream.AbstractStreamProcessor)1 WindowProcessor (org.wso2.siddhi.core.query.processor.stream.window.WindowProcessor)1 ConfigReader (org.wso2.siddhi.core.util.config.ConfigReader)1 Attribute (org.wso2.siddhi.query.api.definition.Attribute)1 SiddhiAppValidationException (org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)1 Filter (org.wso2.siddhi.query.api.execution.query.input.handler.Filter)1 Window (org.wso2.siddhi.query.api.execution.query.input.handler.Window)1 AttributeFunction (org.wso2.siddhi.query.api.expression.AttributeFunction)1 Expression (org.wso2.siddhi.query.api.expression.Expression)1 Variable (org.wso2.siddhi.query.api.expression.Variable)1