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
- Create a new General diagram and give it the name
Hare Diagram
. - Drag the
Hare
part definition from the model tree explorer into the diagram. - Unhide the
existing
state from the structure compartment. - Unhide the structure compartment of the
existing
state. - Create a state with the name
Waiting to start the race
. - Create a state with the name
Considering giving a head start
. - Draw a transition from
Waiting to start the race
toConsidering giving a head start
and give it the namewaiting to considering
. - Create a state with the name
Giving a head start
. - Draw a transition from
Considering giving a head start
toGiving a head start
and give it the nameconsidering to head start
. - Create a state with the name
Running the race
. - Draw a transition from
Considering giving a head start
toRunning the race
and give it the nameconsidering to running
. - Draw a transition from
Giving a head start
toRunning the race
and give it the namehead start to running
. - Create a state with the name
Finished the race
. - Draw a transition from
Running the race
toFinished the race
and give it the namerunning to finished
. - Create a state with the name
Loser
. - Draw a transition from
Finished the race
toLoser
and give it the namefinished to loser
. - Create a state with the name
Winner
. - Draw a transition from
Finished the race
toWinner
and give it the namefinished 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.
-
Select the transition from
Waiting to start the race
toConsidering giving a head start
.-
Create a trigger action.
- Set the trigger kind for the Trigger Expression to be
at
. - Set the expression to be
race.startTime
.
- Set the trigger kind for the Trigger Expression to be
-
Create a trigger action.
-
Select the transition from
Running the race
toFinished the race
.-
Create a trigger action.
- Set the trigger kind for the Trigger Expression to be
when
. - Set the expression to be
distanceCovered >= race.distance
.
- Set the trigger kind for the Trigger Expression to be
- Set the effect action to
Racer::stopRacing
.
-
Create a trigger action.
-
Select the transition from
Finished the race
toLoser
.- Set the guard to be
tortoise.raceFinished
- Set the guard to be
-
Select the transition from
Finished the race
toWinner
.- Set the guard to be
not tortoise.raceFinished
- Set the guard to be
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.
- Open the System Definition diagram.
-
For the
Hare
part definition do the following:- Create an attribute with the name
startingDelay
and set the definition to DurationValue.
- Create an attribute with the name
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.
-
Select the transition from
Considering giving a head start
toGiving a head start
.- Set the guard to be
startingDelay > 0 [s]
- Set the guard to be
-
Select the transition from
Considering giving a head start
toRunning the race
.- Set the guard to be
startingDelay == 0 [s]
- Set the effect action to
Racer::startRacing
.
- Set the guard to be
-
Select the transition from
Giving a head start
toRunning the race
.-
Create a trigger action.
- Set the trigger kind for the Trigger Expression to be
after
. - Set the expression to be
startingDelay
.
- Set the trigger kind for the Trigger Expression to be
- Set the effect action to
Racer::startRacing
.
-
Create a trigger action.
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.
- Unhide the structure compartment of the “Running the race” state.
- Create an action with the name
raceEntry
. - Set the entry action of the state
Running the race
toraceEntry
. - Create a state with the name
Running
. - Draw a transition from
raceEntry
toRunning
and give it the nameentry to running
. - Create a state with the name
Looping
. - Draw a transition from
Running
toLooping
and give it the namerunning to looping
. - Draw a transition from
Looping
toRunning
and give it the namelooping to running
. - Create a state with the name
Considering resting
. - Draw a transition from
Looping
toConsidering resting
and give it the namelooping to considering
. - Draw a transition from
Considering resting
toRunning
and give it the nameconsidering to running
. - Create a state with the name
Resting
. - Draw a transition from
Considering resting
toResting
and give it the nameconsidering to resting
. - Draw a transition from
Resting
toRunning
and give it the nameresting 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.
- Open the System Definition diagram.
-
For the
Hare
part definition do the following:- Create an attribute with the name
restDuration
and set the definition to DurationValue. - Create an attribute with the name
isNapping
, set the definition to Boolean, and give it an initial value offalse
. - Create an attribute with the name
hasTakenANap
, set the definition to Boolean, and give it an initial value offalse
. - Create an action with the name
startNapping
. - Create an action with the name
stopNapping
.
- Create an attribute with the name
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.
-
Select the transition from
Running
toLooping
.-
Create a trigger action.
- Set the trigger kind for the Trigger Expression to be
after
. - Set the expression to be
race.CheckInRate
.
- Set the trigger kind for the Trigger Expression to be
- Set the effect action to
Racer::updateDistance
.
-
Create a trigger action.
-
Select the transition from
Looping
toRunning
.- Set the guard to be
distanceCovered <= tortoise.distanceCovered
.
- Set the guard to be
-
Select the transition from
Looping
toConsidering resting
.- Set the guard to be
distanceCovered > tortoise.distanceCovered
.
- Set the guard to be
-
Select the transition from
Considering resting
toRunning
.- Set the guard to be
hasTakenANap
.
- Set the guard to be
-
Select the transition from
Considering resting
toResting
.- Set the guard to be
not hasTakenANap
. - Set the effect action to
Hare::startNapping
.
- Set the guard to be
-
Select the transition from
Resting
toRunning
.-
Create a trigger action.
- Set the trigger kind for the Trigger Expression to be
after
. - Set the expression to be
restDuration
.
- Set the trigger kind for the Trigger Expression to be
- Set the effect action to
Hare::stopNapping
.
-
Create a trigger action.
- Unhide the
startNapping
andstopNapping
actions from the structure compartment. - Create a new assignment action owned by the
startNapping
action calledupdateIsNapping
. - Set the referent to
isNapping
and the value expression totrue
. - Create a new assignment action owned by the
stopNapping
action calledupdateIsNapping
. - Set the referent to
isNapping
and the value expression tofalse
. - Create a new assignment action owned by the
stopNapping
action calledupdateHasTakenANap
. - Set the referent to
hasTakenANap
and the value expression totrue
.
Completed Hare Diagram
Next Section >