IJoinPoint joinPoint = null;
synchronized (this) { //流程同步器需要处理并发的情况,而activity的节点不需要处理并发情况?同步器节点,可能被同时触发,activity节点不会被同时触发?
tk.setNodeId(this.getSynchronizer().getId());
log.debug("The weight of the Entering TransitionInstance is " + tk.getValue());
// 触发TokenEntered事件
NodeInstanceEvent event1 = new NodeInstanceEvent(this);
event1.setToken(tk);
event1.setEventType(NodeInstanceEvent.NODEINSTANCE_TOKEN_ENTERED);//token 进入
fireNodeEvent(event1);
//汇聚检查
joinPoint = ((ProcessInstance) tk.getProcessInstance()).createJoinPoint(this, tk);// JoinPoint由谁生成比较好?
int value = joinPoint.getValue();
log.debug("The volume of " + this.toString() + " is " + volume);
log.debug("The value of " + this.toString() + " is " + value);
if (value > volume) {//如果value大于同步器容量,那说明出错了
KernelException exception = new KernelException(tk.getProcessInstance(),
this.getSynchronizer(),
"Error:The token count of the synchronizer-instance can NOT be greater than it's volumn ");
throw exception;
}
if (value < volume) {// 如果Value小于容量则继续等待其他弧的汇聚。 (哪些状态为dead的token到此结束,不再向下传递)
return;
}
}
//如果汇聚点的容量和同步器节点的容量相同
IProcessInstance processInstance = tk.getProcessInstance();
// Synchronize的fire条件应该只与joinPoint的value有关(value==volume),与alive无关
NodeInstanceEvent event2 = new NodeInstanceEvent(this);
event2.setToken(tk);
event2.setEventType(NodeInstanceEvent.NODEINSTANCE_FIRED);
fireNodeEvent(event2);
//在此事件监听器中,删除原有的token
NodeInstanceEvent event4 = new NodeInstanceEvent(this);
event4.setToken(tk);
event4.setEventType(NodeInstanceEvent.NODEINSTANCE_LEAVING);
fireNodeEvent(event4);
//首先必须检查是否有满足条件的循环,loop比transition有更高的优先级,
//(只能够有一个loop的条件为true,流程定义的时候需要注意)
boolean doLoop = false;//表示是否有满足条件的循环,false表示没有,true表示有。
if (joinPoint.getAlive()) {
IToken tokenForLoop = new Token(); // 产生新的token
tokenForLoop.setAlive(joinPoint.getAlive());
tokenForLoop.setProcessInstance(processInstance);
tokenForLoop.setStepNumber(joinPoint.getStepNumber()-1);
tokenForLoop.setFromActivityId(joinPoint.getFromActivityId());
for (int i = 0; i < this.leavingLoopInstances.size(); i++) {
ILoopInstance loopInstance = this.leavingLoopInstances.get(i);
doLoop = loopInstance.take(tokenForLoop);
if (doLoop) {
break;
}
}
}
if (!doLoop) {//如果没有循环,则执行transitionInstance
//非顺序流转的需要生成新的token,
boolean activiateDefaultCondition = true;
ITransitionInstance defaultTransInst = null;
for (int i = 0; leavingTransitionInstances != null && i < leavingTransitionInstances.size(); i++) {
ITransitionInstance transInst = leavingTransitionInstances.get(i);
String condition = transInst.getTransition().getCondition();
if (condition != null && condition.equals(ConditionConstant.DEFAULT)) {
defaultTransInst = transInst;
continue;
}
Token token = new Token(); // 产生新的token
token.setAlive(joinPoint.getAlive());
token.setProcessInstance(processInstance);
token.setStepNumber(joinPoint.getStepNumber());
token.setFromActivityId(joinPoint.getFromActivityId());
boolean alive = transInst.take(token);
if (alive) {
activiateDefaultCondition = false;
}
}
if (defaultTransInst != null) {
Token token = new Token();
token.setAlive(activiateDefaultCondition && joinPoint.getAlive());
token.setProcessInstance(processInstance);
token.setStepNumber(joinPoint.getStepNumber());
token.setFromActivityId(joinPoint.getFromActivityId());
defaultTransInst.take(token);
}
}
NodeInstanceEvent event3 = new NodeInstanceEvent(this);
event3.setToken(tk);
event3.setEventType(NodeInstanceEvent.NODEINSTANCE_COMPLETED); //触发同步器节点的监听事件,删除所有和同步器节点相关的token
fireNodeEvent(event3);
}