Each model may have an initializer defined under the INITIALIZER. Designers are allowed to write arbitrary Python code in the initializer. This code is executed at the very beginning of the simulation, even before the simulator finds the default states of the model or the enter actions of those default states are executed. If a model has multiple INITIALIZER descriptors, the code under those descriptors is literally combined.
Similarly, the finalizer is defined under the FINALIZER descriptor. It is a piece of arbitrary Python code. If multiple FINALIZER descriptors are defined in the model description, they are literally combined. The finalizer is executed at the very end of the simulation, after the execution of enter actions of the last entered state. The end of a simulation is defined as one of the following two events:
An example of an initializer and a finalizer (initfinal1.des) is included below:
INITIALIZER: def print_numbers(): for i in range(10): print i, print [DUMP("initialized")] FINALIZER: [DUMP("nothing to do in finalizer")] STATECHART: A [DS] TRANSITION: S: A N: A E: e O: print_numbers()
Here is the simulation result:
initialized ['A'] > e 0 1 2 3 4 5 6 7 8 9 ['A'] > exit nothing to do in finalizer
In the initializer shown above, a function print_numbers is defined to print out numbers from 0 to 9. It is a Python function that can be invoked in the output of the transition. The designer of a model may also use the initializer to define more functions, or to import Python modules to be used throughout the model description, or to initializer the hardware devices that the model controls.
The finalizer may be used to release allocated system resources. In this case, it simply prints out a message.
As is introduced for the first time, the transition in this example is a self-loop, which has the same definition of source state and new state. Note that in some cases, self-loops change the current state of the model, because (if the source/new state has substates) they causes the model to go to the default substate(s) of that state. A self-loop to the deep history of the new state never changes the model state.