public ContextManagerImpl(ContextControllerFactoryServiceContext factoryServiceContext)
      throws ExprValidationException {
    this.contextName = factoryServiceContext.getContextName();
    this.servicesContext = factoryServiceContext.getServicesContext();
    this.factory =
        factoryServiceContext
            .getAgentInstanceContextCreate()
            .getStatementContext()
            .getContextControllerFactoryService()
            .getFactory(factoryServiceContext)[0];
    this.rootContext =
        factory.createNoCallback(
            0, this); // single instance: created here and activated/deactivated later
    this.contextPartitionIdManager =
        factoryServiceContext
            .getAgentInstanceContextCreate()
            .getStatementContext()
            .getContextControllerFactoryService()
            .allocatePartitionIdMgr(
                contextName,
                factoryServiceContext
                    .getAgentInstanceContextCreate()
                    .getStatementContext()
                    .getStatementId());

    StatementAIResourceRegistryFactory resourceRegistryFactory =
        factory.getStatementAIResourceRegistryFactory();

    Map<String, Object> contextProps = factory.getContextBuiltinProps();
    EventType contextPropsType =
        servicesContext.getEventAdapterService().createAnonymousMapType(contextName, contextProps);
    ContextPropertyRegistryImpl registry =
        new ContextPropertyRegistryImpl(factory.getContextDetailPartitionItems(), contextPropsType);
    contextDescriptor =
        new ContextDescriptor(
            contextName,
            factory.isSingleInstanceContext(),
            registry,
            resourceRegistryFactory,
            this,
            factory.getContextDetail());
  }
  private AgentInstance startStatement(
      int contextId,
      ContextControllerStatementDesc statementDesc,
      ContextController originator,
      Object partitionKey,
      Map<String, Object> contextProperties,
      boolean isRecoveringResilient) {

    // build filters
    IdentityHashMap<FilterSpecCompiled, List<FilterValueSetParam>> filterAddendum =
        new IdentityHashMap<FilterSpecCompiled, List<FilterValueSetParam>>();
    originator
        .getFactory()
        .populateFilterAddendums(filterAddendum, statementDesc, partitionKey, contextId);
    AgentInstanceFilterProxy proxy = new AgentInstanceFilterProxyImpl(filterAddendum);

    // build built-in context properties
    contextProperties.put(ContextPropertyEventType.PROP_CTX_NAME, contextName);
    contextProperties.put(ContextPropertyEventType.PROP_CTX_ID, contextId);
    MappedEventBean contextBean =
        (MappedEventBean)
            servicesContext
                .getEventAdapterService()
                .adapterForTypedMap(
                    contextProperties,
                    contextDescriptor.getContextPropertyRegistry().getContextEventType());

    // activate
    StatementAgentInstanceFactoryResult result =
        StatementAgentInstanceUtil.start(
            servicesContext,
            statementDesc.getStatement(),
            false,
            contextId,
            contextBean,
            proxy,
            isRecoveringResilient);

    // save only instance data
    return new AgentInstance(
        result.getStopCallback(), result.getAgentInstanceContext(), result.getFinalView());
  }
  public synchronized ContextControllerInstanceHandle contextPartitionInstantiate(
      Integer optionalContextPartitionId,
      int pathId,
      ContextController originator,
      EventBean optionalTriggeringEvent,
      Map<String, Object> optionalTriggeringPattern,
      Object partitionKey,
      Map<String, Object> contextProperties,
      ContextControllerState states,
      ContextInternalFilterAddendum filterAddendum,
      boolean isRecoveringResilient) {

    // assign context id
    int assignedContextId;
    if (optionalContextPartitionId != null) {
      assignedContextId = optionalContextPartitionId;
      contextPartitionIdManager.addExisting(optionalContextPartitionId);
    } else {
      assignedContextId = contextPartitionIdManager.allocateId();
    }

    // handle leaf creation
    List<AgentInstance> newInstances = new ArrayList<AgentInstance>();
    for (Map.Entry<String, ContextControllerStatementDesc> statementEntry : statements.entrySet()) {
      ContextControllerStatementDesc statementDesc = statementEntry.getValue();
      AgentInstance instance =
          startStatement(
              assignedContextId,
              statementDesc,
              originator,
              partitionKey,
              contextProperties,
              isRecoveringResilient);
      newInstances.add(instance);
    }

    // for all new contexts: evaluate this event for this statement
    if (optionalTriggeringEvent != null || optionalTriggeringPattern != null) {
      for (AgentInstance context : newInstances) {
        StatementAgentInstanceUtil.evaluateEventForStatement(
            servicesContext,
            optionalTriggeringEvent,
            optionalTriggeringPattern,
            context.getAgentInstanceContext());
      }
    }

    // save leaf
    long filterVersion = servicesContext.getFilterService().getFiltersVersion();
    agentInstances.put(
        assignedContextId,
        new ContextControllerTreeAgentInstanceList(
            filterVersion, partitionKey, contextProperties, newInstances));

    // update the filter version for this handle
    factory
        .getFactoryContext()
        .getAgentInstanceContextCreate()
        .getEpStatementAgentInstanceHandle()
        .getStatementFilterVersion()
        .setStmtFilterVersion(filterVersion);

    return new ContextNestedHandleImpl(assignedContextId);
  }
  public StatementAgentInstanceFactoryOnTriggerResult newContext(
      final AgentInstanceContext agentInstanceContext, boolean isRecoveringResilient) {
    List<StopCallback> stopCallbacks = new ArrayList<StopCallback>();
    View view;
    Map<ExprSubselectNode, SubSelectStrategyHolder> subselectStrategies;
    AggregationService aggregationService;
    EvalRootState optPatternRoot;
    Map<ExprTableAccessNode, ExprTableAccessEvalStrategy> tableAccessStrategies;
    final ViewableActivationResult activationResult;

    try {
      if (services.getSchedulableAgentInstanceDirectory() != null) {
        services
            .getSchedulableAgentInstanceDirectory()
            .add(agentInstanceContext.getEpStatementAgentInstanceHandle());
      }

      OnExprViewResult onExprViewResult = determineOnExprView(agentInstanceContext, stopCallbacks);
      view = onExprViewResult.getOnExprView();
      aggregationService = onExprViewResult.getOptionalAggregationService();

      // attach stream to view
      activationResult = activator.activate(agentInstanceContext, false, isRecoveringResilient);
      activationResult.getViewable().addView(view);
      stopCallbacks.add(activationResult.getStopCallback());
      optPatternRoot = activationResult.getOptionalPatternRoot();

      // determine final output view
      view = determineFinalOutputView(agentInstanceContext, view);

      // start subselects
      subselectStrategies =
          EPStatementStartMethodHelperSubselect.startSubselects(
              services, subSelectStrategyCollection, agentInstanceContext, stopCallbacks);

      // plan table access
      tableAccessStrategies =
          EPStatementStartMethodHelperTableAccess.attachTableAccess(
              services, agentInstanceContext, statementSpec.getTableNodes());
    } catch (RuntimeException ex) {
      StopCallback stopCallback =
          StatementAgentInstanceUtil.getStopCallback(stopCallbacks, agentInstanceContext);
      StatementAgentInstanceUtil.stopSafe(stopCallback, statementContext);
      throw ex;
    }

    StatementAgentInstanceFactoryOnTriggerResult onTriggerResult =
        new StatementAgentInstanceFactoryOnTriggerResult(
            view,
            null,
            agentInstanceContext,
            aggregationService,
            subselectStrategies,
            optPatternRoot,
            tableAccessStrategies,
            activationResult);
    if (statementContext.getStatementExtensionServicesContext() != null) {
      statementContext
          .getStatementExtensionServicesContext()
          .contributeStopCallback(onTriggerResult, stopCallbacks);
    }

    log.debug(".start Statement start completed");
    StopCallback stopCallback =
        StatementAgentInstanceUtil.getStopCallback(stopCallbacks, agentInstanceContext);
    onTriggerResult.setStopCallback(stopCallback);

    return onTriggerResult;
  }