next up previous contents
Next: 3. CODE SYNTHESIS WITH Up: 2.5 More Examples Previous: 2.5.13 An example of   Contents


2.5.14 An example of model-specific interface

INTERACTOR is a special descriptor, which starts a piece of Python code to be executed in a thread separated from the thread of the SVM simulator. (If there are multiple INTERACTOR descriptors, the code under them is literally concatenated.

The code under the INTERACTOR descriptor is started after the initializer of the model is executed, but before the simulation of the model actually starts. This code should first initialize the user interface and then call the start method of the eventhandler object. This starts the simulation, and the model begins to accept events and produce outputs. The interactor does not exit after this, but it usually enters a loop. The code in this loop repeatedly accepts events from the user via the interface, and feeds those events to the model by calling the EVENT predefined macro or the event method of the eventhandler object.

The following example (inter1.des) demonstrates a textual model-specific interface:

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]))]

The simulation result is as following:

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

Here are the two points to be emphasized:


next up previous contents
Next: 3. CODE SYNTHESIS WITH Up: 2.5 More Examples Previous: 2.5.13 An example of   Contents
Thomas Huining Feng
2004-04-05