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:

Figure A1: Guards and effects

Prerequisites

Prerequisite Description
Moxie Installation You must have installed Moxie.
Tutorial Project You must start this section with the Moxie simulation project from the previous section. If you did not complete the previous section, you can use the following project from the Moxie 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.

  1. Open the project from the previous section.
  2. 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.
  3. Select the transition from PoweredOff to PoweredOn and drag the end arrow from the PoweredOn state to the choice pseudostate.
  4. Select the choice pseudostate, click the Transition button () on the context toolbar, and then click the PoweredOff state.
  5. 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 Moxie must be opaque behaviors with bodies written as opaque expressions.

  1. Double-click the transition from the PoweredOff state to the choice pseudostate.
  2. In the Effect category, set the Behavior Type to Opaque Behavior.
  3. Set the Body and Language to the string isPoweredOn = true; and click Close.

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.

  1. Double-click the transition from the choice pseudostate to the PoweredOn state.
  2. In the Transition category, set the Guard to the string isPoweredOn and click Close. The string isPoweredOn is a shorthand for isPoweredOn == true, which also works.
  3. Double-click the transition from the choice pseudostate to the PoweredOff state.
  4. In the Transition category, set the Guard to the string else and click Close. The string else informs Moxie to only traverse this transition if all of the other departing transitions evaluate to false.
  5. In the Instance Specification Diagram, note that the isPoweredOn property of the uav.powerSystem starts set to false.
  6. Run () the simulation and observe the PowerSystem State Machine takes the [isPoweredOn] transition at the choice, indicating that the isPoweredOn property is true at that point in the simulation.

    Figure B1: The power system guards and effect

  7. Save your work before continuing.

Next Section >