From 1f84bfe8bdfbfce1dbb18f74432bbf2508376b19 Mon Sep 17 00:00:00 2001 From: gmega Date: Sat, 12 Aug 2023 07:53:29 -0300 Subject: [PATCH] some renaming to clarify concepts --- swarmsim/eventdrivenengine.nim | 12 ++++++------ swarmsim/schedulable.nim | 9 --------- swarmsim/schedulableevent.nim | 11 +++++++++++ swarmsim/types.nim | 9 +++++---- tests/swarmsim/eventdrivenengine.nim | 8 ++++---- 5 files changed, 26 insertions(+), 23 deletions(-) delete mode 100644 swarmsim/schedulable.nim create mode 100644 swarmsim/schedulableevent.nim diff --git a/swarmsim/eventdrivenengine.nim b/swarmsim/eventdrivenengine.nim index 8326be2..07c912e 100644 --- a/swarmsim/eventdrivenengine.nim +++ b/swarmsim/eventdrivenengine.nim @@ -1,25 +1,25 @@ import std/options import pkg/swarmsim/types -import pkg/swarmsim/schedulable +import pkg/swarmsim/schedulableevent proc current_time*(self: EventDrivenEngine): uint64 {.inline.} = self.current_time -proc schedule*(self: EventDrivenEngine, schedulable: Schedulable): EventDrivenEngine = +proc schedule*(self: EventDrivenEngine, schedulable: SchedulableEvent): EventDrivenEngine = self.queue.push(schedulable) self -proc scheduleAll*[T: Schedulable](self: EventDrivenEngine, schedulables: seq[T]): void = +proc scheduleAll*[T: SchedulableEvent](self: EventDrivenEngine, schedulables: seq[T]): void = for schedulable in schedulables: discard self.schedule(schedulable) -proc nextStep*(self: EventDrivenEngine): Option[Schedulable] = +proc nextStep*(self: EventDrivenEngine): Option[SchedulableEvent] = if len(self.queue) == 0: - return none(Schedulable) + return none(SchedulableEvent) let schedulable = self.queue.pop() self.current_time = schedulable.time - schedulable.scheduled(engine = self) + schedulable.atScheduledTime(engine = self) some(schedulable) diff --git a/swarmsim/schedulable.nim b/swarmsim/schedulable.nim deleted file mode 100644 index be2b367..0000000 --- a/swarmsim/schedulable.nim +++ /dev/null @@ -1,9 +0,0 @@ -import pkg/swarmsim/types - -func `<`*(self: Schedulable, other: Schedulable): bool = - return self.time < other.time - -method scheduled*(self: Schedulable, engine: EventDrivenEngine): void {.base.} = - quit "unimplemented" - -export Schedulable diff --git a/swarmsim/schedulableevent.nim b/swarmsim/schedulableevent.nim new file mode 100644 index 0000000..7edfddd --- /dev/null +++ b/swarmsim/schedulableevent.nim @@ -0,0 +1,11 @@ +import pkg/swarmsim/types + +func `<`*(self: SchedulableEvent, other: SchedulableEvent): bool = + return self.time < other.time + +method atScheduledTime*(self: SchedulableEvent, engine: EventDrivenEngine): void {.base.} = + ## Callback invoked by the event engine indicating that this event is due for execution. + ## + quit "unimplemented" + +export SchedulableEvent diff --git a/swarmsim/types.nim b/swarmsim/types.nim index 2f5ed51..1db5e2c 100644 --- a/swarmsim/types.nim +++ b/swarmsim/types.nim @@ -1,14 +1,15 @@ import std/heapqueue type - Schedulable* = ref object of RootObj - ## A `Schedulable` is something that can be scheduled for execution in an - ## `EventDrivenEngine`. + SchedulableEvent* = ref object of RootObj + ## A `SchedulableEvent` is an event that can be scheduled for execution in an `EventDrivenEngine` + ## at a well-defined point in simuliation time. + ## time*: uint64 type EventDrivenEngine* = ref object of RootObj current_time*: uint64 - queue*: HeapQueue[Schedulable] + queue*: HeapQueue[SchedulableEvent] export heapqueue diff --git a/tests/swarmsim/eventdrivenengine.nim b/tests/swarmsim/eventdrivenengine.nim index 67937f7..ff462c1 100644 --- a/tests/swarmsim/eventdrivenengine.nim +++ b/tests/swarmsim/eventdrivenengine.nim @@ -4,14 +4,14 @@ import sugar import std/algorithm -import pkg/swarmsim/schedulable +import pkg/swarmsim/schedulableevent import pkg/swarmsim/eventdrivenengine type - TimedSchedulable = ref object of Schedulable + SimpleSchedulable = ref object of SchedulableEvent scheduledAt: uint64 -method scheduled(schedulable: TimedSchedulable, engine: EventDrivenEngine) = +method atScheduledTime(schedulable: SimpleSchedulable, engine: EventDrivenEngine) = schedulable.scheduledAt = engine.current_time suite "event driven engine tests": @@ -19,7 +19,7 @@ suite "event driven engine tests": test "should run schedulables at the right time": let times = @[1, 10, 5].map(time => uint64(time)) - let schedulables = times.map(time => TimedSchedulable(time: time)) + let schedulables = times.map(time => SimpleSchedulable(time: time)) let engine = EventDrivenEngine()