next up previous contents
Next: 2.5.7 An example of Up: 2.5 More Examples Previous: 2.5.5 An example of   Contents

2.5.6 An example of an initializer and a finalizer

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:

  1. A final state of the model is entered and the final state is a leaf state. If it is not a leaf state, the simulation ends when all the default leaf substates of that state are entered. (I.e., the simulation of the model finishes a macro-step.)

  2. The user stops the simulation manually. This is usually by closing the SVM main window or entering the exit command in the command box (or text console).

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.


next up previous contents
Next: 2.5.7 An example of Up: 2.5 More Examples Previous: 2.5.5 An example of   Contents
Thomas Huining Feng
2004-04-05