Sometimes just once is not enough! For example, when you read a number of different files, replicate the tasks with different parameter values, or perform the same actions on different groups of data you’ll need to execute the same tasks multiple times. However, most probably you can automate the process by building a loop around the task.
Switches automate the execution of alternative tasks by steering the data flow into one of many parallel branches.
This lesson includes exercises. The corresponding data files, solution workflows, and prebuilt, empty exercise workflows with instructions are available in the L2-DS KNIME Analytics Platform for Data Scientists - Advanced folder in the E-Learning repository on the KNIME Hub.
Introduction to Loops
Let’s start with the concept of a loop and its building blocks: loop start, loop body, and loop end. We’ll also show two very basic loops: the counting loop and the generic loop.
Building a Loop
A common way to collect the results from the loop iterations is just to concatenate them. Other options are, for example, joining the results columnwise, or collecting results as flow variables from each iteration. A number of loop specific commands are also available to check the number of iterations, control the progress of the loop, execute the loop stepwise, and stop the execution at an intermediate step.
Other Examples of Loops
The following videos show you more examples of loops: group and chunk loops that handle a subset of rows in each iteration, and a recursive loop that processes the output of the previous iteration.
Reference workflows are available in the Examples/06_Control_Structures/04_Loops on the KNIME Hub.
In this exercise you’ll replace alphabet characters with umlaute (ä, ö and ü), which are some special non ASCII characters specific to the German language, with their ASCII representation in this German sentence:
"Ich wünsche Dir schöne Ferien in Ägypten." (= I hope you have nice holidays in Egypt.)
- Build a recursive loop using the recursive loop nodes with ports for two updated tables
- In each iteration, the loop body should extract one row from the ASCII representation table with the Row Splitter node
- Each iteration should then convert the “umlaut” and the ASCII representation into Flow Variables with the Table Row to Variable node, and use them in the String Manipulation node to make the replacement
- The loop is supposed to stop if the lookup table has less than one row. Therefore, the stopping criterion in the configuration dialog of the loop end node should be set to “minimal number of rows” being equal to 1.
Empty exercise workflow 05_Recursive_Loop in the KNIME Hub course repository.
Connect the table containing the German sentence to the top input port and the ASCII representation table to the bottom input port of the Recursive Loop Start (2 Ports) node.
Use the Row Splitter node to extract the first row from the bottom input of the loop start node (the ASCII representation table). Convert this first row into flow variables with the Table Row to Variable node.
Connect the top input of the loop start node (the German sentence), and the flow variable output of the Table Row to Variable node to a String Manipulation node. Use the following expression in its configuration:
Connect the output of the String Manipulation node to the two first inputs of the Recursive Loop End (2 Ports) node. Connect the bottom output of the Row Splitter node to the bottom input of the loop end node.
Solution workflow 05_Recursive_Loop - Solution in the KNIME Hub course repository.
You’d like to write data in different file formats, but not all at the same time? You’d like to show your data in a scatter plot or a bar chart, but not always both? Or you’d like to take some actions only if a certain threshold is met? In situations like these, you can build the alternative tasks in parallel branches, and control their execution with a switch node.
Reference workflows are available in the Examples/06_Control_Structures/05_Switches repository on the KNIME Hub.