Adding Execution Logic with Guards and Effects
Overview
State machine execution logic is defined by triggers, guards, and effects. A trigger is an event that indicates when a transition should be traversed (immediately, if unspecified); you will explore triggers in later sections. A guard specifies whether a transition may be traversed, and an effect specifies what happens if it is traversed. In this section, you will use an effect to turn on the power system and then a choice and guards to determine the power system's state.
This section covers the following concepts:
- Transitions with guards and effects
- Opaque behaviors and opaque expressions
- Choice pseudostates
Figure A1: Guards and effects
Prerequisites
Prerequisite | Description |
---|---|
Behavior Execution Engine Installation | You must have installed Behavior Execution Engine. |
Tutorial Project | You must start this section with the Behavior Execution Engine simulation project from the previous section. If you did not complete the previous section, you can use the following project from the Behavior Execution Engine installation: \documentation\tutorialFiles\02\MultipleStateMachines.mdzip |
Recommended Reading | Before completing this section, you may want to read the following help topics: |
Instructions
Create a choice pseudostate
A choice is a pseudostate that has one arriving transition and two or more departing transitions. It is represented by a white diamond.
- Open the project from the previous section.
- In the PowerSystem State Machine diagram, select Choice from the toolbar to the left of the canvas and then click anywhere on the canvas to add a choice pseudostate.
-
Select the transition from
PoweredOff
toPoweredOn
and drag the end arrow from thePoweredOn
state to the choice pseudostate. -
Select the choice pseudostate,
click the Transition button () on the context toolbar,
and then click the
PoweredOff
state. -
Select the choice pseudostate again,
click the Transition button () on the context toolbar,
and then click the
PoweredOn
state.
Add a transition effect
An effect is the consequence of taking a specific transition (in addition to the transition going to a new state or pseudostate). Effects in Behavior Execution Engine must be opaque behaviors with bodies written as opaque expressions.
-
Double-click the transition from the
PoweredOff
state to the choice pseudostate. - In the Effect category, set the Behavior Type to Opaque Behavior.
-
Set the Body and Language to the string
isPoweredOn = true;
and click .
Add transition guards
A guard is a boolean expression evaluated at simulation time that either allows or denies the traversal of a transition.
A transition will only be traversed if the guard evaluates to true
,
regardless of how the transition was triggered.
-
Double-click the transition from the choice pseudostate to the
PoweredOn
state. -
In the Transition category,
set the Guard to the string
isPoweredOn
and click . The stringisPoweredOn
is a shorthand forisPoweredOn == true
, which also works. -
Double-click the transition from the choice pseudostate to the
PoweredOff
state. -
In the Transition category,
set the Guard to the string
else
and click . The stringelse
informs Behavior Execution Engine to only traverse this transition if all of the other departing transitions evaluate tofalse
. -
In the Instance Specification Diagram,
note that the
isPoweredOn
property of theuav.powerSystem
starts set tofalse
. - Execute the simulation
and look in the state history trace to observe the
PowerSystem State Machine takes the
[isPoweredOn]
transition at the choice, indicating that theisPoweredOn
property istrue
at that point in the simulation.Figure B1: The power system guards and effect
- Save your work before continuing.
Next Section >