The Hare Diagram

Overview

The Tortoise state behavior diagram was simple in that you know the tortoise starts the race and just runs until it ends. The hare, due to an inflated ego, is going to have a more complex behavior. The hare’s state diagram will have similar basic functions to the tortoise’s state diagram: it will have a waiting state, running the race state, and transition determine a “winner” or “loser” status.

However, you have added wrinkles. The hare could give the tortoise a head start, and the hare also could decide to take a quick (or not so quick) nap after taking the lead. Start by creating the framework for Hare’s behavior diagram.

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.

Instructions

Creating the initial diagram for the Hare

  1. Create a new General diagram and give it the name Hare Diagram.
  2. Drag the Hare part definition from the model tree explorer into the diagram.
  3. Unhide the existing state from the structure compartment.
  4. Unhide the structure compartment of the existing state.
  5. Create a state with the name Waiting to start the race.
  6. Create a state with the name Considering giving a head start.
  7. Draw a transition from Waiting to start the race to Considering giving a head start and give it the name waiting to considering.
  8. Create a state with the name Giving a head start.
  9. Draw a transition from Considering giving a head start to Giving a head start and give it the name considering to head start.
  10. Create a state with the name Running the race.
  11. Draw a transition from Considering giving a head start to Running the race and give it the name considering to running.
  12. Draw a transition from Giving a head start to Running the race and give it the name head start to running.
  13. Create a state with the name Finished the race.
  14. Draw a transition from Running the race to Finished the race and give it the name running to finished.
  15. Create a state with the name Loser.
  16. Draw a transition from Finished the race to Loser and give it the name finished to loser.
  17. Create a state with the name Winner.
  18. Draw a transition from Finished the race to Winner and give it the name finished to winner.

Basic Hare Diagram outline

Defining the transitions for starting and ending the race

You laid out the basic framework for how the hare is going to behave in the simulation. Now populate some of the existing transitions with things that need to occur to drive those transitions. You can describe the basic means for how the hare starts the race, similarly to how the tortoise does, with an absolute time trigger. However, the means to evaluate the finishing of the race will be different. In this case, use a change trigger to determine when the hare will complete the race, as opposed to the guard event in use by the tortoise. You use the change trigger approach here because you are going to turn the “Running the race” state into a composite state with internal activity, and you will need a trigger that can drive the transition out of that internal activity.

  1. Select the transition from Waiting to start the race to Considering giving a head start.
    1. Create a trigger action.
      1. Set the trigger kind for the Trigger Expression to be at.
      2. Set the expression to be race.startTime.
  2. Select the transition from Running the race to Finished the race.
    1. Create a trigger action.
      1. Set the trigger kind for the Trigger Expression to be when.
      2. Set the expression to be distanceCovered >= race.distance.
    2. Set the effect action to Racer::stopRacing.
  3. Select the transition from Finished the race to Loser.
    1. Set the guard to be tortoise.raceFinished
  4. Select the transition from Finished the race to Winner.
    1. Set the guard to be not tortoise.raceFinished

Hare Diagram initial and final transitions defined

Take special notice here of the deliberate choice to use a change trigger instead of a guard for exiting the “Running the Race” state. Behavior Execution Engine will only evaluate the guards once per transition, after the entry of the source state and at the time of the triggering event. When you do not specify any trigger on a transition, the engine will wait for the source state to complete all other actions and state prior to the exit action before considering the guard. Therefore, if you were to use a guard here, it would only ever read that guard once at the completion of the source state and would never check it again.

Updating the system definition for the start delay

You have defined the start of the race and the end of the race. You still need to model attributes to facilitate the transition between “Giving a head start” and “Running the Race”. Before you laid out the behavior, you might not have considered the need to provide a time-delay value for the hare.

  1. Open the System Definition diagram.
  2. For the Hare part definition do the following:
    1. Create an attribute with the name startingDelay and set the definition to DurationValue.

Updated Hare part definition

After adding that attribute into the Hare part definition, you can then redefine it for use in the use case to define how long the Hare “waits” before starting to run the race. As mentioned, unlike the tortoise, the hare’s race-running behavior is going to be more complex due to the additional behaviors of waiting to start and considering a mid-race nap.

Defining the transitions for the Hare’s start delay

Now that you have populated the Hare part definition, update the behavior diagram to model what the hare does with the new attribute.

  1. Select the transition from Considering giving a head start to Giving a head start.
    1. Set the guard to be startingDelay > 0 [s]
  2. Select the transition from Considering giving a head start to Running the race.
    1. Set the guard to be startingDelay == 0 [s]
    2. Set the effect action to Racer::startRacing.
  3. Select the transition from Giving a head start to Running the race.
    1. Create a trigger action.
      1. Set the trigger kind for the Trigger Expression to be after.
      2. Set the expression to be startingDelay.
    2. Set the effect action to Racer::startRacing.

Hare Diagram head start logic complete

Creating the inner state outline for running the race

The hare will be running after giving a head start or not. Once the hare passes the tortoise, we will want a state with guarded transitions to consider taking a nap before resuming the race. You need to account for those decision points and actions while creating this behavior. All instructions or this step will be for inside the “Running the race” inner state.

  1. Unhide the structure compartment of the “Running the race” state.
  2. Create an action with the name raceEntry.
  3. Set the entry action of the state Running the race to raceEntry.
  4. Create a state with the name Running.
  5. Draw a transition from raceEntry to Running and give it the name entry to running.
  6. Create a state with the name Looping.
  7. Draw a transition from Running to Looping and give it the name running to looping.
  8. Draw a transition from Looping to Running and give it the name looping to running.
  9. Create a state with the name Considering resting.
  10. Draw a transition from Looping to Considering resting and give it the name looping to considering.
  11. Draw a transition from Considering resting to Running and give it the name considering to running.
  12. Create a state with the name Resting.
  13. Draw a transition from Considering resting to Resting and give it the name considering to resting.
  14. Draw a transition from Resting to Running and give it the name resting to running.

Hare’s Running the Race internal structure

Updating the system definition for resting

The final thing the hare needs is attributes and actions to facilitate resting.

  1. Open the System Definition diagram.
  2. For the Hare part definition do the following:
    1. Create an attribute with the name restDuration and set the definition to DurationValue.
    2. Create an attribute with the name isNapping, set the definition to Boolean, and give it an initial value of false.
    3. Create an attribute with the name hasTakenANap, set the definition to Boolean, and give it an initial value of false.
    4. Create an action with the name startNapping.
    5. Create an action with the name stopNapping.

Final System Definition diagram

Defining the transition logic for running the race

You created the framework to describe the motion of the hare, but now you need to describe how it moves along this framework via the transitions and effects.

  1. Select the transition from Running to Looping.
    1. Create a trigger action.
      1. Set the trigger kind for the Trigger Expression to be after.
      2. Set the expression to be race.CheckInRate.
    2. Set the effect action to Racer::updateDistance.
  2. Select the transition from Looping to Running.
    1. Set the guard to be distanceCovered <= tortoise.distanceCovered.
  3. Select the transition from Looping to Considering resting.
    1. Set the guard to be distanceCovered > tortoise.distanceCovered.
  4. Select the transition from Considering resting to Running.
    1. Set the guard to be hasTakenANap.
  5. Select the transition from Considering resting to Resting.
    1. Set the guard to be not hasTakenANap.
    2. Set the effect action to Hare::startNapping.
  6. Select the transition from Resting to Running.
    1. Create a trigger action.
      1. Set the trigger kind for the Trigger Expression to be after.
      2. Set the expression to be restDuration.
    2. Set the effect action to Hare::stopNapping.
  7. Unhide the startNapping and stopNapping actions from the structure compartment.
  8. Create a new assignment action owned by the startNapping action called updateIsNapping.
  9. Set the referent to isNapping and the value expression to true.
  10. Create a new assignment action owned by the stopNapping action called updateIsNapping.
  11. Set the referent to isNapping and the value expression to false.
  12. Create a new assignment action owned by the stopNapping action called updateHasTakenANap.
  13. Set the referent to hasTakenANap and the value expression to true.

Completed Hare Diagram


Next Section >