Розділи курсів
Навчання · Курс 9. Основи SysML v2 · Розділ 9.4 з 8
9.4 Стани та переходи 3:51
Напишіть машину станів із тригерами, умовами-охоронцями й діями станів і намалюйте перехід за часом.
Показано Holarch 2.22.
Що ви дізнаєтеся
- Визначити сигнали й визначення стану з початковим станом
- Писати переходи з тригером, умовою-охоронцем і ціллю у скороченій формі та як іменовані переходи
- Задати стану дії entry, do та exit
- Намалювати перехід у поданні переходів станів і задати його назву й тригер
- Зробити так, щоб частина демонструвала визначення стану
Напишіть машину станів SysML v2 у Holarch за навчальним посібником 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).
- Відкрийте свій демопроєкт 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;. - Відкрийте Поведінка → Машини станів, натисніть + Нова діаграма, оберіть
PowertrainStatesу Показує і натисніть Створити. - Виберіть
electricі перетягніть від його маркера доlimpHome; відпустіть на Перехід. На панелі переходу введіть НазваelectricFaultі ТригерFaultDetected, потім натисніть Показати в тексті.
Завдання виконано, коли PowertrainStates містить limpHome, limpStop і перехід electricFault, що приймає FaultDetected, а рядок стану під редактором не повідомляє про проблеми.
Перевірте себе
- Що робить
offпочатковим станомPowertrainStates? - Після
state off;ідеaccept StartRequest if charge > 20 then electric;. Назвіть його тригер, умову-охоронця, джерело й ціль. - Коли виконуються дії entry, do та exit стану?
- Що означає
exhibit state powertrainStates : PowertrainStates;уDrivenCar?
Показати відповіді
Що робить
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 — під час виходу зі стану.
Що означає
exhibit state powertrainStates : PowertrainStates;уDrivenCar?Що автомобіль демонструє цю машину станів: він має використання 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.