You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/tutorial.md
+16-12Lines changed: 16 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,17 @@
1
1
# Tutorial
2
2
3
3
## Basic Concepts
4
-
Simjulia is a discrete-event simulation library. The behavior of active components (like vehicles, customers or messages) is modeled with processes. All processes live in an environment, e.g. a `Simulation`. They interact with the environment and with each other via `Events`.
5
4
6
-
Processes are described by `@resumable functions`. You can call them process function. During their lifetime, they create events and `@yield` them in order to wait for them to be triggered.
5
+
Simjulia is a discrete-event simulation library. The behavior of active components (like vehicles, customers or messages) is modeled with processes. All processes live in an environment. They interact with the environment and with each other via events.
6
+
7
+
Processes are described by `@resumable` functions. You can call them process function. During their lifetime, they create events and `@yield` them in order to wait for them to be triggered.
8
+
9
+
!!! note
10
+
Detailed information about the `@resumable` and the `@yield` macros can be found in the documentation of [ResumableFunctions](https://github.com/BenLauwens/ResumableFunctions.jl.git).
7
11
8
12
When a process yields an event, the process gets suspended. SimJulia resumes the process, when the event occurs (we say that the event is triggered). Multiple processes can wait for the same event. SimJulia resumes them in the same order in which they yielded that event.
9
13
10
-
An important event type is the `Timeout`. Events of this type are triggered after a certain amount of (simulated) time has passed. They allow a process to sleep (or hold its state) for the given time. A `Timeout` and all other events can be created by calling a constructor having the environment as first argument.
14
+
An important event type is the `Timeout`. Events of this type are scheduled after a certain amount of (simulated) time has passed. They allow a process to sleep (or hold its state) for the given time. A `Timeout` and all other events can be created by calling a constructor having the environment as first argument.
11
15
12
16
## Our First Process
13
17
@@ -33,9 +37,9 @@ julia> @resumable function car(env::Environment)
33
37
car (generic function with 1 method)
34
38
```
35
39
36
-
Our car process requires a reference to an `Environment`(`env`) in order to create new events. The car‘s behavior is described in an infinite loop. Remember, the `car` function is a `@resumable function`. Though it will never terminate, it will pass the control flow back to the simulation once a `@yield` statement is reached. Once the yielded event is triggered (“it occurs”), the simulation will resume the function at this statement.
40
+
Our car process requires a reference to an `Environment` in order to create new events. The car‘s behavior is described in an infinite loop. Remember, the `car` function is a `@resumable` function. Though it will never terminate, it will pass the control flow back to the simulation once a `@yield` statement is reached. Once the yielded event is triggered (“it occurs”), the simulation will resume the function at this statement.
37
41
38
-
As said before, our car switches between the states parking and driving. It announces its new state by printing a message and the current simulation time (as returned by the function call `now(env)`). It then calls the constructor `Timeout(env)` to create a timeout event. This event describes the point in time the car is done parking (or driving, respectively). By yielding the event, it signals the simulation that it wants to wait for the event to occur.
42
+
As said before, our car switches between the states parking and driving. It announces its new state by printing a message and the current simulation time (as returned by the function call `now`). It then calls the constructor `Timeout` to create a timeout event. This event describes the point in time the car is done parking (or driving, respectively). By yielding the event, it signals the simulation that it wants to wait for the event to occur.
39
43
40
44
Now that the behavior of our car has been modeled, lets create an instance of it and see how it behaves:
41
45
@@ -76,11 +80,11 @@ Start parking at 14.0
76
80
DocTestSetup = nothing
77
81
```
78
82
79
-
The first thing we need to do is to create an `Environment`, i.e. an instance of `Simulation`. The macro `@process`with as argument a call to the car process function creates a process generator that is started and added to the environment automatically.
83
+
The first thing we need to do is to create an environment, e.g. an instance of `Simulation`. The macro `@process`having as argument a car process function call creates a process that is initialised and added to the environment automatically.
80
84
81
85
Note, that at this time, none of the code of our process function is being executed. Its execution is merely scheduled at the current simulation time.
82
86
83
-
The `Process` returned by the `@process` macro can be used for process interactions (we will cover that in the next section, so we will ignore it for now).
87
+
The `Process` returned by the `@process` macro can be used for process interactions.
84
88
85
89
Finally, we start the simulation by calling `run` and passing an end time to it.
86
90
@@ -90,11 +94,11 @@ The `Process` instance that is returned by `@process` macro can be utilized for
90
94
91
95
### Waiting for a Process
92
96
93
-
As it happens, a SimJulia `Process` can be used like an event. If you yield it, you are resumed once the process has finished. Imagine a car-wash simulation where cars enter the car-wash and wait for the washing process to finish. Or an airport simulation where passengers have to wait until a security check finishes.
97
+
As it happens, a SimJulia `Process` can be used like an event. If you yield it, you are resumed once the process has finished. Imagine a car-wash simulation where cars enter the car-wash and wait for the washing process to finish, or an airport simulation where passengers have to wait until a security check finishes.
94
98
95
-
Lets assume that the car from our last example magically became an electric vehicle. Electric vehicles usually take a lot of time charging their batteries after a trip. They have to wait until their battery is charged before they can start driving again.
99
+
Lets assume that the car from our last example is an electric vehicle. Electric vehicles usually take a lot of time charging their batteries after a trip. They have to wait until their battery is charged before they can start driving again.
96
100
97
-
We can model this with an additional `charge` process for our car. Therefore, we redefine our `car` process function and add a `charge` process function.
101
+
We can model this with an additional charge process for our car. Therefore, we redefine our `car` process function and add a `charge` process function.
98
102
99
103
A new charge process is started every time the vehicle starts parking. By yielding the `Process` instance that the `@process` macro returns, the `run` process starts waiting for it to finish:
100
104
@@ -185,7 +189,7 @@ julia> @resumable function driver(env::Environment, car_process::Process)
185
189
driver (generic function with 1 method)
186
190
```
187
191
188
-
The `driver` process has a reference to the `car` process. After waiting for 3 time steps, it interrupts that process.
192
+
The driver process has a reference to the car process. After waiting for 3 time steps, it interrupts that process.
189
193
190
194
Interrupts are thrown into process functions as `Interrupt` exceptions that can (should) be handled by the interrupted process. The process can then decide what to do next (e.g., continuing to wait for the original event or yielding a new event):
191
195
@@ -379,7 +383,7 @@ julia> run(sim)
379
383
4 leaving the bcs at 14.0
380
384
```
381
385
382
-
Finally, we can start the simulation. Since the `car` processes all terminate on their own in this simulation, we don’t need to specify an until time — the simulation will automatically stop when there are no more events left:
386
+
Finally, we can start the simulation. Since the car processes all terminate on their own in this simulation, we don’t need to specify an until time — the simulation will automatically stop when there are no more events left:
0 commit comments