Holarch
课程章节

培训 · 课程 9. SysML v2 基础 · 第 9.4 章,共 8 章

9.4 状态和转换 3:51

编写带触发器、守卫和状态动作的状态机,并绘制一个定时转换。

演示 Holarch 2.22。

您将学到

  • 定义信号和带初始状态的状态定义
  • 以简写形式和命名转换的形式编写带触发器、守卫和目标的转换
  • 为状态指定 entry、do 和 exit 动作
  • 在状态转换视图中绘制转换,并设置其名称和触发器
  • 让部件展现一个状态定义

在 Holarch 中编写 SysML v2 状态机,基于一个小型 Hybrid Car 项目,依照 SysML v2 教程“状态和转换”:作为定义的信号、带初始状态的状态定义、带触发器和守卫的转换、entry、do 和 exit 动作、命名转换、在其上绘制了定时转换的状态转换视图,以及展现这些状态的汽车。SysML 是 Object Management Group, Inc. 的注册商标。

项目文件

  • 完成项目 (.holarch.json;管理项目 → 项目文件 ▾ → 导入项目 (.json)…)

视频从 holarch.app 播放;参见隐私政策。

练习

在 Holarch 中基于 Hybrid Car 演示项目 (SysML v2) 亲自完成此操作。

  1. 打开您的 Hybrid Car 演示项目(如有需要,请创建:管理项目 → 项目文件 ▾ → 新建演示项目 → Hybrid Car (SysML v2)),打开 模型 → 编辑器 和包 VehicleBehavior。
  2. 在 attribute def Unplug; 之后添加 attribute def FaultDetected;。
  3. 在 state def PowertrainStates 的右花括号之前,添加 state limpHome { entry action warnDriver; do action driveLimited; } 和 transition limpStop first limpHome accept StopRequest then off;。
  4. 打开 行为 → 状态机,单击 + 新建图,在 显示 下选择 PowertrainStates,然后单击 创建。
  5. 选择 electric,从其手柄拖到 limpHome;在 转换 上松开。在转换面板中输入 名称 electricFault 和 触发器 FaultDetected,然后单击 在文本中显示。

当 PowertrainStates 包含 limpHome、limpStop 以及接受 FaultDetected 的转换 electricFault,且编辑器下方的状态报告没有问题时,即完成练习。

自测

  1. 是什么使 off 成为 PowertrainStates 的初始状态?
  2. 在 state off; 之后是 accept StartRequest if charge > 20 then electric;。说出其触发器、守卫、源和目标。
  3. 状态的 entry、do 和 exit 动作分别在何时运行?
  4. DrivenCar 中的 exhibit state powertrainStates : PowertrainStates; 表达了什么?
显示答案
  1. 是什么使 off 成为 PowertrainStates 的初始状态?

    entry; then off;:entry 之后接一个到 off 的 succession,因此状态机从那里开始。视图将其绘制为圆点。

  2. 在 state off; 之后是 accept StartRequest if charge > 20 then electric;。说出其触发器、守卫、源和目标。

    触发器:信号 StartRequest。守卫:charge > 20。目标:electric。这种简写形式以它所跟随的状态 off 为源。

  3. 状态的 entry、do 和 exit 动作分别在何时运行?

    entry 动作在进入状态时运行,do 动作在状态处于活动期间运行,exit 动作在离开状态时运行。

  4. DrivenCar 中的 exhibit state powertrainStates : PowertrainStates; 表达了什么?

    表示汽车展现此状态机:汽车拥有一个 PowertrainStates 的用法,因此其状态就是汽车的状态。

延伸阅读

文字稿

显示文字稿 (旁白和屏幕内容)

Introduction

Chapter four: states and transitions. A state machine says how a system reacts to events, and what it does in each state.

In this chapter you write signals and a state machine with triggers, guards and state actions, draw a timed transition in a view, and let the car exhibit the states.

Start the tutorial

Start the tutorial States and Transitions from Project Files, Start a Tutorial. Its starter project holds the powertrain package with the car, and nothing else yet.

On screen: Project Files ▾ → Start a Tutorial… → SysML v2: States and Transitions → Start; Model → Editor.

The state machine

A state machine reacts to signals, and in SysML v2 they are plain definitions. After the imports, define four: start request, stop request, plug in and unplug.

On screen: After the imports: four attribute defs.

Below the signals, write the state definition. Charge is an input that the transitions read. Entry, then off, makes off the initial state.

On screen: Below Unplug: state def PowertrainStates with three states.

Transitions and state actions

A transition has a trigger, a guard and a target. After the off state, a start request goes to electric when the charge is above twenty, and to hybrid otherwise. This short form starts at the state it follows.

On screen: After state off;: accept StartRequest if charge > 20 then electric; accept StartRequest then hybrid;

Now give the electric state its actions. Replace its line with a body: the entry action runs when the state is entered, the do action while it is active, and the exit action when it is left.

On screen: Select state electric; and type its body.

After the electric state, the powertrain switches to hybrid when the charge drops below fifteen, and turns off on a stop request. The hybrid state turns off on a stop request too.

On screen: After electric's closing brace: if charge < 15 then hybrid; accept StopRequest then off; after state hybrid;: accept StopRequest then off;

Before the closing brace of the state definition, add the charging state and two named transitions. A named transition says where it starts, with first.

On screen: After hybrid's stop transition: state charging; transition plugIn; transition unplugged.

The State Transition View

Open Behavior, then State Machines, and click New Diagram. Keep State Transition View, choose the powertrain states under Shows, and click Create.

On screen: Behavior → State Machines → + New Diagram; Shows: PowertrainBehavior::PowertrainStates; Create.

States show their entry, do and exit actions, and the dot marks the initial state. Each transition reads its trigger, the guard in square brackets, then a slash and the effect.

On screen: The view.

Draw a timed transition

A full charge takes half an hour, then the car switches off. Select the charging state, and drag from its handle to the off state. The drop makes a transition.

On screen: Select charging; drag its handle onto off.

In the transition panel, name it charged, and set its trigger to after thirty minutes.

On screen: Transition panel: Name charged; Trigger after 30 [min].

Open the Editor. The new transition is in the state definition: named charged, it accepts after thirty minutes, and goes to off.

On screen: Model → Editor: the transition charged.

Exhibit

Last, let the car exhibit the states. Inside the car, after its doc line, add an exhibit state usage. The state machine now belongs to the car.

On screen: In part def Car after doc: exhibit state powertrainStates : PowertrainStates;

Recap

Every step is ticked off. Click Mark as Done.

On screen: Step panel → Mark as Done.

Let's recap. You defined signals, wrote a state definition with an initial state, transitions with triggers and guards, and state actions, drew a timed transition that Holarch wrote into the text, and let the car exhibit the states.

Next, requirements say what the car must do, in a form the model can check.