branch (if / else-if / else)
Syntax
- if:
- condition: expression
- op1
- op2
- ...
- else-if:
- condition: expression
- op1
- ...
- else:
- op1
- ...
Description
The branch operation is a conditional control structure — analogous to if/else-if/else in other
programming languages. It evaluates a series of conditions in order and routes the incoming value through the
first matching branch's transform chain, producing the result of that chain as its output.
In simple cases, a conditional expression can be used as an alternative to branches.
Branches
A branch must contain at least one if block, and may optionally be followed by any number of
else-if blocks and at most one else block.
The branch transform heading itself isn't needed in script, and a chain of contiguous if/else-if/else
transforms is enough to infer the branch group.
Each if and else-if block must begin with a condition entry whose value is an
expression that evaluates to true or false. Any remaining entries in the block are a
chain of transform operations that will be executed if the condition matches.
The this identifier in these sub-operations holds the value passed into the branch from the previous step.
The else block has no condition — its operations run when no prior condition matched. If an else is included, it
must appear after an if or else-if, and only one else is permitted per branch.
Condition evaluation
Conditions are evaluated strictly in order. The first condition that evaluates to true determines which branch runs; subsequent conditions are not checked.
The this identifier is available in condition expressions and holds the incoming value at the point of
evaluation.
If any external variable referenced in a condition is UNAVAILABLE, that condition is skipped and evaluation continues to the next one. In any external variable referenced within the rest of the branch is UNAVAILABLE and the branch is not taken, it will not affect the rest of the variable.
When no branch matches
If no condition matches and there is no else block, the incoming value is passed through unchanged.
This also applies when an unavailable external variable prevents all conditions from being evaluated.
Branch chain output
The selected branch's sub-chain receives the same incoming value as input. If the sub-chain produces an unavailable result, the branch operation becomes unavailable.
Nested branches
A branch operation can be nested inside the sub-chain of another branch, allowing multi-level conditional
logic. Each nested branch operates independently on the value passed into it from the outer branch's chain.
Examples
- AdapterScript
variables:
execution:
- source: spindle-speed
- if:
- condition: this > 1000
- state:
- ACTIVE: true
- else-if:
- condition: this > 0
- state:
- READY: true
- else:
- state:
- STOPPED: true
In the above example, spindle-speed is an identifier from another part of the adapter script.
- AdapterScript
variables:
adjusted-temp:
- source: raw-temp
- if:
- condition: unit == "F"
- expression: (this - 32) * 5 / 9
- else-if:
- condition: unit == "K"
- expression: this - 273.15
In the above example, raw-temp and unit are identifiers from another part of the adapter script.
If unit is neither "F" nor "K", the raw temperature value passes through unchanged.
- AdapterScript
variables:
clamped:
- source: sensor
- if:
- condition: this > 100
- expression: 100
- else-if:
- condition: this < 0
- expression: 0
In the above example, values above 100 are clamped to 100 and values below 0 are clamped to 0. Values
within the range pass through unchanged since there is no else.
Nested branch example
- AdapterScript
variables:
trend:
- source: temperature
- if:
- condition: device-connected == false
- expression: "'OFFLINE'"
- else:
- rate-of-change
- if:
- condition: this > 2
- expression: "'RISING'"
- else-if:
- condition: this < -2
- expression: "'FALLING'"
- else:
- expression: "'STABLE'"
In the above example, temperature is an identifier from another part of the adapter script. The outer
branch handles the disconnected case first. When connected, rate-of-change computes the rate
of change of the temperature before the inner branch classifies the trend.