2013-10-21

Getting Started with Tcl in FME: Scripted Parameter

Previous: Other Transformers

Parameter values in a workspace are static, we cannot change them while translating. But before starting translation, we can define a parameter according to the conditions. That is the Scripted (Tcl) Parameter, the steps are:
1. Right-click "User Parameters > Private Parameters" node in the Navigator window, select "Add Parameter" to open "Add/Edit Parameter" dialog box.
2. Define Type, Name and Value of the parameter:
- Select "Scripted (Tcl)" for "Type".
- Specify the parameter name to "Name".
- Set a Tcl script to "Value".

Note: Scripted (Python) Parameter can be also defined with the similar steps just replacing "Tcl" with "Python" in the step 2.
> FME Workbench > Creating a User Parameter > Type: Scripted (Python) and Scripted (Tcl)

Example: URL of a file in a secured FTP server
FME readers can read files saved in a secured FTP server via this format URL as the source dataset setting.
ftp://<user name>:<password>@<server name>/<directory path>/<file name>
> Access a Secured FTP Site and Download the Dataset

The URL is constructed of 5 elements, i.e. user name, password, server name, directory path and file name.
If all the elements are known and also they are static, the URL can be set directly to the source dataset parameter of the reader. But if one or more elements of them will have to be determined at each run-time of the workspace, parameterizing them will be effective. And also the the scripted parameter could be useful in such a case.

Assuming that these 5 published parameters have been defined:
USERNAME: user name
PASSWORD: password
FTP_SERVER: FTP server name
FTP_DIRECTORY: directory path
FILENAME: target file name

set this script to "Value" of the Scripted (Tcl) Parameter:
-----
set usr $FME_MacroValues(USERNAME)
set pswd $FME_MacroValues(PASSWORD)
set svr $FME_MacroValues(FTP_SERVER)
set dir $FME_MacroValues(FTP_DIRECTORY)
set file $FME_MacroValues(FILENAME)
return "ftp://$usr:$pswd@$svr/$dir/$file"
-----

"FME_MacroValues" is a dictionary containing macro (parameter) values, we can get other parameter values (defined before this parameter) via this dictionary in the script.
The returned value of the script will be the value of the parameter; the dataset parameter of a reader can be linked to the scripted parameter.

For example, when USERNAME = "MyName", PASSWORD = "MyPassword", FTP_SERVER = "MyFtpServer", FTP_DIRECTORY = "MyRoot/MyFolder" and FILENAME = "*.xml", the reader whose dataset parameter is linked to this scripted parameter will read XML file(s) from this URL:
ftp://MyName:MyPassword@MyFtpServer/MyRoot/MyFolder/*.xml

I think the scripted parameter like the example above is useful, but in this case, this reader dataset parameter setting also works naturally.
ftp://$(USERNAME):$(PASSWORD)@$(FTP_SERVER)/$(FTP_DIRECTORY)/$(FILENAME)

There is more than one way to skin a cat. I often come across a "cat" when working with FME. Although I won't kill a real cat...

Next: Startup / Shutdown Script

No comments:

Post a Comment