The Tortoise Diagram
Overview
At this point, you should start thinking about how to describe the behaviors of the two different racers as they perform the race simulation. You can use the process of constructing the behaviors to educate and guide aspects of defining the attributes and operations necessary for simulation of these systems. Now start creating some state diagrams.
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 a diagram for the Tortoise
- Create a new General diagram and give it the name
Tortoise Diagram
. - Drag the
Tortoise
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
Running the race
. - Draw a transition from
Waiting to start the race
toRunning the race
and give it the namewaiting to running
.
Tortoise State Diagram
Execution does not require the transitions to have names. We are adding them in this tutorial so that you can differentiate between all the different transitions as they occur in the log with names rather than their element ids. See the troubleshooting page on how to locate the element ids.
Defining the starting transition
Now that you have the first two states, you can begin to examine transitions and how they are fundamentally important in how you can simulate this system via Behavior Execution Engine. The state diagrams you design for the racers are going to use event types to model the state transitions for the racers. In your model, you can make use of several transition triggers: “At” for absolute time events, “After” for relative time events, and “When” for change events. See the Triggers topic for more details on these trigger types.
The race’s startTime
attribute is going to drive the first transition — from waiting to running
the race — so you can use a literal string value to describe that explicit transition.
-
Select the transition from
Waiting to start the race
toRunning the race
.-
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
- Set the effect action to
Racer::startRacing
.
-
Create a Trigger Action.
Tortoise Diagram transition defined
Adding the update logic to Race and Racer
The tortoise is a very simple runner, so you can describe a running behavior via modeling a simple heartbeat loop. Every fixed time increment, we increase the distanceCovered attribute of the tortoise by a fixed increment. However, you have not defined an update rate yet. You need to first add that attribute to the Race part definition before using it in this state diagram.
- Open the System Definition diagram.
- Add an attribute to the
Race
with the namecheckInRate
and set the definition to DurationValue. - Add an action to the
Racer
with the nameupdateDistance
. - Create a new general diagram called
Racer Diagram
. - Drag the
Racer
part definition from the model tree explorer into the diagram. - Unhide all the actions from the structure compartment.
- Create a new Assignment Action owned by the
updateDistance
action calleddoUpdate
. - Set the referent to
distanceCovered
and the value expression todistanceCovered + topSpeed * race.checkInRate
.
Initial Racer Diagram
This diagram shows the shared behavior logic between every racer. Later in this tutorial you will be configuring these actions to perform them as the “effect” of a transition (the effect will reference and subset these actions so they are executed in specific places within the execution). Actions that are subsetted in this way by entry, do, exit, or effect only occur when they are performed.
Creating the race check-in loop
Now that you have updated the Race, you can use it in the Tortoise Diagram.
- Create a state with the name
Looping
. -
Draw a transition from
Running the race
toLooping
and give it the namerunning to looping
.-
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.
-
Draw a transition from
Looping
toRunning the race
and give it the namelooping to running
.- Set the guard to be
distanceCovered < race.distance
.
- Set the guard to be
- Create a state with the name
Finished the race
. -
Draw a transition from
Looping
toFinished the race
and give it the namelooping to finished
.- Set the guard to be
distanceCovered >= race.distance
. - Set the effect action to
Racer::stopRacing
.
- Set the guard to be
Tortoise Diagram Check in Loop
By using a relative time trigger and guarded transitions, you have created a loop where every check in happens as defined by the checkInRate attribute of the race referenced by racer. At which, the tortoise’s distanceCovered attribute will be increased discretely by the amount related to the relative time delay and the runner’s speed. The accuracy of representing the motion of the racers will depend on the size of this time delay between distance updates. While the dynamics are simple in this case, this could cause problems for more complex models where the speed of the racers changes over time, resulting in numerical inaccuracy in the distance travelled over time. For this simple example, this is good enough to model the racers appropriately. You will take a slightly more complex approach with the hare later, which flexes some Behavior Execution Engine muscle, but the tortoise is a slow and steady racer, so the simplest state diagram is the best.
Adding the win and lose conditions
You need to finish building out the tortoise diagram. The tortoise only starts to run, and well, just finishes the race. How does the tortoise determine which racer is the winner or loser?
- Create a state with the name
Winner
. - Create a state with the name
Loser
. - Draw a transition from
Finished the race
toWinner
and give it the namefinished to winner
. - Draw a transition from
Finished the race
toLoser
and give it the namefinished to loser
.
Tortoise Diagram race result transition layout
With the rest of the states defined, you can now look at what is going to drive those transitions. How do you define how the tortoise finishes the race? How do you know which racer finished first? You can use Behavior Execution Engine’s functionality to answer that question for both the tortoise and hare by making determinations about when or if a certain value is or will be greater than another. The SysML v2 language offers a rich expression language that Behavior Execution Engine executes to use math and comparative operations as part of guards, transition triggers, and assignments to facilitate these types of interactions.
Setting the behavior when the racer finishes
First, you need a mechanism to record that the racer has finished the race. Since you defined an action called stopRacing, we will use that to perform an assignment action.
- Open the
Racer
diagram. - Create a new assignment action owned by the
stopRacing
action calledupdateRaceFinished
. - Set the referent to
raceFinished
and the value expression totrue
.
Racer Diagram with race finished logic
Relate the Tortoise and the Hare
Though you know the tortoise has finished, you do not know which racer finished first. You will need to give the tortoise (and the hare) references to each other to fix that.
- Open the
System Definition
diagram. -
Create a part with the name
hare
.- Set the definition to
Hare
. - Draw a referential containment arrow from the
Tortoise
part definition to theHare
part so thatTortoise
can referenceHare
.
- Set the definition to
-
Create a part with the name
tortoise
.- Set the definition to
Tortoise
. - Draw a referential containment arrow from the
Hare
part definition to theTortoise
part so thatHare
can reference Tortoise.
- Set the definition to
Updated System Definition
Defining the transitions to the end states
You created a guard on the transition that asks Behavior Execution Engine to evaluate “if the distance covered by the tortoise is greater than the distance specified in the associated race.” If that condition is true, Behavior Execution Engine will traverse the subsequent transition and as an effect of that transition, the tortoise’s “raceFinished” value switches to “true” from the default setting of false. Now you need to define the logic for winning or losing.
-
Select the transition from
Finished the race
toLoser
.- Set the guard to be
hare.raceFinished
- Set the guard to be
-
Select the transition from
Finished the race
toWinner
.- Set the guard to be
not hare.raceFinished
- Set the guard to be
Completed Tortoise Diagram
Next Section >