public void addStatement(ContextControllerStatementBase statement, boolean isRecoveringResilient)
      throws ExprValidationException {

    // validation down the hierarchy
    ContextControllerStatementCtxCache caches = factory.validateStatement(statement);

    // add statement
    ContextControllerStatementDesc desc =
        new ContextControllerStatementDesc(
            statement, new ContextControllerStatementCtxCache[] {caches});
    statements.put(statement.getStatementContext().getStatementId(), desc);

    // activate if this is the first statement
    if (statements.size() == 1) {
      activate(); // this may itself trigger a callback
    }
    // activate statement in respect to existing context partitions
    else {
      for (Map.Entry<Integer, ContextControllerTreeAgentInstanceList> entry :
          agentInstances.entrySet()) {
        AgentInstance agentInstance =
            startStatement(
                entry.getKey(),
                desc,
                rootContext,
                entry.getValue().getInitPartitionKey(),
                entry.getValue().getInitContextProperties(),
                isRecoveringResilient);
        entry.getValue().getAgentInstances().add(agentInstance);
      }
    }
  }
  public void safeDestroy() {
    if (rootContext != null) {
      // deactivate
      rootContext.deactivate();
      factory.getStateCache().removeContext(contextName);

      for (Map.Entry<Integer, ContextControllerTreeAgentInstanceList> entryCP :
          agentInstances.entrySet()) {
        StatementAgentInstanceUtil.stopAgentInstances(
            entryCP.getValue().getAgentInstances(), null, servicesContext, true);
      }
      agentInstances.clear();
      contextPartitionIdManager.clear();
      statements.clear();
    }
  }
  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());
  }
 public FilterSpecLookupable getFilterLookupable(EventType eventType) {
   return factory.getFilterLookupable(eventType);
 }
  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);
  }