One of the foundations of having a complete programming language is the ability to write loops that have the potential to execute an unlimited number of times. Of course, any program in practice will need to end when certain conditions are met — normally when it has found the solution.
The general form of a loop looks something like below. We have a set of conditions (C1, C2, … Cn) and set of procedures (P1, P2,… Pn). When condition 1 is true we execute procedure 1.
We also have a set of conditions that determine when the computation is complete (CLoop).
Loop while (conditions CLoop are true)
If (Conditions C1 are true) then do P1
If (Conditions C2 are true) then do P2
. . .
If (Conditions Cn are true) then do Pn
End Loop
The simplest type of loop is to run an operation repetitively on a set of records. An example type of operation would be to send an email to every person in a contact list or to compute a value for every record in a list.
Batch updates can be executed in Ruly using a Query Update Operation.
Queries are quite flexible as complex filtering criteria can be defined, and tables can be joined with other tables, so there are virtually unlimited possibilities.
The operation below computes the years of experience and updates each employee’s record with the computed value.
To run the operation, use any one of these methods:
In the next example, we will compute the first 50 Fibonacci numbers. For those not familiar, in the Fibonacci sequence, each number in the sequence is equal to the sum of the 2 previous numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21,…
First, we will create a data table FibN to store the computed values that will also double as a loop manager. It has 3 fields
A business Rule and an Operation will be used to generate the next number in the sequence. Each time a record is created, the rule will call an Operation that will cause another record to be created.
This creates a loop.
The loop ends when the number of iterations has been reached (in this case 50 which is set in the Rule).
The calculation for the nth Fibonacci number Fn is shown below
Here is the complete list of Fibonacci numbers computed by the rule/op above.
They grow surprisingly quickly as you can see.
Let’s look at a more complex example: Consider a table of records where there is a parent/child relationship between records. A tree table. Let’s find the number of child nodes for each node in the tree.
How can this be done using a nocode platform like Ruly?
In this example we will find all of the child parts for the engine, and then determine the total value of the engine. Note that only the lowest leaf nodes in the tree have a value assigned to them.
A recursive rule will be used to find child nodes. A separate table (Car Parts Loop) that links to the Car Parts table is used to manage the process.
Each time a record is created in this table, the rule creates the direct descendants of that node. This process repeats until all nodes are found. A rule implemented in this way mimics a breadth-first search algorithm
Once all the child nodes are found, another rule is then used to sum the individual component costs and find the total value of the engine (or any other composite part).
Many types of problems can be solved quicker and easier using optimization techniques. In optimization, a known, yet not ideal solution is the starting point. The solution is adjusted iteratively until the resulting solution is in an ideal or close to ideal state.
A nocode loop can also be used to solve optimization problems.
A simple application of this technique extends the example above with the Fibonacci numbers. We will compute the ratio of two terms in this sequence to a specific number of decimal places. This number, known as the Golden Ratio has been used in architecture and art since ancient times. It is a rectangle with sides in the approximate ratio of 1.6 to 1.
The same rule from the Fibonacci example is modified to include an accuracy filter. Once the Delta value is within 5 decimal places the loop will stop.
Running the loop now computes the ratio and delta, and then stops as soon as the result is within 5 places. The results are shown below:
After 17 runs the ratio is calculated to 1.61803 rounded to 5 decimal places.
I hope these examples show, that even using a nocode platform, it is possible to do many of the things that would normally require coding expertise. Please contact me at stan.marsden@rulyapp.com if you have any comments or questions.