You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用PlantUML新版Beta语法合并活动图中两个分离分支

在PlantUML Beta活动图中合并分支到共同活动

问题场景

我使用PlantUML新版Beta活动图语法绘制流程,需要将A1+和B1+两个执行分支合并到同一个共同活动后结束,但目前只能重复创建同名活动。尝试用Goto/Label临时解决,却导致布局混乱、线条覆盖元素,效果很差。请问如何实现简洁的合并节点,让两个分支连接到同一个活动?

当前有问题的代码(重复活动)

@startuml
'PlantUML Beta活动图语法
start
:Some Start Activity;
if (Decision AB?) then (A)
    if (Decision A1/A2?) then (A1)
        if (Decision4 A1+/A1-?) then (A1+)
            :Activity for A1+;
            :Activity_2 for A1+;
            :Activity_3 for A1+;
            :Activity_4 for A1+;
            :**TODO Common Activity for A1+, B1+**;
            stop
        else (A1-)
            :Activity for A1-;
            stop
        endif
    else (A2)
    endif
else (B)
    if (Decision B1/B2?) then (B1)
        if (Decision B1+/B1-?) then (B1+)
            :**TODO Common Activity for A1+, B1+**;
            stop
        else (B1-)
        endif
    else (B2)
    endif
endif
:Common activity for A2, B2, B1-;
stop

@enduml

临时方案代码(布局混乱)

@startuml
'PlantUML Beta活动图语法
start
:Some Start Activity;
if (Decision AB?) then (A)
    if (Decision A1/A2?) then (A1)
        if (Decision4 A1+/A1-?) then (A1+)
            :Activity for A1+;
            :Activity_2 for A1+;
            :Activity_3 for A1+;
            :Activity_4 for A1+;
            label myLabel
            :**TODO Common Activity for A1+, B1+**;
            stop
        else (A1-)
            :Activity for A1-;
            stop
        endif
    else (A2)
    endif
else (B)
    if (Decision B1/B2?) then (B1)
        if (Decision B1+/B1-?) then (B1+)
            label dummyLabel
            goto myLabel
        else (B1-)
        endif
    else (B2)
    endif
endif
:Common activity for A2, B2, B1-;
stop

@enduml

解决方案

在PlantUML Beta活动图语法中,无需使用Goto/Label,直接通过分支指向外部定义的共同活动即可实现整洁的合并效果:

  1. 将共同活动移到所有分支外部,仅定义一次
  2. 在需要合并的分支末尾,用-> 活动名称语法指向该共同活动
  3. 确保活动名称完全匹配(包括格式,比如加粗标记)

修正后的代码

@startuml
'PlantUML Beta活动图语法
start
:Some Start Activity;
if (Decision AB?) then (A)
    if (Decision A1/A2?) then (A1)
        if (Decision4 A1+/A1-?) then (A1+)
            :Activity for A1+;
            :Activity_2 for A1+;
            :Activity_3 for A1+;
            :Activity_4 for A1+;
            -> **Common Activity for A1+, B1+**
        else (A1-)
            :Activity for A1-;
            stop
        endif
    else (A2)
        -> Common activity for A2, B2, B1-
    endif
else (B)
    if (Decision B1/B2?) then (B1)
        if (Decision B1+/B1-?) then (B1+)
            -> **Common Activity for A1+, B1+**
        else (B1-)
            -> Common activity for A2, B2, B1-
        endif
    else (B2)
        -> Common activity for A2, B2, B1-
    endif
endif

:**Common Activity for A1+, B1+**;
stop

:Common activity for A2, B2, B1-;
stop
@enduml

说明

  • PlantUML会自动识别指向同一活动的分支,生成整洁的合并连接线,不会出现线条覆盖问题
  • 共同活动只需要定义一次,所有分支通过->跳转即可,避免重复代码
  • 如果需要更显式的合并节点,也可以使用merge语法,但对于此类简单场景,直接指向活动的方式更简洁

内容的提问来源于stack exchange,提问作者Leprechaun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 06:53:14