Pkg.add(“DataStructures”)
import DataStructures.update!
using DataStructures
import Base.Libc.time
abstract type EventScheduler end
mutable struct SimpleEventScheduler{Time} <: EventScheduler running::Bool currEvent::Event currTime::Time prevTime::Time eventList::BasicPriorityQueue{Event, Time} end eventlist(s::EventScheduler) = s.eventList currevent(s::EventScheduler) = s.currEvent prevtime(s::EventScheduler) = s.prevTime currtime(s::EventScheduler) = s.currTime time(s::EventScheduler) = currtime(s) function time(s::EventScheduler, symbol::Symbol) if (symbol === :prev) prevtime(s) elseif (s === :curr) currtime(s) else error("Symbol must be one of :prev, or :curr. Symbol $symbol used instead") end end function nextevent!(s::EventScheduler) s.prevTime = currtime(s) s.currEvent, s.currTime = dequeue_pair!( eventslist(s)) currevent(s) end post(s::EventScheduler, event::Event, ∆t) = enqueue!(eventslist(s), event, ∆t + time(s)) post(s::EventScheduler, Event::Type, ∆t) = post(s, Event(), ∆t) function runsim(sim::DiscreteEventsSim) scheduler = EventScheduler(sim) state = startstate(sim) runsimulation( start(scheduler), state, scheduler, sim) end function runsim(event::Event, sim::DiscreteEventsSim, state, scheduler) while !(isfound(state) || isend(event)) event = nextevent!(scheduler) update!(event, sim, state, scheduler) end measure(state) end function runsim(sim::DiscreteEventsSim, rep) scheduler = EventScheduler(sim) state = startstate(sim) runsimulation( start(scheduler), state, scheduler, sim, rep) end function runsim(event::Event, sim::DiscreteEventsSim, state, scheduler, rep) while !(isfound(state) || isend(event)) verboseprint(event, sim, state, scheduler, rep) event = nextevent!(scheduler) update!(event, sim, state, scheduler) end verboseprint(event, sim, state, scheduler, rep) measure(state) end update!(eventsList::EventsList, event::Event, state, clockTime)