Prev Next

Chart Definition using Simulation

Simulations are a fantastic tool for observing behavior. At any point in the Simulation it is easy to tell where we are and the state we are in. As we step through a simulation this information is typically discarded. In a simulation that shows us, say, traffic numbers over a 24 hour period, we might easily observe the number of vehicles traveling through a tunnel during various hours in the morning and evening. It might be useful to retain this information after the simulation completes, and to use it to provide something meaningful. The DynamicChart feature in Simulation allows us to do just that. Taking the example above, we could record the amount of traffic during each step of the simulation and use it to produce a Chart that would clearly show how much traffic went through the tunnel during the 24 hour period of the simulation. Charts can in effect display for us either a timeline of a simulation or the sum effect of one.

Producing Custom Charts in Simulation

You can fashion all sorts of Charts from any simulation. Each time a simulation is run, any DynamicChart elements referenced (by name) by the simulation are updated. The simulation will search for any named Chart in the same Package as the model.

Follow this simple process:

  1. Create a DynamicChart element for the simulation.
  2. In the initial step of the Simulation, use JavaScript to define a variable to hold the vehicle numbers.
    //
    //  the traffic variable will hold the traffic numbers as simulation proceeds and is initially zero
    //  each element of the array represents a period of the day, morning, afternoon, evening and night.
    //
    var traffic = [0,0,0,0];
  3. Next write the JavaScript that describes, in JSON format, the Chart to produce.
    //
    // The JSON instance describing the chart to produce. (complies with the EA DynamicChart Schema)
    //
    var chartData =
    {
    "Category" : "Column",
      "Type" : "Simple",
      "Title" : "Traffic",
      "Series" :
      [
    { "Label" : "Vehicles",
    "Data" :
    {
    "Type" : "Column",
    "Points" :
    [
    { "Category": "Morning", "Y" : 0 }, //  The Y values of the axis are initially zero
    { "Category": "Afternoon", "Y" : 0 }, //  they will be filled in at end of simulation
    { "Category": "Evening", "Y" : 0 },
    { "Category": "Night", "Y" : 0 }
    ]
    }
    }
      ]
    };
  4. At various transitions in the simulation update the traffic numbers.
    //
    // 2000 vehicles went through the tunnel in the afternoon (element 1)
    //
    traffic[1] += 2000;
  5. At the end of the simulation, use the data captured during the run to fill the series.
    // fill points in series with the number of vehicles for each part of the day
    var dataPoints = chartData.Series[0].Data.Points;
    for(var dp = 0; dp < traffic.length; dp++)
    {
           dataPoints[dp].Y = traffic[dp];
    }
  6. Update the model.
    // Call the EA function to populate the DynamicChart element named 'Vehicles' with this data.
    sim.GenerateChart( "Vehicles", JSON.stringify(chartData));

Default Chart Produced by Simulation

In addition to Charts you specifically produce, a summary Chart can be produced automatically by a simulation. All that is required is adding a DynamicChart Artifact to the Package and giving it the same name as the StateMachine. The default Chart summarizes the State transitions taken during execution of a simulation. If a default Chart is found when the simulation completes, that Chart's data is updated, and displayed automatically.

To add a default Chart to your StateMachine simulation follow these steps:

  1. Locate the Package containing the StateMachine on which to perform the simulation.
  2. Create a Dashboard diagram as a child of that Package.
  3. Add a DynamicChart Artifact to the Dashboard diagram, and give it the same name as the StateMachine.

When the simulation ends, simply open the Dashboard diagram to reveal the summary.