Search in sources :

Example 1 with MetaStateEvent

use of org.ballerinalang.siddhi.core.event.state.MetaStateEvent in project ballerina by ballerina-lang.

the class ExpressionParser method parseExpression.

/**
 * Parse the given expression and create the appropriate Executor by recursively traversing the expression
 *
 * @param expression              Expression to be parsed
 * @param metaEvent               Meta Event
 * @param currentState            Current state number
 * @param tableMap                Event Table Map
 * @param executorList            List to hold VariableExpressionExecutors to update after query parsing @return
 * @param siddhiAppContext        SiddhiAppContext
 * @param groupBy                 is for groupBy expression
 * @param defaultStreamEventIndex Default StreamEvent Index
 * @param queryName               query name of expression belongs to.
 * @return ExpressionExecutor
 */
public static ExpressionExecutor parseExpression(Expression expression, MetaComplexEvent metaEvent, int currentState, Map<String, Table> tableMap, List<VariableExpressionExecutor> executorList, SiddhiAppContext siddhiAppContext, boolean groupBy, int defaultStreamEventIndex, String queryName) {
    try {
        if (expression instanceof And) {
            return new AndConditionExpressionExecutor(parseExpression(((And) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((And) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
        } else if (expression instanceof Or) {
            return new OrConditionExpressionExecutor(parseExpression(((Or) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Or) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
        } else if (expression instanceof Not) {
            return new NotConditionExpressionExecutor(parseExpression(((Not) expression).getExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
        } else if (expression instanceof Compare) {
            if (((Compare) expression).getOperator() == Compare.Operator.EQUAL) {
                return parseEqualCompare(parseExpression(((Compare) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Compare) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
            } else if (((Compare) expression).getOperator() == Compare.Operator.NOT_EQUAL) {
                return parseNotEqualCompare(parseExpression(((Compare) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Compare) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
            } else if (((Compare) expression).getOperator() == Compare.Operator.GREATER_THAN) {
                return parseGreaterThanCompare(parseExpression(((Compare) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Compare) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
            } else if (((Compare) expression).getOperator() == Compare.Operator.GREATER_THAN_EQUAL) {
                return parseGreaterThanEqualCompare(parseExpression(((Compare) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Compare) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
            } else if (((Compare) expression).getOperator() == Compare.Operator.LESS_THAN) {
                return parseLessThanCompare(parseExpression(((Compare) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Compare) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
            } else if (((Compare) expression).getOperator() == Compare.Operator.LESS_THAN_EQUAL) {
                return parseLessThanEqualCompare(parseExpression(((Compare) expression).getLeftExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName), parseExpression(((Compare) expression).getRightExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName));
            }
        } else if (expression instanceof Constant) {
            if (expression instanceof BoolConstant) {
                return new ConstantExpressionExecutor(((BoolConstant) expression).getValue(), Attribute.Type.BOOL);
            } else if (expression instanceof StringConstant) {
                return new ConstantExpressionExecutor(((StringConstant) expression).getValue(), Attribute.Type.STRING);
            } else if (expression instanceof IntConstant) {
                return new ConstantExpressionExecutor(((IntConstant) expression).getValue(), Attribute.Type.INT);
            } else if (expression instanceof LongConstant) {
                return new ConstantExpressionExecutor(((LongConstant) expression).getValue(), Attribute.Type.LONG);
            } else if (expression instanceof FloatConstant) {
                return new ConstantExpressionExecutor(((FloatConstant) expression).getValue(), Attribute.Type.FLOAT);
            } else if (expression instanceof DoubleConstant) {
                return new ConstantExpressionExecutor(((DoubleConstant) expression).getValue(), Attribute.Type.DOUBLE);
            }
        } else if (expression instanceof Variable) {
            return parseVariable((Variable) expression, metaEvent, currentState, executorList, defaultStreamEventIndex);
        } else if (expression instanceof Multiply) {
            ExpressionExecutor left = parseExpression(((Multiply) expression).getLeftValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            ExpressionExecutor right = parseExpression(((Multiply) expression).getRightValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            Attribute.Type type = parseArithmeticOperationResultType(left, right);
            switch(type) {
                case INT:
                    return new MultiplyExpressionExecutorInt(left, right);
                case LONG:
                    return new MultiplyExpressionExecutorLong(left, right);
                case FLOAT:
                    return new MultiplyExpressionExecutorFloat(left, right);
                case DOUBLE:
                    return new MultiplyExpressionExecutorDouble(left, right);
                // Will not happen. Handled in parseArithmeticOperationResultType()
                default:
            }
        } else if (expression instanceof Add) {
            ExpressionExecutor left = parseExpression(((Add) expression).getLeftValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            ExpressionExecutor right = parseExpression(((Add) expression).getRightValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            Attribute.Type type = parseArithmeticOperationResultType(left, right);
            switch(type) {
                case INT:
                    return new AddExpressionExecutorInt(left, right);
                case LONG:
                    return new AddExpressionExecutorLong(left, right);
                case FLOAT:
                    return new AddExpressionExecutorFloat(left, right);
                case DOUBLE:
                    return new AddExpressionExecutorDouble(left, right);
                // Will not happen. Handled in parseArithmeticOperationResultType()
                default:
            }
        } else if (expression instanceof Subtract) {
            ExpressionExecutor left = parseExpression(((Subtract) expression).getLeftValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            ExpressionExecutor right = parseExpression(((Subtract) expression).getRightValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            Attribute.Type type = parseArithmeticOperationResultType(left, right);
            switch(type) {
                case INT:
                    return new SubtractExpressionExecutorInt(left, right);
                case LONG:
                    return new SubtractExpressionExecutorLong(left, right);
                case FLOAT:
                    return new SubtractExpressionExecutorFloat(left, right);
                case DOUBLE:
                    return new SubtractExpressionExecutorDouble(left, right);
                // Will not happen. Handled in parseArithmeticOperationResultType()
                default:
            }
        } else if (expression instanceof Mod) {
            ExpressionExecutor left = parseExpression(((Mod) expression).getLeftValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            ExpressionExecutor right = parseExpression(((Mod) expression).getRightValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            Attribute.Type type = parseArithmeticOperationResultType(left, right);
            switch(type) {
                case INT:
                    return new ModExpressionExecutorInt(left, right);
                case LONG:
                    return new ModExpressionExecutorLong(left, right);
                case FLOAT:
                    return new ModExpressionExecutorFloat(left, right);
                case DOUBLE:
                    return new ModExpressionExecutorDouble(left, right);
                // Will not happen. Handled in parseArithmeticOperationResultType()
                default:
            }
        } else if (expression instanceof Divide) {
            ExpressionExecutor left = parseExpression(((Divide) expression).getLeftValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            ExpressionExecutor right = parseExpression(((Divide) expression).getRightValue(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
            Attribute.Type type = parseArithmeticOperationResultType(left, right);
            switch(type) {
                case INT:
                    return new DivideExpressionExecutorInt(left, right);
                case LONG:
                    return new DivideExpressionExecutorLong(left, right);
                case FLOAT:
                    return new DivideExpressionExecutorFloat(left, right);
                case DOUBLE:
                    return new DivideExpressionExecutorDouble(left, right);
                // Will not happen. Handled in parseArithmeticOperationResultType()
                default:
            }
        } else if (expression instanceof AttributeFunction) {
            // extensions
            Object executor;
            try {
                if ((siddhiAppContext.isFunctionExist(((AttributeFunction) expression).getName())) && (((AttributeFunction) expression).getNamespace()).isEmpty()) {
                    executor = new ScriptFunctionExecutor(((AttributeFunction) expression).getName());
                } else {
                    executor = SiddhiClassLoader.loadExtensionImplementation((AttributeFunction) expression, FunctionExecutorExtensionHolder.getInstance(siddhiAppContext));
                }
            } catch (SiddhiAppCreationException ex) {
                try {
                    executor = SiddhiClassLoader.loadExtensionImplementation((AttributeFunction) expression, AttributeAggregatorExtensionHolder.getInstance(siddhiAppContext));
                } catch (SiddhiAppCreationException e) {
                    throw new SiddhiAppCreationException("'" + ((AttributeFunction) expression).getName() + "' is" + " neither a function extension nor an aggregated attribute extension", expression.getQueryContextStartIndex(), expression.getQueryContextEndIndex());
                }
            }
            ConfigReader configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(((AttributeFunction) expression).getNamespace(), ((AttributeFunction) expression).getName());
            if (executor instanceof FunctionExecutor) {
                FunctionExecutor expressionExecutor = (FunctionExecutor) executor;
                Expression[] innerExpressions = ((AttributeFunction) expression).getParameters();
                ExpressionExecutor[] innerExpressionExecutors = parseInnerExpression(innerExpressions, metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
                expressionExecutor.initExecutor(innerExpressionExecutors, siddhiAppContext, queryName, configReader);
                if (expressionExecutor.getReturnType() == Attribute.Type.BOOL) {
                    return new BoolConditionExpressionExecutor(expressionExecutor);
                }
                return expressionExecutor;
            } else {
                AttributeAggregator attributeAggregator = (AttributeAggregator) executor;
                Expression[] innerExpressions = ((AttributeFunction) expression).getParameters();
                ExpressionExecutor[] innerExpressionExecutors = parseInnerExpression(innerExpressions, metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
                attributeAggregator.initAggregator(innerExpressionExecutors, siddhiAppContext, configReader);
                AbstractAggregationAttributeExecutor aggregationAttributeProcessor;
                if (groupBy) {
                    aggregationAttributeProcessor = new GroupByAggregationAttributeExecutor(attributeAggregator, innerExpressionExecutors, configReader, siddhiAppContext, queryName);
                } else {
                    aggregationAttributeProcessor = new AggregationAttributeExecutor(attributeAggregator, innerExpressionExecutors, siddhiAppContext, queryName);
                }
                SelectorParser.getContainsAggregatorThreadLocal().set("true");
                return aggregationAttributeProcessor;
            }
        } else if (expression instanceof In) {
            Table table = tableMap.get(((In) expression).getSourceId());
            MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(metaEvent, defaultStreamEventIndex, table.getTableDefinition(), defaultStreamEventIndex);
            CompiledCondition compiledCondition = table.compileCondition(((In) expression).getExpression(), matchingMetaInfoHolder, siddhiAppContext, executorList, tableMap, queryName);
            return new InConditionExpressionExecutor(table, compiledCondition, matchingMetaInfoHolder.getMetaStateEvent().getMetaStreamEvents().length, metaEvent instanceof StateEvent, 0);
        } else if (expression instanceof IsNull) {
            IsNull isNull = (IsNull) expression;
            if (isNull.getExpression() != null) {
                ExpressionExecutor innerExpressionExecutor = parseExpression(isNull.getExpression(), metaEvent, currentState, tableMap, executorList, siddhiAppContext, groupBy, defaultStreamEventIndex, queryName);
                return new IsNullConditionExpressionExecutor(innerExpressionExecutor);
            } else {
                String streamId = isNull.getStreamId();
                Integer streamIndex = isNull.getStreamIndex();
                if (metaEvent instanceof MetaStateEvent) {
                    int[] eventPosition = new int[2];
                    if (streamIndex != null) {
                        if (streamIndex <= LAST) {
                            eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = streamIndex + 1;
                        } else {
                            eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = streamIndex;
                        }
                    } else {
                        eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = defaultStreamEventIndex;
                    }
                    eventPosition[STREAM_EVENT_CHAIN_INDEX] = UNKNOWN_STATE;
                    MetaStateEvent metaStateEvent = (MetaStateEvent) metaEvent;
                    if (streamId == null) {
                        throw new SiddhiAppCreationException("IsNull does not support streamId being null", expression.getQueryContextStartIndex(), expression.getQueryContextEndIndex());
                    } else {
                        MetaStreamEvent[] metaStreamEvents = metaStateEvent.getMetaStreamEvents();
                        for (int i = 0, metaStreamEventsLength = metaStreamEvents.length; i < metaStreamEventsLength; i++) {
                            MetaStreamEvent metaStreamEvent = metaStreamEvents[i];
                            AbstractDefinition definition = metaStreamEvent.getLastInputDefinition();
                            if (metaStreamEvent.getInputReferenceId() == null) {
                                if (definition.getId().equals(streamId)) {
                                    eventPosition[STREAM_EVENT_CHAIN_INDEX] = i;
                                    break;
                                }
                            } else {
                                if (metaStreamEvent.getInputReferenceId().equals(streamId)) {
                                    eventPosition[STREAM_EVENT_CHAIN_INDEX] = i;
                                    if (currentState > -1 && metaStreamEvents[currentState].getInputReferenceId() != null && streamIndex != null && streamIndex <= LAST) {
                                        if (streamId.equals(metaStreamEvents[currentState].getInputReferenceId())) {
                                            eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = streamIndex;
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                    }
                    return new IsNullStreamConditionExpressionExecutor(eventPosition);
                } else {
                    return new IsNullStreamConditionExpressionExecutor(null);
                }
            }
        }
        throw new UnsupportedOperationException(expression.toString() + " not supported!");
    } catch (Throwable t) {
        ExceptionUtil.populateQueryContext(t, expression, siddhiAppContext);
        throw t;
    }
}
Also used : Add(org.ballerinalang.siddhi.query.api.expression.math.Add) Or(org.ballerinalang.siddhi.query.api.expression.condition.Or) DivideExpressionExecutorFloat(org.ballerinalang.siddhi.core.executor.math.divide.DivideExpressionExecutorFloat) ModExpressionExecutorDouble(org.ballerinalang.siddhi.core.executor.math.mod.ModExpressionExecutorDouble) ModExpressionExecutorInt(org.ballerinalang.siddhi.core.executor.math.mod.ModExpressionExecutorInt) EqualCompareConditionExpressionExecutorStringString(org.ballerinalang.siddhi.core.executor.condition.compare.equal.EqualCompareConditionExpressionExecutorStringString) NotEqualCompareConditionExpressionExecutorStringString(org.ballerinalang.siddhi.core.executor.condition.compare.notequal.NotEqualCompareConditionExpressionExecutorStringString) ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor) Divide(org.ballerinalang.siddhi.query.api.expression.math.Divide) MultiplyExpressionExecutorLong(org.ballerinalang.siddhi.core.executor.math.multiply.MultiplyExpressionExecutorLong) AddExpressionExecutorLong(org.ballerinalang.siddhi.core.executor.math.add.AddExpressionExecutorLong) Multiply(org.ballerinalang.siddhi.query.api.expression.math.Multiply) AddExpressionExecutorInt(org.ballerinalang.siddhi.core.executor.math.add.AddExpressionExecutorInt) Compare(org.ballerinalang.siddhi.query.api.expression.condition.Compare) DivideExpressionExecutorDouble(org.ballerinalang.siddhi.core.executor.math.divide.DivideExpressionExecutorDouble) ScriptFunctionExecutor(org.ballerinalang.siddhi.core.executor.function.ScriptFunctionExecutor) FunctionExecutor(org.ballerinalang.siddhi.core.executor.function.FunctionExecutor) AddExpressionExecutorFloat(org.ballerinalang.siddhi.core.executor.math.add.AddExpressionExecutorFloat) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) AttributeAggregator(org.ballerinalang.siddhi.core.query.selector.attribute.aggregator.AttributeAggregator) AbstractDefinition(org.ballerinalang.siddhi.query.api.definition.AbstractDefinition) GroupByAggregationAttributeExecutor(org.ballerinalang.siddhi.core.query.selector.attribute.processor.executor.GroupByAggregationAttributeExecutor) Not(org.ballerinalang.siddhi.query.api.expression.condition.Not) MultiplyExpressionExecutorFloat(org.ballerinalang.siddhi.core.executor.math.multiply.MultiplyExpressionExecutorFloat) CompiledCondition(org.ballerinalang.siddhi.core.util.collection.operator.CompiledCondition) And(org.ballerinalang.siddhi.query.api.expression.condition.And) MatchingMetaInfoHolder(org.ballerinalang.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) SubtractExpressionExecutorLong(org.ballerinalang.siddhi.core.executor.math.subtract.SubtractExpressionExecutorLong) IsNullStreamConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.IsNullStreamConditionExpressionExecutor) Subtract(org.ballerinalang.siddhi.query.api.expression.math.Subtract) StringConstant(org.ballerinalang.siddhi.query.api.expression.constant.StringConstant) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) DoubleConstant(org.ballerinalang.siddhi.query.api.expression.constant.DoubleConstant) AbstractAggregationAttributeExecutor(org.ballerinalang.siddhi.core.query.selector.attribute.processor.executor.AbstractAggregationAttributeExecutor) Variable(org.ballerinalang.siddhi.query.api.expression.Variable) MultiplyExpressionExecutorDouble(org.ballerinalang.siddhi.core.executor.math.multiply.MultiplyExpressionExecutorDouble) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) In(org.ballerinalang.siddhi.query.api.expression.condition.In) FloatConstant(org.ballerinalang.siddhi.query.api.expression.constant.FloatConstant) StringConstant(org.ballerinalang.siddhi.query.api.expression.constant.StringConstant) LongConstant(org.ballerinalang.siddhi.query.api.expression.constant.LongConstant) DoubleConstant(org.ballerinalang.siddhi.query.api.expression.constant.DoubleConstant) BoolConstant(org.ballerinalang.siddhi.query.api.expression.constant.BoolConstant) IntConstant(org.ballerinalang.siddhi.query.api.expression.constant.IntConstant) Constant(org.ballerinalang.siddhi.query.api.expression.constant.Constant) ModExpressionExecutorLong(org.ballerinalang.siddhi.core.executor.math.mod.ModExpressionExecutorLong) ScriptFunctionExecutor(org.ballerinalang.siddhi.core.executor.function.ScriptFunctionExecutor) InConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.InConditionExpressionExecutor) FloatConstant(org.ballerinalang.siddhi.query.api.expression.constant.FloatConstant) AddExpressionExecutorDouble(org.ballerinalang.siddhi.core.executor.math.add.AddExpressionExecutorDouble) AndConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.AndConditionExpressionExecutor) MultiplyExpressionExecutorInt(org.ballerinalang.siddhi.core.executor.math.multiply.MultiplyExpressionExecutorInt) IsNullConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.IsNullConditionExpressionExecutor) SubtractExpressionExecutorInt(org.ballerinalang.siddhi.core.executor.math.subtract.SubtractExpressionExecutorInt) IntConstant(org.ballerinalang.siddhi.query.api.expression.constant.IntConstant) SubtractExpressionExecutorDouble(org.ballerinalang.siddhi.core.executor.math.subtract.SubtractExpressionExecutorDouble) GroupByAggregationAttributeExecutor(org.ballerinalang.siddhi.core.query.selector.attribute.processor.executor.GroupByAggregationAttributeExecutor) AggregationAttributeExecutor(org.ballerinalang.siddhi.core.query.selector.attribute.processor.executor.AggregationAttributeExecutor) AbstractAggregationAttributeExecutor(org.ballerinalang.siddhi.core.query.selector.attribute.processor.executor.AbstractAggregationAttributeExecutor) LongConstant(org.ballerinalang.siddhi.query.api.expression.constant.LongConstant) BoolConstant(org.ballerinalang.siddhi.query.api.expression.constant.BoolConstant) Mod(org.ballerinalang.siddhi.query.api.expression.math.Mod) Table(org.ballerinalang.siddhi.core.table.Table) InConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.InConditionExpressionExecutor) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) NotConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.NotConditionExpressionExecutor) ConstantExpressionExecutor(org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor) BoolConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.BoolConditionExpressionExecutor) IsNullStreamConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.IsNullStreamConditionExpressionExecutor) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) OrConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.OrConditionExpressionExecutor) AndConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.AndConditionExpressionExecutor) IsNullConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.IsNullConditionExpressionExecutor) ConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.ConditionExpressionExecutor) ConfigReader(org.ballerinalang.siddhi.core.util.config.ConfigReader) OrConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.OrConditionExpressionExecutor) DivideExpressionExecutorLong(org.ballerinalang.siddhi.core.executor.math.divide.DivideExpressionExecutorLong) AttributeFunction(org.ballerinalang.siddhi.query.api.expression.AttributeFunction) DivideExpressionExecutorInt(org.ballerinalang.siddhi.core.executor.math.divide.DivideExpressionExecutorInt) SubtractExpressionExecutorFloat(org.ballerinalang.siddhi.core.executor.math.subtract.SubtractExpressionExecutorFloat) MetaStateEvent(org.ballerinalang.siddhi.core.event.state.MetaStateEvent) NotConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.NotConditionExpressionExecutor) ModExpressionExecutorFloat(org.ballerinalang.siddhi.core.executor.math.mod.ModExpressionExecutorFloat) MetaStateEvent(org.ballerinalang.siddhi.core.event.state.MetaStateEvent) StateEvent(org.ballerinalang.siddhi.core.event.state.StateEvent) IsNull(org.ballerinalang.siddhi.query.api.expression.condition.IsNull) BoolConditionExpressionExecutor(org.ballerinalang.siddhi.core.executor.condition.BoolConditionExpressionExecutor)

Example 2 with MetaStateEvent

use of org.ballerinalang.siddhi.core.event.state.MetaStateEvent in project ballerina by ballerina-lang.

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
 *                     VariableExpressionExecutor representing given variable
 */
private static ExpressionExecutor parseVariable(Variable variable, MetaComplexEvent metaEvent, int currentState, List<VariableExpressionExecutor> executorList, int defaultStreamEventIndex) {
    String attributeName = variable.getAttributeName();
    int[] eventPosition = new int[2];
    if (variable.getStreamIndex() != null) {
        if (variable.getStreamIndex() <= LAST) {
            eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = variable.getStreamIndex() + 1;
        } else {
            eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = variable.getStreamIndex();
        }
    } else {
        eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = defaultStreamEventIndex;
    }
    eventPosition[STREAM_EVENT_CHAIN_INDEX] = UNKNOWN_STATE;
    if (metaEvent instanceof MetaStreamEvent) {
        MetaStreamEvent metaStreamEvent = (MetaStreamEvent) metaEvent;
        AbstractDefinition abstractDefinition;
        Attribute.Type type;
        if (currentState == HAVING_STATE) {
            abstractDefinition = metaStreamEvent.getOutputStreamDefinition();
            type = abstractDefinition.getAttributeType(attributeName);
            eventPosition[STREAM_EVENT_CHAIN_INDEX] = HAVING_STATE;
        } else {
            abstractDefinition = metaStreamEvent.getLastInputDefinition();
            type = abstractDefinition.getAttributeType(attributeName);
            ((MetaStreamEvent) metaEvent).addData(new Attribute(attributeName, type));
        }
        VariableExpressionExecutor variableExpressionExecutor = new VariableExpressionExecutor(new Attribute(attributeName, type), eventPosition[STREAM_EVENT_CHAIN_INDEX], eventPosition[STREAM_EVENT_INDEX_IN_CHAIN]);
        if (((MetaStreamEvent) metaEvent).getEventType() != MetaStreamEvent.EventType.DEFAULT) {
            variableExpressionExecutor.getPosition()[STREAM_ATTRIBUTE_TYPE_INDEX] = OUTPUT_DATA_INDEX;
            variableExpressionExecutor.getPosition()[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;
        if (variable.getStreamId() == null) {
            MetaStreamEvent[] metaStreamEvents = metaStateEvent.getMetaStreamEvents();
            if (currentState == HAVING_STATE) {
                definition = metaStateEvent.getOutputStreamDefinition();
                try {
                    type = definition.getAttributeType(attributeName);
                    eventPosition[STREAM_EVENT_CHAIN_INDEX] = HAVING_STATE;
                } catch (AttributeNotExistException e) {
                    currentState = UNKNOWN_STATE;
                }
            }
            if (currentState == 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[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[STREAM_EVENT_CHAIN_INDEX] = currentState;
                } catch (AttributeNotExistException e) {
                    throw new SiddhiAppValidationException(e.getMessageWithOutContext() + " Input Stream: " + definition.getId() + " with reference: " + metaStreamEvent.getInputReferenceId(), 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[STREAM_EVENT_CHAIN_INDEX] = i;
                        break;
                    }
                } else {
                    if (metaStreamEvent.getInputReferenceId().equals(variable.getStreamId())) {
                        type = definition.getAttributeType(attributeName);
                        eventPosition[STREAM_EVENT_CHAIN_INDEX] = i;
                        if (currentState > -1 && metaStreamEvents[currentState].getInputReferenceId() != null && variable.getStreamIndex() != null && variable.getStreamIndex() <= LAST) {
                            if (variable.getStreamId().equals(metaStreamEvents[currentState].getInputReferenceId())) {
                                eventPosition[STREAM_EVENT_INDEX_IN_CHAIN] = variable.getStreamIndex();
                            }
                        }
                        break;
                    }
                }
            }
        }
        if (eventPosition[STREAM_EVENT_CHAIN_INDEX] == UNKNOWN_STATE) {
            throw new SiddhiAppValidationException("Stream with reference : " + variable.getStreamId() + " not found");
        }
        VariableExpressionExecutor variableExpressionExecutor = new VariableExpressionExecutor(new Attribute(attributeName, type), eventPosition[STREAM_EVENT_CHAIN_INDEX], eventPosition[STREAM_EVENT_INDEX_IN_CHAIN]);
        if (eventPosition[STREAM_EVENT_CHAIN_INDEX] != HAVING_STATE) {
            MetaStreamEvent metaStreamEvent = ((MetaStateEvent) metaEvent).getMetaStreamEvent(eventPosition[STREAM_EVENT_CHAIN_INDEX]);
            if (metaStreamEvent.getEventType() != MetaStreamEvent.EventType.DEFAULT) {
                variableExpressionExecutor.getPosition()[STREAM_ATTRIBUTE_TYPE_INDEX] = OUTPUT_DATA_INDEX;
                variableExpressionExecutor.getPosition()[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);
        }
        return variableExpressionExecutor;
    }
}
Also used : AttributeNotExistException(org.ballerinalang.siddhi.query.api.exception.AttributeNotExistException) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) AbstractDefinition(org.ballerinalang.siddhi.query.api.definition.AbstractDefinition) SiddhiAppValidationException(org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException) EqualCompareConditionExpressionExecutorStringString(org.ballerinalang.siddhi.core.executor.condition.compare.equal.EqualCompareConditionExpressionExecutorStringString) NotEqualCompareConditionExpressionExecutorStringString(org.ballerinalang.siddhi.core.executor.condition.compare.notequal.NotEqualCompareConditionExpressionExecutorStringString) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) MetaStateEvent(org.ballerinalang.siddhi.core.event.state.MetaStateEvent)

Example 3 with MetaStateEvent

use of org.ballerinalang.siddhi.core.event.state.MetaStateEvent in project ballerina by ballerina-lang.

the class InputStreamParser method parse.

/**
 * Parse an InputStream returning corresponding StreamRuntime.
 *
 * @param inputStream                input stream to be parsed
 * @param siddhiAppContext           associated siddhi siddhiAppContext
 * @param streamDefinitionMap        map containing user given stream definitions
 * @param tableDefinitionMap         table definition map
 * @param windowDefinitionMap        window definition map
 * @param aggregationDefinitionMap   aggregation definition map
 * @param tableMap                   Table Map
 * @param windowMap                  event window map
 * @param aggregationMap             aggregator map
 * @param executors                  List to hold VariableExpressionExecutors to update after query parsing
 * @param latencyTracker             latency tracker
 * @param outputExpectsExpiredEvents is output expects ExpiredEvents
 * @param queryName                  query name of input stream belongs to.
 * @return StreamRuntime
 */
public static StreamRuntime parse(InputStream inputStream, SiddhiAppContext siddhiAppContext, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, Map<String, Table> tableMap, Map<String, Window> windowMap, Map<String, AggregationRuntime> aggregationMap, List<VariableExpressionExecutor> executors, LatencyTracker latencyTracker, boolean outputExpectsExpiredEvents, String queryName) {
    if (inputStream instanceof BasicSingleInputStream || inputStream instanceof SingleInputStream) {
        SingleInputStream singleInputStream = (SingleInputStream) inputStream;
        Window window = windowMap.get(singleInputStream.getStreamId());
        // If stream is from window, allow batch
        boolean batchProcessingAllowed = window != null;
        // processing
        ProcessStreamReceiver processStreamReceiver = new ProcessStreamReceiver(singleInputStream.getStreamId(), latencyTracker, queryName, siddhiAppContext);
        processStreamReceiver.setBatchProcessingAllowed(batchProcessingAllowed);
        return SingleInputStreamParser.parseInputStream((SingleInputStream) inputStream, siddhiAppContext, executors, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, new MetaStreamEvent(), processStreamReceiver, true, outputExpectsExpiredEvents, queryName);
    } else if (inputStream instanceof JoinInputStream) {
        return JoinInputStreamParser.parseInputStream(((JoinInputStream) inputStream), siddhiAppContext, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, windowMap, aggregationMap, executors, latencyTracker, outputExpectsExpiredEvents, queryName);
    } else if (inputStream instanceof StateInputStream) {
        MetaStateEvent metaStateEvent = new MetaStateEvent(inputStream.getAllStreamIds().size());
        return StateInputStreamParser.parseInputStream(((StateInputStream) inputStream), siddhiAppContext, metaStateEvent, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, executors, latencyTracker, queryName);
    } else {
        throw new OperationNotSupportedException();
    }
}
Also used : Window(org.ballerinalang.siddhi.core.window.Window) OperationNotSupportedException(org.ballerinalang.siddhi.core.exception.OperationNotSupportedException) ProcessStreamReceiver(org.ballerinalang.siddhi.core.query.input.ProcessStreamReceiver) BasicSingleInputStream(org.ballerinalang.siddhi.query.api.execution.query.input.stream.BasicSingleInputStream) SingleInputStream(org.ballerinalang.siddhi.query.api.execution.query.input.stream.SingleInputStream) JoinInputStream(org.ballerinalang.siddhi.query.api.execution.query.input.stream.JoinInputStream) BasicSingleInputStream(org.ballerinalang.siddhi.query.api.execution.query.input.stream.BasicSingleInputStream) StateInputStream(org.ballerinalang.siddhi.query.api.execution.query.input.stream.StateInputStream) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent) MetaStateEvent(org.ballerinalang.siddhi.core.event.state.MetaStateEvent)

Example 4 with MetaStateEvent

use of org.ballerinalang.siddhi.core.event.state.MetaStateEvent in project ballerina by ballerina-lang.

the class JoinInputStreamParser method parseInputStream.

public static StreamRuntime parseInputStream(JoinInputStream joinInputStream, SiddhiAppContext siddhiAppContext, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, Map<String, Table> tableMap, Map<String, Window> windowMap, Map<String, AggregationRuntime> aggregationMap, List<VariableExpressionExecutor> executors, LatencyTracker latencyTracker, boolean outputExpectsExpiredEvents, String queryName) {
    try {
        ProcessStreamReceiver leftProcessStreamReceiver;
        ProcessStreamReceiver rightProcessStreamReceiver;
        MetaStreamEvent leftMetaStreamEvent = new MetaStreamEvent();
        MetaStreamEvent rightMetaStreamEvent = new MetaStreamEvent();
        String leftInputStreamId = ((SingleInputStream) joinInputStream.getLeftInputStream()).getStreamId();
        String rightInputStreamId = ((SingleInputStream) joinInputStream.getRightInputStream()).getStreamId();
        boolean leftOuterJoinProcessor = false;
        boolean rightOuterJoinProcessor = false;
        if (joinInputStream.getAllStreamIds().size() == 2) {
            setEventType(streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, leftMetaStreamEvent, leftInputStreamId);
            setEventType(streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, rightMetaStreamEvent, rightInputStreamId);
            leftProcessStreamReceiver = new ProcessStreamReceiver(leftInputStreamId, latencyTracker, queryName, siddhiAppContext);
            leftProcessStreamReceiver.setBatchProcessingAllowed(leftMetaStreamEvent.getEventType() == WINDOW);
            rightProcessStreamReceiver = new ProcessStreamReceiver(rightInputStreamId, latencyTracker, queryName, siddhiAppContext);
            rightProcessStreamReceiver.setBatchProcessingAllowed(rightMetaStreamEvent.getEventType() == WINDOW);
            if ((leftMetaStreamEvent.getEventType() == TABLE || leftMetaStreamEvent.getEventType() == AGGREGATE) && (rightMetaStreamEvent.getEventType() == TABLE || rightMetaStreamEvent.getEventType() == AGGREGATE)) {
                throw new SiddhiAppCreationException("Both inputs of join " + leftInputStreamId + " and " + rightInputStreamId + " are from static sources");
            }
            if (leftMetaStreamEvent.getEventType() != AGGREGATE && rightMetaStreamEvent.getEventType() != AGGREGATE) {
                if (joinInputStream.getPer() != null) {
                    throw new SiddhiAppCreationException("When joining " + leftInputStreamId + " and " + rightInputStreamId + " 'per' cannot be used as neither of them is an aggregation ");
                } else if (joinInputStream.getWithin() != null) {
                    throw new SiddhiAppCreationException("When joining " + leftInputStreamId + " and " + rightInputStreamId + " 'within' cannot be used as neither of them is an aggregation ");
                }
            }
        } else {
            if (windowDefinitionMap.containsKey(joinInputStream.getAllStreamIds().get(0))) {
                leftMetaStreamEvent.setEventType(WINDOW);
                rightMetaStreamEvent.setEventType(WINDOW);
                rightProcessStreamReceiver = new MultiProcessStreamReceiver(joinInputStream.getAllStreamIds().get(0), 1, latencyTracker, queryName, siddhiAppContext);
                rightProcessStreamReceiver.setBatchProcessingAllowed(true);
                leftProcessStreamReceiver = rightProcessStreamReceiver;
            } else if (streamDefinitionMap.containsKey(joinInputStream.getAllStreamIds().get(0))) {
                rightProcessStreamReceiver = new MultiProcessStreamReceiver(joinInputStream.getAllStreamIds().get(0), 2, latencyTracker, queryName, siddhiAppContext);
                leftProcessStreamReceiver = rightProcessStreamReceiver;
            } else {
                throw new SiddhiAppCreationException("Input of join is from static source " + leftInputStreamId + " and " + rightInputStreamId);
            }
        }
        SingleStreamRuntime leftStreamRuntime = SingleInputStreamParser.parseInputStream((SingleInputStream) joinInputStream.getLeftInputStream(), siddhiAppContext, executors, streamDefinitionMap, leftMetaStreamEvent.getEventType() != TABLE ? null : tableDefinitionMap, leftMetaStreamEvent.getEventType() != WINDOW ? null : windowDefinitionMap, leftMetaStreamEvent.getEventType() != AGGREGATE ? null : aggregationDefinitionMap, tableMap, leftMetaStreamEvent, leftProcessStreamReceiver, true, outputExpectsExpiredEvents, queryName);
        for (VariableExpressionExecutor variableExpressionExecutor : executors) {
            variableExpressionExecutor.getPosition()[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = 0;
        }
        int size = executors.size();
        SingleStreamRuntime rightStreamRuntime = SingleInputStreamParser.parseInputStream((SingleInputStream) joinInputStream.getRightInputStream(), siddhiAppContext, executors, streamDefinitionMap, rightMetaStreamEvent.getEventType() != TABLE ? null : tableDefinitionMap, rightMetaStreamEvent.getEventType() != WINDOW ? null : windowDefinitionMap, rightMetaStreamEvent.getEventType() != AGGREGATE ? null : aggregationDefinitionMap, tableMap, rightMetaStreamEvent, rightProcessStreamReceiver, true, outputExpectsExpiredEvents, queryName);
        for (int i = size; i < executors.size(); i++) {
            VariableExpressionExecutor variableExpressionExecutor = executors.get(i);
            variableExpressionExecutor.getPosition()[SiddhiConstants.STREAM_EVENT_CHAIN_INDEX] = 1;
        }
        setStreamRuntimeProcessorChain(leftMetaStreamEvent, leftStreamRuntime, leftInputStreamId, tableMap, windowMap, aggregationMap, executors, outputExpectsExpiredEvents, queryName, joinInputStream.getWithin(), joinInputStream.getPer(), siddhiAppContext, joinInputStream.getLeftInputStream());
        setStreamRuntimeProcessorChain(rightMetaStreamEvent, rightStreamRuntime, rightInputStreamId, tableMap, windowMap, aggregationMap, executors, outputExpectsExpiredEvents, queryName, joinInputStream.getWithin(), joinInputStream.getPer(), siddhiAppContext, joinInputStream.getRightInputStream());
        MetaStateEvent metaStateEvent = new MetaStateEvent(2);
        metaStateEvent.addEvent(leftMetaStreamEvent);
        metaStateEvent.addEvent(rightMetaStreamEvent);
        switch(joinInputStream.getType()) {
            case FULL_OUTER_JOIN:
                leftOuterJoinProcessor = true;
                rightOuterJoinProcessor = true;
                break;
            case RIGHT_OUTER_JOIN:
                rightOuterJoinProcessor = true;
                break;
            case LEFT_OUTER_JOIN:
                leftOuterJoinProcessor = true;
                break;
        }
        JoinProcessor leftPreJoinProcessor = new JoinProcessor(true, true, leftOuterJoinProcessor, 0);
        JoinProcessor leftPostJoinProcessor = new JoinProcessor(true, false, leftOuterJoinProcessor, 0);
        FindableProcessor leftFindableProcessor = insertJoinProcessorsAndGetFindable(leftPreJoinProcessor, leftPostJoinProcessor, leftStreamRuntime, siddhiAppContext, outputExpectsExpiredEvents, queryName, joinInputStream.getLeftInputStream());
        JoinProcessor rightPreJoinProcessor = new JoinProcessor(false, true, rightOuterJoinProcessor, 1);
        JoinProcessor rightPostJoinProcessor = new JoinProcessor(false, false, rightOuterJoinProcessor, 1);
        FindableProcessor rightFindableProcessor = insertJoinProcessorsAndGetFindable(rightPreJoinProcessor, rightPostJoinProcessor, rightStreamRuntime, siddhiAppContext, outputExpectsExpiredEvents, queryName, joinInputStream.getRightInputStream());
        leftPreJoinProcessor.setFindableProcessor(rightFindableProcessor);
        leftPostJoinProcessor.setFindableProcessor(rightFindableProcessor);
        rightPreJoinProcessor.setFindableProcessor(leftFindableProcessor);
        rightPostJoinProcessor.setFindableProcessor(leftFindableProcessor);
        Expression compareCondition = joinInputStream.getOnCompare();
        if (compareCondition == null) {
            compareCondition = Expression.value(true);
        }
        MatchingMetaInfoHolder rightMatchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(metaStateEvent, 0, rightMetaStreamEvent.getLastInputDefinition(), UNKNOWN_STATE);
        CompiledCondition leftCompiledCondition = rightFindableProcessor.compileCondition(compareCondition, rightMatchingMetaInfoHolder, siddhiAppContext, executors, tableMap, queryName);
        MatchingMetaInfoHolder leftMatchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(metaStateEvent, 1, leftMetaStreamEvent.getLastInputDefinition(), UNKNOWN_STATE);
        CompiledCondition rightCompiledCondition = leftFindableProcessor.compileCondition(compareCondition, leftMatchingMetaInfoHolder, siddhiAppContext, executors, tableMap, queryName);
        if (joinInputStream.getTrigger() != JoinInputStream.EventTrigger.LEFT) {
            populateJoinProcessors(rightMetaStreamEvent, rightInputStreamId, rightPreJoinProcessor, rightPostJoinProcessor, rightCompiledCondition);
        }
        if (joinInputStream.getTrigger() != JoinInputStream.EventTrigger.RIGHT) {
            populateJoinProcessors(leftMetaStreamEvent, leftInputStreamId, leftPreJoinProcessor, leftPostJoinProcessor, leftCompiledCondition);
        }
        JoinStreamRuntime joinStreamRuntime = new JoinStreamRuntime(siddhiAppContext, metaStateEvent);
        joinStreamRuntime.addRuntime(leftStreamRuntime);
        joinStreamRuntime.addRuntime(rightStreamRuntime);
        return joinStreamRuntime;
    } catch (Throwable t) {
        ExceptionUtil.populateQueryContext(t, joinInputStream, siddhiAppContext);
        throw t;
    }
}
Also used : ProcessStreamReceiver(org.ballerinalang.siddhi.core.query.input.ProcessStreamReceiver) MultiProcessStreamReceiver(org.ballerinalang.siddhi.core.query.input.MultiProcessStreamReceiver) FindableProcessor(org.ballerinalang.siddhi.core.query.processor.stream.window.FindableProcessor) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) SingleStreamRuntime(org.ballerinalang.siddhi.core.query.input.stream.single.SingleStreamRuntime) JoinStreamRuntime(org.ballerinalang.siddhi.core.query.input.stream.join.JoinStreamRuntime) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) MultiProcessStreamReceiver(org.ballerinalang.siddhi.core.query.input.MultiProcessStreamReceiver) MetaStateEvent(org.ballerinalang.siddhi.core.event.state.MetaStateEvent) CompiledCondition(org.ballerinalang.siddhi.core.util.collection.operator.CompiledCondition) Expression(org.ballerinalang.siddhi.query.api.expression.Expression) SingleInputStream(org.ballerinalang.siddhi.query.api.execution.query.input.stream.SingleInputStream) MatchingMetaInfoHolder(org.ballerinalang.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) JoinProcessor(org.ballerinalang.siddhi.core.query.input.stream.join.JoinProcessor) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)

Example 5 with MetaStateEvent

use of org.ballerinalang.siddhi.core.event.state.MetaStateEvent in project ballerina by ballerina-lang.

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.ballerinalang.siddhi.query.api.execution.query.input.handler.Window) Variable(org.ballerinalang.siddhi.query.api.expression.Variable) ExpressionExecutor(org.ballerinalang.siddhi.core.executor.ExpressionExecutor) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) AbstractStreamProcessor(org.ballerinalang.siddhi.core.query.processor.stream.AbstractStreamProcessor) Attribute(org.ballerinalang.siddhi.query.api.definition.Attribute) FilterProcessor(org.ballerinalang.siddhi.core.query.processor.filter.FilterProcessor) StreamFunction(org.ballerinalang.siddhi.query.api.execution.query.input.handler.StreamFunction) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) ConfigReader(org.ballerinalang.siddhi.core.util.config.ConfigReader) MetaStateEvent(org.ballerinalang.siddhi.core.event.state.MetaStateEvent) Expression(org.ballerinalang.siddhi.query.api.expression.Expression) Filter(org.ballerinalang.siddhi.query.api.execution.query.input.handler.Filter) WindowProcessor(org.ballerinalang.siddhi.core.query.processor.stream.window.WindowProcessor) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)

Aggregations

MetaStateEvent (org.ballerinalang.siddhi.core.event.state.MetaStateEvent)22 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)19 VariableExpressionExecutor (org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor)10 Attribute (org.ballerinalang.siddhi.query.api.definition.Attribute)8 AbstractDefinition (org.ballerinalang.siddhi.query.api.definition.AbstractDefinition)7 MatchingMetaInfoHolder (org.ballerinalang.siddhi.core.util.collection.operator.MatchingMetaInfoHolder)6 Variable (org.ballerinalang.siddhi.query.api.expression.Variable)5 ArrayList (java.util.ArrayList)4 SiddhiAppCreationException (org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)4 ExpressionExecutor (org.ballerinalang.siddhi.core.executor.ExpressionExecutor)4 SingleStreamRuntime (org.ballerinalang.siddhi.core.query.input.stream.single.SingleStreamRuntime)4 Expression (org.ballerinalang.siddhi.query.api.expression.Expression)4 OperationNotSupportedException (org.ballerinalang.siddhi.core.exception.OperationNotSupportedException)3 ConstantExpressionExecutor (org.ballerinalang.siddhi.core.executor.ConstantExpressionExecutor)3 ConditionExpressionExecutor (org.ballerinalang.siddhi.core.executor.condition.ConditionExpressionExecutor)3 ProcessStreamReceiver (org.ballerinalang.siddhi.core.query.input.ProcessStreamReceiver)3 StateEventCloner (org.ballerinalang.siddhi.core.event.state.StateEventCloner)2 StateEventPool (org.ballerinalang.siddhi.core.event.state.StateEventPool)2 StreamEventCloner (org.ballerinalang.siddhi.core.event.stream.StreamEventCloner)2 StreamEventPool (org.ballerinalang.siddhi.core.event.stream.StreamEventPool)2