run static method
Starts the simulation, executing all pending actions in time-order until it finishes or is stopped.
Implementation
static Future<void> run() async {
if (simulationHasEnded) {
throw Exception('Simulation has already been run and ended.'
' To run a new simulation, use Simulator.reset().');
}
// Yield execution to the event loop before beginning the simulation.
await Future(() {});
while (hasStepsRemaining() &&
_simExceptions.isEmpty &&
!_simulationEndRequested &&
(_maxSimTime < 0 || _currentTimestamp < _maxSimTime)) {
try {
await tick();
} catch (_, __) {
// trigger the end of simulation if an error occurred
_simulationEndedCompleter.complete();
rethrow;
}
}
// initially, just log the exceptions
for (final err in _simExceptions) {
logger.severe(err.exception.toString(), err.exception, err.stackTrace);
}
if (_currentTimestamp >= _maxSimTime && _maxSimTime > 0) {
logger.warning('Simulation ended due to maximum simulation time.');
}
while (_endOfSimulationActions.isNotEmpty) {
final endOfSimAction = _endOfSimulationActions.removeFirst();
try {
await endOfSimAction();
} catch (_) {
// trigger the end of simulation if an error occurred
_simulationEndedCompleter.complete();
rethrow;
}
}
_simulationEndedCompleter.complete();
// now, rethrow any exceptions now that sim is over and end of sim actions
// have all executed
for (final err in _simExceptions) {
throw err.exception;
}
await simulationEnded;
}