public synchronized void contextPartitionTerminate(
     ContextControllerInstanceHandle contextNestedHandle,
     Map<String, Object> terminationProperties) {
   ContextNestedHandleImpl handle = (ContextNestedHandleImpl) contextNestedHandle;
   ContextControllerTreeAgentInstanceList entry =
       agentInstances.remove(handle.getContextPartitionOrPathId());
   if (entry != null) {
     StatementAgentInstanceUtil.stopAgentInstances(
         entry.getAgentInstances(), terminationProperties, servicesContext, false);
     entry.getAgentInstances().clear();
     contextPartitionIdManager.removeId(contextNestedHandle.getContextPartitionOrPathId());
   }
 }
  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 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);
  }