The class design of SVM is shown in Figure 6.1. (This class diagram only shows the important attributes and methods.) Class EventHandler is the main class that loads the model from a text file, builds internal data structures for the model, and simulates the model on demand. It can be used with different user interfaces: the TextualInterface class defines the default textual interface that accepts input and produces output on the console; the GraphicalInterface class defines the default graphical interface; and the CursesInterface class defines the default curses interface (for UNIX only) to be used in the text mode with colors. Designers may define model-specific interfaces. Examples of model-specific interfaces are discussed in later chapters.
Class SVMFrontEnd provides a front end of the simulation environment for the end users. It accepts command-line parameters and initializes an instance of EventHandler with the model description. It also interacts with the model user through one of the user interfaces.
Class Generator uses EventHandler to parse DCharts models. It generates source code in different target-languages from the internal structures created by EventHandler. Class SCCFrontEnd provides a command-line front end for the code generator.
As EventHandler is the core of the parser and the simulator, it is necessary to introduce some of its methods and attributes here:
Appends an event to the global event list. The event will be handled after all the events before it in the list are consumed. Parameter event is the event name. params is a parameter or a Python list of parameters for the event. internal is a boolean that denotes whether the event is generated by the model itself or is received as a message from a port. lock is a semaphore. If it is non-null, the simulator will release this lock once this event is handled. The following piece of code uses a semaphore to schedule an event and waits until it is handled:
... import thread // import the Python threading library lock = thread.allocate_lock() // allocate a lock lock.acquire() // acquire the lock for the first time eventhandler.event("e", [], 1, lock) // raise event lock.acquire() // acquire the lock again; block until the event is handled del lock // destroy the lock ...
Raises an event and waits until the event is handled (as the code segment above). Its parameters have exactly the same meaning as the event method.
Starts the simulation. It executes the initializer of the model and places the model in its initial default state.
Ends the simulation. If the model is not in a final state (the finalizer has not been executed yet), the finalizer is executed; otherwise (the finalizer has been executed), the SVM simulator simply exits.
Takes a snapshot of the current state of the model. The snapshot is returned as a string.
Takes a snapshot and save the snapshot in the file named by the filename parameter. The file is a plain text file. The user may manually edit it. (Note: it is the modifier's responsibility to make sure that the file is still meaningful.)
Restores a previously taken snapshot (saved in a string) and resumes the simulation. Information about the current simulation is completely lost.
Restores a previously taken snapshot stored in the file named by the filename parameter.
Returns a list of the names of enabled events. The result depends on the current state of the simulation.
Returns true if state1 is equal to state2 or state1 is a substate of state2.
Returns whether the model is currently in state. If check_substate is false, the simulator only checks leaf states. Hence, the result is true if and only if the model is in state and state is a leaf state. If check_substate is true, the function returns true if and only if the model is in state, whether it is a leaf state or not.
Returns whether state is inner-transition-first.
A list of strings enumerating all the leaf states that the model is currently in.
To simulate DCharts models, EventHandler requires the support of other classes. These classes are not necessary for code generation in SCC:
The EventHandler class is a parser and a simulator. It can be reused in other applications. For example, the SCC code synthesizer uses this class to parse textual model descriptions; an application that needs a DCharts simulator (such as AToM with the DCharts plugin) may use it to simulate models.
The command-line to invoke SVM is discussed in [39]. It includes a complete description on how to start the simulation of a model, how to choose among the default interfaces, and how to redefine macros for the model.