next up previous contents
Next: 4. SVM INTEGRATED WITH Up: 3. CODE SYNTHESIS WITH Previous: 3.3 An example of   Contents

3.4 Reusing the Synthesized Code

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.des
However, 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:

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.


next up previous contents
Next: 4. SVM INTEGRATED WITH Up: 3. CODE SYNTHESIS WITH Previous: 3.3 An example of   Contents
Thomas Huining Feng
2004-04-05