public ContextDetailInitiatedTerminated(ContextDetailCondition start, ContextDetailCondition end, bool overlapping, ExprNode[] distinctExpressions)
 {
     _start               = start;
     _end                 = end;
     _overlapping         = overlapping;
     _distinctExpressions = distinctExpressions;
 }
Пример #2
0
 public static void AddEndpointTypes(String contextName, ContextDetailCondition endpoint, IDictionary <String, Object> properties, ICollection <String> allTags)
 {
     if (endpoint is ContextDetailConditionFilter)
     {
         var filter = (ContextDetailConditionFilter)endpoint;
         if (filter.OptionalFilterAsName != null)
         {
             if (properties.ContainsKey(filter.OptionalFilterAsName))
             {
                 throw new ExprValidationException("For context '" + contextName + "' the stream or tag name '" + filter.OptionalFilterAsName + "' is already declared");
             }
             allTags.Add(filter.OptionalFilterAsName);
             properties.Put(filter.OptionalFilterAsName, filter.FilterSpecCompiled.FilterForEventType);
         }
     }
     if (endpoint is ContextDetailConditionPattern)
     {
         var pattern = (ContextDetailConditionPattern)endpoint;
         foreach (var entry in pattern.PatternCompiled.TaggedEventTypes)
         {
             if (properties.ContainsKey(entry.Key) && !properties.Get(entry.Key).Equals(entry.Value.First))
             {
                 throw new ExprValidationException("For context '" + contextName + "' the stream or tag name '" + entry.Key + "' is already declared");
             }
             allTags.Add(entry.Key);
             properties.Put(entry.Key, entry.Value.First);
         }
     }
 }
 public static ContextControllerCondition GetEndpoint(String contextName,
                                                      EPServicesContext servicesContext,
                                                      AgentInstanceContext agentInstanceContext,
                                                      ContextDetailCondition endpoint,
                                                      ContextControllerConditionCallback callback,
                                                      ContextInternalFilterAddendum filterAddendum,
                                                      bool isStartEndpoint,
                                                      int nestingLevel,
                                                      int pathId,
                                                      int subpathId) {
     if (endpoint is ContextDetailConditionCrontab) {
         var crontab = (ContextDetailConditionCrontab) endpoint;
         var scheduleSlot = agentInstanceContext.StatementContext.ScheduleBucket.AllocateSlot();
         return new ContextControllerConditionCrontab(agentInstanceContext.StatementContext, scheduleSlot, crontab, callback, filterAddendum);
     }
     else if (endpoint is ContextDetailConditionFilter) {
         var filter = (ContextDetailConditionFilter) endpoint;
         return new ContextControllerConditionFilter(servicesContext, agentInstanceContext, filter, callback, filterAddendum);
     }
     else if (endpoint is ContextDetailConditionPattern) {
         var key = new ContextStatePathKey(nestingLevel, pathId, subpathId);
         var pattern = (ContextDetailConditionPattern) endpoint;
         return new ContextControllerConditionPattern(servicesContext, agentInstanceContext, pattern, callback, filterAddendum, isStartEndpoint, key);
     }
     else if (endpoint is ContextDetailConditionTimePeriod) {
         var timePeriod = (ContextDetailConditionTimePeriod) endpoint;
         var scheduleSlot = agentInstanceContext.StatementContext.ScheduleBucket.AllocateSlot();
         return new ContextControllerConditionTimePeriod(contextName, agentInstanceContext, scheduleSlot, timePeriod, callback, filterAddendum);
     }
     else if (endpoint is ContextDetailConditionImmediate) {
         return new ContextControllerConditionImmediate();
     }
     throw new IllegalStateException("Unrecognized context range endpoint " + endpoint.GetType());
 }
Пример #4
0
 private static void CollectExpressions(IList <ExprNode> expressions, ContextDetailCondition endpoint)
 {
     if (endpoint is ContextDetailConditionCrontab)
     {
         var crontab = (ContextDetailConditionCrontab)endpoint;
         expressions.AddAll(crontab.Crontab);
     }
 }
Пример #5
0
 internal ContextDetailMatchPair(ContextDetailCondition condition, MatchEventSpec matches, ISet <string> allTags)
 {
     Condition = condition;
     Matches   = matches;
     AllTags   = allTags;
 }
Пример #6
0
        private ContextDetailMatchPair ValidateRewriteContextCondition(
            EPServicesContext servicesContext,
            StatementContext statementContext,
            ContextDetailCondition endpoint,
            ISet <string> eventTypesReferenced,
            MatchEventSpec priorMatches,
            ISet <string> priorAllTags)
        {
            if (endpoint is ContextDetailConditionCrontab)
            {
                var crontab  = (ContextDetailConditionCrontab)endpoint;
                var schedule = ExprNodeUtility.ToCrontabSchedule(ExprNodeOrigin.CONTEXTCONDITION, crontab.Crontab, statementContext, false);
                crontab.Schedule = schedule;
                return(new ContextDetailMatchPair(crontab, new MatchEventSpec(), new LinkedHashSet <string>()));
            }

            if (endpoint is ContextDetailConditionTimePeriod)
            {
                var timePeriod        = (ContextDetailConditionTimePeriod)endpoint;
                var validationContext =
                    new ExprValidationContext(
                        new StreamTypeServiceImpl(servicesContext.EngineURI, false),
                        statementContext.EngineImportService,
                        statementContext.StatementExtensionServicesContext, null,
                        statementContext.SchedulingService,
                        statementContext.VariableService, statementContext.TableService,
                        GetDefaultAgentInstanceContext(statementContext), statementContext.EventAdapterService,
                        statementContext.StatementName, statementContext.StatementId, statementContext.Annotations,
                        statementContext.ContextDescriptor, statementContext.ScriptingService, false, false, false, false,
                        null, false);
                ExprNodeUtility.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 '" + ExprNodeUtility.ToExpressionStringMinPrecedenceSafe(timePeriod.TimePeriod) + "'");
                    }
                }
                return(new ContextDetailMatchPair(timePeriod, new MatchEventSpec(), new LinkedHashSet <string>()));
            }

            if (endpoint is ContextDetailConditionPattern)
            {
                var pattern = (ContextDetailConditionPattern)endpoint;
                var matches = ValidatePatternContextConditionPattern(statementContext, pattern, eventTypesReferenced, priorMatches, priorAllTags);
                return(new ContextDetailMatchPair(pattern, matches.First, matches.Second));
            }

            if (endpoint is ContextDetailConditionFilter)
            {
                var filter = (ContextDetailConditionFilter)endpoint;
                ValidateNotTable(servicesContext, filter.FilterSpecRaw.EventTypeName);

                // 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, new StreamSpecOptions());
                    var compiled = (FilterStreamSpecCompiled)rawExpr.Compile(statementContext, eventTypesReferenced, false, Collections.GetEmptyList <int>(), false, true, false, filter.OptionalFilterAsName);
                    filter.FilterSpecCompiled = compiled.FilterSpec;
                    var matchEventSpec = new MatchEventSpec();
                    var filterForType  = compiled.FilterSpec.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);
                    }
                    return(new ContextDetailMatchPair(filter, matchEventSpec, allTags));
                }

                // compile as pattern if there are prior matches to consider, since this is a type of followed-by relationship
                var factoryNode = servicesContext.PatternNodeFactory.MakeFilterNode(filter.FilterSpecRaw, filter.OptionalFilterAsName, 0);
                var pattern     = new ContextDetailConditionPattern(factoryNode, true, false);
                var matches     = ValidatePatternContextConditionPattern(statementContext, pattern, eventTypesReferenced, priorMatches, priorAllTags);
                return(new ContextDetailMatchPair(pattern, matches.First, matches.Second));
            }
            else if (endpoint is ContextDetailConditionImmediate)
            {
                return(new ContextDetailMatchPair(endpoint, new MatchEventSpec(), new LinkedHashSet <string>()));
            }
            else
            {
                throw new IllegalStateException("Unrecognized endpoint type " + endpoint);
            }
        }
Пример #7
0
 protected ContextControllerCondition MakeEndpoint(ContextDetailCondition endpoint, ContextInternalFilterAddendum filterAddendum, bool isStartEndpoint, int subPathId)
 {
     return(ContextControllerConditionFactory.GetEndpoint(_factory.FactoryContext.ContextName, _factory.FactoryContext.ServicesContext, _factory.FactoryContext.AgentInstanceContextCreate,
                                                          endpoint, this, filterAddendum, isStartEndpoint,
                                                          _factory.FactoryContext.NestingLevel, _pathId, subPathId));
 }
Пример #8
0
        private static ContextDetail WalkChoice(EsperEPL2GrammarParser.CreateContextChoiceContext ctx, IDictionary <ITree, ExprNode> astExprNodeMap, IDictionary <ITree, EvalFactoryNode> astPatternNodeMap, PropertyEvalSpec propertyEvalSpec, FilterSpecRaw filterSpec)
        {
            // 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 = new ExprNode[0];
                    }
                    else
                    {
                        distinctExpressions = ASTExprHelper.ExprCollectSubNodesPerNode(ctx.createContextDistinct().expressionList().expression(), astExprNodeMap);
                    }
                }

                ContextDetailCondition startEndpoint;
                if (ctx.START() != null)
                {
                    bool immediate = CheckNow(ctx.i);
                    if (immediate)
                    {
                        startEndpoint = new ContextDetailConditionImmediate();
                    }
                    else
                    {
                        startEndpoint = GetContextCondition(ctx.r1, astExprNodeMap, astPatternNodeMap, propertyEvalSpec, false);
                    }
                }
                else
                {
                    bool immediate = CheckNow(ctx.i);
                    startEndpoint = GetContextCondition(ctx.r1, astExprNodeMap, astPatternNodeMap, propertyEvalSpec, immediate);
                }

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

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

                    IList <String> propertyNames = new List <String>();
                    IList <EsperEPL2GrammarParser.EventPropertyContext> properties = partition.eventProperty();
                    foreach (EsperEPL2GrammarParser.EventPropertyContext property in properties)
                    {
                        String propertyName = ASTUtil.GetPropertyName(property, 0);
                        propertyNames.Add(propertyName);
                    }
                    ASTExprHelper.ExprCollectSubNodes(partition, 0, astExprNodeMap); // remove expressions

                    rawSpecs.Add(new ContextDetailPartitionItem(filterSpec, propertyNames));
                }
                return(new ContextDetailPartitioned(rawSpecs));
            }

            // hash
            else if (ctx.COALESCE() != null)
            {
                IList <EsperEPL2GrammarParser.CreateContextCoalesceItemContext> coalesces = ctx.createContextCoalesceItem();
                IList <ContextDetailHashItem> rawSpecs = new List <ContextDetailHashItem>(coalesces.Count);
                foreach (EsperEPL2GrammarParser.CreateContextCoalesceItemContext coalesce in coalesces)
                {
                    ExprChainedSpec func = ASTLibFunctionHelper.GetLibFunctionChainSpec(coalesce.libFunctionNoClass(), astExprNodeMap);
                    filterSpec       = ASTFilterSpecHelper.WalkFilterSpec(coalesce.eventFilterExpression(), propertyEvalSpec, astExprNodeMap);
                    propertyEvalSpec = null;
                    rawSpecs.Add(new ContextDetailHashItem(func, filterSpec));
                }

                String granularity = ctx.g.Text;
                if (!granularity.ToLower().Equals("granularity"))
                {
                    throw ASTWalkException.From("Expected 'granularity' keyword after list of coalesce items, found '" + granularity + "' instead");
                }
                var    num            = ASTConstantHelper.Parse(ctx.number());
                String preallocateStr = ctx.p != null ? ctx.p.Text : null;
                if (preallocateStr != null && !preallocateStr.ToLower().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 ContextDetailHash(rawSpecs, num.AsInt(), preallocateStr != null));
            }

            // categorized
            if (ctx.createContextGroupItem() != null)
            {
                IList <EsperEPL2GrammarParser.CreateContextGroupItemContext> grps = ctx.createContextGroupItem();
                IList <ContextDetailCategoryItem> items = new List <ContextDetailCategoryItem>();
                foreach (EsperEPL2GrammarParser.CreateContextGroupItemContext grp in grps)
                {
                    ExprNode exprNode = ASTExprHelper.ExprCollectSubNodes(grp, 0, astExprNodeMap)[0];
                    String   name     = grp.i.Text;
                    items.Add(new ContextDetailCategoryItem(exprNode, name));
                }
                filterSpec = ASTFilterSpecHelper.WalkFilterSpec(ctx.eventFilterExpression(), propertyEvalSpec, astExprNodeMap);
                return(new ContextDetailCategory(items, filterSpec));
            }

            throw new IllegalStateException("Unrecognized context detail type");
        }