コースのチャプター
トレーニング · トラック 9. SysML v2 の基礎 · チャプター 9.4(全 8 チャプター)
9.4 状態と遷移 3:51
トリガー、ガード、状態アクションを持つステートマシンを記述し、時間による遷移を描きます。
Holarch 2.22 の画面です。
学習内容
- シグナルと、初期状態を持つ状態定義を定義する
- トリガー、ガード、遷移先を持つ遷移を、短縮形と名前付きの遷移で記述する
- 状態に entry、do、exit のアクションを与える
- 状態遷移ビューで遷移を描き、その名前とトリガーを設定する
- パートに状態定義を示させる(exhibit)
小さな Hybrid Car プロジェクトで SysML v2 チュートリアル「States and Transitions」に沿って、Holarch で SysML v2 のステートマシンを記述します。定義としてのシグナル、初期状態を持つ状態定義、トリガーとガードを持つ遷移、entry・do・exit のアクション、名前付きの遷移、時間による遷移を描いた状態遷移ビュー、そして状態を示す車を扱います。SysML は Object Management Group, Inc. の登録商標です。
プロジェクトファイル
- 完成したプロジェクト (.holarch.json。プロジェクトを管理 → プロジェクトファイル ▾ → プロジェクトをインポート (.json)…)
動画は holarch.app から再生されます。プライバシーポリシーをご覧ください。
演習
Hybrid Car デモプロジェクト(SysML v2)を使って、Holarch で実際に操作します。
- Hybrid Car デモプロジェクトを開き(必要に応じて作成します:プロジェクトを管理 → プロジェクトファイル ▾ → 新しいデモプロジェクト → Hybrid Car (SysML v2))、モデル → エディターとパッケージ
VehicleBehaviorを開きます。 attribute def Unplug;の後にattribute def FaultDetected;を追加します。state def PowertrainStatesの閉じ中かっこの前に、state limpHome { entry action warnDriver; do action driveLimited; }とtransition limpStop first limpHome accept StopRequest then off;を追加します。- 振る舞い → State Machines を開き、+ 新しいダイアグラムをクリックして、表示内容で
PowertrainStatesを選び、作成をクリックします。 electricを選択し、そのハンドルからlimpHomeへドラッグして、遷移の上にドロップします。遷移パネルで名前にelectricFault、トリガーにFaultDetectedと入力し、テキストで表示をクリックします。
PowertrainStates に limpHome、limpStop、そして FaultDetected を受け付ける遷移 electricFault があり、エディターの下のステータスに問題が表示されなければ完了です。
確認問題
- 何によって
offがPowertrainStatesの初期状態になっていますか? state off;の後にaccept StartRequest if charge > 20 then electric;があります。そのトリガー、ガード、遷移元、遷移先を挙げてください。- 状態の entry、do、exit の各アクションはいつ実行されますか?
DrivenCar内のexhibit state powertrainStates : PowertrainStates;は何を表していますか?
解答を表示
何によって
offがPowertrainStatesの初期状態になっていますか?entry; then off;:entry の後にoffへのサクセッションが続くため、ステートマシンはそこから始まります。ビューではそれを点として描きます。state off;の後にaccept StartRequest if charge > 20 then electric;があります。そのトリガー、ガード、遷移元、遷移先を挙げてください。トリガーはシグナル StartRequest、ガードは
charge > 20、遷移先はelectricです。この短縮形は、直前の状態であるoffから始まります。状態の entry、do、exit の各アクションはいつ実行されますか?
entry アクションは状態に入ったとき、do アクションは状態がアクティブな間、exit アクションは状態を出るときに実行されます。
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.