转到序列图中?
Goto in Sequence Diagram?
如何在序列图中显示 goto 语句。
例如在下图中,一旦 "sleep till hold period" 过期,我想将控制权交还给 "is_item_inventory_onhold_state(item_id)" 语句。我怎样才能证明这是一个图表?
我正在使用 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
未显示转到本身。您只需显示发送了哪些操作。您可以在 goto 发生的地方添加注释。但是,我认为不应该以任何方式鼓励使用 gotos。相反,它可能应该进行一些异常处理。
根据您的评论,您可以使用这样的 break
片段:
它会打破外层循环,所以 is_item...
在 sleep...
之后重复。
注意 根据@AxelScheithauer 的评论,break
将只留下封闭的片段。但是,我认为这是一个规范缺陷。中断 "usually" 与循环控制流一起使用(加上一个案例控制流;但 UML 没有用于此的片段)。命名 break
片段可能是最好的,这样很明显它会影响外循环片段(如我编辑的图片所示)。
你不需要 'goto',就像我们很长时间以来不在语言中使用它们一样
添加第二个循环,用组合片段出去'break'
序列图中不支持 Goto(有充分的理由)。请改用 loop
和 break
运算符的组合。看这张图:
sequencediagram.org/Item Executor
sequencediagram.org/Item Executor (with Execution Specifications)
关于这张图的一些评论
break-fragment
留下紧邻的片段。因此它必须直接包含在 loop-fragment
中。如果它在一个 alt 片段中,则只剩下这个片段。
alt
和 opt
片段都不使用 guard
。发生的片段是由 reply
消息与某个 return-value
的出现选择的。如果你想使用守卫,你必须将 return 值分配给局部变量。这将发生在 alt 片段上方(见下图)。
- return 值以冒号显示。消息名称将在此之前,但是当它很明显时可以省略(就像这里一样)。
execution specifications
(有时称为 "activations")仅在有助于提高可读性的地方显示。与流行的看法相反,它们不是强制性的。
- UML 不知道每个循环。因此我添加了迭代器操作。术语 "for each item in a list" 不是
guard
条件。如果你想避免拼出迭代器,你可以使用 - semantic free - comment
附加到循环。为此滥用布尔守卫是没有意义的。如果你想要一个正式的定义,你必须添加你自己的刻板印象 «for each loop»
- 我假设
ItemExecutor
和 ItemStateService
是 class-names。他们需要一个前面的冒号来将它们与角色名称区分开来。当然,如果角色名称和 class 名称相同,则您的图表可能是正确的。
- "Acknowledge" 消息只是
execute
消息的 reply
消息。因此它将具有相同的名称(此处省略)。
- 在带有执行规范的版本中,绘图工具不允许
execution specifications
的末尾与 reply-messages
的 send events
重合,这本来是正确的.
alt
和 break
片段的守卫示例(摘录):
sequencediagram.org/Item Executor (with Execution Specifications and guards)
如何在序列图中显示 goto 语句。
例如在下图中,一旦 "sleep till hold period" 过期,我想将控制权交还给 "is_item_inventory_onhold_state(item_id)" 语句。我怎样才能证明这是一个图表?
我正在使用 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
未显示转到本身。您只需显示发送了哪些操作。您可以在 goto 发生的地方添加注释。但是,我认为不应该以任何方式鼓励使用 gotos。相反,它可能应该进行一些异常处理。
根据您的评论,您可以使用这样的 break
片段:
它会打破外层循环,所以 is_item...
在 sleep...
之后重复。
注意 根据@AxelScheithauer 的评论,break
将只留下封闭的片段。但是,我认为这是一个规范缺陷。中断 "usually" 与循环控制流一起使用(加上一个案例控制流;但 UML 没有用于此的片段)。命名 break
片段可能是最好的,这样很明显它会影响外循环片段(如我编辑的图片所示)。
你不需要 'goto',就像我们很长时间以来不在语言中使用它们一样
添加第二个循环,用组合片段出去'break'
序列图中不支持 Goto(有充分的理由)。请改用 loop
和 break
运算符的组合。看这张图:
sequencediagram.org/Item Executor (with Execution Specifications)
关于这张图的一些评论
break-fragment
留下紧邻的片段。因此它必须直接包含在loop-fragment
中。如果它在一个 alt 片段中,则只剩下这个片段。alt
和opt
片段都不使用guard
。发生的片段是由reply
消息与某个return-value
的出现选择的。如果你想使用守卫,你必须将 return 值分配给局部变量。这将发生在 alt 片段上方(见下图)。- return 值以冒号显示。消息名称将在此之前,但是当它很明显时可以省略(就像这里一样)。
execution specifications
(有时称为 "activations")仅在有助于提高可读性的地方显示。与流行的看法相反,它们不是强制性的。- UML 不知道每个循环。因此我添加了迭代器操作。术语 "for each item in a list" 不是
guard
条件。如果你想避免拼出迭代器,你可以使用 - semantic free -comment
附加到循环。为此滥用布尔守卫是没有意义的。如果你想要一个正式的定义,你必须添加你自己的刻板印象«for each loop»
- 我假设
ItemExecutor
和ItemStateService
是 class-names。他们需要一个前面的冒号来将它们与角色名称区分开来。当然,如果角色名称和 class 名称相同,则您的图表可能是正确的。 - "Acknowledge" 消息只是
execute
消息的reply
消息。因此它将具有相同的名称(此处省略)。 - 在带有执行规范的版本中,绘图工具不允许
execution specifications
的末尾与reply-messages
的send events
重合,这本来是正确的.
alt
和 break
片段的守卫示例(摘录):