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

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

View File

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