next up previous contents
Next: 3.4 Reusing the Synthesized Up: 3. CODE SYNTHESIS WITH Previous: 3.2.4 C# code synthesis   Contents

3.3 An example of model-specific interface

Interactor, like other parts of a model that contain arbitrary Python code (e.g., initializer and finalizer), is supported by SCC. The interactor of a model is executed in a separate thread, and the exiting of that thread denotes the end of the execution. If Python is the target language, the thread is created with the methods provided by the thread native Python module. (This requires the Python environment to be compiled with thread support.) If C++ is the target language, the thread is created with the functions in the pthread library. This library is available in both Linux and Windows.

Here is the model (inter1.des) copied from section 2.5.14:

STATECHART:
    A [DS]

INTERACTOR:
    import thread
    import string

    lock=thread.allocate_lock()

    # make sure the model is started before the events are sent to it
    lock.acquire()
    eventhandler.start(lock)
    lock.acquire()
    lock.release()

    # repeatedly handle events
    cmd=""
    while cmd!="quit":
      sys.__stdout__.write("CMD >  ")
      cmd=string.strip(sys.__stdin__.readline())
      if cmd!="quit":

        # split the cmd into [event, param] tuple
        [event, param]=string.split(cmd, ",")

        lock.acquire()
        [EVENT(string.strip(event), [eval(string.strip(param))], lock)]
        lock.acquire()
        lock.release()

    # shutdown the simulator
    eventhandler.shutdown()

TRANSITION:
    S: A
    N: A
    E: eval
    O: [DUMP("result: "+str([PARAMS][0]))]

To compile this model into Python code, execute the following command:

scc -lpython --ext inter1.des
The generated Python source (inter1.py) can be executed with command:
python inter1.py
The execution result is as following, which is the same as the result shown in section 2.5.14:

CMD >  eval, 1+1
result: 2
CMD >  eval, "message"+": %s"%"Hello World"
result: message: Hello World
CMD >  eval, 2*3.1415927*0.5
result: 3.1415927
CMD >  quit

Similarly, the following is the command to synthesize C++ code from the same model:

scc -lcpp --ext inter1.des
And the following is the command to compile the generated C++ source code (inter1.cpp):
g++ -I/usr/include/python2.2 -L/usr/lib/python2.2/config -Xlinker -export-dynamic \
inter1.cpp -lpython2.2 -lm -ldl -lpthread -lutil -o inter1

The execution result of the binary is exactly the same as above.


next up previous contents
Next: 3.4 Reusing the Synthesized Up: 3. CODE SYNTHESIS WITH Previous: 3.2.4 C# code synthesis   Contents
Thomas Huining Feng
2004-04-05