Flow Control
In this tutorial, we’ll go into how App’s run in Flojoy and some of the tools offered.
Block execution order
Most use cases of Flojoy will require multiple Blocks. Therefore, it’s important to know the order that the Blocks will run. In the case where one Block is connected to another, the Block on the left (with the output connected) will run first.
In this example, the BASIC_OSCILLATOR
Block will run first. Note that even if the LINE
Block is moved to the left of the other Block, it will still run second because the connections are the deciding factor. The LINE
plot requires the data from BASIC_OSCILLATOR
to run.
One debugging setting you can change is Block Delay (Settings -> Block Settings -> Block Delay). Change this to 1000 and each Block will be delayed 1 second after the last one. Then you can more clearly see the execution order (look at the top of the App). Let’s look at the default app and decide what order the Blocks will run in.
Obviously LINSPACE
will run first. Next, it’s not so obvious. The reality is that the ADD
block relies on the three Blocks before it. The order that these three parallel Blocks will run is random. Next the ADD
block will run followed by the Viz Blocks in a undetermined order.
Flow Control
There are various Blocks for flow control available. We’ll go through an example of these now. If you want to follow along please clear your canvas, add:
- 2x
RAND
LOOP
FEEDBACK
APPEND
SCATTER
HISTOGRAM
and lay them out as shown.
Change the following parameters: for both RAND
size = 1, FEEDBACK
referred_block = APPEND
, LOOP
num_loops = 100.
The app is:
- The first
RAND
Block generates a random scalar - The
LOOP
Block outputs this scalar to the top right connection and: - Runs the Blocks connected to the loop 100 times:
- Inside the loop, the
FEEDBACK
Block outputs what theAPPEND
block last output - The
RAND 1
Block generates a random scalar - The
APPEND
Block appends the top input to the end of the bottom- e.g. if 2 is input to the top and a list of [1, 5, 3] is input on the bottom, the result will be [1, 5, 3, 2]
- Inside the loop, the
The result is a list (AKA a Vector) of random values that grows by 1 after every loop.
Try it yourself to see it in action (try changing the number of loops).
A couple of things to note:
- Note that during the first loop,
APPEND
has not yet output anything. In this case,FEEDBACK
will instead pass through the input, unchanged, as its output. This is the reason for the firstRAND
Block. - If you set num_loops for
LOOP
to -1, the loop will continue indefinitely, unless cancelled.