示例#1
0
        public static void AddEndpointTypes(
            ContextSpecCondition endpoint,
            IDictionary<string, object> properties,
            ISet<string> allTags)
        {
            if (endpoint is ContextSpecConditionFilter) {
                var filter = (ContextSpecConditionFilter) endpoint;
                if (filter.OptionalFilterAsName != null) {
                    allTags.Add(filter.OptionalFilterAsName);
                    properties.Put(filter.OptionalFilterAsName, filter.FilterSpecCompiled.FilterForEventType);
                }
            }

            if (endpoint is ContextSpecConditionPattern) {
                var pattern = (ContextSpecConditionPattern) endpoint;
                foreach (var entry in pattern.PatternCompiled.TaggedEventTypes) {
                    if (properties.ContainsKey(entry.Key) && !properties.Get(entry.Key).Equals(entry.Value.First)) {
                        throw new ExprValidationException(
                            "The stream or tag name '" + entry.Key + "' is already declared");
                    }

                    allTags.Add(entry.Key);
                    properties.Put(entry.Key, entry.Value.First);
                }
            }
        }
示例#2
0
 public ContextSpecKeyed(
     IList<ContextSpecKeyedItem> items,
     IList<ContextSpecConditionFilter> optionalInit,
     ContextSpecCondition optionalTermination)
 {
     Items = items;
     OptionalInit = optionalInit;
     OptionalTermination = optionalTermination;
 }
示例#3
0
 internal ContextDetailMatchPair(
     ContextSpecCondition condition,
     MatchEventSpec matches,
     ISet<string> allTags)
 {
     Condition = condition;
     Matches = matches;
     AllTags = allTags;
 }
 private static void CollectExpressions(
     IList<ExprNode> expressions,
     ContextSpecCondition endpoint)
 {
     if (endpoint is ContextSpecConditionCrontab crontab) {
         foreach (var crontabItem in crontab.Crontabs) {
             expressions.AddAll(crontabItem);
         }
     }
 }
示例#5
0
 internal ContextDetailMatchPair(
     ContextSpecCondition condition,
     MatchEventSpec matches,
     ISet<string> allTags,
     IList<StmtClassForgeableFactory> additionalForgeables)
 {
     Condition = condition;
     Matches = matches;
     AllTags = allTags;
     AdditionalForgeables = additionalForgeables;
 }
示例#6
0
 public ContextSpecInitiatedTerminated(
     ContextSpecCondition startCondition,
     ContextSpecCondition endCondition,
     bool overlapping,
     ExprNode[] distinctExpressions)
 {
     StartCondition = startCondition;
     EndCondition = endCondition;
     IsOverlapping = overlapping;
     DistinctExpressions = distinctExpressions;
 }
示例#7
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");
        }
示例#8
0
        private ContextDetailMatchPair ValidateRewriteContextCondition(
            bool isStartCondition,
            int nestingLevel,
            ContextSpecCondition endpoint,
            ISet<string> eventTypesReferenced,
            MatchEventSpec priorMatches,
            ISet<string> priorAllTags,
            CreateContextValidationEnv validationEnv)
        {
            if (endpoint is ContextSpecConditionCrontab) {
                var crontab = (ContextSpecConditionCrontab) endpoint;
                var forgesPerCrontab = new ExprForge[crontab.Crontabs.Count][];
                for (int i = 0; i < crontab.Crontabs.Count; i++) {
                    var item = crontab.Crontabs[i];
                    var forges = ScheduleExpressionUtil.CrontabScheduleValidate(
                        ExprNodeOrigin.CONTEXTCONDITION,
                        item,
                        false,
                        validationEnv.StatementRawInfo,
                        validationEnv.Services);
                    forgesPerCrontab[i] = forges;
                }
                crontab.ForgesPerCrontab = forgesPerCrontab;
                validationEnv.ScheduleHandleCallbackProviders.Add(crontab);
                return new ContextDetailMatchPair(
                    crontab,
                    new MatchEventSpec(),
                    new LinkedHashSet<string>(),
                    EmptyList<StmtClassForgeableFactory>.Instance);
            }

            if (endpoint is ContextSpecConditionTimePeriod) {
                var timePeriod = (ContextSpecConditionTimePeriod) endpoint;
                var validationContext = new ExprValidationContextBuilder(
                    new StreamTypeServiceImpl(false),
                    validationEnv.StatementRawInfo,
                    validationEnv.Services).Build();
                ExprNodeUtilityValidate.GetValidatedSubtree(
                    ExprNodeOrigin.CONTEXTCONDITION,
                    timePeriod.TimePeriod,
                    validationContext);
                if (timePeriod.TimePeriod.IsConstantResult) {
                    if (timePeriod.TimePeriod.EvaluateAsSeconds(null, true, null) < 0) {
                        throw new ExprValidationException(
                            "Invalid negative time period expression '" +
                            ExprNodeUtilityPrint.ToExpressionStringMinPrecedenceSafe(timePeriod.TimePeriod) +
                            "'");
                    }
                }

                validationEnv.ScheduleHandleCallbackProviders.Add(timePeriod);
                return new ContextDetailMatchPair(
                    timePeriod,
                    new MatchEventSpec(),
                    new LinkedHashSet<string>(),
                    EmptyList<StmtClassForgeableFactory>.Instance);
            }

            if (endpoint is ContextSpecConditionPattern) {
                var pattern = (ContextSpecConditionPattern) endpoint;
                var matches = ValidatePatternContextConditionPattern(
                    isStartCondition,
                    nestingLevel,
                    pattern,
                    eventTypesReferenced,
                    priorMatches,
                    priorAllTags,
                    validationEnv);

                var validatedDesc = ValidatePatternContextConditionPattern(
                    isStartCondition,
                    nestingLevel,
                    pattern,
                    eventTypesReferenced,
                    priorMatches,
                    priorAllTags,
                    validationEnv);
                return new ContextDetailMatchPair(
                    pattern,
                    validatedDesc.MatchEventSpec,
                    validatedDesc.AllTags,
                    validatedDesc.AdditionalForgeables);
            }

            if (endpoint is ContextSpecConditionFilter) {
                var filter = (ContextSpecConditionFilter) endpoint;
                ValidateNotTable(filter.FilterSpecRaw.EventTypeName, validationEnv.Services);

                // compile as filter if there are no prior match to consider
                if (priorMatches == null ||
                    priorMatches.ArrayEventTypes.IsEmpty() && priorMatches.TaggedEventTypes.IsEmpty()) {
                    var rawExpr = new FilterStreamSpecRaw(
                        filter.FilterSpecRaw,
                        ViewSpec.EMPTY_VIEWSPEC_ARRAY,
                        null,
                        StreamSpecOptions.DEFAULT);
                    var compiledDesc = StreamSpecCompiler.Compile(
                        rawExpr,
                        eventTypesReferenced,
                        false,
                        false,
                        true,
                        false,
                        filter.OptionalFilterAsName,
                        0,
                        validationEnv.StatementRawInfo,
                        validationEnv.Services);
                    var compiled = (FilterStreamSpecCompiled) compiledDesc.StreamSpecCompiled;

                    filter.FilterSpecCompiled = compiled.FilterSpecCompiled;
                    var matchEventSpec = new MatchEventSpec();
                    var filterForType = compiled.FilterSpecCompiled.FilterForEventType;
                    var allTags = new LinkedHashSet<string>();
                    if (filter.OptionalFilterAsName != null) {
                        matchEventSpec.TaggedEventTypes.Put(
                            filter.OptionalFilterAsName,
                            new Pair<EventType, string>(filterForType, rawExpr.RawFilterSpec.EventTypeName));
                        allTags.Add(filter.OptionalFilterAsName);
                    }

                    validationEnv.FilterSpecCompileds.Add(compiled.FilterSpecCompiled);
                    var serdeForgeables = SerdeEventTypeUtility.Plan(
                        filter.FilterSpecCompiled.FilterForEventType,
                        validationEnv.StatementRawInfo,
                        validationEnv.Services.SerdeEventTypeRegistry,
                        validationEnv.Services.SerdeResolver);
                    var allForgeables = compiledDesc.AdditionalForgeables
                        .Concat(serdeForgeables)
                        .ToList();
                    return new ContextDetailMatchPair(filter, matchEventSpec, allTags, allForgeables);
                }

                // compile as pattern if there are prior matches to consider, since this is a type of followed-by relationship
                EvalForgeNode forgeNode = new EvalFilterForgeNode(validationEnv.Services.IsAttachPatternText, filter.FilterSpecRaw, filter.OptionalFilterAsName, 0);
                var pattern = new ContextSpecConditionPattern(forgeNode, true, false);
                var validated = ValidatePatternContextConditionPattern(
                    isStartCondition,
                    nestingLevel,
                    pattern,
                    eventTypesReferenced,
                    priorMatches,
                    priorAllTags,
                    validationEnv);
                return new ContextDetailMatchPair(pattern, validated.MatchEventSpec, validated.AllTags, validated.AdditionalForgeables);
            }

            if (endpoint is ContextSpecConditionImmediate || endpoint is ContextSpecConditionNever) {
                return new ContextDetailMatchPair(
                    endpoint,
                    new MatchEventSpec(),
                    new LinkedHashSet<String>(),
                    EmptyList<StmtClassForgeableFactory>.Instance);
            }

            throw new IllegalStateException("Unrecognized endpoint type " + endpoint);
        }