Actors / Roles
Roles are like classes in object oriented programming. They have states and actions. But here, they may represent a participant in a system. It could be a,
- microservice
- process
- thread
- just a role (Eg. coordinator, participant in 2-Phase Commit)
- database
If you are writing a design doc, and created a block diagram with it, they most likely represent a role in the system.
Roles can have state, actions, functions and even assertions like a top level spec. So basically, roles just provide a way to organize the code.
Role is just a collection of states, actions and functions.
The syntax should be familiar to most python programmers.
The state variables will be initialized in the Init
action,
and the variables can be accessed using self
reference.
(Java and other languages use this
instead of self
)
To initialize, just call the constructor with the role name.
|
|
Run it in the FizzBee playground, you can see the two possible states, and the graph.
You can pass in arguments to the role constructor as named parameters. The parameters will be automatically assigned to the role variables.
For example, you can assign a unique id to a role instance.
|
|
Now, notice the NAME is available as self.NAME. You can set multiple parameters as needed.
Roles have a unique ID assigned to them, which can be accessed using self._id__
.
|
|
See Symmetric Roles for more details on symmetry reduction and how it applies to roles.
Again, like in the top level spec, you can define functions in the role.
|
|
You can create and delete roles dynamically. When a role is created, from the next yield point on, the actions would be automatically scheduled.
Similarly, when a role is deleted, the actions would be removed from the scheduler.
|
|
Read more about Role Communication
Take a look at the 2-Phase Commit example for a more complex example of roles in action.