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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.2.0

* Setting to determine how often `Future.delayed` is used instead of `Future.microtask` during event execution to allow GUI refresh.
* Adding numeric properties and counter.

## 0.1.0

* Initial release
Expand Down
1 change: 0 additions & 1 deletion example/example.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:simdart/simdart.dart';
import 'package:simdart/src/sim_result.dart';

void main() async {
final SimDart sim = SimDart(includeTracks: true);
Expand Down
4 changes: 4 additions & 0 deletions lib/simdart.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
export 'src/event.dart';
export 'src/event_context.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';
export 'src/sim_num.dart';
export 'src/sim_property.dart';
export 'src/sim_counter.dart';
24 changes: 1 addition & 23 deletions lib/src/event.dart
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
import 'dart:async';
import 'dart:math';

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

/// The event to be executed.
///
/// A function that represents an event in the simulation. It receives an
/// [EventContext] that provides data about the event's execution state and context.
typedef Event = void Function(EventContext context);

/// Represents the context of an event in the simulation.
///
/// Encapsulates the information and state of an event being executed
/// within the simulation.
abstract interface class EventContext implements EventSchedulerInterface {
/// Pauses the execution of the event for the specified [delay] in simulation time.
///
/// The event is re-added to the simulation's event queue and will resume after
/// the specified delay has passed.
///
/// 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;
}
36 changes: 36 additions & 0 deletions lib/src/event_context.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'dart:async';
import 'dart:math';

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

/// Represents the context of an event in the simulation.
///
/// Encapsulates the information and state of an event being executed
/// within the simulation.
abstract interface class EventContext implements EventSchedulerInterface {
/// Pauses the execution of the event for the specified [delay] in simulation time.
///
/// The event is re-added to the simulation's event queue and will resume after
/// the specified delay has passed.
///
/// 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;

/// Creates a new [SimCounter] instance with the given name.
///
/// - [name]: The name of the counter. This is used to identify the counter in logs or reports.
/// - Returns: A new instance of [SimCounter].
SimCounter counter(String name);

/// Creates a new [SimNum] instance with the given name.
///
/// - [name]: The name of the numeric metric. This is used to identify the metric in logs or reports.
/// - Returns: A new instance of [SimNum].
SimNum num(String name);
}
14 changes: 14 additions & 0 deletions lib/src/internal/event_action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import 'dart:math';

import 'package:meta/meta.dart';
import 'package:simdart/src/event.dart';
import 'package:simdart/src/event_context.dart';
import 'package:simdart/src/internal/resource.dart';
import 'package:simdart/src/internal/time_action.dart';
import 'package:simdart/src/interval.dart';
import 'package:simdart/src/sim_counter.dart';
import 'package:simdart/src/sim_num.dart';
import 'package:simdart/src/simdart.dart';
import 'package:simdart/src/simulation_track.dart';

Expand All @@ -24,6 +27,7 @@ class EventAction extends TimeAction implements EventContext {

/// The name of the event.
final String? _eventName;

String get eventName => _eventName ?? hashCode.toString();

/// The event to be executed.
Expand Down Expand Up @@ -161,4 +165,14 @@ class EventAction extends TimeAction implements EventContext {
resourceId: resourceId,
name: name);
}

@override
SimCounter counter(String name) {
return SimDartHelper.counter(sim: _sim, name: name);
}

@override
SimNum num(String name) {
return SimDartHelper.num(sim: _sim, name: name);
}
}
2 changes: 1 addition & 1 deletion lib/src/internal/resource.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:meta/meta.dart';
import 'package:simdart/src/event.dart';
import 'package:simdart/src/event_context.dart';

@internal
abstract class Resource {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/internal/sim_configuration_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ abstract interface class SimConfigurationInterface {
/// Default: `false`
bool get includeTracks;

/// Determines how often `Future.delayed` is used instead of `microtask` during event execution.
/// Determines how often `Future.delayed` is used instead of `Future.microtask` during events 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;
int get executionPriority;
}
152 changes: 0 additions & 152 deletions lib/src/internal/time_loop.dart

This file was deleted.

37 changes: 37 additions & 0 deletions lib/src/sim_counter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:simdart/src/sim_property.dart';

/// A class to track event counts in a discrete event simulation.
///
/// This class extends [SimProperty] and provides methods to increment and reset a counter.
/// It is useful for counting occurrences of specific events, such as arrivals, departures, or errors.
class SimCounter extends SimProperty {
int _value = 0;

/// Creates a new [SimCounter] instance.
///
/// Optionally, provide a [name] to identify the counter.
SimCounter({super.name});

/// The current value of the counter.
int get value => _value;

/// Increments the counter by 1.
void inc() {
_value++;
}

/// Increments the counter by a specified value.
///
/// - [value]: The value to increment the counter by.
void incBy(int value) {
if (value > 0) {
_value += value;
}
}

/// Resets the counter to 0.
@override
void reset() {
_value = 0;
}
}
Loading