some renaming to clarify concepts

This commit is contained in:
gmega 2023-08-12 07:53:29 -03:00
parent 5509594cd9
commit 1f84bfe8bd
5 changed files with 26 additions and 23 deletions

View File

@ -1,25 +1,25 @@
import std/options import std/options
import pkg/swarmsim/types import pkg/swarmsim/types
import pkg/swarmsim/schedulable import pkg/swarmsim/schedulableevent
proc current_time*(self: EventDrivenEngine): uint64 {.inline.} = self.current_time 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.queue.push(schedulable)
self 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: for schedulable in schedulables:
discard self.schedule(schedulable) discard self.schedule(schedulable)
proc nextStep*(self: EventDrivenEngine): Option[Schedulable] = proc nextStep*(self: EventDrivenEngine): Option[SchedulableEvent] =
if len(self.queue) == 0: if len(self.queue) == 0:
return none(Schedulable) return none(SchedulableEvent)
let schedulable = self.queue.pop() let schedulable = self.queue.pop()
self.current_time = schedulable.time self.current_time = schedulable.time
schedulable.scheduled(engine = self) schedulable.atScheduledTime(engine = self)
some(schedulable) some(schedulable)

View File

@ -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

View File

@ -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

View File

@ -1,14 +1,15 @@
import std/heapqueue import std/heapqueue
type type
Schedulable* = ref object of RootObj SchedulableEvent* = ref object of RootObj
## A `Schedulable` is something that can be scheduled for execution in an ## A `SchedulableEvent` is an event that can be scheduled for execution in an `EventDrivenEngine`
## `EventDrivenEngine`. ## at a well-defined point in simuliation time.
##
time*: uint64 time*: uint64
type type
EventDrivenEngine* = ref object of RootObj EventDrivenEngine* = ref object of RootObj
current_time*: uint64 current_time*: uint64
queue*: HeapQueue[Schedulable] queue*: HeapQueue[SchedulableEvent]
export heapqueue export heapqueue

View File

@ -4,14 +4,14 @@ import sugar
import std/algorithm import std/algorithm
import pkg/swarmsim/schedulable import pkg/swarmsim/schedulableevent
import pkg/swarmsim/eventdrivenengine import pkg/swarmsim/eventdrivenengine
type type
TimedSchedulable = ref object of Schedulable SimpleSchedulable = ref object of SchedulableEvent
scheduledAt: uint64 scheduledAt: uint64
method scheduled(schedulable: TimedSchedulable, engine: EventDrivenEngine) = method atScheduledTime(schedulable: SimpleSchedulable, engine: EventDrivenEngine) =
schedulable.scheduledAt = engine.current_time schedulable.scheduledAt = engine.current_time
suite "event driven engine tests": suite "event driven engine tests":
@ -19,7 +19,7 @@ suite "event driven engine tests":
test "should run schedulables at the right time": test "should run schedulables at the right time":
let times = @[1, 10, 5].map(time => uint64(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() let engine = EventDrivenEngine()