Step 7: Show summary statistics updated in real time

Update indicators

Let’s use the 'Summary statistics' tab to show the Service level and overall expenses.

Go to the SimulationStatisticsPart class (in the '…​application' bundle, in the '…​parts.simulation' package). Replace its onShowModel() method with the following:

protected void onShowModel(Model model) {
    if (model != null) {
        List<Indicator> data = List.of(
                new Indicator("Service level", () -> model.getStatistics().getServiceLevel(),
                        Formats.getDefaultFormats()::percent, false),
                new Indicator("Expenses", () -> model.getStatistics().getExpenses(),
                        Formats.getDefaultFormats()::dollarTwoDecimals, false));
        tableView.setData(data);
    } else {
        tableView.setData(Collections.emptyList());
    }
}

Entities shown in the summary statistics tab are called indicators. Initially, there were two indicators (for the number for nodes and arcs). We add another two, for SL and expenses.

Although we call tableView.setData once, the data still gets updated during simulation.

Here is how it works:

  1. In the SimulationStatisticsPart.createComposite() method, note the viewUpdaterService.getStatsUpdater().addView(tableView); line. This subscribes the tableView to the update events generated by the object returned from the getStatsUpdater() method.

  2. The getStatsUpdater() method returns the SimulationViewUpdater object that emits update requests every 50 ms when the simulation is running, and does not emit any events when simulation is stopped.

Check the result

Start the program and open the scenario you created earlier. Switch to the Simulation mode and run the model. You will see the 'Summary statistics' part populated with new indicators that will be updated during simulation.

New summary statistics items