public override EPStatementStartResult StartInternal(EPServicesContext services, StatementContext statementContext, bool isNewStatement, bool isRecoveringStatement, bool isRecoveringResilient)
        {
            // validate use of table: may not both read and write
            ValidateTableAccessUse(StatementSpec.IntoTableSpec, StatementSpec.TableNodes);

            var contextName = StatementSpec.OptionalContextName;
            var defaultAgentInstanceContext = GetDefaultAgentInstanceContext(statementContext);
            var selectDesc = EPStatementStartMethodSelectUtil.Prepare(StatementSpec, services, statementContext, isRecoveringResilient, defaultAgentInstanceContext, IsQueryPlanLogging(services), null, null, null);

            statementContext.StatementAgentInstanceFactory = selectDesc.StatementAgentInstanceFactorySelect;

            // allow extension to walk
            statementContext.StatementExtensionServicesContext.PreStartWalk(selectDesc);

            // Determine context
            EPStatementStopMethod stopStatementMethod;
            Viewable           finalViewable;
            AggregationService aggregationService;
            IDictionary <ExprSubselectNode, SubSelectStrategyHolder>       subselectStrategyInstances;
            IDictionary <ExprPriorNode, ExprPriorEvalStrategy>             priorStrategyInstances;
            IDictionary <ExprPreviousNode, ExprPreviousEvalStrategy>       previousStrategyInstances;
            IDictionary <ExprTableAccessNode, ExprTableAccessEvalStrategy> tableAccessStrategyInstances;
            var preloadList = Collections.GetEmptyList <StatementAgentInstancePreload>();
            RegexExprPreviousEvalStrategy matchRecognizePrevEvalStrategy;

            // With context - delegate instantiation to context
            if (StatementSpec.OptionalContextName != null)
            {
                // use statement-wide agent-instance-specific aggregation service
                aggregationService = statementContext.StatementAgentInstanceRegistry.AgentInstanceAggregationService;

                // use statement-wide agent-instance-specific subselects
                var aiRegistryExpr = statementContext.StatementAgentInstanceRegistry.AgentInstanceExprService;

                subselectStrategyInstances = new Dictionary <ExprSubselectNode, SubSelectStrategyHolder>();
                foreach (var entry in selectDesc.SubSelectStrategyCollection.Subqueries)
                {
                    var specificService = aiRegistryExpr.AllocateSubselect(entry.Key);
                    entry.Key.Strategy = specificService;

                    IDictionary <ExprPriorNode, ExprPriorEvalStrategy> subselectPriorStrategies = new Dictionary <ExprPriorNode, ExprPriorEvalStrategy>();
                    foreach (var subselectPrior in entry.Value.PriorNodesList)
                    {
                        var specificSubselectPriorService = aiRegistryExpr.AllocatePrior(subselectPrior);
                        subselectPriorStrategies.Put(subselectPrior, specificSubselectPriorService);
                    }

                    IDictionary <ExprPreviousNode, ExprPreviousEvalStrategy> subselectPreviousStrategies = new Dictionary <ExprPreviousNode, ExprPreviousEvalStrategy>();
                    foreach (var subselectPrevious in entry.Value.PrevNodesList)
                    {
                        var specificSubselectPreviousService = aiRegistryExpr.AllocatePrevious(subselectPrevious);
                        subselectPreviousStrategies.Put(subselectPrevious, specificSubselectPreviousService);
                    }

                    var subselectAggregation = aiRegistryExpr.AllocateSubselectAggregation(entry.Key);
                    var strategyHolder       = new SubSelectStrategyHolder(specificService, subselectAggregation, subselectPriorStrategies, subselectPreviousStrategies, null, null, null);
                    subselectStrategyInstances.Put(entry.Key, strategyHolder);
                }

                // use statement-wide agent-instance-specific "prior"
                priorStrategyInstances = new Dictionary <ExprPriorNode, ExprPriorEvalStrategy>();
                foreach (var priorNode in selectDesc.ViewResourceDelegateUnverified.PriorRequests)
                {
                    var specificService = aiRegistryExpr.AllocatePrior(priorNode);
                    priorStrategyInstances.Put(priorNode, specificService);
                }

                // use statement-wide agent-instance-specific "previous"
                previousStrategyInstances = new Dictionary <ExprPreviousNode, ExprPreviousEvalStrategy>();
                foreach (var previousNode in selectDesc.ViewResourceDelegateUnverified.PreviousRequests)
                {
                    var specificService = aiRegistryExpr.AllocatePrevious(previousNode);
                    previousStrategyInstances.Put(previousNode, specificService);
                }

                // use statement-wide agent-instance-specific match-recognize "previous"
                matchRecognizePrevEvalStrategy = aiRegistryExpr.AllocateMatchRecognizePrevious();

                // use statement-wide agent-instance-specific tables
                tableAccessStrategyInstances = new Dictionary <ExprTableAccessNode, ExprTableAccessEvalStrategy>();
                if (StatementSpec.TableNodes != null)
                {
                    foreach (ExprTableAccessNode tableNode in StatementSpec.TableNodes)
                    {
                        var specificService = aiRegistryExpr.AllocateTableAccess(tableNode);
                        tableAccessStrategyInstances.Put(tableNode, specificService);
                    }
                }

                var mergeView = new ContextMergeView(selectDesc.ResultSetProcessorPrototypeDesc.ResultSetProcessorFactory.ResultEventType);
                finalViewable = mergeView;

                var statement = new ContextManagedStatementSelectDesc(StatementSpec, statementContext, mergeView, selectDesc.StatementAgentInstanceFactorySelect,
                                                                      selectDesc.ResultSetProcessorPrototypeDesc.AggregationServiceFactoryDesc.Expressions,
                                                                      selectDesc.SubSelectStrategyCollection);
                services.ContextManagementService.AddStatement(contextName, statement, isRecoveringResilient);
                var selectStop = selectDesc.StopMethod;
                stopStatementMethod = new ProxyEPStatementStopMethod(() =>
                {
                    services.ContextManagementService.StoppedStatement(
                        contextName,
                        statementContext.StatementName,
                        statementContext.StatementId,
                        statementContext.Expression,
                        statementContext.ExceptionHandlingService);
                    selectStop.Stop();
                });

                selectDesc.DestroyCallbacks.AddCallback(new EPStatementDestroyCallbackContext(services.ContextManagementService, contextName, statementContext.StatementName, statementContext.StatementId));
            }
            // Without context - start here
            else
            {
                var resultOfStart = (StatementAgentInstanceFactorySelectResult)selectDesc.StatementAgentInstanceFactorySelect.NewContext(defaultAgentInstanceContext, isRecoveringResilient);
                finalViewable = resultOfStart.FinalView;
                var startResultStop = services.EpStatementFactory.MakeStopMethod(resultOfStart);
                var selectStop      = selectDesc.StopMethod;
                stopStatementMethod = new ProxyEPStatementStopMethod(() =>
                {
                    StatementAgentInstanceUtil.StopSafe(startResultStop, statementContext);
                    selectStop.Stop();
                });
                aggregationService           = resultOfStart.OptionalAggegationService;
                subselectStrategyInstances   = resultOfStart.SubselectStrategies;
                priorStrategyInstances       = resultOfStart.PriorNodeStrategies;
                previousStrategyInstances    = resultOfStart.PreviousNodeStrategies;
                tableAccessStrategyInstances = resultOfStart.TableAccessEvalStrategies;
                preloadList = resultOfStart.PreloadList;

                matchRecognizePrevEvalStrategy = null;
                if (resultOfStart.TopViews.Length > 0)
                {
                    EventRowRegexNFAViewService matchRecognize = EventRowRegexHelper.RecursiveFindRegexService(resultOfStart.TopViews[0]);
                    if (matchRecognize != null)
                    {
                        matchRecognizePrevEvalStrategy = matchRecognize.PreviousEvaluationStrategy;
                    }
                }

                if (statementContext.StatementExtensionServicesContext != null && statementContext.StatementExtensionServicesContext.StmtResources != null)
                {
                    StatementResourceHolder holder = statementContext.StatementExtensionServicesContext.ExtractStatementResourceHolder(resultOfStart);
                    statementContext.StatementExtensionServicesContext.StmtResources.Unpartitioned = holder;
                    statementContext.StatementExtensionServicesContext.PostProcessStart(resultOfStart, isRecoveringResilient);
                }
            }

            var matchRecognizeNodes = selectDesc.StatementAgentInstanceFactorySelect.ViewResourceDelegate.PerStream[0].MatchRecognizePreviousRequests;

            // assign strategies to expression nodes
            EPStatementStartMethodHelperAssignExpr.AssignExpressionStrategies(
                selectDesc, aggregationService, subselectStrategyInstances, priorStrategyInstances,
                previousStrategyInstances, matchRecognizeNodes, matchRecognizePrevEvalStrategy,
                tableAccessStrategyInstances);

            // execute preload if any
            foreach (var preload in preloadList)
            {
                preload.ExecutePreload();
            }

            // handle association to table
            if (StatementSpec.IntoTableSpec != null)
            {
                services.StatementVariableRefService.AddReferences(statementContext.StatementName, StatementSpec.IntoTableSpec.Name);
            }

            return(new EPStatementStartResult(finalViewable, stopStatementMethod, selectDesc.DestroyCallbacks));
        }
        protected override StatementAgentInstanceFactoryResult NewContextInternal(AgentInstanceContext agentInstanceContext, bool isRecoveringResilient)
        {
            IList <StopCallback> stopCallbacks = new List <StopCallback>(2);

            Viewable finalView;
            var      viewableActivationResult = new ViewableActivationResult[_eventStreamParentViewableActivators.Length];
            IDictionary <ExprSubselectNode, SubSelectStrategyHolder> subselectStrategies;
            AggregationService aggregationService;

            Viewable[] streamViews;
            Viewable[] eventStreamParentViewable;
            Viewable[] topViews;
            IDictionary <ExprPriorNode, ExprPriorEvalStrategy>             priorNodeStrategies;
            IDictionary <ExprPreviousNode, ExprPreviousEvalStrategy>       previousNodeStrategies;
            IDictionary <ExprTableAccessNode, ExprTableAccessEvalStrategy> tableAccessStrategies;
            RegexExprPreviousEvalStrategy         regexExprPreviousEvalStrategy = null;
            IList <StatementAgentInstancePreload> preloadList = new List <StatementAgentInstancePreload>();

            EvalRootState[] patternRoots;
            StatementAgentInstancePostLoad postLoadJoin = null;
            var suppressSameEventMatches = false;
            var discardPartialsOnMatch   = false;
            EvalRootMatchRemover evalRootMatchRemover = null;

            try {
                // create root viewables
                eventStreamParentViewable = new Viewable[_numStreams];
                patternRoots = new EvalRootState[_numStreams];

                for (var stream = 0; stream < _eventStreamParentViewableActivators.Length; stream++)
                {
                    var activationResult = _eventStreamParentViewableActivators[stream].Activate(agentInstanceContext, false, isRecoveringResilient);
                    viewableActivationResult[stream] = activationResult;
                    stopCallbacks.Add(activationResult.StopCallback);
                    suppressSameEventMatches = activationResult.IsSuppressSameEventMatches;
                    discardPartialsOnMatch   = activationResult.IsDiscardPartialsOnMatch;

                    // add stop callback for any stream-level viewable when applicable
                    if (activationResult.Viewable is StopCallback)
                    {
                        stopCallbacks.Add((StopCallback)activationResult.Viewable);
                    }

                    eventStreamParentViewable[stream] = activationResult.Viewable;
                    patternRoots[stream] = activationResult.OptionalPatternRoot;
                    if (stream == 0)
                    {
                        evalRootMatchRemover = activationResult.OptEvalRootMatchRemover;
                    }

                    if (activationResult.OptionalLock != null)
                    {
                        agentInstanceContext.EpStatementAgentInstanceHandle.StatementAgentInstanceLock = activationResult.OptionalLock;
                        _statementContext.DefaultAgentInstanceLock = activationResult.OptionalLock;
                    }
                }

                // compile view factories adding "prior" as necessary
                var viewFactoryChains = new IList <ViewFactory> [_numStreams];
                for (var i = 0; i < _numStreams; i++)
                {
                    var viewFactoryChain = _unmaterializedViewChain[i].FactoryChain;

                    // add "prior" view factory
                    var hasPrior = _viewResourceDelegate.PerStream[i].PriorRequests != null && !_viewResourceDelegate.PerStream[i].PriorRequests.IsEmpty();
                    if (hasPrior)
                    {
                        var priorEventViewFactory = EPStatementStartMethodHelperPrior.GetPriorEventViewFactory(
                            agentInstanceContext.StatementContext, i, viewFactoryChain.IsEmpty(), false, -1);
                        viewFactoryChain = new List <ViewFactory>(viewFactoryChain);
                        viewFactoryChain.Add(priorEventViewFactory);
                    }
                    viewFactoryChains[i] = viewFactoryChain;
                }

                // create view factory chain context: holds stream-specific services
                var viewFactoryChainContexts = new AgentInstanceViewFactoryChainContext[_numStreams];
                for (var i = 0; i < _numStreams; i++)
                {
                    viewFactoryChainContexts[i] = AgentInstanceViewFactoryChainContext.Create(viewFactoryChains[i], agentInstanceContext, _viewResourceDelegate.PerStream[i]);
                }

                // handle "prior" nodes and their strategies
                priorNodeStrategies = EPStatementStartMethodHelperPrior.CompilePriorNodeStrategies(_viewResourceDelegate, viewFactoryChainContexts);

                // handle "previous" nodes and their strategies
                previousNodeStrategies = EPStatementStartMethodHelperPrevious.CompilePreviousNodeStrategies(_viewResourceDelegate, viewFactoryChainContexts);

                // materialize views
                streamViews = new Viewable[_numStreams];
                topViews    = new Viewable[_numStreams];
                for (var i = 0; i < _numStreams; i++)
                {
                    var hasPreviousNode = _viewResourceDelegate.PerStream[i].PreviousRequests != null && !_viewResourceDelegate.PerStream[i].PreviousRequests.IsEmpty();

                    var createResult = _services.ViewService.CreateViews(eventStreamParentViewable[i], viewFactoryChains[i], viewFactoryChainContexts[i], hasPreviousNode);
                    topViews[i]    = createResult.TopViewable;
                    streamViews[i] = createResult.FinalViewable;

                    var isReuseableView = _eventStreamParentViewableActivators[i] is ViewableActivatorStreamReuseView;
                    if (isReuseableView)
                    {
                        var viewsCreated = createResult.NewViews;
                        var stopCallback = new ProxyStopCallback(() => {
                            ViewServiceHelper.RemoveFirstUnsharedView(viewsCreated);
                        });
                        stopCallbacks.Add(stopCallback);
                    }

                    // add views to stop callback if applicable
                    AddViewStopCallback(stopCallbacks, createResult.NewViews);
                }

                // determine match-recognize "previous"-node strategy (none if not present, or one handling and number of nodes)
                var matchRecognize = EventRowRegexHelper.RecursiveFindRegexService(topViews[0]);
                if (matchRecognize != null)
                {
                    regexExprPreviousEvalStrategy = matchRecognize.PreviousEvaluationStrategy;
                    stopCallbacks.Add(new ProxyStopCallback(matchRecognize.Stop));
                }

                // start subselects
                subselectStrategies = EPStatementStartMethodHelperSubselect.StartSubselects(_services, _subSelectStrategyCollection, agentInstanceContext, stopCallbacks, isRecoveringResilient);

                // plan table access
                tableAccessStrategies = EPStatementStartMethodHelperTableAccess.AttachTableAccess(_services, agentInstanceContext, _statementSpec.TableNodes);

                // obtain result set processor and aggregation services
                var processorPair      = EPStatementStartMethodHelperUtil.StartResultSetAndAggregation(_resultSetProcessorFactoryDesc, agentInstanceContext, false, null);
                var resultSetProcessor = processorPair.First;
                aggregationService = processorPair.Second;
                stopCallbacks.Add(aggregationService);
                stopCallbacks.Add(resultSetProcessor);

                // for just 1 event stream without joins, handle the one-table process separately.
                JoinPreloadMethod   joinPreloadMethod;
                JoinSetComposerDesc joinSetComposer = null;
                if (streamViews.Length == 1)
                {
                    finalView         = HandleSimpleSelect(streamViews[0], resultSetProcessor, agentInstanceContext, evalRootMatchRemover, suppressSameEventMatches, discardPartialsOnMatch);
                    joinPreloadMethod = null;
                }
                else
                {
                    var joinPlanResult = HandleJoin(_typeService.StreamNames, streamViews, resultSetProcessor,
                                                    agentInstanceContext, stopCallbacks, _joinAnalysisResult, isRecoveringResilient);
                    finalView         = joinPlanResult.Viewable;
                    joinPreloadMethod = joinPlanResult.PreloadMethod;
                    joinSetComposer   = joinPlanResult.JoinSetComposerDesc;
                }

                // for stoppable final views, add callback
                if (finalView is StopCallback)
                {
                    stopCallbacks.Add((StopCallback)finalView);
                }

                // Replay any named window data, for later consumers of named data windows
                if (_services.EventTableIndexService.AllowInitIndex(isRecoveringResilient))
                {
                    var hasNamedWindow             = false;
                    var namedWindowPostloadFilters = new FilterSpecCompiled[_statementSpec.StreamSpecs.Length];
                    var namedWindowTailViews       = new NamedWindowTailViewInstance[_statementSpec.StreamSpecs.Length];
                    var namedWindowFilters         = new IList <ExprNode> [_statementSpec.StreamSpecs.Length];

                    for (var i = 0; i < _statementSpec.StreamSpecs.Length; i++)
                    {
                        var streamNum  = i;
                        var streamSpec = _statementSpec.StreamSpecs[i];

                        if (streamSpec is NamedWindowConsumerStreamSpec)
                        {
                            hasNamedWindow = true;
                            var namedSpec = (NamedWindowConsumerStreamSpec)streamSpec;
                            NamedWindowProcessor processor = _services.NamedWindowMgmtService.GetProcessor(namedSpec.WindowName);
                            var processorInstance          = processor.GetProcessorInstance(agentInstanceContext);
                            if (processorInstance != null)
                            {
                                var consumerView = processorInstance.TailViewInstance;
                                namedWindowTailViews[i] = consumerView;
                                var view = (NamedWindowConsumerView)viewableActivationResult[i].Viewable;

                                // determine preload/postload filter for index access
                                if (!namedSpec.FilterExpressions.IsEmpty())
                                {
                                    namedWindowFilters[streamNum] = namedSpec.FilterExpressions;
                                    try {
                                        var streamName = streamSpec.OptionalStreamName != null ? streamSpec.OptionalStreamName : consumerView.EventType.Name;
                                        var types      = new StreamTypeServiceImpl(consumerView.EventType, streamName, false, _services.EngineURI);
                                        var tagged     = new LinkedHashMap <string, Pair <EventType, string> >();
                                        namedWindowPostloadFilters[i] = FilterSpecCompiler.MakeFilterSpec(types.EventTypes[0], types.StreamNames[0],
                                                                                                          namedSpec.FilterExpressions, null, tagged, tagged, types, null, _statementContext, Collections.SingletonSet(0));
                                    }
                                    catch (Exception ex) {
                                        Log.Warn("Unexpected exception analyzing filter paths: " + ex.Message, ex);
                                    }
                                }

                                // preload view for stream unless the expiry policy is batch window
                                var preload = !consumerView.TailView.IsParentBatchWindow && consumerView.HasFirst();
                                if (preload)
                                {
                                    if (isRecoveringResilient && _numStreams < 2)
                                    {
                                        preload = false;
                                    }
                                }
                                if (preload)
                                {
                                    var yesRecoveringResilient = isRecoveringResilient;
                                    var preloadFilterSpec      = namedWindowPostloadFilters[i];
                                    preloadList.Add(new ProxyStatementAgentInstancePreload()
                                    {
                                        ProcExecutePreload = () => {
                                            var snapshot       = consumerView.Snapshot(preloadFilterSpec, _statementContext.Annotations);
                                            var eventsInWindow = new List <EventBean>(snapshot.Count);
                                            ExprNodeUtility.ApplyFilterExpressionsIterable(snapshot, namedSpec.FilterExpressions, agentInstanceContext, eventsInWindow);
                                            EventBean[] newEvents = eventsInWindow.ToArray();
                                            view.Update(newEvents, null);
                                            if (!yesRecoveringResilient && joinPreloadMethod != null && !joinPreloadMethod.IsPreloading && agentInstanceContext.EpStatementAgentInstanceHandle.OptionalDispatchable != null)
                                            {
                                                agentInstanceContext.EpStatementAgentInstanceHandle.OptionalDispatchable.Execute();
                                            }
                                        },
                                    });
                                }
                            }
                            else
                            {
                                Log.Info("Named window access is out-of-context, the named window '" + namedSpec.WindowName + "' has been declared for a different context then the current statement, the aggregation and join state will not be initialized for statement expression [" + _statementContext.Expression + "]");
                            }

                            preloadList.Add(new ProxyStatementAgentInstancePreload()
                            {
                                ProcExecutePreload = () => {
                                    // in a join, preload indexes, if any
                                    if (joinPreloadMethod != null)
                                    {
                                        joinPreloadMethod.PreloadFromBuffer(streamNum);
                                    }
                                    else
                                    {
                                        if (agentInstanceContext.EpStatementAgentInstanceHandle.OptionalDispatchable != null)
                                        {
                                            agentInstanceContext.EpStatementAgentInstanceHandle.OptionalDispatchable.Execute();
                                        }
                                    }
                                },
                            });
                        }
                    }

                    // last, for aggregation we need to send the current join results to the result set processor
                    if ((hasNamedWindow) && (joinPreloadMethod != null) && (!isRecoveringResilient) && _resultSetProcessorFactoryDesc.ResultSetProcessorFactory.HasAggregation)
                    {
                        preloadList.Add(new ProxyStatementAgentInstancePreload()
                        {
                            ProcExecutePreload = () => {
                                joinPreloadMethod.PreloadAggregation(resultSetProcessor);
                            },
                        });
                    }

                    if (isRecoveringResilient)
                    {
                        postLoadJoin = new StatementAgentInstancePostLoadSelect(streamViews, joinSetComposer, namedWindowTailViews, namedWindowPostloadFilters, namedWindowFilters, _statementContext.Annotations, agentInstanceContext);
                    }
                    else if (joinSetComposer != null)
                    {
                        postLoadJoin = new StatementAgentInstancePostLoadIndexVisiting(joinSetComposer.JoinSetComposer);
                    }
                }
            }
            catch (Exception) {
                var stopCallback = StatementAgentInstanceUtil.GetStopCallback(stopCallbacks, agentInstanceContext);
                StatementAgentInstanceUtil.StopSafe(stopCallback, _statementContext);
                if (InstrumentationHelper.ENABLED)
                {
                    InstrumentationHelper.Get().AContextPartitionAllocate();
                }
                throw;
            }

            var selectResult = new StatementAgentInstanceFactorySelectResult(finalView, null, agentInstanceContext, aggregationService, subselectStrategies, priorNodeStrategies, previousNodeStrategies, regexExprPreviousEvalStrategy, tableAccessStrategies, preloadList, patternRoots, postLoadJoin, topViews, eventStreamParentViewable, viewableActivationResult);

            if (_statementContext.StatementExtensionServicesContext != null)
            {
                _statementContext.StatementExtensionServicesContext.ContributeStopCallback(selectResult, stopCallbacks);
            }
            var stopCallbackX = StatementAgentInstanceUtil.GetStopCallback(stopCallbacks, agentInstanceContext);

            selectResult.StopCallback = stopCallbackX;

            return(selectResult);
        }