Actors & Data Streams


Actors

Definition

Actors are typically defined in terms of a behaviour and a mailbox. The behaviour of an actor describes its internal state and the messages that can be processed. This example implements an actor behaviour called Wind that implements a simple simulation of wind which emits random values that correspond to the current wind speed. This actor behaviour has 1 local field called rng, a constructor called init which initializes the rng field with a new random number generator (an object). It also defines a method called blow that is responsible for periodically emitting values to a data stream.

Data Streams

The Wind actor behaviour declares 1 data stream called speed. This means that any actor spawned from the Wind behaviour will export a stream called speed to which they can emit values. The emitting of values is showcased by the blow method. The first statement in its body is an emit! statement, which is the key ingredient for emitting a new value to the speed stream.

Monitoring Data Streams

The Main actor behaviour shows how actors can monitor data streams. Two key concepts are required: qualification and monitoring. A qualification expression such as mistral.speed returns a reference (an object) to the speed stream that is exported by the actor referenced by mistral. Actors can monitor data streams via the special monitor! statement. When an actor monitors a data stream, this means that every time a new value is emitted to the stream, then the actors that are monitoring the stream will receive this new value as a message into their mailbox. In this case, a 'print-wind-speed message will be inserted into the mailbox. Thus, every time a new value is emitted to the mistral.speed stream, the Main actor will eventually invoke its print-wind-speed method when the message is processed.