This guide provides an overview and brief instructions on how to use Condition blocks to switch workflow branches depending on configurable conditions.
With the Condition block, you can:
- Validate input parameters. For example, make sure that the user-specified parameter values are within the valid range.
- Control the computation order. For example, switch between workflow branches that implement the computation in different ways.
- Make decisions based on intermediate results. For example, stop or continue the calculation cycle depending upon the value of a certain variable.
These capabilities are provided by switching between output port groups depending upon the conditions imposed on the input data. The block matches the input data with the conditions specified in it and, if the data meets any of the conditions, outputs the data to the corresponding port group. Each condition has its own output port group. Data that does not meet any of the specified conditions is output to a separate default port group.
Setting up the input ports¶
Data arrives through the input ports of the block. The block starts running only if there is data on each input port. Otherwise, the block does not output any data.
Drag the block onto the workflow design surface, and then double-click it to open the configuration dialog. Initially, the block has no ports. When configuring the block, you first need to add ports to receive input data.
In the configuration dialog, click the button next to the Ports field to add the desired ports. Enter a suitable name for each port. This name identifies the input port, its associated variable in the condition expressions, and the corresponding output port in each output port group.
After adding ports, you can proceed to setting up conditions.
Setting up conditions¶
Conditions are built upon logical expressions that can include pieces of
input data. Expressions use double-quoted input port names as variables
representing input data. The input data meets the condition if the
condition expression evaluates to
To add conditions, in the configuration dialog, click the button in the Conditions pane. Enter a suitable name for each condition. This name also identifies the output port group for the corresponding condition.
When the block is running, its conditions are checked from top to bottom
in the list. Input data is output to the port group of the first
condition that it matches. The output port group of the last condition
in the list is used to output data that does not match any of the
specified conditions. This condition is named
else by default, has no
configurable expression, and can be considered the negation of all
conditions that precede it in the list.
You can change the order of checking conditions by moving them up and
down the list. To do this, use the
buttons in the
Conditions pane. The
else condition that ends the list cannot be
Condition expressions allow the use of basic comparison operators, such
as "equals" (
"a"=="b"), "less than" (
"a"<"b"), "greater or equal"
"a">="b") and so on, as well as the logical
Arithmetic operators are also allowed. The
in operator can be used to
check if a value is contained in a certain array (
"v" in "arr") or in
a certain string as a substring (
"v" in 'some long string').
Parentheses allow you to control the order in which the expression is
When composing expressions, observe the following basic rules:
- Each variable name must be enclosed in double quotes. For example, the
"x"<0checks the value of the input port named
x. The variable name representing the port value is enclosed in double quotes:
If the variable value is an array, the element index must be outside the double quotes surrounding the name of the variable, for example
Array indexing is zero-based, so for example
"result"is the third element of the value received to the
If the variable value is a dictionary, the item key must be outside the double quotes, for example
For more on composing expressions, see Condition expressions.
Reviewing output port groups¶
The number of ports in each output port group is the same as the number of the block input ports. Each output port group is associated with one of the block conditions. If the input port values meet a certain condition, they are sent to the output ports of the group associated with that condition.
The identifier of the output port group is the name of its condition.
The name of each output port in the group is composed of the condition
name and the name of the respective input port. The dot is used as a
For example, a Condition block with two input ports
x2 and two
case A and
case B will have the following output ports:
Assuming that in the block configuration dialog the conditions are
placed in the above order (first
case A, then
case B), this
configuration works as follows:
- If the values of
x2meet the condition
case A, they are output to
case A.x2, respectively, and the block finishes.
- Otherwise, the block continues to check the condition
case B. If the input values meet this condition, they are output to
case B.x2, and the block finishes.
- If the input values do not meet any of the above conditions, they are
Conditions are defined by logical expressions that evaluate to
False. A condition is deemed met if its expression evaluates to
Condition expressions must conform to the basic Python syntax for
expressions and may contain variables, literals, and operators.
Names of the variables in the condition expression are the names of the input ports. When evaluating an expression, each variable is replaced with the value of the corresponding port.
Strings and numbers can be used as literals. A literal string must be
enclosed in single quotes (
'string') - you cannot use double quotes
as a double-quoted string is considered a variable identifier.
Compound values, such as lists, matrices, and dictionaries, can be
passed to expression variables through block input ports. The creation
of such values within an expression is not allowed. For instance, a list
[1, 2, 3]) in an expression will cause an error when
evaluating the expression.
The expression is successfully evaluated when the values of expected
data types are substituted into it from the input ports. If the port
value does not match the expected data type of the respective variable,
evaluation of the expression may fail. For instance, a numeric value of
"x" results in an error when evaluating the expression
"x" is expected to be a list.
The following operators are allowed in condition expressions:
==- equal to, not to be confused with the
=assignment operator; when used with strings, the comparison is case-sensitive
<- less than
>- greater than
<=- less than or equal to
>=- greater than or equal to
%- division modulo (remainder of division)
Trueif both operands are
Trueif either operand is
Trueif the operand is
Falseif the operand is
Membership test operator
Trueif the left operand is contained in the right one; with strings, works as a substring search.
Some typical condition expressions are:
"x">=0 and "x"<=1- check if the numeric value of the
xport is in the range from 0 to 1.
"r0"<"r1" and "r1"<"r2" and "r2"<"r3"- check if the numeric values of the ports
r3are strictly in ascending order.
"f_error"/"f"<0.05- assuming that values of the
fports are numeric, check that the
fratio is less than
"func"<"func"- assuming the value of the
funcport is a list of numbers, check if the first number in the list is less than the second number (note that indexing is zero-based).
"preset"=='Mode N'- check if the value of the
presetport is the string
'Mode N'. Such a condition can be used, for example, to switch between workflow branches depending upon values of a certain port. String comparisons are case-sensitive:
'mode n'is not equal to
"name" in 'universe'- check if the string value of the
nameport is a substring of
'universe'. For example, this expression evaluates to
Trueif the value of the
'ver'. Such tests are also case-sensitive:
'I'is not in
"dict"['preset']=='default'- assuming the value of the
dictport is a dictionary, check if the value under the
'preset'key is the string