Conditional I/O

yggdrasil also supports conditional input/output logic within the YAML specification file to allow a model to send/receive output/input to/from different destinations/sources based on the value of an input/output variable. yggdrasil supports both basic logic (e.g. less than, equal to) and more complex methods for evaluating the state implemented via Python functions.

Conditional Output

In the example below, the process represented by model B changes depending on the value of the input so that we can represent the relationship as a piecewise set of two functions with each one being valid under different conditions. In this case modelB_function1 is only valid if the input is <=2 and modelB_function2 is only valid if the input is >2.

Model Code:

1def modelA_function(in_val):
2    out_val = in_val
3    print("modelA_function(%s) = %s" % (in_val, out_val))
4    return out_val
1def modelB_function1(in_val):
2    # Only valid if in_val <= 2
3    out_val = in_val**2
4    print("modelB_function1(%s) = %s" % (in_val, out_val))
5    return in_val, out_val
1def modelB_function2(in_val):
2    # Only valid if in_val > 2
3    out_val = 2 * in_val**2
4    print("modelB_function2(%s) = %s" % (in_val, out_val))
5    return in_val, out_val
6
7
8def condition_function2(in_val):
9    return (in_val > 2)

(Example in other languages)

To instruct yggdrasil to pass output from model A to the correct model B function under the required conditions, the YAML should contain a connection to both model B function inputs with the filter parameter added to both.

Model YAML:

 1models:
 2  - name: python_modelA
 3    language: python
 4    args: ./src/conditional_io_modelA.py
 5    function: modelA_function
 6
 7  - name: python_modelB1
 8    language: python
 9    args: ./src/conditional_io_modelB1.py
10    function: modelB_function1
11
12  - name: python_modelB2
13    language: python
14    args: ./src/conditional_io_modelB2.py
15    function: modelB_function2
16
17connections:
18  - input: ./Input/input.txt  # Connection between file and model A input
19    output: python_modelA:input
20    filetype: table
21  - input: python_modelA:output  # Connection between model A output & model B input
22    outputs:
23      - name: python_modelB1:input
24        filter:
25          statement: "%x% <= 2"
26      - name: python_modelB2:input
27        filter:
28          function: ./src/conditional_io_modelB2.py:condition_function2
29  - inputs:
30      - python_modelB1:output  # Connection between model B, function 1 output and file
31      - python_modelB2:output  # Connection between model B, function 2 output and file
32    output: ./output.txt
33    filetype: table
34    field_names: InputMass,Mass
35    field_units: g,g**2
36    format_str: "%.1lf\t%.1lf\n"

(Example in other languages)

filter values are maps with either statement or function parameters (see here) and any additional filter parameters (see here). statement filters are simple expressions of equality/inequality (in Python syntax) that reference the connection input as %x% (e.g. the filter for python_modelB1:input above). Alternatively, you can provide a value for the function parameter which encodes a filter using a Python function. function values should be of the form <filename>:<function name> where filename is the full path to the location of a Python source file containing the desired function that should be used to determine if the condition is satisfied and function name is the name of the desired function (e.g. the filter for python_modelB2:input above). Functions used in such cases should take a single argument (the variable or tuple of variables being passed by the connection), and return a boolean (the validity of the condition being represented). The path to the file containing the function can be absolute or relative to the directory containing the yaml file.

Filter Options

General Filter Options

Option

Type

Required

Description

filtertype

string

Pass every message (Options described here)

Filter Types

Filtertype

Description

direct

Pass every message

function

Filter messages based on a function

statement

Filter messages based on a statement

Type Specific Filter Options

Option

Type

Valid For ‘Filtertype’ Of

Description

function

function

[‘function’]

The handle for a callable Python object (e.g. function) that should be used to determine if a message should be filtered or a string of the form “<function file>:<function name>” identifying a function where “<function file>” is the module or Python file containing the function and “<function name>” is the name of the function. The function should take the message as input and return a boolean, True if the message should pass through the filter, False if it should not.

statement

string

[‘statement’]

Python statement in terms of the message as represented by the string “%x%” that should evaluate to a boolean, True if the message should pass through the filter, False if it should not. The statement should only use a limited set of builtins and the math library (See yggdrasil.tools.safe_eval). If more complex relationships are required, use the FunctionFilter class.