// 创建流程实例publicExecutionEntitycreateProcessInstanceWithInitialFlowElement(ProcessDefinitionprocessDefinition,StringbusinessKey,StringprocessInstanceName,FlowElementinitialFlowElement,Processprocess){CommandContextcommandContext=Context.getCommandContext();// Create the process instanceStringinitiatorVariableName=null;if(initialFlowElementinstanceofStartEvent){initiatorVariableName=((StartEvent)initialFlowElement).getInitiator();}// 创建父节点,这里会新增一条 ACT_RU_EXECUTION 表的数据// scope 为 true,processInstanceId 就是自己的IdExecutionEntityprocessInstance=commandContext.getExecutionEntityManager().createProcessInstanceExecution(processDefinition,businessKey,processDefinition.getTenantId(),initiatorVariableName);...// Create the first execution that will visit all the process definition elements// 创建子节点,这里会新增一条 ACT_RU_EXECUTION 表的数据// scope 为 false, processInstanceId 是父节点的 IdExecutionEntityexecution=commandContext.getExecutionEntityManager().createChildExecution(processInstance);execution.setCurrentFlowElement(initialFlowElement);// 启动流程,一般就会创建两条数据在 ACT_RU_EXECUTION 表中,这个很重要returnprocessInstance;}
// 启动流程实例publicvoidstartProcessInstance(ExecutionEntityprocessInstance,CommandContextcommandContext,Map<String,Object>variables,FlowElementinitialFlowElement,Map<String,Object>transientVariables){Processprocess=ProcessDefinitionUtil.getProcess(processInstance.getProcessDefinitionId());createProcessVariables(processInstance,variables,transientVariables,process);recordStartProcessInstance(commandContext,initialFlowElement,processInstance);...省略了事件子流程的代码// 在创建流程时,会将子节点加入到父节点的节点列表中, 这样就可以从流程实例中获取子节点// There will always be one child execution createdExecutionEntityexecution=processInstance.getExecutions().get(0);execution.setAppVersion(processInstance.getAppVersion());// 流转节点,这个非常重要, 会在下一节继续分析commandContext.getAgenda().planContinueProcessOperation(execution);}