Search in sources :

Example 1 with MultiValueVariableFunctionExecutor

use of io.siddhi.core.executor.MultiValueVariableFunctionExecutor in project siddhi by wso2.

the class ExpressionParser method parseVariable.

/**
 * Parse and validate the given Siddhi variable and return a VariableExpressionExecutor
 *
 * @param variable           Variable to be parsed
 * @param metaEvent          Meta event used to collect execution info of stream associated with query
 * @param currentState       Current State Number
 * @param executorList       List to hold VariableExpressionExecutors to update after query parsing @return
 * @param siddhiQueryContext Siddhi Query Context
 * @return VariableExpressionExecutor representing given variable
 */
private static ExpressionExecutor parseVariable(Variable variable, MetaComplexEvent metaEvent, int currentState, List<VariableExpressionExecutor> executorList, int defaultStreamEventIndex, SiddhiQueryContext siddhiQueryContext) {
    String attributeName = variable.getAttributeName();
    int[] eventPosition = new int[2];
    if (variable.getStreamIndex() != null) {
        if (variable.getStreamIndex() <= SiddhiConstants.LAST) {
            eventPosition[SiddhiConstants.STREAM_EVENT_INDEX_IN_CHAIN] = variable.getStreamIndex() + 1;
        } else {
            eventPosition[SiddhiConstants.STREAM_EVENT_INDEX_IN_CHAIN] = variable.getStreamIndex();
        }
    } else {
        eventPosition[SiddhiConstants.STREAM_EVENT_INDEX_IN_CHAIN] = defaultStreamEventIndex;
    }
    eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = SiddhiConstants.UNKNOWN_STATE;
    if (metaEvent instanceof MetaStreamEvent) {
        MetaStreamEvent metaStreamEvent = (MetaStreamEvent) metaEvent;
        AbstractDefinition abstractDefinition;
        Attribute.Type type;
        if (currentState == SiddhiConstants.HAVING_STATE) {
            abstractDefinition = metaStreamEvent.getOutputStreamDefinition();
            type = abstractDefinition.getAttributeType(attributeName);
            eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = SiddhiConstants.HAVING_STATE;
        } else {
            abstractDefinition = metaStreamEvent.getLastInputDefinition();
            type = abstractDefinition.getAttributeType(attributeName);
            ((MetaStreamEvent) metaEvent).addData(new Attribute(attributeName, type));
        }
        if (variable.getStreamId() != null && !variable.getStreamId().equals(abstractDefinition.getId())) {
            throw new SiddhiAppCreationException("Id '" + variable.getStreamId() + "' not defined within the " + "current scope", variable, siddhiQueryContext.getSiddhiAppContext());
        }
        VariableExpressionExecutor variableExpressionExecutor = new VariableExpressionExecutor(new Attribute(attributeName, type), eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX], eventPosition[SiddhiConstants.STREAM_EVENT_INDEX_IN_CHAIN]);
        if (((MetaStreamEvent) metaEvent).getEventType() != MetaStreamEvent.EventType.DEFAULT) {
            variableExpressionExecutor.getPosition()[SiddhiConstants.STREAM_ATTRIBUTE_TYPE_INDEX] = SiddhiConstants.OUTPUT_DATA_INDEX;
            variableExpressionExecutor.getPosition()[SiddhiConstants.STREAM_ATTRIBUTE_INDEX_IN_TYPE] = abstractDefinition.getAttributePosition(variableExpressionExecutor.getAttribute().getName());
        }
        if (executorList != null) {
            executorList.add(variableExpressionExecutor);
        }
        return variableExpressionExecutor;
    } else {
        MetaStateEvent metaStateEvent = (MetaStateEvent) metaEvent;
        Attribute.Type type = null;
        AbstractDefinition definition = null;
        String firstInput = null;
        boolean multiValue = false;
        if (variable.getStreamId() == null) {
            MetaStreamEvent[] metaStreamEvents = metaStateEvent.getMetaStreamEvents();
            if (currentState == SiddhiConstants.HAVING_STATE) {
                definition = metaStateEvent.getOutputStreamDefinition();
                try {
                    type = definition.getAttributeType(attributeName);
                    eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = SiddhiConstants.HAVING_STATE;
                } catch (AttributeNotExistException e) {
                    currentState = SiddhiConstants.UNKNOWN_STATE;
                }
            }
            if (currentState == SiddhiConstants.UNKNOWN_STATE) {
                for (int i = 0; i < metaStreamEvents.length; i++) {
                    MetaStreamEvent metaStreamEvent = metaStreamEvents[i];
                    definition = metaStreamEvent.getLastInputDefinition();
                    if (type == null) {
                        try {
                            type = definition.getAttributeType(attributeName);
                            firstInput = "Input Stream: " + definition.getId() + " with " + "reference: " + metaStreamEvent.getInputReferenceId();
                            eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = i;
                        } catch (AttributeNotExistException e) {
                        // do nothing
                        }
                    } else {
                        try {
                            definition.getAttributeType(attributeName);
                            throw new SiddhiAppValidationException(firstInput + " and Input Stream: " + definition.getId() + " with " + "reference: " + metaStreamEvent.getInputReferenceId() + " contains attribute " + "with same" + " name '" + attributeName + "'");
                        } catch (AttributeNotExistException e) {
                        // do nothing as its expected
                        }
                    }
                }
            } else if (currentState >= 0) {
                MetaStreamEvent metaStreamEvent = metaStreamEvents[currentState];
                definition = metaStreamEvent.getLastInputDefinition();
                try {
                    type = definition.getAttributeType(attributeName);
                    eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = currentState;
                } catch (AttributeNotExistException e) {
                    ExpressionExecutor functionExecutor = constructEventExpressionExecutor(variable, currentState, siddhiQueryContext, eventPosition, metaStateEvent);
                    if (functionExecutor != null) {
                        return functionExecutor;
                    }
                    throw new SiddhiAppValidationException(e.getMessageWithOutContext() + " Input stream '" + definition.getId() + "' with reference '" + metaStreamEvent.getInputReferenceId() + "' for attribute '" + variable.getAttributeName() + "'", e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex());
                }
            }
        } else {
            MetaStreamEvent[] metaStreamEvents = metaStateEvent.getMetaStreamEvents();
            for (int i = 0, metaStreamEventsLength = metaStreamEvents.length; i < metaStreamEventsLength; i++) {
                MetaStreamEvent metaStreamEvent = metaStreamEvents[i];
                definition = metaStreamEvent.getLastInputDefinition();
                if (metaStreamEvent.getInputReferenceId() == null) {
                    if (definition.getId().equals(variable.getStreamId())) {
                        type = definition.getAttributeType(attributeName);
                        eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = i;
                        break;
                    }
                } else {
                    if (metaStreamEvent.getInputReferenceId().equals(variable.getStreamId())) {
                        type = definition.getAttributeType(attributeName);
                        eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = i;
                        if (currentState > -1 && metaStreamEvents[currentState].getInputReferenceId() != null && variable.getStreamIndex() != null && variable.getStreamIndex() <= SiddhiConstants.LAST) {
                            if (variable.getStreamId().equals(metaStreamEvents[currentState].getInputReferenceId())) {
                                eventPosition[SiddhiConstants.STREAM_EVENT_INDEX_IN_CHAIN] = variable.getStreamIndex();
                            }
                        } else if (currentState == SiddhiConstants.UNKNOWN_STATE && variable.getStreamIndex() == null) {
                            multiValue = metaStreamEvent.isMultiValue();
                        }
                        break;
                    }
                }
            }
        }
        if (eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] == SiddhiConstants.UNKNOWN_STATE) {
            if (variable.getStreamId() == null) {
                ExpressionExecutor functionExecutor = constructEventExpressionExecutor(variable, currentState, siddhiQueryContext, eventPosition, metaStateEvent);
                if (functionExecutor != null) {
                    return functionExecutor;
                }
                throw new SiddhiAppValidationException("No matching stream reference found for attribute '" + variable.getAttributeName() + "'");
            } else {
                throw new SiddhiAppValidationException("Stream with reference '" + variable.getStreamId() + "' not found for attribute '" + variable.getAttributeName() + "'");
            }
        }
        VariableExpressionExecutor variableExpressionExecutor = new VariableExpressionExecutor(new Attribute(attributeName, type), eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX], eventPosition[SiddhiConstants.STREAM_EVENT_INDEX_IN_CHAIN]);
        if (eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] != SiddhiConstants.HAVING_STATE) {
            MetaStreamEvent metaStreamEvent = ((MetaStateEvent) metaEvent).getMetaStreamEvent(eventPosition[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX]);
            if (metaStreamEvent.getEventType() != MetaStreamEvent.EventType.DEFAULT) {
                variableExpressionExecutor.getPosition()[SiddhiConstants.STREAM_ATTRIBUTE_TYPE_INDEX] = SiddhiConstants.OUTPUT_DATA_INDEX;
                variableExpressionExecutor.getPosition()[SiddhiConstants.STREAM_ATTRIBUTE_INDEX_IN_TYPE] = metaStreamEvent.getLastInputDefinition().getAttributePosition(variableExpressionExecutor.getAttribute().getName());
                for (Attribute attribute : metaStreamEvent.getLastInputDefinition().getAttributeList()) {
                    metaStreamEvent.addOutputData(new Attribute(attribute.getName(), attribute.getType()));
                }
            }
            metaStreamEvent.addData(new Attribute(attributeName, type));
        }
        if (executorList != null) {
            executorList.add(variableExpressionExecutor);
        }
        if (multiValue) {
            FunctionExecutor functionExecutor = new MultiValueVariableFunctionExecutor();
            ExpressionExecutor[] innerExpressionExecutors = new ExpressionExecutor[] { variableExpressionExecutor };
            functionExecutor.initExecutor(innerExpressionExecutors, ProcessingMode.BATCH, null, false, siddhiQueryContext);
            return functionExecutor;
        }
        return variableExpressionExecutor;
    }
}
Also used : IsNullConditionExpressionExecutor(io.siddhi.core.executor.condition.IsNullConditionExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) NotConditionExpressionExecutor(io.siddhi.core.executor.condition.NotConditionExpressionExecutor) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor) ConditionExpressionExecutor(io.siddhi.core.executor.condition.ConditionExpressionExecutor) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) OrConditionExpressionExecutor(io.siddhi.core.executor.condition.OrConditionExpressionExecutor) BoolConditionExpressionExecutor(io.siddhi.core.executor.condition.BoolConditionExpressionExecutor) InConditionExpressionExecutor(io.siddhi.core.executor.condition.InConditionExpressionExecutor) AndConditionExpressionExecutor(io.siddhi.core.executor.condition.AndConditionExpressionExecutor) IsNullStreamConditionExpressionExecutor(io.siddhi.core.executor.condition.IsNullStreamConditionExpressionExecutor) Attribute(io.siddhi.query.api.definition.Attribute) FunctionExecutor(io.siddhi.core.executor.function.FunctionExecutor) EventVariableFunctionExecutor(io.siddhi.core.executor.EventVariableFunctionExecutor) MultiValueVariableFunctionExecutor(io.siddhi.core.executor.MultiValueVariableFunctionExecutor) ScriptFunctionExecutor(io.siddhi.core.executor.function.ScriptFunctionExecutor) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) AbstractDefinition(io.siddhi.query.api.definition.AbstractDefinition) SiddhiAppValidationException(io.siddhi.query.api.exception.SiddhiAppValidationException) EqualCompareConditionExpressionExecutorStringString(io.siddhi.core.executor.condition.compare.equal.EqualCompareConditionExpressionExecutorStringString) NotEqualCompareConditionExpressionExecutorStringString(io.siddhi.core.executor.condition.compare.notequal.NotEqualCompareConditionExpressionExecutorStringString) MultiValueVariableFunctionExecutor(io.siddhi.core.executor.MultiValueVariableFunctionExecutor) MetaStateEvent(io.siddhi.core.event.state.MetaStateEvent) AttributeNotExistException(io.siddhi.query.api.exception.AttributeNotExistException) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent)

Aggregations

MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)1 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)1 SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)1 ConstantExpressionExecutor (io.siddhi.core.executor.ConstantExpressionExecutor)1 EventVariableFunctionExecutor (io.siddhi.core.executor.EventVariableFunctionExecutor)1 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)1 MultiValueVariableFunctionExecutor (io.siddhi.core.executor.MultiValueVariableFunctionExecutor)1 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)1 AndConditionExpressionExecutor (io.siddhi.core.executor.condition.AndConditionExpressionExecutor)1 BoolConditionExpressionExecutor (io.siddhi.core.executor.condition.BoolConditionExpressionExecutor)1 ConditionExpressionExecutor (io.siddhi.core.executor.condition.ConditionExpressionExecutor)1 InConditionExpressionExecutor (io.siddhi.core.executor.condition.InConditionExpressionExecutor)1 IsNullConditionExpressionExecutor (io.siddhi.core.executor.condition.IsNullConditionExpressionExecutor)1 IsNullStreamConditionExpressionExecutor (io.siddhi.core.executor.condition.IsNullStreamConditionExpressionExecutor)1 NotConditionExpressionExecutor (io.siddhi.core.executor.condition.NotConditionExpressionExecutor)1 OrConditionExpressionExecutor (io.siddhi.core.executor.condition.OrConditionExpressionExecutor)1 EqualCompareConditionExpressionExecutorStringString (io.siddhi.core.executor.condition.compare.equal.EqualCompareConditionExpressionExecutorStringString)1 NotEqualCompareConditionExpressionExecutorStringString (io.siddhi.core.executor.condition.compare.notequal.NotEqualCompareConditionExpressionExecutorStringString)1 FunctionExecutor (io.siddhi.core.executor.function.FunctionExecutor)1 ScriptFunctionExecutor (io.siddhi.core.executor.function.ScriptFunctionExecutor)1