Events¶
AbstractEvent¶
-
abstract AbstractEvent
The parent type for all events is AbstractEvent
.
An event:
- may happen (
triggered
returnsfalse
), - is going to happen (
triggered
returnstrue
), - is happening (
processed
returnsfalse
) or - has happened (
processed
returnstrue
).
Every event is bound to an environment and is initially not triggered. Events are scheduled for processing by the environment after they are triggered by either succeed
, fail
or trigger
. These methods also set the value of the event.
An event has a list of callbacks. A callback can be any function as long as it accepts an instance of type Event
as its first argument. Once an event gets processed, all callbacks will be invoked. Callbacks can do further processing with the value it has produced.
Failed events are never silently ignored and will raise an exception upon being processed.
-
triggered
(ev::AbstractEvent) → Bool¶
Returns true
if the event has been triggered and its callbacks are about to be invoked.
-
processed
(ev::AbstractEvent) → Bool¶
Returns true
if the event has been processed (i.e., its callbacks have been invoked).
-
value
(ev::AbstractEvent) → Any¶
Returns the value
of the event if it is available, otherwise returns nothing
. The value is available when the event has been triggered.
-
append_callback
(ev::AbstractEvent, callback::Function, args...)¶
Adds a process function to the event. The first argument of the function callback
is an AbstractEnvironment
. Optional arguments can be specified by args...
. If the event is already processed an EventProcessed
exception is thrown.
-
succeed
(ev::AbstractEvent, value=nothing) → AbstractEvent¶
Sets the event’s value and schedule it for processing by the environment. Returns the event instance. Throws an EventTriggered
exception if this event has already been triggered.
-
fail
(ev::AbstractEvent, exc::Exception) → AbstractEvent¶
Sets the exception as the events value, mark it as failed and schedule it for processing by the environment. Returns the event instance. Throws an EventTriggered
exception if this event has already been triggered.
-
trigger
(cause::AbstractEvent, ev::AbstractEvent) → AbstractEvent¶
Schedules the event with the state and value of the cause event. Returns the event instance. Throws an EventTriggered
exception if this event has already been triggered.
This method can be used directly as a callback function to trigger chain reactions.
Event¶
-
Event <: AbstractEvent
An event that may happen at some point in time.
-
Event
(env::AbstractEnvironment) → Event¶
Constructor of Event
with one argument env
, the environment where the event lives in.
Timeout¶
-
Timeout <: AbstractEvent
An event that gets triggered after a delay
has passed.
-
Timeout
(env::AbstractEnvironment, delay::Float64, value=nothing) → Timeout¶
This event is automatically triggered when it is created. The value
argument is optional.
EventOperator¶
-
EventOperator <: AbstractEvent
An event that gets triggered once the condition function eval
returns true
on the given list of events
.
The value of an Eventoperator is an instance of Dict{AbstractEvent, Any}
which allows convenient access to the input events and their values. The value will only contain entries for those events that occurred before the condition is processed.
If one of the events fails, the condition also fails and forwards the exception of the failing event.
-
EventOperator
(eval::Function, events...) → EventOperator¶
The eval
function receives a tuple of target events: eval(events...)
. If it returns true
, the event is triggered.
-
AllOf
(events...) → EventOperator¶
Constructor for an EventOperator
that is triggered if all of a list of events have been successfully triggered. Fails immediately if any of events
failed.
-
AnyOf
(events...) → EventOperator¶
Constructor for an EventOperator
that is triggered if any of a list of events has been successfully triggered. Fails immediately if any of events
failed.
-
(&)(ev1::AbstractEvent, ev2::AbstractEvent) -> EventOperator
Shortcut for AllOf(ev1, ev2)
.
-
(|)(ev1::AbstractEvent, ev2::AbstractEvent) -> EventOperator
Shortcut for AnyOf(ev1, ev2)
.