in core/src/main/java/com/alibaba/smart/framework/engine/bpmn/behavior/gateway/helper/InclusiveGatewayHelper.java [206:263]
public static int calcCountOfTheJoinLatch(ActivityTreeNode node, List<String> triggerActivityIds, ProcessInstance processInstance) {
// 生成缓存键
String cacheKey = generateJoinLatchCountCacheKey(processInstance, node, triggerActivityIds);
// 检查缓存中是否已存在计算结果
Integer cachedCount = JOIN_LATCH_COUNT_CACHE.get(cacheKey);
if (cachedCount != null) {
return cachedCount;
}
// 缓存中不存在,执行原有逻辑计算结果
// 获取所有触发的叶子节点
List<ActivityTreeNode> triggeredLeafNodes = getTriggeredLeafNodes(node, triggerActivityIds);
if (triggeredLeafNodes.isEmpty()) {
JOIN_LATCH_COUNT_CACHE.put(cacheKey, 0);
return 0;
}
int count = 0;
// 用于记录已经计数过的祖先节点ID
Set<String> allCountedAncestorIds = new HashSet<>();
// 遍历所有触发的叶子节点
for (ActivityTreeNode leaf : triggeredLeafNodes) {
// 获取该叶子节点的所有祖先节点ID(包括自身)
Set<String> ancestorIdSet = new HashSet<>();
String leafId = leaf.getActivityId();
onlyCollectAncestors(leafId, leaf, ancestorIdSet);
// 检查是否与已计数的祖先节点有重叠
boolean hasOverlap = false;
if(CollectionUtil.isNotEmpty(ancestorIdSet)){
for (String ancestorId : ancestorIdSet) {
if (allCountedAncestorIds.contains(ancestorId)) {
hasOverlap = true;
break;
}
}
// 如果没有重叠,则计数+1,并将当前叶子节点的祖先节点ID添加到已计数集合中 (避免因触发同一祖先的两个子节点,而导致重复计数 unbalanced gateway)
if (!hasOverlap) {
count++;
allCountedAncestorIds.addAll(ancestorIdSet);
}
}else {
// 去除子节点和 root 节点, ancestorIdSet 为空,那么这种情况下是简单场景
count++;
}
}
// 将计算结果存入缓存
JOIN_LATCH_COUNT_CACHE.put(cacheKey, count);
return count;
}