While steps
The while step in Gloop is very similar to the while
statement in Java and
other programming languages (but with extra features). Like in Java, when the execution of its children (block) has
completed, the while step will run them all over again as long as its condition is met.
While steps in Gloop rely on a combination of the Expression
and While On Error
properties to determine whether to run again or not. When the while step needs to determine whether it's going to
run again, it will evaluate the Expression
property if the chosen language is not plain text, or use the literal
value of the Expression
property (if it is plain text) and do the following:
- If the result of the expression is a
Number
(primitive or not) or aString
with a numeric value, it will check if the number is less than or equal to the number of times the while step has executed. If it is, it will run again. This means that you can write anExpression
of10
and the while step will run 10 times. - If the result of the expression is a
boolean
orBoolean
, it will use its value to determine whether to run again. - If the result of the expression is a
String
which isn't numeric, it will check if the value of theString
istrue
(case insensitive). - Any other types of results from the
Expression
will result in an exception being thrown.
The while step will introduce two variables to Gloop while it's being executed:
$gloopIndex
- a zero-based counter, which increments at the end of the run$gloopCount
- a one-based counter, which also increments at the end of the run
While text
If your while step only has a plain number as the Expression
, its text in the
service editor will appear as While $gloopIndex < <expressionValue>
; this makes your
Gloop code much easier to read. Otherwise, it will appear as While <expressionValue>
, as shown below:
While there's an error
While steps have a unique property called While On Error
. If this is set to true
, the while step will run again
only if the Expression
condition is met and an exception was thrown whilst invoking its child steps. This is
useful for retrying an operation that sometimes may not work; for example, trying to connect to remote servers or
URLs. When this is true
, exceptions are ignored and the while step will start over. If While On Error
is false
and
an exception is thrown, the while step will not catch it and the Gloop engine will throw it back to Martini. For
instances like this, you can catch the error with a parent block step with a catch
or a parent
while step whose While On Error
is true
instead.
Adding elements to an array
While steps also have an Output Array
property (same as the iterate step). This makes it
easy to map or transform data under the while step and have it added to an array or cursor.
The array that the step is adding to will appear as a single item in the Mapper view when you're in the context of
the while step. Look closely at the Mapper view below to see how the while step changes what's available in the
Mapper. You will notice the $gloopIndex
and $gloopCount
variables being added, and the output array (which in
this example is called myArray
) changes from an array to a single item (the array brackets badge on the
bottom-right doesn't appear anymore) when the Mapper is underneath the while step.
Other features
While steps can also be configured to delay at the end of an iteration (useful in combination with the While
On Error
property when retrying things) and can close your Gloop cursors after finishing if you choose
to do so (by setting Close Output Cursor
to true
).