Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/simdart.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export 'src/event.dart';
export 'src/execution_priority.dart';
export 'src/interval.dart';
export 'src/observable.dart';
export 'src/resource_configurator.dart' hide ResourcesConfiguratorHelper;
export 'src/simdart.dart' hide SimDartHelper;
export 'src/simulation_track.dart';
export 'src/start_time_handling.dart';
export 'src/sim_result.dart';
6 changes: 6 additions & 0 deletions lib/src/event.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:math';

import 'package:simdart/src/internal/event_scheduler_interface.dart';

Expand All @@ -20,4 +21,9 @@ abstract interface class EventContext implements EventSchedulerInterface {
///
/// Throws an [ArgumentError] if the delay is negative.
Future<void> wait(int delay);

/// The instance of the random number generator used across the simulation.
/// It is initialized once and reused to improve performance, avoiding the need to
/// instantiate a new `Random` object for each event.
Random get random;
}
18 changes: 0 additions & 18 deletions lib/src/execution_priority.dart

This file was deleted.

4 changes: 4 additions & 0 deletions lib/src/internal/event_action.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:math';

import 'package:meta/meta.dart';
import 'package:simdart/src/event.dart';
Expand Down Expand Up @@ -32,6 +33,9 @@ class EventAction extends TimeAction implements EventContext {

final SimDart _sim;

@override
Random get random => _sim.random;

/// The resource id required by the event.
final String? resourceId;

Expand Down
16 changes: 7 additions & 9 deletions lib/src/internal/sim_configuration_interface.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import 'package:meta/meta.dart';
import 'package:simdart/src/execution_priority.dart';
import 'package:simdart/src/start_time_handling.dart';

@internal
abstract interface class SimConfigurationInterface {
/// Specifies how the simulation handles start times in the past.
StartTimeHandling get startTimeHandling;

/// Defines the priority of task execution in the simulation.
///
/// - `highPriority`: Uses `Future.microtask` for immediate execution, prioritizing
/// processing without blocking the UI.
/// - `lowPriority`: Uses `Future.delayed(Duration.zero)` to ensure non-blocking
/// execution, allowing the UI to remain responsive.
ExecutionPriority get executionPriority;

/// Determines whether simulation tracks should be included in the simulation result.
///
/// When set to `true`, the simulation will collect and return a list of [SimulationTrack]
Expand All @@ -23,4 +14,11 @@ abstract interface class SimConfigurationInterface {
///
/// Default: `false`
bool get includeTracks;

/// Determines how often `Future.delayed` is used instead of `microtask` during event execution.
///
/// - `0`: Always uses `microtask`.
/// - `1`: Alternates between `microtask` and `Future.delayed`.
/// - `N > 1`: Executes `N` events with `microtask` before using `Future.delayed`.
int get executionPriorityCounter;
}
36 changes: 16 additions & 20 deletions lib/src/internal/time_loop.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:async';
import 'dart:math' as math;

import 'package:collection/collection.dart';
import 'package:meta/meta.dart';
import 'package:simdart/src/execution_priority.dart';
import 'package:simdart/src/internal/now_interface.dart';
import 'package:simdart/src/internal/sim_result_interface.dart';
import 'package:simdart/src/internal/time_action.dart';
Expand All @@ -19,19 +19,18 @@ class TimeLoop
{required int? now,
required this.beforeRun,
required this.includeTracks,
required this.executionPriority,
required this.startTimeHandling}) {
_now = now ?? 0;
_priorityScheduler = executionPriority == ExecutionPriority.high
? _highPrioritySchedule
: _lowPrioritySchedule;
}
required int executionPriorityCounter,
required this.startTimeHandling})
: executionPriorityCounter = math.max(executionPriorityCounter, 0),
_now = now ?? 0;

@override
final StartTimeHandling startTimeHandling;

@override
final ExecutionPriority executionPriority;
final int executionPriorityCounter;

int _executionCount = 0;

final Function beforeRun;

Expand All @@ -57,8 +56,6 @@ class TimeLoop
int? get duration => _duration;
int? _duration;

late final Function _priorityScheduler;

bool _nextEventScheduled = false;

late int? _until;
Expand Down Expand Up @@ -106,15 +103,14 @@ class TimeLoop
void _scheduleNextEvent() {
assert(!_nextEventScheduled, 'Multiple schedules for the next event.');
_nextEventScheduled = true;
_priorityScheduler.call();
}

void _highPrioritySchedule() {
Future.microtask(_consumeFirstEvent);
}

void _lowPrioritySchedule() {
Future.delayed(Duration.zero, _consumeFirstEvent);
if (executionPriorityCounter == 0 ||
_executionCount < executionPriorityCounter) {
_executionCount++;
Future.microtask(_consumeFirstEvent);
} else {
_executionCount = 0;
Future.delayed(Duration.zero, _consumeFirstEvent);
}
}

void addAction(TimeAction action) {
Expand Down
11 changes: 5 additions & 6 deletions lib/src/simdart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'dart:math';

import 'package:meta/meta.dart';
import 'package:simdart/src/event.dart';
import 'package:simdart/src/execution_priority.dart';
import 'package:simdart/src/internal/event_action.dart';
import 'package:simdart/src/internal/event_scheduler_interface.dart';
import 'package:simdart/src/internal/now_interface.dart';
Expand Down Expand Up @@ -48,19 +47,22 @@ class SimDart
int? now,
this.secondarySortByName = false,
this.includeTracks = false,
ExecutionPriority executionPriority = ExecutionPriority.high,
this.executionPriorityCounter = 0,
int? seed})
: random = Random(seed) {
_loop = TimeLoop(
now: now,
includeTracks: includeTracks,
beforeRun: _beforeRun,
executionPriority: executionPriority,
executionPriorityCounter: executionPriorityCounter,
startTimeHandling: startTimeHandling);
}

late final TimeLoop _loop;

@override
final int executionPriorityCounter;

@override
final bool includeTracks;

Expand Down Expand Up @@ -202,9 +204,6 @@ class SimDart
}
}

@override
ExecutionPriority get executionPriority => _loop.executionPriority;

@override
int get now => _loop.now;

Expand Down
2 changes: 1 addition & 1 deletion test/time_loop_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void main() {
TimeLoop loop = TimeLoop(
includeTracks: true,
now: null,
executionPriority: ExecutionPriority.high,
executionPriorityCounter: 0,
beforeRun: () {},
startTimeHandling: StartTimeHandling.throwErrorIfPast);

Expand Down
Loading