Skip to content

Commit beae09f

Browse files
committed
Adding error handling. #18
1 parent c3c80cd commit beae09f

15 files changed

+208
-197
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Added
44
* `stop` method in `SimContext`.
5+
* Error handling.
56

67
## 0.3.0
78

example/example.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import 'dart:async';
33
import 'package:simdart/simdart.dart';
44

55
void main() async {
6-
76
final SimDart sim = SimDart(listener: ConsoleEventListener());
87

98
sim.process(event: _eventA, name: 'A');
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import 'package:meta/meta.dart';
22

33
@internal
4-
class CompleterInterrupt{
5-
6-
}
4+
class CompleterInterrupt {}

lib/src/internal/debug_listener.dart

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,7 @@ import 'package:meta/meta.dart';
22

33
@internal
44
abstract class DebugListener {
5-
6-
void onScheduleNextAction();
7-
8-
void onExecuteAction();
9-
10-
void onStop();
11-
125
void onAddCompleter();
136

147
void onRemoveCompleter();
158
}
16-

lib/src/internal/event_action.dart

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class EventAction extends TimeAction implements SimContext {
5050

5151
@override
5252
void execute() {
53-
print('execute: $eventName');
5453
if (_eventCompleter != null) {
5554
throw StateError('This event is yielding');
5655
}
@@ -62,43 +61,38 @@ class EventAction extends TimeAction implements SimContext {
6261
executionHash: hashCode);
6362

6463
event(this).then((_) {
65-
print('then $eventName ${sim.runState.name}');
66-
if (_eventCompleter != null && sim.runState==RunState.running) {
67-
SimDartHelper.removeCompleter(sim: sim, completer: _eventCompleter!.completer);
68-
//TODO method or throw?
69-
print('aqui 2?');
70-
SimDartHelper.error(
71-
sim: sim,
72-
error:
73-
StateError(
74-
"Next event is being scheduled, but the current one is still paused waiting for continuation. Did you forget to use 'await'?"));
75-
return;
76-
}
77-
sim.listener?.onEvent(
78-
name: eventName,
79-
time: sim.now,
80-
phase: EventPhase.finished,
81-
executionHash: hashCode);
82-
SimDartHelper.scheduleNextAction(sim: sim);
64+
if (_eventCompleter != null && sim.runState == RunState.running) {
65+
SimDartHelper.removeCompleter(
66+
sim: sim, completer: _eventCompleter!.completer);
67+
SimDartHelper.error(
68+
sim: sim,
69+
error: StateError(
70+
"Next event is being scheduled, but the current one is still paused waiting for continuation. Did you forget to use 'await'?"));
71+
return;
72+
}
73+
sim.listener?.onEvent(
74+
name: eventName,
75+
time: sim.now,
76+
phase: EventPhase.finished,
77+
executionHash: hashCode);
78+
SimDartHelper.scheduleNextAction(sim: sim);
8379
}).catchError((error) {
84-
print('ZZ $error');
85-
//TODO rever
86-
if(error is! CompleterInterrupt) {
87-
print('aqui 3?');
80+
if (error is! CompleterInterrupt) {
8881
SimDartHelper.error(sim: sim, error: error);
82+
SimDartHelper.scheduleNextAction(sim: sim);
8983
}
9084
});
9185
}
9286

9387
@override
9488
Future<void> wait(int delay) async {
9589
if (_eventCompleter != null) {
96-
SimDartHelper.removeCompleter(sim: sim, completer: _eventCompleter!.completer);
97-
//TODO method or throw?
98-
print('aqui 1?');
90+
SimDartHelper.removeCompleter(
91+
sim: sim, completer: _eventCompleter!.completer);
9992
SimDartHelper.error(
10093
sim: sim,
101-
error: StateError("The event is already waiting. Did you forget to use 'await'?"));
94+
error: StateError(
95+
"The event is already waiting. Did you forget to use 'await'?"));
10296
return;
10397
}
10498

@@ -119,7 +113,7 @@ class EventAction extends TimeAction implements SimContext {
119113
order: order));
120114

121115
SimDartHelper.scheduleNextAction(sim: sim);
122-
await _eventCompleter!.future;
116+
await _eventCompleter!.future;
123117
}
124118

125119
@override
@@ -145,13 +139,13 @@ class EventAction extends TimeAction implements SimContext {
145139
}
146140

147141
@override
148-
void stop(){
149-
SimDartHelper.addAction(sim: sim, action: StopAction(start: sim.now, sim:sim));
142+
void stop() {
143+
SimDartHelper.addAction(
144+
sim: sim, action: StopAction(start: sim.now, sim: sim));
150145
}
151146

152147
@override
153148
SimCounter counter(String name) {
154-
print("counter");
155149
return sim.counter(name);
156150
}
157151

@@ -162,7 +156,7 @@ class EventAction extends TimeAction implements SimContext {
162156
}
163157

164158
class EventCompleter {
165-
EventCompleter({required this.event}){
159+
EventCompleter({required this.event}) {
166160
SimDartHelper.addCompleter(sim: event.sim, completer: completer);
167161
}
168162

@@ -180,7 +174,7 @@ class EventCompleter {
180174
time: event.sim.now,
181175
phase: EventPhase.resumed,
182176
executionHash: hashCode);
183-
completer.complete();
177+
completer.complete();
184178
event._eventCompleter = null;
185179
SimDartHelper.removeCompleter(sim: event.sim, completer: completer);
186180
}

lib/src/internal/stop_action.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@ import 'package:simdart/src/internal/time_action.dart';
33
import 'package:simdart/src/simdart.dart';
44

55
@internal
6-
class StopAction extends TimeAction{
7-
8-
StopAction({required super.start, super.order=-1, required this.sim});
6+
class StopAction extends TimeAction {
7+
StopAction({required super.start, super.order = -1, required this.sim});
98

109
final SimDart sim;
1110

1211
@override
1312
void execute() {
14-
print('entrou no stopAction');
1513
SimDartHelper.stop(sim: sim);
1614
SimDartHelper.scheduleNextAction(sim: sim);
1715
}
18-
19-
}
16+
}

lib/src/resources.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,13 @@ class ResourcesContext extends Resources {
153153
/// - Returns: A [Future] that completes when the resource is acquired.
154154
Future<void> acquire(String name) async {
155155
if (_event.eventCompleter != null) {
156-
SimDartHelper.removeCompleter(sim: _sim, completer: _event.eventCompleter!.completer);
156+
SimDartHelper.removeCompleter(
157+
sim: _sim, completer: _event.eventCompleter!.completer);
157158
//TODO method or throw?
158159
SimDartHelper.error(
159160
sim: _sim,
160-
error:StateError("This event should be waiting. Did you forget to use 'await'?"));
161+
error: StateError(
162+
"This event should be waiting. Did you forget to use 'await'?"));
161163
return;
162164
}
163165
_ResourceImpl? resource = _store._map[name];
@@ -172,7 +174,7 @@ class ResourcesContext extends Resources {
172174
_event.buildCompleter();
173175
resource._waiting.add(_event);
174176
SimDartHelper.scheduleNextAction(sim: _sim);
175-
await _event.eventCompleter!.future;
177+
await _event.eventCompleter!.future;
176178
return await acquire(name);
177179
}
178180
}

lib/src/simdart.dart

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class SimDart implements SimDartInterface {
4949
RunState _runState = RunState.notStarted;
5050
RunState get runState => _runState;
5151

52-
DebugListener? _debugListener;
52+
DebugListener? _debugListener;
5353

5454
final SimListener? listener;
5555

@@ -110,7 +110,6 @@ class SimDart implements SimDartInterface {
110110
/// - [until]: The time at which execution should stop. Execution will include events
111111
/// scheduled at this time (inclusive). If null, execution will continue indefinitely.
112112
Future<SimResult> run({int? until}) async {
113-
114113
if (runState != RunState.notStarted) {
115114
throw StateError('Simulation has already started.');
116115
}
@@ -131,11 +130,7 @@ class SimDart implements SimDartInterface {
131130
_startTime = null;
132131
_scheduleNextAction();
133132
await _terminator.future;
134-
135-
print('terminou');
136-
print('foi com erro? -> ${_error.runtimeType}: $_error');
137133
if (_error != null) {
138-
print('ue? erro?');
139134
_runState = RunState.error;
140135
throw _error!;
141136
} else {
@@ -147,18 +142,16 @@ class SimDart implements SimDartInterface {
147142
}
148143

149144
void stop() {
150-
print("stop");
151-
_addAction(StopAction(start: now, sim:this));
145+
_addAction(StopAction(start: now, sim: this));
152146
}
153147

154148
void _disposeCompleterList() {
155149
while (_completerList.isNotEmpty) {
156150
Completer<void> completer = _completerList.removeAt(0);
157151
_debugListener?.onRemoveCompleter();
158152
if (!completer.isCompleted) {
159-
print('vai CompleterInterrupt');
160153
// prevents subsequent methods from being executed after complete inside the async method.
161-
completer.completeError(CompleterInterrupt());
154+
completer.completeError(CompleterInterrupt());
162155
}
163156
}
164157
}
@@ -237,8 +230,6 @@ class SimDart implements SimDartInterface {
237230

238231
void _scheduleNextAction() {
239232
if (!_nextActionScheduled) {
240-
print('_scheduleNextAction');
241-
_debugListener?.onScheduleNextAction();
242233
_nextActionScheduled = true;
243234
if (executionPriority == 0 || _executionCount < executionPriority) {
244235
_executionCount++;
@@ -251,16 +242,16 @@ class SimDart implements SimDartInterface {
251242
}
252243

253244
void _addAction(TimeAction action) {
254-
if (_runState==RunState.running || _runState==RunState.notStarted) {
245+
if (_runState == RunState.running || _runState == RunState.notStarted) {
255246
_actions.add(action);
256247
}
257248
}
258249

259250
Future<void> _nextAction() async {
260251
_nextActionScheduled = false;
261-
if (_actions.isEmpty || runState!=RunState.running) {
252+
if (_actions.isEmpty || runState != RunState.running) {
262253
_disposeCompleterList();
263-
if(!_terminator.isCompleted) {
254+
if (!_terminator.isCompleted) {
264255
_terminator.complete();
265256
}
266257
return;
@@ -282,7 +273,6 @@ class SimDart implements SimDartInterface {
282273

283274
_startTime ??= now;
284275

285-
_debugListener?.onExecuteAction();
286276
action.execute();
287277
}
288278
}
@@ -320,21 +310,19 @@ class SimDartHelper {
320310
sim._debugListener?.onRemoveCompleter();
321311
}
322312

323-
static void error({required SimDart sim, required StateError error}) {
324-
print('error');
325-
if(sim._error==null) {
313+
static void error({required SimDart sim, required Object error}) {
314+
if (sim._error == null) {
326315
sim._error = error;
327316
sim._runState = RunState.error;
328317
}
329318
}
330-
331-
static void setDebugListener({required SimDart sim, required DebugListener? listener}) {
332-
sim._debugListener=listener;
319+
320+
static void setDebugListener(
321+
{required SimDart sim, required DebugListener? listener}) {
322+
sim._debugListener = listener;
333323
}
334324

335325
static void stop({required SimDart sim}) {
336-
print('stopando');
337-
sim._debugListener?.onStop();
338326
sim._runState = RunState.stopped;
339327
}
340328
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: simdart
22
description: A discrete event simulation package for Dart, designed for modeling and analyzing processes and systems.
3-
version: 0.3.0
3+
version: 0.4.0
44
repository: https://github.com/SimDart/simdart
55

66
topics:

0 commit comments

Comments
 (0)