|
1 | 1 | # Events |
| 2 | + |
| 3 | +SimJulia includes an extensive set of event types for various purposes. All of them are descendants of `AbstractEvent`. Here the following events are discussed: |
| 4 | + |
| 5 | +- `Event` |
| 6 | +- `Timeout` |
| 7 | +- `Operator` |
| 8 | + |
| 9 | +The guide to resources describes the various resource events. |
| 10 | + |
| 11 | +## Event basics |
| 12 | + |
| 13 | +SimJulia events are very similar – if not identical — to deferreds, futures or promises. Instances of the type AbstractEvent are used to describe any kind of events. Events can be in one of the following states. An event: |
| 14 | + |
| 15 | +- might happen (idle), |
| 16 | +- is going to happen (scheduled) or |
| 17 | +- has happened (processed). |
| 18 | + |
| 19 | +They traverse these states exactly once in that order. Events are also tightly bound to time and time causes events to advance their state. |
| 20 | + |
| 21 | +Initially, events are idle and the function `state` returns `SimJulia.idle`. |
| 22 | + |
| 23 | +If an event gets scheduled at a given time, it is inserted into SimJulia’s event queue. The function `state` returns `SimJulia.scheduled`. |
| 24 | + |
| 25 | +As long as the event is not processed, you can add callbacks to an event. Callbacks are function having an `AbstractEvent` as first parameter. |
| 26 | + |
| 27 | +An event becomes processed when SimJulia pops it from the event queue and calls all of its callbacks. It is now no longer possible to add callbacks. The function `state` returns `SimJulia.processed`. |
| 28 | + |
| 29 | +Events also have a value. The value can be set before or when the event is scheduled and can be retrieved via the function `value` or, within a process, by yielding the event (`value = @yield event`). |
| 30 | + |
| 31 | +## Adding callbacks to an event |
| 32 | + |
| 33 | +“What? Callbacks? I’ve never seen no callbacks!”, you might think if you have worked your way through the tutorial. |
| 34 | + |
| 35 | +That’s on purpose. The most common way to add a callback to an event is yielding it from your process function (`@yield event`). This will add the process’ `resume` function as a callback. That’s how your process gets resumed when it yielded an event. |
| 36 | + |
| 37 | +However, you can add any function to the list of callbacks as long as it accepts `AbstractEvent` or a descendant as first parameter: |
| 38 | + |
| 39 | +```jldoctest |
| 40 | +julia> using SimJulia |
| 41 | +
|
| 42 | +julia> function my_callback(ev::AbstractEvent) |
| 43 | + println("Called back from ", ev) |
| 44 | + end |
| 45 | +my_callback (generic function with 1 method) |
| 46 | +
|
| 47 | +julia> sim = Simulation() |
| 48 | +SimJulia.Simulation time: 0.0 active_process: nothing |
| 49 | +
|
| 50 | +julia> ev = Event(sim) |
| 51 | +SimJulia.Event 1 |
| 52 | +
|
| 53 | +julia> @callback my_callback(ev) |
| 54 | +(::#3) (generic function with 1 method) |
| 55 | +
|
| 56 | +julia> succeed(ev) |
| 57 | +SimJulia.Event 1 |
| 58 | +
|
| 59 | +julia> run(sim) |
| 60 | +Called back from SimJulia.Event 1 |
| 61 | +``` |
0 commit comments