public void deactivateContextPartitions(Set<Integer> agentInstanceIds) {
   for (Integer agentInstanceId : agentInstanceIds) {
     ContextControllerTreeAgentInstanceList list = agentInstances.get(agentInstanceId);
     StatementAgentInstanceUtil.stopAgentInstances(
         list.getAgentInstances(), null, servicesContext, false);
   }
 }
  private void removeStatement(String statementId) {
    ContextControllerStatementDesc statementDesc = statements.get(statementId);
    if (statementDesc == null) {
      return;
    }

    for (Map.Entry<Integer, ContextControllerTreeAgentInstanceList> contextPartitionEntry :
        agentInstances.entrySet()) {
      Iterator<AgentInstance> instanceIt =
          contextPartitionEntry.getValue().getAgentInstances().iterator();
      for (; instanceIt.hasNext(); ) {
        AgentInstance instance = instanceIt.next();
        if (!instance
            .getAgentInstanceContext()
            .getStatementContext()
            .getStatementId()
            .equals(statementId)) {
          continue;
        }
        StatementAgentInstanceUtil.stop(
            instance.getStopCallback(),
            instance.getAgentInstanceContext(),
            instance.getFinalView(),
            servicesContext,
            true);
        instanceIt.remove();
      }
    }

    statements.remove(statementId);
  }
 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();
    }
  }
  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 void handleFilterFault(EventBean theEvent, long version) {
   StatementAgentInstanceUtil.handleFilterFault(
       theEvent, version, servicesContext, agentInstances);
 }
  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;
  }