// }
}
INetInstance netInstance = rtCtx.getKernelManager().getNetInstance(
workflowProcess.getId(), thisTaskInst.getVersion());
IActivityInstance targetActivityInstance = null;
if (targetActivityId!=null){
targetActivityInstance = (IActivityInstance) netInstance
.getWFElementInstance(targetActivityId);
}
IActivityInstance thisActivityInstance = (IActivityInstance) netInstance
.getWFElementInstance(thisTaskInst.getActivityId());
if (thisActivityInstance == null) {
WorkflowProcess process = thisTaskInst.getWorkflowProcess();
throw new EngineException(thisTaskInst.getProcessInstanceId(),
process, thisTaskInst.getTaskId(), "系统没有找到与activityId="
+ thisTaskInst.getActivityId()
+ "对应activityInstance,无法执行abort操作。");
}
if (targetActivityInstance != null) {
((TaskInstance) thisTaskInst)
.setTargetActivityId(targetActivityInstance.getActivity()
.getId());
}
// 第一步,首先Abort当前taskInstance
persistenceService.abortTaskInstance((TaskInstance) thisTaskInst);
// 触发相应的事件
TaskInstanceEvent e = new TaskInstanceEvent();
e.setSource(thisTaskInst);
e.setWorkflowSession(currentSession);
e.setProcessInstance(processInstance);
e.setEventType(TaskInstanceEvent.AFTER_TASK_INSTANCE_COMPLETE);
if (this.defaultTaskInstanceEventListener != null) {
this.defaultTaskInstanceEventListener.onTaskInstanceEventFired(e);
}
this.fireTaskInstanceEvent(thisTaskInst, e);
// 第二步,检查ActivityInstance是否可以结束
if (!activityInstanceCanBeCompleted(thisTaskInst)) {
return;
}
// 第三步,尝试结束对应的activityInstance
List<IToken> tokens = persistenceService.findTokensForProcessInstance(
thisTaskInst.getProcessInstanceId(), thisTaskInst
.getActivityId());
// System.out.println("Inside TaskInstance.complete(targetActivityInstance):: tokens.size is "+tokens.size());
if (tokens == null || tokens.size() == 0) {
return;// 表明activityInstance已经结束了。
}
if (tokens.size() > 1) {
WorkflowProcess process = thisTaskInst.getWorkflowProcess();
throw new EngineException(thisTaskInst.getProcessInstanceId(),
process, thisTaskInst.getTaskId(), "与activityId="
+ thisTaskInst.getActivityId() + "对应的token数量(="
+ tokens.size() + ")不正确,正确只能为1,因此无法完成complete操作");
}
IToken token = tokens.get(0);
// stepNumber不相等,不允许执行结束操作。
if (token.getStepNumber().intValue() != thisTaskInst.getStepNumber()
.intValue()) {
return;
}
if (token.isAlive() == false) {
WorkflowProcess process = thisTaskInst.getWorkflowProcess();
throw new EngineException(thisTaskInst.getProcessInstanceId(),
process, thisTaskInst.getTaskId(), "与activityId="
+ thisTaskInst.getActivityId()
+ "对应的token.alive=false,因此无法完成complete操作");
}
token.setProcessInstance(processInstance);
//调整token布局
if (targetActivityId != null) {
List<Synchronizer> allSynchronizersAndEnds = new ArrayList<Synchronizer>();
allSynchronizersAndEnds.addAll(workflowProcess.getSynchronizers());
allSynchronizersAndEnds.addAll(workflowProcess.getEndNodes());
for (int i=0;i<allSynchronizersAndEnds.size();i++){
Synchronizer synchronizer = allSynchronizersAndEnds.get(i);
if (synchronizer.getName().equals("Synchronizer4")){
System.out.println(synchronizer.getName());
}
int volumn = 0;
if (synchronizer instanceof EndNode){
volumn = synchronizer.getEnteringTransitions().size();
}else{
volumn = synchronizer.getEnteringTransitions().size()*synchronizer.getLeavingTransitions().size();
}
IToken tokenTmp = new Token();
tokenTmp.setNodeId(synchronizer.getId());
tokenTmp.setAlive(false);
tokenTmp.setProcessInstanceId(thisTaskInst.getProcessInstanceId());
tokenTmp.setStepNumber(-1);
List<String> incomingTransitionIds = new ArrayList<String>();
boolean reachable = false;
List<Transition> enteringTrans = synchronizer.getEnteringTransitions();
for (int m=0;m<aliveActivityIdsAfterJump.size();m++){
String aliveActivityId = aliveActivityIdsAfterJump.get(m);
if (workflowProcess.isReachable(aliveActivityId, synchronizer.getId())){
Transition trans = null;
reachable = true;
for (int j=0;j<enteringTrans.size();j++){
trans = enteringTrans.get(j);
Node fromNode = (Node)trans.getFromNode();
if (workflowProcess.isReachable(aliveActivityId, fromNode.getId())){
if (!incomingTransitionIds.contains(trans.getId())){
incomingTransitionIds.add(trans.getId());
}
}
}
}
}
if (reachable){
tokenTmp.setValue(volumn-(incomingTransitionIds.size()*volumn/enteringTrans.size()));
IToken virtualToken = getJoinInfo(allTokens,synchronizer.getId());
if (virtualToken!=null){
persistenceService.deleteTokensForNode(thisTaskInst.getProcessInstanceId(), synchronizer.getId());
}
if (tokenTmp.getValue()!=0){
tokenTmp.setProcessInstance(processInstance);
persistenceService.saveOrUpdateToken(tokenTmp);
}
}
}
}
thisActivityInstance.complete(token, targetActivityInstance);
}