Skip to content

Commit

Permalink
Merge bdd2d5f into 33612d7
Browse files Browse the repository at this point in the history
  • Loading branch information
hdavid16 committed Sep 13, 2020
2 parents 33612d7 + bdd2d5f commit 76c0573
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 12 deletions.
12 changes: 8 additions & 4 deletions examples/ccenter.jl
Expand Up @@ -43,10 +43,14 @@ input = Channel{Caller}(Inf)
output = Caller[]
s1 = Server(1, M_a, 0.0)
s2 = Server(2, M_b, 0.0)
process!(clock, Prc(1, serve, s1, input, output, N))
process!(clock, Prc(2, serve, s2, input, output, N))
event!(clock, fun(arrive, clock, input, count), every, M_arr)
run!(clock, 5000)
# process!(clock, Prc(1, serve, s1, input, output, N))
# process!(clock, Prc(2, serve, s2, input, output, N))
# event!(clock, fun(arrive, clock, input, count), every, M_arr)
# run!(clock, 5000)
@process 1 serve(clock, s1, input, output, N)
@process 2 serve(clock, s2, input, output, N)
@event arrive(clock,input,count) every M_arr
@run! clock 5000

# using Plots
# wt = [c.t₂ - c.t₁ for c in output]
Expand Down
8 changes: 5 additions & 3 deletions examples/intro.jl
Expand Up @@ -25,8 +25,10 @@ clock = Clock() # create a clock
input = Channel{Int}(Inf)
output = Channel{Int}(Inf)
for i in 1:3 # start three server processes
process!(clock, Prc(i, serve, i, input, output, Exponential(1/μ)))
# process!(clock, Prc(i, serve, i, input, output, Exponential(1/μ)))
@process i serve(clock, i, input, output, Exponential(1/μ))
end
# create a repeating event for 10 arrivals
event!(clock, fun(arrive, clock, input, count), every, Exponential(1/λ), n=10)
run!(clock, 20) # run the clock
# event!(clock, fun(arrive, clock, input, count), every, Exponential(1/λ), n=10)
@event arrive(clock, input, count) every Exponential(1/λ) 10
@run! clock 20 # run the clock
11 changes: 7 additions & 4 deletions examples/invctrl.jl
Expand Up @@ -53,13 +53,16 @@ const a = 5 # minimum amount
const Q = 6000.0 # replenishment amount
const M₁ = Exponential(1/λ) # customer arrival time distribution
const M₂ = Exponential(1/ρ) # replenishment time distribution
const X = TruncatedNormal(μ, σ, a, Inf) # demand distribution
const X = truncated(Normal(μ, σ), a, Inf) # demand distribution

clock = Clock()
s = Station(Q, Float64[0.0], Float64[Q], 0, 0, 0.0, 0.0)
event!(clock, fun(replenish, clock, s, Q), every, M₂)
event!(clock, fun(customer, clock, s, X), every, M₁)
println(run!(clock, 5000))
# event!(clock, fun(replenish, clock, s, Q), every, M₂)
# event!(clock, fun(customer, clock, s, X), every, M₁)
# println(run!(clock, 5000))
@event replenish(clock, s, Q) every M₂
@event customer(clock, s, X) every M₁
println(@run! clock 5000)

@show fuel_sold = s.qs;
@show loss_rate = s.ql/s.qs;
Expand Down
4 changes: 3 additions & 1 deletion src/DiscreteEvents.jl
Expand Up @@ -53,6 +53,7 @@ include("timer.jl")
include("printing.jl")
include("resources.jl")
include("utils.jl")
include("macros.jl")

export Clock, PClock, RTClock, setUnit!, 𝐶,
Action, Timing, at, after, every, before, until,
Expand All @@ -63,7 +64,8 @@ export Clock, PClock, RTClock, setUnit!, 𝐶,
createRTClock, stopRTClock,
PrcException,
Resource,
onthread, pseed!
onthread, pseed!,
@process, @event, @run!


Random.seed!(123)
Expand Down
45 changes: 45 additions & 0 deletions src/macros.jl
@@ -0,0 +1,45 @@
"""
@proceses
Create a process from a function.
Note: the first arg to the function being passed must be an AbstractClock
"""
macro process(id, expr)
expr.head != :call && error("Expression is not a function call.")
f = expr.args[1] #extract function passed
c = expr.args[2] #first function arg must be an AbstractClock
args = expr.args[3:end] #extract other function args
p = :(Prc($id, $f, $(args...))) #create Prc struct
esc(:(process!($c,$p))) #execute process!
end

"""
@event
Schedule an event.
Note: if 3 arguments are passed after the function being called,
the third one is assumed to be the keyword argument `n`.
"""
macro event(expr, args...)
expr.head != :call && error("Expression is not a function call.")
f = expr.args[1] #extract function passed
c = expr.args[2] #first function arg must be an AbstractClock
fargs = expr.args[2:end] #extract other function args
ex = :(fun($f, $(fargs...))) #create Action
if length(args) <= 2
esc(:(event!($c, $ex, $(args...)))) #execute event!
else
esc(:(event!($c, $ex, $(args[1:2]...), n = $args[end]))) #execute event!
end
end

"""
@run!
Run a simulation for a given duration.
"""
macro run!(clk, duration)
return esc(:(run!($clk, $duration)))
end

0 comments on commit 76c0573

Please sign in to comment.