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

  1. Create a new General diagram and give it the name Tortoise Diagram.
  2. Drag the Tortoise 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 Running the race .
  7. Draw a transition from Waiting to start the race to Running the race and give it the name waiting 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.

  1. Select the transition from Waiting to start the race to Running the race.
    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. Set the effect action to Racer::startRacing.

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.

  1. Open the System Definition diagram.
  2. Add an attribute to the Race with the name checkInRate and set the definition to DurationValue.
  3. Add an action to the Racer with the name updateDistance.
  4. Create a new general diagram called Racer Diagram.
  5. Drag the Racer part definition from the model tree explorer into the diagram.
  6. Unhide all the actions from the structure compartment.
  7. Create a new Assignment Action owned by the updateDistance action called doUpdate.
  8. Set the referent to distanceCovered and the value expression to distanceCovered + 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.

  1. Create a state with the name Looping.
  2. Draw a transition from Running the race to Looping and give it the name 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.
  3. Draw a transition from Looping to Running the race and give it the name looping to running.
    1. Set the guard to be distanceCovered < race.distance.
  4. Create a state with the name Finished the race.
  5. Draw a transition from Looping to Finished the race and give it the name looping to finished.
    1. Set the guard to be distanceCovered >= race.distance.
    2. Set the effect action to Racer::stopRacing.

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?

  1. Create a state with the name Winner.
  2. Create a state with the name Loser.
  3. Draw a transition from Finished the race to Winner and give it the name finished to winner.
  4. Draw a transition from Finished the race to Loser and give it the name finished 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.

  1. Open the Racer diagram.
  2. Create a new assignment action owned by the stopRacing action called updateRaceFinished.
  3. Set the referent to raceFinished and the value expression to true.

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.

  1. Open the System Definition diagram.
  2. Create a part with the name hare.
    1. Set the definition to Hare.
    2. Draw a referential containment arrow from the Tortoise part definition to the Hare part so that Tortoise can reference Hare.
  3. Create a part with the name tortoise.
    1. Set the definition to Tortoise.
    2. Draw a referential containment arrow from the Hare part definition to the Tortoise part so that Hare can reference Tortoise.

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.

  1. Select the transition from Finished the race to Loser.
    1. Set the guard to be hare.raceFinished
  2. Select the transition from Finished the race to Winner.
    1. Set the guard to be not hare.raceFinished

Completed Tortoise Diagram


Next Section >