Holarch
課程章節

培訓 · 第 9 單元:SysML v2 基礎 · 第 9.4 章,共 8 章

9.4 狀態與轉換 3:51

撰寫具有觸發條件、防護條件與狀態動作的狀態機,並繪製定時轉換。

展示 Holarch 2.22。

您將學到

  • 定義訊號,以及具有初始狀態的狀態定義
  • 以簡短形式與具名轉換兩種方式,撰寫具有觸發條件、防護條件與目標的轉換
  • 為狀態指定 entry、do 與 exit 動作
  • 在狀態轉換檢視中繪製轉換,並設定其名稱與觸發條件
  • 讓零件展現狀態定義

在 Holarch 中撰寫 SysML v2 狀態機,依循 SysML v2 教學課程 States and Transitions,以一個小型 Hybrid Car 專案進行:以定義表示的訊號、具有初始狀態的狀態定義、具有觸發條件與防護條件的轉換、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.