// obtain factory for output limiting
OutputProcessViewFactory outputViewFactory = OutputProcessViewFactoryFactory.make(statementSpec, services.getInternalEventRouter(), statementContext, resultSetProcessorPrototypeDesc.getResultSetProcessorFactory().getResultEventType());
// Factory for statement-context instances
StatementAgentInstanceFactorySelect contextFactory = new StatementAgentInstanceFactorySelect(
numStreams, eventStreamParentViewableActivators,
statementContext, statementSpec, services,
typeService, unmaterializedViewChain, resultSetProcessorPrototypeDesc, joinAnalysisResult, isRecoveringResilient,
joinSetComposerPrototype, subSelectStrategyCollection, viewResourceDelegateVerified, outputViewFactory);
// Determine context
EPStatementStopMethod stopStatementMethod;
EPStatementDestroyMethod destroyStatementMethod;
Viewable finalViewable;
AggregationService aggregationService;
Map<ExprSubselectNode, SubSelectStrategyHolder> subselectStrategyInstances;
Map<ExprPriorNode, ExprPriorEvalStrategy> priorStrategyInstances;
Map<ExprPreviousNode, ExprPreviousEvalStrategy> previousStrategyInstances;
List<StatementAgentInstancePreload> preloadList = Collections.emptyList();
// With context - delegate instantiation to context
if (statementSpec.getOptionalContextName() != null) {
// use statement-wide agent-instance-specific aggregation service
aggregationService = statementContext.getStatementAgentInstanceRegistry().getAgentInstanceAggregationService();
// use statement-wide agent-instance-specific subselects
AIRegistryExpr aiRegistryExpr = statementContext.getStatementAgentInstanceRegistry().getAgentInstanceExprService();
subselectStrategyInstances = new HashMap<ExprSubselectNode, SubSelectStrategyHolder>();
for (Map.Entry<ExprSubselectNode, SubSelectStrategyFactoryDesc> entry : subSelectStrategyCollection.getSubqueries().entrySet()) {
AIRegistrySubselect specificService = aiRegistryExpr.allocateSubselect(entry.getKey());
entry.getKey().setStrategy(specificService);
Map<ExprPriorNode, ExprPriorEvalStrategy> subselectPriorStrategies = new HashMap<ExprPriorNode, ExprPriorEvalStrategy>();
for (ExprPriorNode subselectPrior : entry.getValue().getPriorNodesList()) {
AIRegistryPrior specificSubselectPriorService = aiRegistryExpr.allocatePrior(subselectPrior);
subselectPriorStrategies.put(subselectPrior, specificSubselectPriorService);
}
Map<ExprPreviousNode, ExprPreviousEvalStrategy> subselectPreviousStrategies = new HashMap<ExprPreviousNode, ExprPreviousEvalStrategy>();
for (ExprPreviousNode subselectPrevious : entry.getValue().getPrevNodesList()) {
AIRegistryPrevious specificSubselectPreviousService = aiRegistryExpr.allocatePrevious(subselectPrevious);
subselectPreviousStrategies.put(subselectPrevious, specificSubselectPreviousService);
}
subselectStrategyInstances.put(entry.getKey(), new SubSelectStrategyHolder(specificService, aggregationService, subselectPriorStrategies, subselectPreviousStrategies));
}
// use statement-wide agent-instance-specific "prior"
priorStrategyInstances = new HashMap<ExprPriorNode, ExprPriorEvalStrategy>();
for (ExprPriorNode priorNode : viewResourceDelegateUnverified.getPriorRequests()) {
AIRegistryPrior specificService = aiRegistryExpr.allocatePrior(priorNode);
priorStrategyInstances.put(priorNode, specificService);
}
// use statement-wide agent-instance-specific "previous"
previousStrategyInstances = new HashMap<ExprPreviousNode, ExprPreviousEvalStrategy>();
for (ExprPreviousNode previousNode : viewResourceDelegateUnverified.getPreviousRequests()) {
AIRegistryPrevious specificService = aiRegistryExpr.allocatePrevious(previousNode);
previousStrategyInstances.put(previousNode, specificService);
}
ContextMergeView mergeView = new ContextMergeView(resultSetProcessorPrototypeDesc.getResultSetProcessorFactory().getResultEventType());
finalViewable = mergeView;
ContextManagedStatementSelectDesc statement = new ContextManagedStatementSelectDesc(statementSpec, statementContext, mergeView, contextFactory,
resultSetProcessorPrototypeDesc.getAggregationServiceFactoryDesc().getExpressions(),
subSelectStrategyCollection);
services.getContextManagementService().addStatement(contextName, statement);
stopStatementMethod = new EPStatementStopMethod(){
public void stop() {
services.getContextManagementService().stoppedStatement(contextName, statementContext.getStatementName(), statementContext.getStatementId());
stopMethod.stop();
}
};
destroyStatementMethod = new EPStatementDestroyMethod(){
public void destroy() {
services.getContextManagementService().destroyedStatement(contextName, statementContext.getStatementName(), statementContext.getStatementId());
}
};
}
// Without context - start here
else {
final StatementAgentInstanceFactorySelectResult resultOfStart = contextFactory.newContext(agentInstanceContext);
finalViewable = resultOfStart.getFinalView();
stopStatementMethod = new EPStatementStopMethod() {
public void stop() {
StatementAgentInstanceUtil.stopSafe(resultOfStart.getStopCallback(), statementContext);
stopMethod.stop();