15.3. Usage

15.3.1. Python Executable

There are two options for using Python as script language, the (old) "Python" and the "Python (Python Agent)" option.

If the "Python" option was chosen, the path to the Python executable must be chosen at workflow start (in the second page of the workflow executiondialog). This must be done for every Script component of the workflow. If all components shall use the same Python interpreter, the “Apply to all” button helps.

"Python (Python Agent)" is a new experimental implementation for using Python as script language that aims to improve on the (old) "Python" option and will replace it in the future. If you want to use the Python Agent, the path to the Python executable must be configured in RCE configuration file. For further information please see chapter 2.2 of the RCE User Guide.

15.3.2. Script API

For interacting with RCE in the script execution, there is an API. All methods of this API are listed here .

How to use the script API:

Define your inputs and outputs in the Inputs/Outputs tab of the Properties view (appearing for the selected Script component on double click). Write your script in the Script tab (in the same Properties view). You can either do it in the text box or in a separate text editor by clicking on the button “Open in Editor”. For interacting with RCE from a script, there is a module called "RCE". To get an overview of all RCE API methods, look at the script API detailed description The most important methods there are reading inputs and writing outputs. For reading an input, call the method

RCE.read_input(String input_name)

or

RCE.read_input(String input_name, default value)

You can write outputs within your script with

RCE.write_output(String output_name, OutputDataType
				value)

Thereby, the type (OutputDataType) of the value must fit the data type of the output (as defined in the tab Inputs/Outputs). File and Directory are represented by the absolute file paths.

Note

The module RCE uses is already imported in the script during execution.

Examples:

If you like to double an incoming value (x is an input of type Integer and y an output of type Integer):

RCE.write_output("y", RCE.read_input("x")*2)

If you like to access an incoming file (f_in is an input of type File):

file = open(RCE.read_input("f_in"),"r")

If you like to send a file to an output (f_out is an output of type File):

absolute_file_path = /home/user_1/my_file.txt
				RCE.write_output("f_out", absolute_file_path)

If an output is not needed any more (e.g. you want to end an inner loop), you can close an output using the command:

RCE.close_output(String output_name)

Example:

RCE.close_output(“y”)

The following components will get the finished signal.

If a script fails because of some invalid parameters sent by a Parametric Study or Optimizer component, you can send a "not a value" signal to your output(s). This signal indicates that the script failed because of invalid parameters and did not fail at all. This signal is ignored by most of the components, only the Parametric Study and the Optimizer component handle this signal. For sending it, use

RCE.write_not_a_value_output(String outputname)

For the other API methods refer to the example workflow "Script_with_all_API_methods.wf" from the workflow examples project or to the script reference found below and in the user guide.

15.3.3. Script component states

The Script component is able to keep its state from one run to another. Use the API to write and read state variables. The values are stored in a Python dictionary. They must be compatible with the RCE data types. Script components of nested loops are reset if the nested loop has been terminated. Resetting a script component in a nested loop also resets its state map.

15.3.4. Input File Factory

The Input File Factory is an extension of the Script API that aims to write Python input parameter files during a workflow run. To call the Input File Factory the user must first create a file using the command file = RCE.create_input_file(). Afterwards the user can add variable declarations, comments or Python dictionaries by calling the previously created file (e.g. file.add_variable(name,value)). Finally,the stored data must be written to the file system by executing the command write_to_file(filename), whereat the name of the file is the given filename.

Note

It is not allowed to enter a relative or absolute path for filename.

When the Input File Factory is called from an integrated tool, the file is written to the working directorie's Input directory or to the tool directory depending on the user configuration. When a script component calls the factory, the file is written to the temp directory. Use the RCE_write_output command to forward the file in a workflow.

Example Script:

Assume that the Script Component receives an input called "float" with value 1.0.

f = RCE.read_input("float")							// read input

input_file = RCE.create_input_file()						// create an empty input file 
					
input_file.add_comment("This is an example input file")			// add comment
input_file.add_variable("float",f)						// add float variable 
input_file.add_dictionary("exampleDict")					// create empty dictionary
input_file.add_value_to_dictionary("exampleDict","key1","value1")	// add (key,value) pair to dictionary
input_file.add_value_to_dictionary("exampleDict","key2","value2") 
					
file = input_file.write_to_file("example.py")			    // write input file to data management

To foward the file in a workflow to an output called "file", use:

RCE.write_output("file", file)

The written file looks like this:

# This is an example input file
float = 1.0
exampleDict = {'key1': 'value1', 'key2': 'value2'}