Esempio n. 1
0
        public static IList<OnTriggerSetAssignment> GetOnTriggerSetAssignments(
            EsperEPL2GrammarParser.OnSetAssignmentListContext ctx,
            IDictionary<ITree, ExprNode> astExprNodeMap)
        {
            if (ctx == null || ctx.onSetAssignment().IsEmpty())
            {
                return new EmptyList<OnTriggerSetAssignment>();
            }

            IList<EsperEPL2GrammarParser.OnSetAssignmentContext> ctxs = ctx.onSetAssignment();
            IList<OnTriggerSetAssignment> assignments = new List<OnTriggerSetAssignment>(ctx.onSetAssignment().Length);
            foreach (EsperEPL2GrammarParser.OnSetAssignmentContext assign in ctxs)
            {
                ExprNode childEvalNode;
                if (assign.chainable() != null)
                {
                    ExprNode prop = ASTExprHelper.ExprCollectSubNodes(assign.chainable(), 0, astExprNodeMap)[0];
                    ExprNode value = ASTExprHelper.ExprCollectSubNodes(assign.expression(), 0, astExprNodeMap)[0];
                    ExprEqualsNode equals = new ExprEqualsNodeImpl(false, false);
                    equals.AddChildNode(prop);
                    equals.AddChildNode(value);
                    childEvalNode = equals;
                }
                else
                {
                    childEvalNode = ASTExprHelper.ExprCollectSubNodes(assign, 0, astExprNodeMap)[0];
                }

                assignments.Add(new OnTriggerSetAssignment(childEvalNode));
            }

            return assignments;
        }
Esempio n. 2
0
        public static ExprNode MathGetExpr(
            IParseTree ctx,
            IDictionary<ITree, ExprNode> astExprNodeMap,
            Configuration configurationInformation)
        {
            int count = 1;
            ExprNode @base = ASTExprHelper.ExprCollectSubNodes(ctx.GetChild(0), 0, astExprNodeMap)[0];

            while (true)
            {
                int token = ASTUtil.GetAssertTerminatedTokenType(ctx.GetChild(count));
                MathArithTypeEnum mathArithTypeEnum = TokenToMathEnum(token);

                ExprNode right = ASTExprHelper.ExprCollectSubNodes(ctx.GetChild(count + 1), 0, astExprNodeMap)[0];

                ExprMathNode math = new ExprMathNode(
                    mathArithTypeEnum,
                    configurationInformation.Compiler.Expression.IsIntegerDivision,
                    configurationInformation.Compiler.Expression.IsDivisionByZeroReturnsNull);
                math.AddChildNode(@base);
                math.AddChildNode(right);
                @base = math;

                count += 2;
                if (count >= ctx.ChildCount)
                {
                    break;
                }
            }

            return @base;
        }
Esempio n. 3
0
        private static CreateIndexItem Walk(
            EsperEPL2GrammarParser.CreateIndexColumnContext col,
            IDictionary<ITree, ExprNode> astExprNodeMap)
        {
            IList<ExprNode> expressions = new EmptyList<ExprNode>();
            if (col.i != null)
            {
                expressions = ASTExprHelper.ExprCollectSubNodes(col.i, 0, astExprNodeMap);
            }
            else if (col.expression() != null)
            {
                expressions = ASTExprHelper.ExprCollectSubNodes(col.expression(), 0, astExprNodeMap);
            }

            string type = CreateIndexType.HASH.GetName().ToLowerInvariant();
            if (col.t != null)
            {
                type = col.t.Text;
            }

            IList<ExprNode> parameters = new EmptyList<ExprNode>();
            if (col.p != null)
            {
                parameters = ASTExprHelper.ExprCollectSubNodes(col.p, 0, astExprNodeMap);
            }

            return new CreateIndexItem(expressions, type, parameters);
        }
Esempio n. 4
0
        private static ContextSpecCondition GetContextCondition(
            EsperEPL2GrammarParser.CreateContextRangePointContext ctx,
            IDictionary<ITree, ExprNode> astExprNodeMap,
            IDictionary<ITree, EvalForgeNode> astPatternNodeMap,
            PropertyEvalSpec propertyEvalSpec,
            bool immediate)
        {
            if (ctx == null)
            {
                return ContextSpecConditionNever.INSTANCE;
            }

            if (ctx.crontabLimitParameterSetList() != null) {
                var crontabs = new List<IList<ExprNode>>();
                foreach (var crontabCtx in ctx.crontabLimitParameterSetList().crontabLimitParameterSet()) {
                    var crontab = ASTExprHelper.ExprCollectSubNodes(crontabCtx, 0, astExprNodeMap);
                    crontabs.Add(crontab);
                }
                return new ContextSpecConditionCrontab(crontabs, immediate);
            }

            if (ctx.patternInclusionExpression() != null)
            {
                var evalNode = ASTExprHelper.PatternGetRemoveTopNode(ctx.patternInclusionExpression(), astPatternNodeMap);
                var inclusive = false;
                if (ctx.i != null)
                {
                    var ident = ctx.i.Text;
                    if (ident != null && !ident.ToLowerInvariant().Equals("inclusive"))
                    {
                        throw ASTWalkException.From("Expected 'inclusive' keyword after '@', found '" + ident + "' instead");
                    }

                    inclusive = true;
                }

                return new ContextSpecConditionPattern(evalNode, inclusive, immediate);
            }

            if (ctx.createContextFilter() != null)
            {
                if (immediate)
                {
                    throw ASTWalkException.From("Invalid use of 'now' with initiated-by stream, this combination is not supported");
                }

                return GetContextDetailConditionFilter(ctx.createContextFilter(), propertyEvalSpec, astExprNodeMap);
            }

            if (ctx.AFTER() != null)
            {
                var timePeriod = (ExprTimePeriod) ASTExprHelper.ExprCollectSubNodes(ctx.timePeriod(), 0, astExprNodeMap)[0];
                return new ContextSpecConditionTimePeriod(timePeriod, immediate);
            }

            throw new IllegalStateException("Unrecognized child type");
        }
Esempio n. 5
0
        private static CreateTableColumn GetColumn(
            EsperEPL2GrammarParser.CreateTableColumnContext ctx,
            IDictionary<ITree, ExprNode> astExprNodeMap,
            StatementSpecMapEnv mapEnv)
        {
            var columnName = ctx.n.Text;

            ExprNode optExpression = null;
            if (ctx.builtinFunc() != null || ctx.chainable() != null)
            {
                optExpression = ASTExprHelper.ExprCollectSubNodes(ctx, 0, astExprNodeMap)[0];
            }

            var optType = ASTClassIdentifierHelper.Walk(ctx.classIdentifierWithDimensions());

            var primaryKey = false;
            if (ctx.p != null)
            {
                if (!string.Equals(ctx.p.Text, "primary", StringComparison.InvariantCultureIgnoreCase))
                {
                    throw ASTWalkException.From("Invalid keyword '" + ctx.p.Text + "' encountered, expected 'primary key'");
                }

                if (!string.Equals(ctx.k.Text, "key", StringComparison.InvariantCultureIgnoreCase))
                {
                    throw ASTWalkException.From("Invalid keyword '" + ctx.k.Text + "' encountered, expected 'primary key'");
                }

                primaryKey = true;
            }

            IList<AnnotationDesc> annots = new EmptyList<AnnotationDesc>();
            if (ctx.annotationEnum() != null)
            {
                annots = new List<AnnotationDesc>(ctx.annotationEnum().Length);
                foreach (EsperEPL2GrammarParser.AnnotationEnumContext anctx in ctx.annotationEnum())
                {
                    annots.Add(ASTAnnotationHelper.Walk(anctx, mapEnv.ImportService));
                }
            }

            if (ctx.typeExpressionAnnotation() != null)
            {
                if (annots.IsEmpty())
                {
                    annots = new List<AnnotationDesc>();
                }

                foreach (EsperEPL2GrammarParser.TypeExpressionAnnotationContext anno in ctx.typeExpressionAnnotation())
                {
                    annots.Add(new AnnotationDesc(anno.n.Text, anno.v.Text));
                }
            }

            return new CreateTableColumn(columnName, optExpression, optType, annots, primaryKey);
        }
Esempio n. 6
0
 public static FilterSpecRaw WalkFilterSpec(
     EsperEPL2GrammarParser.EventFilterExpressionContext ctx,
     PropertyEvalSpec propertyEvalSpec,
     IDictionary<ITree, ExprNode> astExprNodeMap)
 {
     var eventName = ASTUtil.UnescapeClassIdent(ctx.classIdentifier());
     var exprNodes = ctx.expressionList() != null
         ? ASTExprHelper.ExprCollectSubNodes(ctx.expressionList(), 0, astExprNodeMap)
         : new List<ExprNode>(1);
     return new FilterSpecRaw(eventName, exprNodes, propertyEvalSpec);
 }
Esempio n. 7
0
        private static GroupByClauseElement WalkCombinable(
            EsperEPL2GrammarParser.GroupByCombinableExprContext ctx,
            IDictionary<ITree, ExprNode> astExprNodeMap)
        {
            if (ctx.e1 != null && ctx.LPAREN() == null)
            {
                ExprNode expr = ASTExprHelper.ExprCollectSubNodes(ctx.e1, 0, astExprNodeMap)[0];
                return new GroupByClauseElementExpr(expr);
            }

            var combined = ASTExprHelper.ExprCollectSubNodes(ctx, 0, astExprNodeMap);
            return new GroupByClauseElementCombinedExpr(combined);
        }
Esempio n. 8
0
        public static void AddOptionalSimpleProperty(
            ExprNode exprNode,
            IToken token,
            VariableCompileTimeResolver variableCompileTimeResolver,
            StatementSpecRaw spec)
        {
            if (token == null)
            {
                return;
            }

            ExprNode node = ASTExprHelper.ResolvePropertyOrVariableIdentifier(token.Text, variableCompileTimeResolver, spec);
            exprNode.AddChildNode(node);
        }
Esempio n. 9
0
        private static GroupByClauseElement WalkChoice(
            EsperEPL2GrammarParser.GroupByListChoiceContext choice,
            IDictionary<ITree, ExprNode> astExprNodeMap)
        {
            if (choice.e1 != null)
            {
                var expr = ASTExprHelper.ExprCollectSubNodes(choice.e1, 0, astExprNodeMap)[0];
                return new GroupByClauseElementExpr(expr);
            }

            if (choice.groupByCubeOrRollup() != null)
            {
                return WalkCubeOrRollup(choice.groupByCubeOrRollup(), astExprNodeMap);
            }

            return WalkGroupingSets(choice.groupByGroupingSets().groupBySetsChoice(), astExprNodeMap);
        }
Esempio n. 10
0
        public static IList <ExprNode> GetExprNodesLibFunc(
            EsperEPL2GrammarParser.LibFunctionArgsContext ctx,
            IDictionary <ITree, ExprNode> astExprNodeMap)
        {
            if (ctx == null)
            {
                return(EmptyList <ExprNode> .Instance);
            }

            IList <EsperEPL2GrammarParser.LibFunctionArgItemContext> args = ctx.libFunctionArgItem();

            if (args == null || args.IsEmpty())
            {
                return(EmptyList <ExprNode> .Instance);
            }

            IList <ExprNode> parameters = new List <ExprNode>(args.Count);

            foreach (EsperEPL2GrammarParser.LibFunctionArgItemContext arg in args)
            {
                if (arg.expressionLambdaDecl() != null)
                {
                    IList <string>     lambdaparams = GetLambdaGoesParams(arg.expressionLambdaDecl());
                    ExprLambdaGoesNode goes         = new ExprLambdaGoesNode(lambdaparams);
                    ExprNode           lambdaExpr   = ASTExprHelper.ExprCollectSubNodes(arg.expressionWithNamed(), 0, astExprNodeMap)[0];
                    goes.AddChildNode(lambdaExpr);
                    parameters.Add(goes);
                }
                else
                {
                    IList <ExprNode> @params   = ASTExprHelper.ExprCollectSubNodes(arg.expressionWithNamed(), 0, astExprNodeMap);
                    ExprNode         parameter = @params[0];
                    parameters.Add(parameter);
                }
            }

            return(parameters);
        }
Esempio n. 11
0
        public static void AddChainablesInternal(
            EsperEPL2GrammarParser.ChainableElementsContext ctx,
            IDictionary <ITree, ExprNode> astExprNodeMap,
            IList <Chainable> chain)
        {
            foreach (var context in ctx.chainableAtomicWithOpt())
            {
                var       optionalChainable = context.q != null;
                var       atomic            = context.chainableAtomic();
                Chainable chainable;
                if (atomic.chainableArray() != null)
                {
                    var @params = ASTExprHelper.ExprCollectSubNodes(atomic.chainableArray(), 0, astExprNodeMap);
                    chainable = new ChainableArray(false, optionalChainable, @params);
                }
                else
                {
                    chainable = GetChainable(context.chainableAtomic().chainableWithArgs(), optionalChainable, astExprNodeMap);
                }

                chain.Add(chainable);
            }
        }
Esempio n. 12
0
        public static RowRecogExprRepeatDesc WalkOptionalRepeat(
            EsperEPL2GrammarParser.MatchRecogPatternRepeatContext ctx,
            IDictionary<ITree, ExprNode> astExprNodeMap)
        {
            if (ctx == null)
            {
                return null;
            }

            var e1 = ctx.e1 == null ? null : ASTExprHelper.ExprCollectSubNodes(ctx.e1, 0, astExprNodeMap)[0];
            var e2 = ctx.e2 == null ? null : ASTExprHelper.ExprCollectSubNodes(ctx.e2, 0, astExprNodeMap)[0];

            if (ctx.comma == null && ctx.e1 != null)
            {
                return new RowRecogExprRepeatDesc(null, null, e1);
            }

            if (e1 == null && e2 == null)
            {
                throw ASTWalkException.From("Invalid match-recognize quantifier '" + ctx.GetText() + "', expecting an expression");
            }

            return new RowRecogExprRepeatDesc(e1, e2, null);
        }
Esempio n. 13
0
        public static ExprTimePeriod TimePeriodGetExprAllParams(
            EsperEPL2GrammarParser.TimePeriodContext ctx,
            IDictionary<ITree, ExprNode> astExprNodeMap,
            VariableCompileTimeResolver variableCompileTimeResolver,
            StatementSpecRaw spec,
            Configuration config,
            TimeAbacus timeAbacus)
        {
            ExprNode[] nodes = new ExprNode[9];
            for (int i = 0; i < ctx.ChildCount; i++)
            {
                IParseTree unitRoot = ctx.GetChild(i);

                ExprNode valueExpr;
                if (ASTUtil.IsTerminatedOfType(unitRoot.GetChild(0), EsperEPL2GrammarLexer.IDENT))
                {
                    string ident = unitRoot.GetChild(0).GetText();
                    valueExpr = ASTExprHelper.ResolvePropertyOrVariableIdentifier(ident, variableCompileTimeResolver, spec);
                }
                else
                {
                    AtomicReference<ExprNode> @ref = new AtomicReference<ExprNode>();
                    ExprAction action = (exprNode, astExprNodeMapX, node) => {
                        astExprNodeMapX.Remove(node);
                        @ref.Set(exprNode);
                    };
                    ASTExprHelper.RecursiveFindRemoveChildExprNode(unitRoot.GetChild(0), astExprNodeMap, action);
                    valueExpr = @ref.Get();
                }

                if (ASTUtil.GetRuleIndexIfProvided(unitRoot) == EsperEPL2GrammarParser.RULE_microsecondPart)
                {
                    nodes[8] = valueExpr;
                }

                if (ASTUtil.GetRuleIndexIfProvided(unitRoot) == EsperEPL2GrammarParser.RULE_millisecondPart)
                {
                    nodes[7] = valueExpr;
                }

                if (ASTUtil.GetRuleIndexIfProvided(unitRoot) == EsperEPL2GrammarParser.RULE_secondPart)
                {
                    nodes[6] = valueExpr;
                }

                if (ASTUtil.GetRuleIndexIfProvided(unitRoot) == EsperEPL2GrammarParser.RULE_minutePart)
                {
                    nodes[5] = valueExpr;
                }

                if (ASTUtil.GetRuleIndexIfProvided(unitRoot) == EsperEPL2GrammarParser.RULE_hourPart)
                {
                    nodes[4] = valueExpr;
                }

                if (ASTUtil.GetRuleIndexIfProvided(unitRoot) == EsperEPL2GrammarParser.RULE_dayPart)
                {
                    nodes[3] = valueExpr;
                }

                if (ASTUtil.GetRuleIndexIfProvided(unitRoot) == EsperEPL2GrammarParser.RULE_weekPart)
                {
                    nodes[2] = valueExpr;
                }

                if (ASTUtil.GetRuleIndexIfProvided(unitRoot) == EsperEPL2GrammarParser.RULE_monthPart)
                {
                    nodes[1] = valueExpr;
                }

                if (ASTUtil.GetRuleIndexIfProvided(unitRoot) == EsperEPL2GrammarParser.RULE_yearPart)
                {
                    nodes[0] = valueExpr;
                }
            }

            ExprTimePeriod timeNode = new ExprTimePeriodImpl(
                nodes[0] != null, nodes[1] != null,
                nodes[2] != null, nodes[3] != null,
                nodes[4] != null, nodes[5] != null,
                nodes[6] != null, nodes[7] != null,
                nodes[8] != null, timeAbacus);

            foreach (ExprNode node in nodes)
            {
                if (node != null)
                {
                    timeNode.AddChildNode(node);
                }
            }

            return timeNode;
        }
Esempio n. 14
0
        private static ContextSpec WalkChoice(
            EsperEPL2GrammarParser.CreateContextChoiceContext ctx,
            IDictionary<ITree, ExprNode> astExprNodeMap,
            IDictionary<ITree, EvalForgeNode> astPatternNodeMap,
            PropertyEvalSpec propertyEvalSpec)
        {
            // temporal fixed (start+end) and overlapping (initiated/terminated)
            if (ctx.START() != null || ctx.INITIATED() != null)
            {
                ExprNode[] distinctExpressions = null;
                if (ctx.createContextDistinct() != null)
                {
                    if (ctx.createContextDistinct().expressionList() == null)
                    {
                        distinctExpressions = ExprNodeUtilityQuery.EMPTY_EXPR_ARRAY;
                    }
                    else
                    {
                        distinctExpressions = ASTExprHelper.ExprCollectSubNodesPerNode(
                            ctx.createContextDistinct().expressionList().expression(), astExprNodeMap);
                    }
                }

                ContextSpecCondition startEndpoint;
                if (ctx.START() != null)
                {
                    var immediate = CheckNow(ctx.i);
                    if (immediate)
                    {
                        startEndpoint = ContextSpecConditionImmediate.INSTANCE;
                    }
                    else
                    {
                        startEndpoint = GetContextCondition(ctx.r1, astExprNodeMap, astPatternNodeMap, propertyEvalSpec, false);
                    }
                }
                else
                {
                    var immediate = CheckNow(ctx.i);
                    startEndpoint = GetContextCondition(ctx.r1, astExprNodeMap, astPatternNodeMap, propertyEvalSpec, immediate);
                }

                var overlapping = ctx.INITIATED() != null;
                var endEndpoint = GetContextCondition(ctx.r2, astExprNodeMap, astPatternNodeMap, propertyEvalSpec, false);
                return new ContextSpecInitiatedTerminated(startEndpoint, endEndpoint, overlapping, distinctExpressions);
            }

            // partitioned
            if (ctx.PARTITION() != null)
            {
                IList<EsperEPL2GrammarParser.CreateContextPartitionItemContext> partitions = ctx.createContextPartitionItem();
                IList<ContextSpecKeyedItem> rawSpecs = new List<ContextSpecKeyedItem>();
                foreach (var partition in partitions)
                {
                    var filterSpec = ASTFilterSpecHelper.WalkFilterSpec(partition.eventFilterExpression(), propertyEvalSpec, astExprNodeMap);
                    propertyEvalSpec = null;

                    IList<string> propertyNames = new List<string>();
                    IList<EsperEPL2GrammarParser.ChainableContext> properties = partition.chainable();
                    foreach (var property in properties)
                    {
                        var propertyName = ASTUtil.GetPropertyName(property, 0);
                        propertyNames.Add(propertyName);
                    }

                    ASTExprHelper.ExprCollectSubNodes(partition, 0, astExprNodeMap); // remove expressions

                    rawSpecs.Add(
                        new ContextSpecKeyedItem(
                            filterSpec, propertyNames, partition.keywordAllowedIdent() == null ? null : partition.keywordAllowedIdent().GetText()));
                }

                IList<ContextSpecConditionFilter> optionalInit = null;
                if (ctx.createContextPartitionInit() != null)
                {
                    optionalInit = GetContextPartitionInit(ctx.createContextPartitionInit().createContextFilter(), astExprNodeMap);
                }

                ContextSpecCondition optionalTermination = null;
                if (ctx.createContextPartitionTerm() != null)
                {
                    optionalTermination = GetContextCondition(
                        ctx.createContextPartitionTerm().createContextRangePoint(), astExprNodeMap, astPatternNodeMap, propertyEvalSpec, false);
                }

                return new ContextSpecKeyed(rawSpecs, optionalInit, optionalTermination);
            }

            if (ctx.COALESCE() != null)
            {
                // hash
                IList<EsperEPL2GrammarParser.CreateContextCoalesceItemContext> coalesces = ctx.createContextCoalesceItem();
                IList<ContextSpecHashItem> rawSpecs = new List<ContextSpecHashItem>(coalesces.Count);
                foreach (var coalesce in coalesces)
                {
                    var chain = ASTChainSpecHelper.GetChainables(coalesce.chainable(), astExprNodeMap);
                    var func = chain[0];
                    var filterSpec = ASTFilterSpecHelper.WalkFilterSpec(coalesce.eventFilterExpression(), propertyEvalSpec, astExprNodeMap);
                    propertyEvalSpec = null;
                    rawSpecs.Add(new ContextSpecHashItem(func, filterSpec));
                }

                var granularity = ctx.g.Text;
                if (!granularity.ToLowerInvariant().Equals("granularity"))
                {
                    throw ASTWalkException.From("Expected 'granularity' keyword after list of coalesce items, found '" + granularity + "' instead");
                }

                var num = ASTConstantHelper.Parse(ctx.number());
                var preallocateStr = ctx.p?.Text;
                if (preallocateStr != null && !preallocateStr.ToLowerInvariant().Equals("preallocate"))
                {
                    throw ASTWalkException.From(
                        "Expected 'preallocate' keyword after list of coalesce items, found '" + preallocateStr + "' instead");
                }

                if (!num.GetType().IsNumericNonFP() || num.GetType().GetBoxedType() == typeof(long?))
                {
                    throw ASTWalkException.From("Granularity provided must be an int-type number, received " + num.GetType() + " instead");
                }

                return new ContextSpecHash(rawSpecs, num.AsInt32(), preallocateStr != null);
            }

            // categorized
            if (ctx.createContextGroupItem() != null)
            {
                IList<EsperEPL2GrammarParser.CreateContextGroupItemContext> grps = ctx.createContextGroupItem();
                IList<ContextSpecCategoryItem> items = new List<ContextSpecCategoryItem>();
                foreach (var grp in grps)
                {
                    var exprNode = ASTExprHelper.ExprCollectSubNodes(grp, 0, astExprNodeMap)[0];
                    var name = grp.i.Text;
                    items.Add(new ContextSpecCategoryItem(exprNode, name));
                }

                var filterSpec = ASTFilterSpecHelper.WalkFilterSpec(ctx.eventFilterExpression(), propertyEvalSpec, astExprNodeMap);
                return new ContextSpecCategory(items, filterSpec);
            }

            throw new IllegalStateException("Unrecognized context detail type");
        }
Esempio n. 15
0
        public static MyPair WalkExpressionDecl(
            EsperEPL2GrammarParser.ExpressionDeclContext ctx,
            IList<string> scriptBodies,
            IDictionary<ITree, ExprNode> astExprNodeMap,
            CommonTokenStream tokenStream)
        {
            var name = ctx.name.Text;

            if (ctx.alias != null)
            {
                if (!ctx.alias.Text.ToLowerInvariant().Trim().Equals("alias"))
                {
                    throw ASTWalkException.From(
                        "For expression alias '" + name + "' expecting 'alias' keyword but received '" + ctx.alias.Text + "'");
                }

                if (ctx.columnList() != null)
                {
                    throw ASTWalkException.From(
                        "For expression alias '" + name + "' expecting no parameters but received '" + tokenStream.GetText(ctx.columnList()) + "'");
                }

                if (ctx.expressionDef() != null && ctx.expressionDef().expressionLambdaDecl() != null)
                {
                    throw ASTWalkException.From(
                        "For expression alias '" + name + "' expecting an expression without parameters but received '" +
                        tokenStream.GetText(ctx.expressionDef().expressionLambdaDecl()) + "'");
                }

                if (ctx.expressionDef().stringconstant() != null)
                {
                    throw ASTWalkException.From("For expression alias '" + name + "' expecting an expression but received a script");
                }

                var node = ASTExprHelper.ExprCollectSubNodes(ctx, 0, astExprNodeMap)[0];
                var alias = ctx.name.Text;
                var expressionUnmap = StatementSpecMapper.Unmap(node);
                var decl = new ExpressionDeclItem(alias, new string[0], true);
                decl.OptionalSoda = expressionUnmap;
                return new MyPair(decl, null);
            }

            if (ctx.expressionDef().stringconstant() != null)
            {
                var expressionText = scriptBodies.DeleteAt(0);
                var parameters = ASTUtil.GetIdentList(ctx.columnList());
                var optionalReturnType = ctx.classIdentifier() == null ? null : ASTUtil.UnescapeClassIdent(ctx.classIdentifier());
                var optionalReturnTypeArray = ctx.array != null;
                var optionalDialect = ctx.expressionDialect() == null ? null : ctx.expressionDialect().d.Text;
                var optionalEventTypeName = ASTTypeExpressionAnnoHelper.ExpectMayTypeAnno(ctx.typeExpressionAnnotation(), tokenStream);
                var script = new ExpressionScriptProvided(
                    name, expressionText, parameters.ToArray(),
                    optionalReturnType, optionalReturnTypeArray, optionalEventTypeName, optionalDialect);
                return new MyPair(null, script);
            }

            var ctxexpr = ctx.expressionDef();
            var inner = ASTExprHelper.ExprCollectSubNodes(ctxexpr.expression(), 0, astExprNodeMap)[0];

            IList<string> parametersNames = new EmptyList<string>();
            var lambdactx = ctxexpr.expressionLambdaDecl();
            if (ctxexpr.expressionLambdaDecl() != null)
            {
                parametersNames = ASTLambdaHelper.GetLambdaGoesParams(lambdactx);
            }

            var expression = StatementSpecMapper.Unmap(inner);
            var expr = new ExpressionDeclItem(name, parametersNames.ToArray(), false);
            expr.OptionalSoda = expression;
            return new MyPair(expr, null);
        }
Esempio n. 16
0
        public static OutputLimitSpec BuildOutputLimitSpec(
            CommonTokenStream tokenStream,
            EsperEPL2GrammarParser.OutputLimitContext ctx,
            IDictionary<ITree, ExprNode> astExprNodeMap)
        {
            var displayLimit = OutputLimitLimitType.DEFAULT;
            if (ctx.k != null)
            {
                switch (ctx.k.Type)
                {
                    case EsperEPL2GrammarParser.FIRST:
                        displayLimit = OutputLimitLimitType.FIRST;
                        break;

                    case EsperEPL2GrammarParser.LAST:
                        displayLimit = OutputLimitLimitType.LAST;
                        break;

                    case EsperEPL2GrammarParser.SNAPSHOT:
                        displayLimit = OutputLimitLimitType.SNAPSHOT;
                        break;

                    case EsperEPL2GrammarParser.ALL:
                        displayLimit = OutputLimitLimitType.ALL;
                        break;

                    default:
                        throw ASTWalkException.From("Encountered unrecognized token " + ctx.k.Text, tokenStream, ctx);
                }
            }

            // next is a variable, or time period, or number
            string variableName = null;
            double? rate = null;
            ExprNode whenExpression = null;
            IList<ExprNode> crontabScheduleSpec = null;
            IList<OnTriggerSetAssignment> thenExpressions = null;
            ExprTimePeriod timePeriodExpr = null;
            OutputLimitRateType rateType;
            ExprNode andAfterTerminateExpr = null;
            IList<OnTriggerSetAssignment> andAfterTerminateSetExpressions = null;

            if (ctx.t != null)
            {
                rateType = OutputLimitRateType.TERM;
                if (ctx.expression() != null)
                {
                    andAfterTerminateExpr = ASTExprHelper.ExprCollectSubNodes(ctx.expression(), 0, astExprNodeMap)[0];
                }

                if (ctx.onSetExpr() != null)
                {
                    andAfterTerminateSetExpressions = ASTExprHelper.GetOnTriggerSetAssignments(ctx.onSetExpr().onSetAssignmentList(), astExprNodeMap);
                }
            }
            else if (ctx.wh != null)
            {
                rateType = OutputLimitRateType.WHEN_EXPRESSION;
                whenExpression = ASTExprHelper.ExprCollectSubNodes(ctx.expression(), 0, astExprNodeMap)[0];
                if (ctx.onSetExpr() != null)
                {
                    thenExpressions = ASTExprHelper.GetOnTriggerSetAssignments(ctx.onSetExpr().onSetAssignmentList(), astExprNodeMap);
                }
            }
            else if (ctx.at != null)
            {
                rateType = OutputLimitRateType.CRONTAB;
                crontabScheduleSpec = ASTExprHelper.ExprCollectSubNodes(ctx.crontabLimitParameterSet(), 0, astExprNodeMap);
            }
            else
            {
                if (ctx.ev != null)
                {
                    rateType = ctx.e != null ? OutputLimitRateType.EVENTS : OutputLimitRateType.TIME_PERIOD;
                    if (ctx.i != null)
                    {
                        variableName = ctx.i.Text;
                    }
                    else if (ctx.timePeriod() != null)
                    {
                        timePeriodExpr = (ExprTimePeriod) ASTExprHelper.ExprCollectSubNodes(ctx.timePeriod(), 0, astExprNodeMap)[0];
                    }
                    else
                    {
                        ASTExprHelper.ExprCollectSubNodes(ctx.number(), 0, astExprNodeMap); // remove
                        rate = ctx.number().GetText().AsDouble();
                    }
                }
                else
                {
                    rateType = OutputLimitRateType.AFTER;
                }
            }

            // get the AFTER time period
            ExprTimePeriod afterTimePeriodExpr = null;
            int? afterNumberOfEvents = null;
            if (ctx.outputLimitAfter() != null)
            {
                if (ctx.outputLimitAfter().timePeriod() != null)
                {
                    ExprNode expression = ASTExprHelper.ExprCollectSubNodes(ctx.outputLimitAfter(), 0, astExprNodeMap)[0];
                    afterTimePeriodExpr = (ExprTimePeriod) expression;
                }
                else
                {
                    var constant = ASTConstantHelper.Parse(ctx.outputLimitAfter().number());
                    afterNumberOfEvents = constant.AsInt32();
                }
            }

            var andAfterTerminate = false;
            if (ctx.outputLimitAndTerm() != null)
            {
                andAfterTerminate = true;
                if (ctx.outputLimitAndTerm().expression() != null)
                {
                    andAfterTerminateExpr = ASTExprHelper.ExprCollectSubNodes(ctx.outputLimitAndTerm().expression(), 0, astExprNodeMap)[0];
                }

                if (ctx.outputLimitAndTerm().onSetExpr() != null)
                {
                    andAfterTerminateSetExpressions = ASTExprHelper.GetOnTriggerSetAssignments(
                        ctx.outputLimitAndTerm().onSetExpr().onSetAssignmentList(), astExprNodeMap);
                }
            }

            return new OutputLimitSpec(
                rate, variableName, rateType, displayLimit, whenExpression, thenExpressions, crontabScheduleSpec, timePeriodExpr, afterTimePeriodExpr,
                afterNumberOfEvents, andAfterTerminate, andAfterTerminateExpr, andAfterTerminateSetExpressions);
        }