The State Class
The decomposable Python model is defined by the State
class. The class must be named State
, it cannot be a subclass (even of object
), and it must have an __init__
constructor method with no extra parameters. For the model to receive external inputs, at least one method starting with receive_
should be present. To impose constraints on the valid inputs, some optional corresponding validate_
methods can be added:
State Constructor
The __init__
constructor declares all the state variables and initializes them. It can only receive a single self
argument and only constant value assignments to the self
attributes are allowed inside the constructor body. These assignment statements also act as state variable declarations - you can only reference the self
attributes that you declared in the constructor:
The attributes must be unique and explicitly initialized with some initial values.
Receive Methods
Adding a receive_ActionName
method to the State
class creates an ActionName
action. Arguments of such methods are defining the parameters of the actions. For example:
In this class we’ve declared two actions: the Reset
action has a value
integer parameter, and the Multiply
action has two parameters - another value
integer and a twice
boolean value. Each argument must have an associated type hint. Default argument values are not allowed, as well as variable-length (*args
) and keyword arguments (**kwargs
).
Validate Methods
To constrain the parameter space of the incoming action, one can declare a validate_
method for the same action name. Such a method must accept the same list of parameters as the corresponding receive_
method and must return a boolean value: returned True
means that the values of the action are allowed.
Above we’ve constrained the value
parameter of the Reset
method to always be positive. While the value
parameter of the validate_Multiply
method is required to be positive only when the twice
parameter is True
.