Skip to content

Commit da1d7b2

Browse files
authored
Version 0.2.0
* Methods for scheduling new events have been added to the EventContext. (#6) * Class for simulation result. Tracks were added to the result and no longer as a listener. * Formatting. * Using a counter to prioritize between microtask and delayed.#8 (#9) * Removing nullable from simulation result. #2 * Add support for accumulating simulation results (#10) * Removing unnecessary import. #2 * Adding num and counter properties. #4 * Removing unnecessary import * Removing unnecessary imports. * Renaming EventContext to SimContext. #2 * Allow creating new resources during simulation. #2 * Removing SimResult interface. #2 * Add acquire and release resource methods * Renaming methods. #11 * Simplifying the API. Event receiving SimDart instance. * Adding acquire and release methods for resources. #11 * Removing unused import. * Fixing unrecovered events after waiting for resource. #11 * Fixing async methods. #11 * Updating README. #11 * Updating CHANGELOG. #11 * Formatting. #11 * Fixing hidden member name. #11
1 parent 6bff398 commit da1d7b2

34 files changed

+1378
-894
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 0.2.0
2+
3+
* Setting to determine how often `Future.delayed` is used instead of `Future.microtask` during event execution to allow GUI refresh.
4+
* Adding numeric properties and counter.
5+
* Allow creating new resources during simulation.
6+
* Methods for acquiring and releasing resources within events.
7+
18
## 0.1.0
29

310
* Initial release

README.md

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
[![](https://img.shields.io/pub/v/simdart.svg)](https://pub.dev/packages/simdart)
22
[![](https://img.shields.io/badge/%F0%9F%91%8D%20and%20%E2%AD%90-are%20free-yellow)](#)
3-
[![](https://img.shields.io/badge/Under%20Development-blue)](#)
43

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

@@ -80,36 +79,42 @@ A collection of different interval types used to control event timing in simulat
8079
import 'package:simdart/simdart.dart';
8180
8281
void main() async {
83-
final SimDart sim = SimDart(onTrack: (track) => print(track));
82+
final SimDart sim = SimDart();
8483
8584
sim.process(event: _a, name: 'A');
8685
sim.process(event: _b, start: 5, name: 'B');
8786
8887
await sim.run();
8988
}
9089
91-
void _a(EventContext context) async {
90+
Future<void> _a(SimContext context) async {
91+
print('[${context.now}][${context.eventName}] start');
9292
await context.wait(10);
93-
context.sim.process(event: _c, delay: 1, name: 'C');
93+
context.process(event: _c, delay: 1, name: 'C');
94+
print('[${context.now}][${context.eventName}] end');
9495
}
9596
96-
void _b(EventContext context) async {
97+
Future<void> _b(SimContext context) async {
98+
print('[${context.now}][${context.eventName}] start');
9799
await context.wait(1);
100+
print('[${context.now}][${context.eventName}] end');
98101
}
99102
100-
void _c(EventContext context) async {
103+
Future<void> _c(SimContext context) async {
104+
print('[${context.now}][${context.eventName}] start');
101105
await context.wait(10);
106+
print('[${context.now}][${context.eventName}] end');
102107
}
103108
```
104109

105110
Output:
106111
```
107-
[0][A][executed]
108-
[5][B][executed]
109-
[6][B][resumed]
110-
[10][A][resumed]
111-
[11][C][executed]
112-
[21][C][resumed]
112+
[0][A] start
113+
[5][B] start
114+
[6][B] end
115+
[10][A] end
116+
[11][C] start
117+
[21][C] end
113118
```
114119

115120
### Resource capacity
@@ -118,35 +123,35 @@ Output:
118123
import 'package:simdart/simdart.dart';
119124
120125
void main() async {
121-
final SimDart sim = SimDart(onTrack: (track) => print(track));
126+
final SimDart sim = SimDart();
122127
123-
sim.resources.limited(id: 'resource', capacity: 2);
128+
sim.resources.limited(id: 'resource', capacity: 1);
124129
125-
sim.process(event: _a, name: 'A1', resourceId: 'resource');
126-
sim.process(event: _a, name: 'A2', start: 1, resourceId: 'resource');
127-
sim.process(event: _a, name: 'A3', start: 2, resourceId: 'resource');
128-
sim.process(event: _b, name: 'B', start: 3);
130+
sim.process(event: _eventResource, name: 'A');
131+
sim.process(event: _eventResource, name: 'B');
129132
130133
await sim.run();
131134
}
132135
133-
void _a(EventContext context) async {
136+
Future<void> _eventResource(SimContext context) async {
137+
print('[${context.now}][${context.eventName}] acquiring resource...');
138+
await context.resources.acquire('resource');
139+
print('[${context.now}][${context.eventName}] resource acquired');
134140
await context.wait(10);
141+
print('[${context.now}][${context.eventName}] releasing resource...');
142+
context.resources.release('resource');
135143
}
136144
137-
void _b(EventContext context) async {}
138145
```
139146

140147
Output:
141148
```
142-
[0][A1][executed]
143-
[1][A2][executed]
144-
[2][A3][rejected]
145-
[3][B][executed]
146-
[10][A1][resumed]
147-
[10][A3][executed]
148-
[11][A2][resumed]
149-
[20][A3][resumed]
149+
[0][A] acquiring resource...
150+
[0][A] resource acquired
151+
[0][B] acquiring resource...
152+
[10][A] releasing resource...
153+
[10][B] resource acquired
154+
[20][B] releasing resource...
150155
```
151156

152157
### Repeating process
@@ -155,23 +160,25 @@ Output:
155160
import 'package:simdart/simdart.dart';
156161
157162
void main() async {
158-
final SimDart sim = SimDart(onTrack: (track) => print(track));
163+
final SimDart sim = SimDart();
159164
160165
sim.repeatProcess(
161166
event: _a,
162167
start: 1,
163-
name: 'A',
168+
name: (start) => 'A$start',
164169
interval: Interval.fixed(fixedInterval: 2, untilTime: 5));
165170
166171
await sim.run();
167172
}
168173
169-
void _a(EventContext context) async {}
174+
Future<void> _a(SimContext context) async {
175+
print('[${context.now}][${context.eventName}]');
176+
}
170177
```
171178

172179
Output:
173180
```
174-
[1][A][executed]
175-
[3][A][executed]
176-
[5][A][executed]
181+
[1][A1]
182+
[3][A3]
183+
[5][A5]
177184
```

example/example.dart

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import 'package:simdart/simdart.dart';
22

33
void main() async {
4-
final SimDart sim = SimDart(onTrack: (track) => print(track));
4+
final SimDart sim = SimDart(includeTracks: true);
55

6-
sim.process(event: _a, name: 'A');
6+
sim.process(event: _eventA, name: 'A');
77

8-
await sim.run(until: 10);
8+
SimResult result = await sim.run();
9+
10+
result.tracks?.forEach((track) => print(track));
11+
print('startTime: ${result.startTime}');
12+
print('duration: ${result.duration}');
913
}
1014

11-
void _a(EventContext context) async {
15+
Future<void> _eventA(SimContext context) async {
1216
await context.wait(2);
13-
context.sim.process(event: _a, delay: 2, name: 'A');
17+
context.process(event: _eventB, delay: 2, name: 'B');
1418
}
19+
20+
Future<void> _eventB(SimContext context) async {}

lib/simdart.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
export 'src/start_time_handling.dart';
2-
export 'src/simdart.dart' hide SimDartHelper;
31
export 'src/event.dart';
4-
export 'src/simulation_track.dart';
52
export 'src/interval.dart';
63
export 'src/observable.dart';
7-
export 'src/resource_configurator.dart' hide ResourcesConfiguratorHelper;
8-
export 'src/execution_priority.dart';
4+
export 'src/resources_context.dart';
5+
export 'src/resources.dart';
6+
export 'src/simdart.dart' hide SimDartHelper;
7+
export 'src/simulation_track.dart';
8+
export 'src/start_time_handling.dart';
9+
export 'src/sim_result.dart';
10+
export 'src/sim_num.dart';
11+
export 'src/sim_property.dart';
12+
export 'src/sim_counter.dart';
13+
export 'src/sim_context.dart';

lib/src/event.dart

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,6 @@
1-
import 'dart:async';
2-
3-
import 'package:simdart/src/simdart.dart';
1+
import 'package:simdart/src/sim_context.dart';
42

53
/// The event to be executed.
64
///
7-
/// A function that represents an event in the simulation. It receives an
8-
/// [EventContext] that provides data about the event's execution state and context.
9-
typedef Event = void Function(EventContext context);
10-
11-
/// Represents the context of an event in the simulation.
12-
///
13-
/// Encapsulates the information and state of an event being executed
14-
/// within the simulation.
15-
mixin EventContext {
16-
/// The simulation instance managing this event.
17-
SimDart get sim;
18-
19-
/// Pauses the execution of the event for the specified [delay] in simulation time.
20-
///
21-
/// The event is re-added to the simulation's event queue and will resume after
22-
/// the specified delay has passed.
23-
///
24-
/// Throws an [ArgumentError] if the delay is negative.
25-
Future<void> wait(int delay);
26-
}
5+
/// A function that represents an event in the simulation.
6+
typedef Event = Future<void> Function(SimContext context);

lib/src/execution_priority.dart

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'package:meta/meta.dart';
2+
import 'package:simdart/src/internal/time_action.dart';
3+
4+
@internal
5+
class CompleterAction extends TimeAction {
6+
CompleterAction(
7+
{required super.start, required this.complete, required super.order});
8+
9+
final Function complete;
10+
11+
@override
12+
void execute() {
13+
complete.call();
14+
}
15+
}

0 commit comments

Comments
 (0)