UML 状态图中的转换:最好使用触发器还是守卫? [英] Transitions in UML state charts: better to use triggers or guards?

查看:59
本文介绍了UML 状态图中的转换:最好使用触发器还是守卫?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 UML 状态图的设计中,我似乎可以选择使用触发器保护逻辑来实现状态之间的转换.

In the design of UML state charts it appears that I can chose to use either triggers or guard logic to achieve transitions between states.

那么哪个更好用?给定相同的转换逻辑,触发器的行为与守卫有什么不同吗?一个相对于另一个的优点/缺点是什么?

So which is better to use? Given the same logic for transition, does a trigger behave any differently than a guard? What are the benefits/drawbacks of one over the other?

是否可能因特定工具而有所不同,或者 UML 标准是否严格定义了任一转换方法的行为?

Are there perhaps differences depending on the particular tool, or does the UML standard strictly define the behaviors of either method of transition?

我目前正在使用 Simulink Stateflow 来设计状态机.

I'm presently using Simulink Stateflow to design a state machine.

推荐答案

这两个是不同的概念.

触发器 是一个 事件 事件,它使转换成为可能,而 guard 是一个 条件,必须将其评估为 true 才能继续进行转换.

Trigger is an event occurrence which enables the transition, while guard is a condition that must be evaluated to true in order for the transition to proceed.

所以你不能互换使用它们——它们有不同的作用.

So you cannot use them interchangeably — they have different roles.

另请注意,默认保护(如果未指定)是 [true],因此触发器通常足以从一种状态移动到另一种状态.

Also note that the default guard (if none is specified) is [true], so the trigger is often sufficient to move from one state to another.

更新:

总结:

  • 触发器(事件)是对象接收到的一些新数据(任何数据类型).
  • Guard 是对已经存在于对象中的某些数据的布尔表达式.
  • Trigger (event) is some new data (of any data type) that was received by the object.
  • Guard is boolean expression on some data that is alrady present in the object.

触发器(事件)是其他参与者触发的外部事件 - 用户按下按钮,浏览器请求页面加载等.因此在上图中,每次用户按下数字锁上的数字时,它都会触发按下数字"事件.

Trigger (event) is an outside event that some other actor fired - user has pressed a button, browser requested a page load, etc. So in the image above, every time user presses a digit on a digital lock it fires "pressed digit" event.

如果 pin(数字序列)有效,则将启用到 unlocked 状态的转换.

If the pin (sequence of digits) is valid, then the transition to unlocked state will be enabled.

另一种看待它的方式:

如果您按下键盘键,系统会触发 keypress 事件,这将是一个触发器,其值为按下的键.然后你可以制作一个守卫 [pressedKey = enter] (守卫总是一个布尔表达式).

If you press a keyboard key the system fires a keypress event, that would be a trigger whose value is the pressed key. Then you can make a guard [pressedKey = enter] (guard is always a boolean expression).

然而,这里只有守卫是不够的,因为没有什么可比的.

However having just the guard is not enough here, because there would be nothing to compare against.

这篇关于UML 状态图中的转换:最好使用触发器还是守卫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆