public EPStatementStartMethodCreateVariable(StatementSpecCompiled statementSpec) {
super(statementSpec);
}
public EPStatementStartResult startInternal(final EPServicesContext services, final StatementContext statementContext, boolean isNewStatement, boolean isRecoveringStatement, boolean isRecoveringResilient) throws ExprValidationException, ViewProcessingException {
final CreateVariableDesc createDesc = statementSpec.getCreateVariableDesc();
// Get assignment value
Object value = null;
if (createDesc.getAssignment() != null)
{
// Evaluate assignment expression
StreamTypeService typeService = new StreamTypeServiceImpl(new EventType[0], new String[0], new boolean[0], services.getEngineURI(), false);
ExprEvaluatorContextStatement evaluatorContextStmt = new ExprEvaluatorContextStatement(statementContext);
ExprValidationContext validationContext = new ExprValidationContext(typeService, statementContext.getMethodResolutionService(), null, statementContext.getSchedulingService(), statementContext.getVariableService(), evaluatorContextStmt, statementContext.getEventAdapterService(), statementContext.getStatementName(), statementContext.getStatementId(), statementContext.getAnnotations(), statementContext.getContextDescriptor(), false, false);
ExprNode validated = ExprNodeUtility.getValidatedSubtree(ExprNodeOrigin.VARIABLEASSIGN, createDesc.getAssignment(), validationContext);
value = validated.getExprEvaluator().evaluate(null, true, evaluatorContextStmt);
}
// Create variable
try {
services.getVariableService().createNewVariable(statementSpec.getOptionalContextName(), createDesc.getVariableName(), createDesc.getVariableType(), createDesc.isConstant(), createDesc.isArray(), value, services.getEngineImportService());
}
catch (VariableExistsException ex) {
// for new statement we don't allow creating the same variable
if (isNewStatement) {
throw new ExprValidationException("Cannot create variable: " + ex.getMessage(), ex);
}
}
catch (VariableDeclarationException ex) {
throw new ExprValidationException("Cannot create variable: " + ex.getMessage(), ex);
}
EPStatementDestroyMethod destroyMethod = new EPStatementDestroyMethod() {
public void destroy() {
try {
services.getStatementVariableRefService().removeReferencesStatement(statementContext.getStatementName());
}
catch (RuntimeException ex) {
log.error("Error removing variable '" + createDesc.getVariableName() + "': " + ex.getMessage());
}
}
};
EPStatementStopMethod stopMethod = new EPStatementStopMethod(){
public void stop()
{
}
};
VariableMetaData variableMetaData = services.getVariableService().getVariableMetaData(createDesc.getVariableName());
Viewable outputView;
if (statementSpec.getOptionalContextName() != null) {
EventType eventType = CreateVariableView.getEventType(statementContext.getStatementId(), services.getEventAdapterService(), variableMetaData);
StatementAgentInstanceFactoryCreateVariable contextFactory = new StatementAgentInstanceFactoryCreateVariable(statementContext, services, variableMetaData, eventType);
ContextMergeView mergeView = new ContextMergeView(eventType);
outputView = mergeView;
ContextManagedStatementCreateVariableDesc statement = new ContextManagedStatementCreateVariableDesc(statementSpec, statementContext, mergeView, contextFactory);
services.getContextManagementService().addStatement(statementSpec.getOptionalContextName(), statement, isRecoveringResilient);
}
else {
// allocate
services.getVariableService().allocateVariableState(createDesc.getVariableName(), VariableService.NOCONTEXT_AGENTINSTANCEID, statementContext.getExtensionServicesContext());
final CreateVariableView createView = new CreateVariableView(statementContext.getStatementId(), services.getEventAdapterService(), services.getVariableService(), createDesc.getVariableName(), statementContext.getStatementResultService());
services.getVariableService().registerCallback(createDesc.getVariableName(), 0, createView);
statementContext.getStatementStopService().addSubscriber(new StatementStopCallback() {
public void statementStopped()
{
services.getVariableService().unregisterCallback(createDesc.getVariableName(), 0, createView);
}
});
// Create result set processor, use wildcard selection
statementSpec.getSelectClauseSpec().setSelectExprList(new SelectClauseElementWildcard());
statementSpec.setSelectStreamDirEnum(SelectClauseStreamSelectorEnum.RSTREAM_ISTREAM_BOTH);
StreamTypeService typeService = new StreamTypeServiceImpl(new EventType[] {createView.getEventType()}, new String[] {"create_variable"}, new boolean[] {true}, services.getEngineURI(), false);
AgentInstanceContext agentInstanceContext = getDefaultAgentInstanceContext(statementContext);
ResultSetProcessorFactoryDesc resultSetProcessorPrototype = ResultSetProcessorFactoryFactory.getProcessorPrototype(
statementSpec, statementContext, typeService, null, new boolean[0], true, ContextPropertyRegistryImpl.EMPTY_REGISTRY, null);
ResultSetProcessor resultSetProcessor = EPStatementStartMethodHelperAssignExpr.getAssignResultSetProcessor(agentInstanceContext, resultSetProcessorPrototype);
// Attach output view
OutputProcessViewFactory outputViewFactory = OutputProcessViewFactoryFactory.make(statementSpec, services.getInternalEventRouter(), agentInstanceContext.getStatementContext(), resultSetProcessor.getResultEventType(), null);
OutputProcessViewBase outputViewBase = outputViewFactory.makeView(resultSetProcessor, agentInstanceContext);
createView.addView(outputViewBase);
outputView = outputViewBase;
services.getStatementVariableRefService().addReferences(statementContext.getStatementName(), Collections.singleton(createDesc.getVariableName()));
}
return new EPStatementStartResult(outputView, stopMethod, destroyMethod);
}