Skip to content
Merged
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 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.
* Allow creating new resources during simulation.
* Methods for acquiring and releasing resources within events.

## 0.1.0

* Initial release
Expand Down
75 changes: 41 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[![](https://img.shields.io/pub/v/simdart.svg)](https://pub.dev/packages/simdart)
[![](https://img.shields.io/badge/%F0%9F%91%8D%20and%20%E2%AD%90-are%20free-yellow)](#)
[![](https://img.shields.io/badge/Under%20Development-blue)](#)

![](https://simdart.github.io/simdart-assets/simdart-text-128h.png)

Expand Down Expand Up @@ -80,36 +79,42 @@ A collection of different interval types used to control event timing in simulat
import 'package:simdart/simdart.dart';

void main() async {
final SimDart sim = SimDart(onTrack: (track) => print(track));
final SimDart sim = SimDart();

sim.process(event: _a, name: 'A');
sim.process(event: _b, start: 5, name: 'B');

await sim.run();
}

void _a(EventContext context) async {
Future<void> _a(SimContext context) async {
print('[${context.now}][${context.eventName}] start');
await context.wait(10);
context.sim.process(event: _c, delay: 1, name: 'C');
context.process(event: _c, delay: 1, name: 'C');
print('[${context.now}][${context.eventName}] end');
}

void _b(EventContext context) async {
Future<void> _b(SimContext context) async {
print('[${context.now}][${context.eventName}] start');
await context.wait(1);
print('[${context.now}][${context.eventName}] end');
}

void _c(EventContext context) async {
Future<void> _c(SimContext context) async {
print('[${context.now}][${context.eventName}] start');
await context.wait(10);
print('[${context.now}][${context.eventName}] end');
}
```

Output:
```
[0][A][executed]
[5][B][executed]
[6][B][resumed]
[10][A][resumed]
[11][C][executed]
[21][C][resumed]
[0][A] start
[5][B] start
[6][B] end
[10][A] end
[11][C] start
[21][C] end
```

### Resource capacity
Expand All @@ -118,35 +123,35 @@ Output:
import 'package:simdart/simdart.dart';

void main() async {
final SimDart sim = SimDart(onTrack: (track) => print(track));
final SimDart sim = SimDart();

sim.resources.limited(id: 'resource', capacity: 2);
sim.resources.limited(id: 'resource', capacity: 1);

sim.process(event: _a, name: 'A1', resourceId: 'resource');
sim.process(event: _a, name: 'A2', start: 1, resourceId: 'resource');
sim.process(event: _a, name: 'A3', start: 2, resourceId: 'resource');
sim.process(event: _b, name: 'B', start: 3);
sim.process(event: _eventResource, name: 'A');
sim.process(event: _eventResource, name: 'B');

await sim.run();
}

void _a(EventContext context) async {
Future<void> _eventResource(SimContext context) async {
print('[${context.now}][${context.eventName}] acquiring resource...');
await context.resources.acquire('resource');
print('[${context.now}][${context.eventName}] resource acquired');
await context.wait(10);
print('[${context.now}][${context.eventName}] releasing resource...');
context.resources.release('resource');
}

void _b(EventContext context) async {}
```

Output:
```
[0][A1][executed]
[1][A2][executed]
[2][A3][rejected]
[3][B][executed]
[10][A1][resumed]
[10][A3][executed]
[11][A2][resumed]
[20][A3][resumed]
[0][A] acquiring resource...
[0][A] resource acquired
[0][B] acquiring resource...
[10][A] releasing resource...
[10][B] resource acquired
[20][B] releasing resource...
```

### Repeating process
Expand All @@ -155,23 +160,25 @@ Output:
import 'package:simdart/simdart.dart';

void main() async {
final SimDart sim = SimDart(onTrack: (track) => print(track));
final SimDart sim = SimDart();

sim.repeatProcess(
event: _a,
start: 1,
name: 'A',
name: (start) => 'A$start',
interval: Interval.fixed(fixedInterval: 2, untilTime: 5));

await sim.run();
}

void _a(EventContext context) async {}
Future<void> _a(SimContext context) async {
print('[${context.now}][${context.eventName}]');
}
```

Output:
```
[1][A][executed]
[3][A][executed]
[5][A][executed]
[1][A1]
[3][A3]
[5][A5]
```
16 changes: 11 additions & 5 deletions example/example.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import 'package:simdart/simdart.dart';

void main() async {
final SimDart sim = SimDart(onTrack: (track) => print(track));
final SimDart sim = SimDart(includeTracks: true);

sim.process(event: _a, name: 'A');
sim.process(event: _eventA, name: 'A');

await sim.run(until: 10);
SimResult result = await sim.run();

result.tracks?.forEach((track) => print(track));
print('startTime: ${result.startTime}');
print('duration: ${result.duration}');
}

void _a(EventContext context) async {
Future<void> _eventA(SimContext context) async {
await context.wait(2);
context.sim.process(event: _a, delay: 2, name: 'A');
context.process(event: _eventB, delay: 2, name: 'B');
}

Future<void> _eventB(SimContext context) async {}
15 changes: 10 additions & 5 deletions lib/simdart.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
export 'src/start_time_handling.dart';
export 'src/simdart.dart' hide SimDartHelper;
export 'src/event.dart';
export 'src/simulation_track.dart';
export 'src/interval.dart';
export 'src/observable.dart';
export 'src/resource_configurator.dart' hide ResourcesConfiguratorHelper;
export 'src/execution_priority.dart';
export 'src/resources_context.dart';
export 'src/resources.dart';
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';
export 'src/sim_context.dart';
26 changes: 3 additions & 23 deletions lib/src/event.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
import 'dart:async';

import 'package:simdart/src/simdart.dart';
import 'package:simdart/src/sim_context.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.
mixin EventContext {
/// The simulation instance managing this event.
SimDart get sim;

/// 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);
}
/// A function that represents an event in the simulation.
typedef Event = Future<void> Function(SimContext context);
18 changes: 0 additions & 18 deletions lib/src/execution_priority.dart

This file was deleted.

15 changes: 15 additions & 0 deletions lib/src/internal/completer_action.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:meta/meta.dart';
import 'package:simdart/src/internal/time_action.dart';

@internal
class CompleterAction extends TimeAction {
CompleterAction(
{required super.start, required this.complete, required super.order});

final Function complete;

@override
void execute() {
complete.call();
}
}
Loading