Loop Design Pattern

Credits: Mark Dobbie and Jonas Zahn

There are two ways to implement loops within services. One method is to write it entirely within Javascript, the other is to do it diagrammatically.

Loop

The preceding image illustrates a loop implemented diagrammatically. Some of these steps actually use small snippets of Javascript, but these are very simple constructs that business users can easily understand. The loop requires a counter to indicate the number of times the loop has been iterated. An Integer may be used for this purpose and is set to zero during the initialisation of the loop. Any other variable initialisation may also be performed during this step. For example:
tw.local.count = 0;
tw.local.policyDebt = 0;

Loops are typically used to iterate over a list variable. The list may be empty, so the loop exit criteria must be checked before looping commences. The decision gateway Yes path will require a condition to be specified. For example:
tw.local.count < tw.local.claims.listLength

Some action will be performed each time round the loop. Typically, this may be a Service, a Server Script or a combination. The list variable and counter may be used together to identify the next item within the list. This may be mapped to either an input or an output parameter of a service, or directly within a Server Script. For example:
tw.local.policyDebt += tw.local.claims[tw.local.count].debt;

Before re-entering the decision gateway the counter is incremented. This either locates the next item within the list or triggers the exit condition. For example:
tw.local.count++;

The following code segment illustrates a loop implemented entirely with Javascript:
tw.local.policyDebt = 0;
for(varcount = 0; count

The advantages of using the Javascript approach are:

  • Performance improvement. There is a minimal run-time performance overhead when using the diagrammatic approach.
  • The speed of development. An experienced Javascript programmer may prefer this approach as it is quicker to implement.

The advantages of using the diagrammatic approach are:

  • Easy for business users to understand. i.e. They don't have to become a skilled Javascript programmer.
  • Easy to debug. The process inspector allows BPM developers to step through each step within the loop and examine variables. Conversely, Server Scripts are executed atomically, so it is not possible to step through each step within a loop written entirely within Javascript.
  • Avoids infinite loops. It is possible to define a loop either using Javascript or diagrammatically where the exit criteria is never satisfied. i.e. it loops forever. IBM BPM provides a facility to detect infinite loops. With diagrammatic loops it is possible for IBM BPM to stop services running infinite loops by using the admin console, whereas the only way to stop infinite loops written entirely in Javascript is to restart the process server.
  • Allows nested services to be used within the loop. This is also technically possible within Javascript but it has its drawbacks - the syntax it is not business user friendly and services invoked using Javascript cannot contain coaches.

Additional Notes

If you run a simple test you may see exponential performance impact as you increase the number of loops. However, this is likely due to the structure of your test.

When you add even a small operation, say a 20ms unit of work to be performed inside the illustrated test with a single loop of size 100, the results are no longer different between scripting and diagramming your loop. You'd have 210s (canvas) vs. 200.055s (script). For larger units of work, the difference approaches zero and becomes negligible.

Unless the unit of work inside the loop is insignificant (less than 10ms) and the size of the loop is significant (larger than 100) the performance difference between diagrammatic and script is negligible. On the other hand, there will always be the JS sync issue in anything prior to TW7.

The only time a script loop makes any sense is for very small loop size and/or very small unit of work contained inside the loop.

Join The Discussion

Your email address will not be published. Required fields are marked *