Skip to content

Commit b5be5fc

Browse files
committed
Add Basics guide
1 parent 68bb9df commit b5be5fc

4 files changed

Lines changed: 52 additions & 4 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ julia> Pkg.add("SimJulia")
4343
## Release Notes
4444

4545
* 2017: v0.5
46-
* The old way of making processes is deprecated in favor of the semi-coroutine approach as implemented in [ResumableFunctions](https://github.com/BenLauwens/ResumableFunctions.jl.git). The `@process` macro replaces the `@coroutine` macro. The old `@process` macro is temporarily renamed `@oldprocess` and will be removed when the infrastructure supporting the `produce` and the `consume` functions are no longer available in Julia. (DONE)
46+
* The old way of making processes is deprecated in favor of the semi-coroutine approach as implemented in [ResumableFunctions](https://github.com/BenLauwens/ResumableFunctions.jl.git). The `@process` macro replaces the `@coroutine` macro. The old `@process` macro is temporarily renamed `@oldprocess` and will be removed when the infrastructure supporting the `produce` and the `consume` functions is no longer available in Julia. (DONE)
4747
* This version no longer integrates a continuous time solver. A continuous simulation framework based on [DISCO](http://www.akira.ruc.dk/~keld/research/DISCO/) and inspired by the standalone [QSS](https://sourceforge.net/projects/qssengine/) solver using SimJulia as its discrete-event engine can be found in the repository [QuantizedStateSystems](https://github.com/BenLauwens/QuantizedStateSystems.jl.git) (WIP):
48-
* Documentation is automated with [Documenter.jl](https://github.com/JuliaDocs/Documenter.jl) (WIP).
48+
* Documentation is automated with [Documenter.jl](https://github.com/JuliaDocs/Documenter.jl) (WIP: Overview and Tutorial OK).
4949
* 2017: v0.4.1, the `resumable` and `yield` macro are put in a seperate package [ResumableFunctions](https://github.com/BenLauwens/ResumableFunctions.jl.git):
5050
* Users have to take into account the following syntax change: `@yield return arg` is replaced by `@yield arg`.
5151
* 2017: v0.4 only supports Julia v0.6 and above. It is a complete rewrite: more julian and less pythonic. The discrete event features are on par with v0.3 (SimPy v3) and following features are added:

docs/make.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ makedocs(
99
pages = [
1010
"Home" => "index.md",
1111
"Tutorial" => "tutorial.md",
12-
"Topical Guides" => "guides/index.md",
12+
"Topical Guides" => ["Basics" => "guides/basics.md",],
1313
"Examples" => ["Ross" => "examples/ross.md",],
1414
"API" => "api.md"
1515
]

docs/src/guides/basics.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# SimJulia basics
2+
3+
This guide describes the basic concepts of SimJulia: How does it work? What are processes, events and the environment? What can I do with them?
4+
5+
## How SimJulia works
6+
7+
If you break SimJulia down, it is just an asynchronous event dispatcher. You generate events and schedule them at a given simulation time. Events are sorted by priority, simulation time, and an increasing event id. An event also has a list of callbacks, which are executed when the event is triggered and processed by the event loop. Events may also have a return value.
8+
9+
The components involved in this are the `Environment`, events and the process functions that you write.
10+
11+
Process functions implement your simulation model, that is, they define the behavior of your simulation. They are `@resumable` functions that `@yield` instances of `AbstractEvent`.
12+
13+
The environment stores these events in its event list and keeps track of the current simulation time.
14+
15+
If a process function yields an event, SimJulia adds the process to the event’s callbacks and suspends the process until the event is triggered and processed. When a process waiting for an event is resumed, it will also receive the event’s value.
16+
17+
Here is a very simple example that illustrates all this; the code is more verbose than it needs to be to make things extra clear. You find a compact version of it at the end of this section:
18+
19+
```jldoctest
20+
using ResumableFunctions
21+
using SimJulia
22+
23+
@resumable function example(env::Environment)
24+
event = Timeout(env, 1, value=42)
25+
value = @yield event
26+
println("now=", now(env), ", value=", value)
27+
end
28+
29+
sim = Simulation()
30+
@process example(sim)
31+
run(sim)
32+
33+
# output
34+
35+
now=1.0, value=42
36+
```
37+
38+
The `example` process function above first creates a `Timeout` event. It passes the environment, a delay, and a value to it. The `Timeout` schedules itself at `now + delay` (that’s why the environment is required); other event types usually schedule themselves at the current simulation time.
39+
40+
The process function then yields the event and thus gets suspended. It is resumed, when SimJulia processes the `Timeout` event. The process function also receives the event’s value (42) – this is, however, optional, so `@yield event` would have been okay if the you were not interested in the value or if the event had no value at all.
41+
42+
Finally, the process function prints the current simulation time (that is accessible via the `now` function) and the `Timeout`’s value.
43+
44+
If all required process functions are defined, you can instantiate all objects for your simulation. In most cases, you start by creating an instance of `Environement`, e.g. a `Simulation`, because you’ll need to pass it around a lot when creating everything else.
45+
46+
Starting a process function involves two things:
47+
48+
- You have to call the macro `@process` with as argument a function call to the process function. (This will not execute any code of that function yet.) This will schedule an initialisation event at the current simulation time which starts the execution of the process function. The process instance is also an event that is triggered when the process function returns.
49+
- Finally, you can start SimJulia’s event loop. By default, it will run as long as there are events in the event list, but you can also let it stop earlier by providing an until argument.

docs/src/guides/index.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)