From 543be5e61c86f5c068c35827bfcacdaba9acca32 Mon Sep 17 00:00:00 2001 From: Andrei Smirnov Date: Fri, 2 Feb 2024 19:30:37 -0500 Subject: [PATCH 1/8] issue #106 - added code file with ported example from old SimJulia doc, tested, seems to be working as expected --- docs/src/guides/events.md | 51 ++++++++++++++++++++++++++++++++++++++- examples/school.jl | 40 ++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 examples/school.jl diff --git a/docs/src/guides/events.md b/docs/src/guides/events.md index 54f03de..f349ec7 100755 --- a/docs/src/guides/events.md +++ b/docs/src/guides/events.md @@ -58,4 +58,53 @@ ConcurrentSim.Event 1 julia> run(sim) Called back from ConcurrentSim.Event 1 -``` \ No newline at end of file +``` + +## Example usages of Event + +The simple mechanics outlined above provide a great flexibility in the way events can be used. + +One example for this is that events can be shared. They can be created by a process or outside of the context of a process. They can be passed to other processes and chained: + +``` +# based on example from +# https://simjuliajl.readthedocs.io/en/stable/topical_guides/3_events.html#example-usages-for-event +# + +using ResumableFunctions +using ConcurrentSim + +mutable struct School + class_ends :: Event + pupil_procs :: Vector{Process} + bell_proc :: Process + function School(env::Simulation) + school = new() + school.class_ends = Event(env) + school.pupil_procs = Process[@process pupil(env, school) for i=1:3] + school.bell_proc = @process bell(env, school) + return school + end +end + +@resumable function bell(env::Simulation, school::School) + for i=1:2 + @yield timeout(env, 45.0) + succeed(school.class_ends) + #school.class_ends = Event(env) -- ?? orig SimJulia example somehow works + println() + end +end + +@resumable function pupil(env::Simulation, school::School) + for i=1:2 + print(" \\o/") + @yield school.class_ends + school.class_ends = Event(env) # after yield event is idle + end +end + +env = Simulation() +school = School(env) +run(env) +``` diff --git a/examples/school.jl b/examples/school.jl new file mode 100644 index 0000000..b0ef79f --- /dev/null +++ b/examples/school.jl @@ -0,0 +1,40 @@ +# based on example from +# https://simjuliajl.readthedocs.io/en/stable/topical_guides/3_events.html#example-usages-for-event +# + +using ResumableFunctions +using ConcurrentSim + +mutable struct School + class_ends :: Event + pupil_procs :: Vector{Process} + bell_proc :: Process + function School(env::Simulation) + school = new() + school.class_ends = Event(env) + school.pupil_procs = Process[@process pupil(env, school) for i=1:3] + school.bell_proc = @process bell(env, school) + return school + end +end + +@resumable function bell(env::Simulation, school::School) + for i=1:2 + @yield timeout(env, 45.0) + succeed(school.class_ends) + #school.class_ends = Event(env) -- ?? orig SimJulia example somehow works + println() + end +end + +@resumable function pupil(env::Simulation, school::School) + for i=1:2 + print(" \\o/") + @yield school.class_ends + school.class_ends = Event(env) # after yield event is idle + end +end + +env = Simulation() +school = School(env) +run(env) From 5e9acc27370b97876eda134077abb07aaa00eb1b Mon Sep 17 00:00:00 2001 From: Stefan Krastanov Date: Thu, 8 Feb 2024 07:26:18 -0500 Subject: [PATCH 2/8] trigger ci From 177c7889a2a9e12ea9a22c9d848d8e0c76ff7416 Mon Sep 17 00:00:00 2001 From: Andrei Smirnov Date: Thu, 8 Feb 2024 11:41:47 -0500 Subject: [PATCH 3/8] CHANGELOG.md updated, school.jl example removed from examples dir, example in events.md marked as jldoctest --- CHANGELOG.md | 4 ++++ docs/src/guides/events.md | 2 +- examples/school.jl | 40 --------------------------------------- 3 files changed, 5 insertions(+), 41 deletions(-) delete mode 100644 examples/school.jl diff --git a/CHANGELOG.md b/CHANGELOG.md index 8eef07b..b005661 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # News +## v1.4.1 - 2024-02-08 + +- added example into events.md - code is taken from SimJulia old doc, ported and tested + ## v1.4.0 - 2023-08-07 - Implement a `DelayQueue`, i.e. a `QueueStore` with latency between the store and take events. diff --git a/docs/src/guides/events.md b/docs/src/guides/events.md index f349ec7..bf09c52 100755 --- a/docs/src/guides/events.md +++ b/docs/src/guides/events.md @@ -66,7 +66,7 @@ The simple mechanics outlined above provide a great flexibility in the way event One example for this is that events can be shared. They can be created by a process or outside of the context of a process. They can be passed to other processes and chained: -``` +```jldoctest # based on example from # https://simjuliajl.readthedocs.io/en/stable/topical_guides/3_events.html#example-usages-for-event # diff --git a/examples/school.jl b/examples/school.jl deleted file mode 100644 index b0ef79f..0000000 --- a/examples/school.jl +++ /dev/null @@ -1,40 +0,0 @@ -# based on example from -# https://simjuliajl.readthedocs.io/en/stable/topical_guides/3_events.html#example-usages-for-event -# - -using ResumableFunctions -using ConcurrentSim - -mutable struct School - class_ends :: Event - pupil_procs :: Vector{Process} - bell_proc :: Process - function School(env::Simulation) - school = new() - school.class_ends = Event(env) - school.pupil_procs = Process[@process pupil(env, school) for i=1:3] - school.bell_proc = @process bell(env, school) - return school - end -end - -@resumable function bell(env::Simulation, school::School) - for i=1:2 - @yield timeout(env, 45.0) - succeed(school.class_ends) - #school.class_ends = Event(env) -- ?? orig SimJulia example somehow works - println() - end -end - -@resumable function pupil(env::Simulation, school::School) - for i=1:2 - print(" \\o/") - @yield school.class_ends - school.class_ends = Event(env) # after yield event is idle - end -end - -env = Simulation() -school = School(env) -run(env) From a3aad4aa4ba9fbc1aa5d0ac5a7d024002120303e Mon Sep 17 00:00:00 2001 From: Andrei Smirnov Date: Thu, 8 Feb 2024 14:10:47 -0500 Subject: [PATCH 4/8] added output of school example, attempt to pass github checks --- docs/src/guides/events.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/src/guides/events.md b/docs/src/guides/events.md index bf09c52..b3bb3c0 100755 --- a/docs/src/guides/events.md +++ b/docs/src/guides/events.md @@ -107,4 +107,9 @@ end env = Simulation() school = School(env) run(env) + +#output + + \o/ \o/ \o/ + \o/ \o/ \o/ ``` From ece96568ec82e037cd751dfa74834af808a03d6f Mon Sep 17 00:00:00 2001 From: Andrei Smirnov Date: Thu, 8 Feb 2024 15:27:58 -0500 Subject: [PATCH 5/8] typo fixed in test case output --- docs/src/guides/events.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/guides/events.md b/docs/src/guides/events.md index b3bb3c0..8ac4dc6 100755 --- a/docs/src/guides/events.md +++ b/docs/src/guides/events.md @@ -108,7 +108,7 @@ env = Simulation() school = School(env) run(env) -#output +# output \o/ \o/ \o/ \o/ \o/ \o/ From 9771cf6b4050229208d04c52084b5c8ecc6f2dbe Mon Sep 17 00:00:00 2001 From: Stefan Krastanov Date: Thu, 8 Feb 2024 15:37:58 -0500 Subject: [PATCH 6/8] remove old link to avoid giving it SEO weight --- CHANGELOG.md | 2 +- docs/src/guides/events.md | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b005661..5732598 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## v1.4.1 - 2024-02-08 -- added example into events.md - code is taken from SimJulia old doc, ported and tested +- Added examples to the documentation that were lost around the time of the rewrite for v0.5 in 2018. ## v1.4.0 - 2023-08-07 diff --git a/docs/src/guides/events.md b/docs/src/guides/events.md index 8ac4dc6..1a8ed9b 100755 --- a/docs/src/guides/events.md +++ b/docs/src/guides/events.md @@ -67,10 +67,6 @@ The simple mechanics outlined above provide a great flexibility in the way event One example for this is that events can be shared. They can be created by a process or outside of the context of a process. They can be passed to other processes and chained: ```jldoctest -# based on example from -# https://simjuliajl.readthedocs.io/en/stable/topical_guides/3_events.html#example-usages-for-event -# - using ResumableFunctions using ConcurrentSim From e6b061b9f5d7ab3b83fb8ba65e3992dce8cde59e Mon Sep 17 00:00:00 2001 From: Stefan Krastanov Date: Thu, 8 Feb 2024 16:08:51 -0500 Subject: [PATCH 7/8] fix a bug in the translation --- CHANGELOG.md | 2 +- docs/src/guides/events.md | 35 ++++++++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5732598..e8690d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # News -## v1.4.1 - 2024-02-08 +## v1.4.1 - dev - Added examples to the documentation that were lost around the time of the rewrite for v0.5 in 2018. diff --git a/docs/src/guides/events.md b/docs/src/guides/events.md index 1a8ed9b..17d8f57 100755 --- a/docs/src/guides/events.md +++ b/docs/src/guides/events.md @@ -64,7 +64,9 @@ Called back from ConcurrentSim.Event 1 The simple mechanics outlined above provide a great flexibility in the way events can be used. -One example for this is that events can be shared. They can be created by a process or outside of the context of a process. They can be passed to other processes and chained: +One example for this is that events can be shared. They can be created by a process or outside of the context of a process. They can be passed to other processes and chained. + +Below we give such an example, however this is a **very low-level example** and you would probably prefer to use the safer and more user-friendly [`Resource`](@ref) or [`Store`](@ref). ```jldoctest using ResumableFunctions @@ -77,7 +79,7 @@ mutable struct School function School(env::Simulation) school = new() school.class_ends = Event(env) - school.pupil_procs = Process[@process pupil(env, school) for i=1:3] + school.pupil_procs = Process[@process pupil(env, school, i) for i=1:3] school.bell_proc = @process bell(env, school) return school end @@ -85,18 +87,19 @@ end @resumable function bell(env::Simulation, school::School) for i=1:2 + println("starting the bell timer at t=$(now(env))") @yield timeout(env, 45.0) succeed(school.class_ends) - #school.class_ends = Event(env) -- ?? orig SimJulia example somehow works - println() + school.class_ends = Event(env) # the event is now idle (i.e. spent) so we need to create a new one + println("bell is ringing at t=$(now(env))") end end -@resumable function pupil(env::Simulation, school::School) +@resumable function pupil(env::Simulation, school::School, pupil) for i=1:2 - print(" \\o/") + println("pupil $pupil goes to class") @yield school.class_ends - school.class_ends = Event(env) # after yield event is idle + println("pupil $pupil leaves class at t=$(now(env))") end end @@ -106,6 +109,20 @@ run(env) # output - \o/ \o/ \o/ - \o/ \o/ \o/ +pupil 1 goes to class +pupil 2 goes to class +pupil 3 goes to class +starting the bell timer at t=0.0 +bell is ringing at t=45.0 +starting the bell timer at t=45.0 +pupil 1 leaves class at t=45.0 +pupil 1 goes to class +pupil 2 leaves class at t=45.0 +pupil 2 goes to class +pupil 3 leaves class at t=45.0 +pupil 3 goes to class +bell is ringing at t=90.0 +pupil 1 leaves class at t=90.0 +pupil 2 leaves class at t=90.0 +pupil 3 leaves class at t=90.0 ``` From 21ee5fd0e111d960e00fc0e2bd04c30f7a7ff089 Mon Sep 17 00:00:00 2001 From: Stefan Krastanov Date: Thu, 8 Feb 2024 16:12:58 -0500 Subject: [PATCH 8/8] docstring for Resource --- src/resources/containers.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/resources/containers.jl b/src/resources/containers.jl index 9cf063a..9bd0196 100755 --- a/src/resources/containers.jl +++ b/src/resources/containers.jl @@ -40,6 +40,7 @@ function Container{T}(env::Environment, capacity::N=one(N); level=zero(N)) where Container{N, T}(env, capacity; level=N(level)) end +"""An alias for `Container{Int, Int}`, one of the most frequently used types of synchronization primitive.""" const Resource = Container{Int, Int} function put!(con::Container{N, T}, amount::N; priority=zero(T)) where {N<:Real, T<:Number}