The code generated by SCC from a model description can be reused by other applications. In that case, the code becomes a part of the application, and the end user may not know that this part is generated from a DCharts model. An example of use is that designer models the control logic of a system in DCharts. The model can be validated with tools. The designer then synthesizes target code from the model. This code is thus incorporated into the whole system. It provides robust support for the control logic.
The following is a very simple model (abc.des) that specifies (sort of) control logic:
STATECHART: A [DS] B C D [FS] INITIALIZER: Accept = 0 FINALIZER: Accept = 1 TRANSITION: S: A N: B E: a TRANSITION: S: B N: C E: b TRANSITION: S: C N: D E: c
This model checks whether events a, b and c appear in the same order in all the events that it receives. If the check is successful, the flag Accepted is set to 1; otherwise, it is set to 0. The following event lists are checked successfully (each character is an event in the string):
abc aebdac eghabbabcyucvb
The following are invalid event lists that cause failure:
a abbdadayule cababauolp
This model can be executed in SVM:
svm -t abc.desHowever, though the Accept flag is set in the model, it is not displayed to the user (or model tester).
Before being used in an application (written in native Python), the model must first be compiled into Python source (abc.py) with the following command:
scc -lpython --ext abc.des
This code is used in an application (abc_file.py) that tests a text file according to the same logic. The name of the text file is specified on the command-line:
import code import sys import thread # Import the abc class from the abc model (abc.py) from abc import abc # Get the file name from the command-line fname=sys.argv[1] # Read in the while file infile=open(fname, "r") content=infile.read() infile.close() # Create an interpreter with the local dictionary # (so that the Accept flag can be access in the current scope) interpreter=code.InteractiveInterpreter(locals()) # Create a lock to synchronize the events lock=thread.allocate_lock() # Initialize a model with the interpreter abc_model=abc(interpreter) abc_model.initModel() for c in content: # Treat every character as an event and handle it lock.acquire() abc_model.event(c, [], lock) lock.acquire() lock.release() # Print out the result if Accept: print "success" else: print "failure"
This Python source is the application that imports the code generated by SCC, and uses the methods in it to check a text file. Here are several important notes:
In this example, the interpreter is created with the local dictionary (returned by the Python function locals). As a result, the action code is executed as if it is directly written in the current context. The Accept flag can thus be accessed. (However, this may cause the name conflict between the variables in the model and the variables in the current scope of the native program.)
Now, edit a text file and type in some characters. Save it as abc_test.txt. For example:
dsa dewrevbd dtgbtyrtvc
To check this file, execute the following command (make sure that abc.py is in the same directory):
python abc_file.py abc_test.txt
As expected, the result is ``success''.
It is similar to reuse the code synthesized in other target languages. This is not discussed here.