FME_BEGIN_TCL and FME_END_TCL

Translation Begin/End Hooks

When the FME translation engine is incorporated into larger systems, it may be useful to have FME perform some additional site-specific custom processing either before or after each translation.

Users can specify multiple FME_BEGIN_TCL and FME_END_TCL directives. Errors encountered during the execution of the begin/end Tcl scripts will fail the translation.

The FME_BEGIN_TCL and FME_END_TCL mapping file directives specify Tcl scripts to be run either before or after (respectively) the actual translation. The FME_END_TCL script has access to a number of variables which contain the statistics and status of the translation, and as such can be used to write customized logs or insert records into a database (perhaps using the TclODBC or Oratcl Tcl extensions - see examples 6 and 7 in Tcl Examples, respectively) to make a record of the translation’s activity.

The FME_END_TCL script can also read through the log file produced by FME to extract certain messages and record those results as well.

The FME_BEGIN_TCL and FME_END_TCL scripts will share a Tcl interpreter, which means that the FME_BEGIN_TCL script can set up some Tcl global variables that will later be available to the FME_END_TCL script. However, in most scenarios it is likely that only an FME_END_TCL script will be needed. Lastly, the FME_BEGIN_TCL and FME_END_TCL interpreter is completely separate from any other Tcl interpreters used in FME; for example, the @TCL and @Tcl2 FME functions do not share this interpreter.

The scripts to be executed for FME_BEGIN_TCL or FME_END_TCL can be specified right in the mapping file, or they can be “source’d” in from an external file. If they are specified in the mapping file, then any quotation marks must be escaped with a \ and continuation characters must be used to join all the lines in the script together into one logical FME mapping file line. This also then requires that the ; statement separator be used in the Tcl code.

Alternately, if the Tcl script is placed in an external file and source’d in, then native Tcl quoting and formatting may be used.  For example:

    FME_END_TCL proc finalize {} { \
           ... Tcl code here ...   \
       };                          \
       finalize;
        or
         
    FME_END_TCL source $(FME_MF_DIR_UNIX)/finalization.tcl

Note that if the script contains a procedure definition, the script must call the procedure it defined, otherwise the procedure itself will not be executed and no processing will occur.

If any errors are encountered during the processing of either the FME_BEGIN_TCL or FME_END_TCL script, the errors are written into the log file. If there is no log file, the errors will not be reported. Any errors raised by the Tcl script will be up propagated to the FME translator and will cause the translation to terminate with an error code, so that external callers of the FME translator can take appropriate action.

These hooks are not available within the definition of a custom format. They can, of course, be used in a mapping file or workspace that itself employs a custom format.

The FME Objects Tcl interface may not be used within the either of the FME_BEGIN_TCL or FME_END_TCL scripts.  This is because using FME Objects at the translation start/end times would cause a configuration conflict within the FME translation system.  However, the FME_BEGIN_TCL/FME_END_TCL scripts can themselves start new processes via the “exec” Tcl command, and these new processes could involve FME Objects Tcl without any conflict.