Holarch
Capitoli del corso

Formazione · Percorso 9. Fondamenti di SysML v2 · Capitolo 9.4 di 8

9.4 States and Transitions 3:51

Scrivere una macchina a stati con trigger, guardie e azioni di stato, e disegnare una transizione temporizzata.

Mostra Holarch 2.22.

← 9.3 Actions and Flows · Tutti i capitoli · 9.5 Requirements →

Che cosa si impara

  • Definire segnali e una definizione di stato con uno stato iniziale
  • Scrivere transizioni con un trigger, una guardia e una destinazione, nella forma breve e come transizioni con nome
  • Assegnare a uno stato azioni entry, do ed exit
  • Disegnare una transizione in una Vista di transizione di stato e impostarne nome e trigger
  • Fare in modo che una part presenti (exhibit) una definizione di stato

Scrivere una macchina a stati SysML v2 in Holarch, seguendo il tutorial SysML v2 States and Transitions su un piccolo progetto Hybrid Car: segnali come definizioni, una definizione di stato con uno stato iniziale, transizioni con trigger e guardie, azioni entry, do ed exit, transizioni con nome, una Vista di transizione di stato con una transizione temporizzata disegnata su di essa e un’auto che presenta gli stati. SysML è un marchio registrato di Object Management Group, Inc.

File di progetto

  • Progetto completato (.holarch.json; Gestisci progetti → File di progetto ▾ → Importa progetto (.json)…)

Il video viene riprodotto da holarch.app; vedere l’Informativa sulla privacy.

Esercitazione

Provare in prima persona in Holarch, sul progetto demo Hybrid Car (SysML v2).

  1. Aprire il progetto demo Hybrid Car (crearlo se necessario: Gestisci progetti → File di progetto ▾ → Nuovo progetto demo → Hybrid Car (SysML v2)), aprire Modello → Editor e il package VehicleBehavior.
  2. Dopo attribute def Unplug;, aggiungere attribute def FaultDetected;.
  3. Prima della parentesi graffa di chiusura di state def PowertrainStates, aggiungere state limpHome { entry action warnDriver; do action driveLimited; } e transition limpStop first limpHome accept StopRequest then off;.
  4. Aprire Comportamento → Macchine a stati, fare clic su + Nuovo diagramma, scegliere PowertrainStates in Mostra e fare clic su Crea.
  5. Selezionare electric e trascinare dalla sua maniglia a limpHome; rilasciare su Transizione. Nel pannello della transizione, inserire il Nome electricFault e il Trigger FaultDetected, poi fare clic su Mostra nel testo.

L’esercizio è completato quando PowertrainStates contiene limpHome, limpStop e la transizione electricFault che accetta FaultDetected, e lo stato sotto l’editor non segnala problemi.

Verifica

  1. Che cosa rende off lo stato iniziale di PowertrainStates?
  2. Dopo state off; viene accept StartRequest if charge > 20 then electric;. Indicarne trigger, guardia, sorgente e destinazione.
  3. Quando vengono eseguite le azioni entry, do ed exit di uno stato?
  4. Che cosa dice exhibit state powertrainStates : PowertrainStates; in DrivenCar?
Mostra le risposte
  1. Che cosa rende off lo stato iniziale di PowertrainStates?

    entry; then off;: l’entry è seguita da una successione verso off, quindi la macchina a stati parte da lì. La vista lo disegna come il punto.

  2. Dopo state off; viene accept StartRequest if charge > 20 then electric;. Indicarne trigger, guardia, sorgente e destinazione.

    Trigger: il segnale StartRequest. Guardia: charge > 20. Destinazione: electric. La forma breve parte dallo stato che segue, off.

  3. Quando vengono eseguite le azioni entry, do ed exit di uno stato?

    L’azione entry viene eseguita quando si entra nello stato, l’azione do mentre lo stato è attivo e l’azione exit quando si esce dallo stato.

  4. Che cosa dice exhibit state powertrainStates : PowertrainStates; in DrivenCar?

    Che l’auto presenta questa macchina a stati: l’auto possiede un uso di PowertrainStates, quindi i suoi stati sono gli stati dell’auto.

Per saperne di più

Trascrizione

Mostra la trascrizione (narrazione e contenuto dello schermo)

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.