代码之家  ›  专栏  ›  技术社区  ›  Lokesh Agrawal

序列图中的Goto?

  •  1
  • Lokesh Agrawal  · 技术社区  · 7 年前

    如何在序列图中显示goto语句。

    例如,在下图中,一旦“休眠到保持期”过期,我想将控制权带回“is_item_inventory_onhold_state(item_id)”语句。如何显示这是一个图表?

    enter image description here

    我正在使用 https://sequencediagram.org/ 创建这些图表。

    为生成上述图表而编写的代码:

    title Item Executor
    
    loop  for each item in a list 
    Client->ItemExecutor: execute(item)
    
    ItemExecutor -> ItemStateService:is_item_inventory_onhold_state(item_id)
    
    alt True - Item state is on hold
    ItemStateService -->ItemExecutor: True
    ItemExecutor ->ItemExecutor: sleep till hold period 
    
    goto  ItemExecutor -> ItemStateService:is_item_inventory_onhold_state(item_id)
    
    else False - Item is not in Held State
    ItemStateService -->ItemExecutor:False
    ItemExecutor ->ItemExecutor: do_something()
    
    end
    
    ItemExecutor ->Client : Acknowledge
    end 
    
    3 回复  |  直到 7 年前
        1
  •  4
  •   Axel Scheithauer    7 年前

    序列图中不支持Goto(有充分的理由)。结合使用 loop 和 break 接线员。请参见此图: enter image description here sequencediagram.org/Item Executor

    sequencediagram.org/Item Executor (with Execution Specifications)

    关于这张图的几点注记

    • 这个 break-fragment 留下立即封闭的片段。因此,它必须直接包含在 loop-fragment
    • alt 和 opt 片段不使用 guard reply 带有某种特殊含义的信息 return-value . 如果要使用保护,必须将返回值指定给局部变量。这将发生在alt片段之上(见下图)。
    • 返回值用前面的冒号显示。消息名应该在前面,但是如果很明显,可以忽略它(这里就是这样)。
    • execution specifications (有时称为“激活”)仅在有助于可读性的地方显示。与普遍的看法相反,它们不是强制性的。
    • UML不知道每一个循环。因此,我添加了迭代器操作。术语“列表中的每个项目”不是一个术语 警卫 comment 连接到循环。为此错误使用布尔值保护毫无意义。如果你想要一个正式的定义,你必须添加你自己的原型 «for each loop»
    • ItemExecutor 和 ItemStateService 是类名。它们需要在前面加一个冒号,以区别于角色名。当然,如果角色名和类名相同,那么您的关系图可能是正确的。
    • 回复 execute 消息因此,它将具有相同的名称(此处省略)。
    • 在具有执行规范的版本中,绘图工具不允许结束 符合 send events reply-messages ,这是正确的。

    带防护装置的示例 和 片段(节选): with guards sequencediagram.org/Item Executor (with Execution Specifications and guards)

        2
  •  2
  •   bruno    7 年前

    你不需要“goto”,就像我们很久没有在语言中使用它们一样

        3
  •  2
  •   qwerty_so Rainier Wolfcastle    7 年前

    未显示转到本身。您只需显示发送了哪些操作。你可以在转到发生的地方加一张便条。然而,我认为不应该以任何方式鼓励使用gotos。相反,它可能需要一些异常处理。

    根据您的评论,您可以使用 break 像这样的片段:

    enter image description here

    它将打破外环,因此 is_item... 之后重复 sleep... .

    根据@AxelScheithauer的评论 打破 显然,它会影响外循环片段(如我编辑的图片所示)。