Chapitres du cours
Formation · Parcours 9. Les fondamentaux de SysML v2 · Chapitre 9.4 sur 8
9.4 États et transitions 3:51
Écrire une machine à états avec des déclencheurs, des gardes et des actions d'état, et dessiner une transition temporisée.
Montre Holarch 2.22.
Ce que vous allez apprendre
- Définir des signaux et une définition d'état avec un état initial
- Écrire des transitions avec un déclencheur, une garde et une cible, en forme courte et sous forme de transitions nommées
- Donner à un état des actions entry, do et exit
- Dessiner une transition dans une Vue des transitions d'état et définir son nom et son déclencheur
- Faire présenter une définition d'état par une part (exhibit)
Écrivez une machine à états SysML v2 dans Holarch, en suivant le tutoriel SysML v2 States and Transitions sur un petit projet Hybrid Car : des signaux sous forme de définitions, une définition d'état avec un état initial, des transitions avec déclencheurs et gardes, des actions entry, do et exit, des transitions nommées, une Vue des transitions d'état avec une transition temporisée dessinée dessus, et une voiture qui présente les états. SysML est une marque déposée d'Object Management Group, Inc.
Fichiers de projet
- Projet terminé (.holarch.json ; Gérer les projets → Fichiers de projet ▾ → Importer un projet (.json)…)
La vidéo est diffusée depuis holarch.app ; voir la Politique de confidentialité.
Pratique
Faites-le vous-même dans Holarch, sur le projet de démonstration Hybrid Car (SysML v2).
- Ouvrez votre projet de démonstration Hybrid Car (créez-le si besoin : Gérer les projets → Fichiers de projet ▾ → Nouveau projet de démonstration → Hybrid Car (SysML v2)), ouvrez Modèle → Éditeur et le package
VehicleBehavior. - Après
attribute def Unplug;, ajoutezattribute def FaultDetected;. - Avant l'accolade fermante de
state def PowertrainStates, ajoutezstate limpHome { entry action warnDriver; do action driveLimited; }ettransition limpStop first limpHome accept StopRequest then off;. - Ouvrez Comportement → Machines à états, cliquez sur + Nouveau diagramme, choisissez
PowertrainStatessous Affiche et cliquez sur Créer. - Sélectionnez
electricet faites glisser depuis sa poignée jusqu'àlimpHome; déposez sur Transition. Dans le panneau de transition, saisissez le NomelectricFaultet le DéclencheurFaultDetected, puis cliquez sur Afficher dans le texte.
C'est terminé quand PowertrainStates contient limpHome, limpStop et la transition electricFault qui accepte FaultDetected, et que l'état sous l'éditeur ne signale aucun problème.
Vérifiez vos connaissances
- Qu'est-ce qui fait de
offl'état initial dePowertrainStates? - Après
state off;vientaccept StartRequest if charge > 20 then electric;. Nommez son déclencheur, sa garde, sa source et sa cible. - Quand les actions entry, do et exit d'un état s'exécutent-elles ?
- Que dit
exhibit state powertrainStates : PowertrainStates;dansDrivenCar?
Afficher les réponses
Qu'est-ce qui fait de
offl'état initial dePowertrainStates?entry; then off;: l'entrée est suivie d'une succession versoff, donc la machine à états démarre là. La vue la dessine sous forme de point.Après
state off;vientaccept StartRequest if charge > 20 then electric;. Nommez son déclencheur, sa garde, sa source et sa cible.Déclencheur : le signal StartRequest. Garde :
charge > 20. Cible :electric. La forme courte part de l'état qu'elle suit,off.Quand les actions entry, do et exit d'un état s'exécutent-elles ?
L'action entry s'exécute à l'entrée dans l'état, l'action do tant que l'état est actif, et l'action exit à la sortie de l'état.
Que dit
exhibit state powertrainStates : PowertrainStates;dansDrivenCar?Que la voiture présente cette machine à états : la voiture possède un usage de PowertrainStates, donc ses états sont les états de la voiture.
Pour aller plus loin
Transcription
Afficher la transcription (narration et contenu de l'écran)
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.