// Use a wildcard select if the select-clause is empty, such as for on-delete.
// For on-select the select clause is not empty.
if (statementSpec.getSelectClauseSpec().getSelectExprList().size() == 0) {
statementSpec.getSelectClauseSpec().add(new SelectClauseElementWildcard());
}
AgentInstanceContext agentInstanceContext = getDefaultAgentInstanceContext();
resultSetProcessorPrototype = ResultSetProcessorFactoryFactory.getProcessorPrototype(
statementSpec, agentInstanceContext, typeService, null, new boolean[0], true, contextPropertyRegistry);
InternalEventRouter routerService = null;
boolean addToFront = false;
if (statementSpec.getInsertIntoDesc() != null || onTriggerDesc instanceof OnTriggerMergeDesc) {
routerService = services.getInternalEventRouter();
}
if (statementSpec.getInsertIntoDesc() != null) {
addToFront = statementContext.getNamedWindowService().isNamedWindow(statementSpec.getInsertIntoDesc().getEventTypeName());
}
boolean isDistinct = statementSpec.getSelectClauseSpec().isDistinct();
EventType selectResultEventType = resultSetProcessorPrototype.getResultSetProcessorFactory().getResultEventType();
StatementMetricHandle createNamedWindowMetricsHandle = processor.getCreateNamedWindowMetricsHandle();
onExprFactory = NamedWindowOnExprFactoryFactory.make(namedWindowType, onTriggerDesc.getWindowName(), namedWindowName,
onTriggerDesc,
activatorResultEventType, streamSpec.getOptionalStreamName(), addToFront, routerService,
selectResultEventType,
statementContext, createNamedWindowMetricsHandle, isDistinct);
}
// variable assignments
else if (statementSpec.getOnTriggerDesc() instanceof OnTriggerSetDesc)
{
OnTriggerSetDesc desc = (OnTriggerSetDesc) statementSpec.getOnTriggerDesc();
StreamTypeService typeService = new StreamTypeServiceImpl(new EventType[] {activatorResultEventType}, new String[] {streamSpec.getOptionalStreamName()}, new boolean[] {true}, services.getEngineURI(), false);
ExprValidationContext validationContext = new ExprValidationContext(typeService, statementContext.getMethodResolutionService(), null, statementContext.getSchedulingService(), statementContext.getVariableService(), getDefaultAgentInstanceContext(), statementContext.getEventAdapterService(), statementContext.getStatementName(), statementContext.getStatementId(), statementContext.getAnnotations(), statementContext.getContextDescriptor());
// Materialize sub-select views
subSelectStrategyCollection = EPStatementStartMethodHelperSubselect.planSubSelect(services, statementContext, queryPlanLogging, subSelectStreamDesc, new String[]{streamSpec.getOptionalStreamName()}, new EventType[]{activatorResultEventType}, new String[]{triggereventTypeName}, stopCallbacks, statementSpec.getAnnotations(), statementSpec.getDeclaredExpressions(), contextPropertyRegistry);
for (OnTriggerSetAssignment assignment : desc.getAssignments()) {
ExprNode validated = ExprNodeUtility.getValidatedSubtree(assignment.getExpression(), validationContext);
assignment.setExpression(validated);
}
try {
ExprEvaluatorContextStatement exprEvaluatorContext = new ExprEvaluatorContextStatement(statementContext);
onSetVariableViewFactory = new OnSetVariableViewFactory(statementContext.getStatementId(), desc, statementContext.getEventAdapterService(), statementContext.getVariableService(), statementContext.getStatementResultService(), exprEvaluatorContext);
}
catch (VariableValueException ex) {
throw new ExprValidationException("Error in variable assignment: " + ex.getMessage(), ex);
}
outputEventType = onSetVariableViewFactory.getEventType();
}
// split-stream use case
else
{
OnTriggerSplitStreamDesc desc = (OnTriggerSplitStreamDesc) statementSpec.getOnTriggerDesc();
String streamName = streamSpec.getOptionalStreamName();
if (streamName == null)
{
streamName = "stream_0";
}
StreamTypeService typeService = new StreamTypeServiceImpl(new EventType[] {activatorResultEventType}, new String[] {streamName}, new boolean[] {true}, services.getEngineURI(), false);
if (statementSpec.getInsertIntoDesc() == null)
{
throw new ExprValidationException("Required insert-into clause is not provided, the clause is required for split-stream syntax");
}
if ((!statementSpec.getGroupByExpressions().isEmpty()) || (statementSpec.getHavingExprRootNode() != null) || (!statementSpec.getOrderByList().isEmpty()))
{
throw new ExprValidationException("A group-by clause, having-clause or order-by clause is not allowed for the split stream syntax");
}
// Materialize sub-select views
subSelectStrategyCollection = EPStatementStartMethodHelperSubselect.planSubSelect(services, statementContext, queryPlanLogging, subSelectStreamDesc, new String[]{streamSpec.getOptionalStreamName()}, new EventType[]{activatorResultEventType}, new String[]{triggereventTypeName}, stopCallbacks, statementSpec.getAnnotations(), statementSpec.getDeclaredExpressions(), contextPropertyRegistry);
EPStatementStartMethodHelperValidate.validateNodes(statementSpec, statementContext, typeService, null);
AgentInstanceContext agentInstanceContext = getDefaultAgentInstanceContext();
ResultSetProcessorFactoryDesc[] processorFactories = new ResultSetProcessorFactoryDesc[desc.getSplitStreams().size() + 1];
ExprNode[] whereClauses = new ExprNode[desc.getSplitStreams().size() + 1];
processorFactories[0] = ResultSetProcessorFactoryFactory.getProcessorPrototype(
statementSpec, agentInstanceContext, typeService, null, new boolean[0], false, contextPropertyRegistry);
whereClauses[0] = statementSpec.getFilterRootNode();
boolean[] isNamedWindowInsert = new boolean[desc.getSplitStreams().size() + 1];
isNamedWindowInsert[0] = false;
int index = 1;
for (OnTriggerSplitStream splits : desc.getSplitStreams())
{
StatementSpecCompiled splitSpec = new StatementSpecCompiled();
splitSpec.setInsertIntoDesc(splits.getInsertInto());
splitSpec.setSelectClauseSpec(StatementLifecycleSvcImpl.compileSelectAllowSubselect(splits.getSelectClause()));
splitSpec.setFilterExprRootNode(splits.getWhereClause());
EPStatementStartMethodHelperValidate.validateNodes(splitSpec, statementContext, typeService, null);
processorFactories[index] = ResultSetProcessorFactoryFactory.getProcessorPrototype(
splitSpec, agentInstanceContext, typeService, null, new boolean[0], false, contextPropertyRegistry);
whereClauses[index] = splitSpec.getFilterRootNode();
isNamedWindowInsert[index] = statementContext.getNamedWindowService().isNamedWindow(splits.getInsertInto().getEventTypeName());
index++;
}
splitDesc = new StatementAgentInstanceFactoryOnTriggerSplitDesc(processorFactories, whereClauses, isNamedWindowInsert);
}
// For on-delete/set/update/merge, create an output processor that passes on as a wildcard the underlying event
if ((statementSpec.getOnTriggerDesc().getOnTriggerType() == OnTriggerType.ON_DELETE) ||
(statementSpec.getOnTriggerDesc().getOnTriggerType() == OnTriggerType.ON_SET) ||
(statementSpec.getOnTriggerDesc().getOnTriggerType() == OnTriggerType.ON_UPDATE) ||
(statementSpec.getOnTriggerDesc().getOnTriggerType() == OnTriggerType.ON_MERGE))
{
StatementSpecCompiled defaultSelectAllSpec = new StatementSpecCompiled();
defaultSelectAllSpec.getSelectClauseSpec().add(new SelectClauseElementWildcard());
StreamTypeService streamTypeService = new StreamTypeServiceImpl(new EventType[] {outputEventType}, new String[] {"trigger_stream"}, new boolean[] {true}, services.getEngineURI(), false);
outputResultSetProcessorPrototype = ResultSetProcessorFactoryFactory.getProcessorPrototype(defaultSelectAllSpec, getDefaultAgentInstanceContext(), streamTypeService, null, new boolean[0], true, contextPropertyRegistry);
}
EventType resultEventType = resultSetProcessorPrototype == null ? null : resultSetProcessorPrototype.getResultSetProcessorFactory().getResultEventType();
OutputProcessViewFactory outputViewFactory = OutputProcessViewFactoryFactory.make(statementSpec, services.getInternalEventRouter(), statementContext, resultEventType);
// create context factory
StatementAgentInstanceFactoryOnTrigger contextFactory = new StatementAgentInstanceFactoryOnTrigger(statementContext, statementSpec, services, activator, subSelectStrategyCollection, resultSetProcessorPrototype, validatedJoin, activatorResultEventType, splitDesc, outputResultSetProcessorPrototype, onSetVariableViewFactory, onExprFactory, outputViewFactory, isRecoveringStatement);
// perform start of hook-up to start
Viewable finalViewable;
EPStatementStopMethod stopStatementMethod;
EPStatementDestroyMethod destroyStatementMethod;
Map<ExprSubselectNode, SubSelectStrategyHolder> subselectStrategyInstances;
AggregationService aggregationService;
// 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 (ExprSubselectNode node : subSelectStrategyCollection.getSubqueries().keySet()) {
AIRegistrySubselect specificService = aiRegistryExpr.allocateSubselect(node);
node.setStrategy(specificService);
subselectStrategyInstances.put(node, new SubSelectStrategyHolder(specificService, null, null, null));
}
ContextMergeView mergeView = new ContextMergeView(resultSetProcessorPrototype.getResultSetProcessorFactory().getResultEventType());
finalViewable = mergeView;
ContextManagedStatementOnTriggerDesc statement = new ContextManagedStatementOnTriggerDesc(statementSpec, statementContext, mergeView, contextFactory);
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 {
AgentInstanceContext agentInstanceContext = getDefaultAgentInstanceContext();
final StatementAgentInstanceFactoryOnTriggerResult resultOfStart = contextFactory.newContext(agentInstanceContext);
finalViewable = resultOfStart.getFinalView();
stopStatementMethod = new EPStatementStopMethod() {
public void stop() {
resultOfStart.getStopCallback().stop();