Skip to main content

Loops Plugin

This Plugin implements functions for handling loops in your scheme.

The LoopsPlugin main actions, conditions, rules and API.

Terms

Loop

Allows you to re-execute parts of your scheme a certain number of times, according to the predetermined logic.

for loop: executes the loop body (a part of the scheme specified by the predetermined logic) while a certain boolean expression is true.

foreach loop: iterates over the elements of the list and executes the loop body (a part of the scheme specified by the predetermined logic) for each element.

In general, the both types of loops are similar; the main difference is their initialization only.

Initialization

Loop for initialization example

1

In this example, a for loop named "Loop" is created.

At each of the loop iterations, the start value (Start value: 1) increases (Step type: Increment) by the specified value (Step: 1) until the current value exceeds the end value (End value: 10).

This loop will be executed 10 times.

The counter will be set to the values: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.

The current value of the counter can be received from the "LoopCounterValue" variable.

The current state of the loop can be received from the "LoopState" variable.

Loop foreach initialization example

2

In this example, a foreach loop named "Loop" is created.

At each of the loop iterations, the next element will be taken from the list (Values: one, two, three, four, five).

This loop will be executed 5 times.

The counter will be set to the values: one, two, three, four, five.

The current value of the counter can be received from the "LoopCounterValue" variable.

The current state of the loop can be received from the "LoopState" variable.

Important notice

  • Each loop must have a unique name.
  • Using non-unique names will overwrite the results of the loop with the same name recorded earlier.
  • Using the same name for nested loops will lead to unpredictable behavior.

LoopStates

ValueDescription
DefaultIteration can be continued.
CompleteLoop is completed.
ContinueCall Continue operator.
BreakCall Break operator.

To call the Break or Continue operators, you can use special extensions of ProcessInstance, for example:

using OptimaJet.Workflow.Plugins.LoopsPlugin;

// Equivalent to calling the Continue operator
await processInstance.LoopContinueAsync("LoopName").ConfigureAwait(false);

// Equivalent to calling the Break operator
await processInstance.LoopBreakAsync("LoopName").ConfigureAwait(false);

Or:

using OptimaJet.Workflow.Plugins.LoopsPlugin;

// Get the current loop state
LoopState loopState = await processInstance.GetLoopStateAsync("LoopName")
.ConfigureAwait(false);

// Set the loop state
await processInstance.SetLoopStateAsync("LoopName", LoopState.Break)
.ConfigureAwait(false);

Or, using the name of the parameter specified in the form.

LoopCounter

An object of the class describing the loop counter.

Fields:

public class LoopCounter
{
// the index of the current iteration
public int Index { get; set; }
// the current value of the counter
public object Value { get; set; }
}

To get the current/next/previous value of the counter, you can use the extensions of ProcessInstance, for example:

using OptimaJet.Workflow.Plugins.LoopsPlugin;

// Get the current value of the counter
LoopCounter currentLoopCounter = await processInstance
.GetLoopCurrentCounterValueAsync("LoopName").ConfigureAwait(false);

// Get the previous value of the counter
LoopCounter prevLoopCounter = await processInstance
.GetLoopPrevCounterValueAsync("LoopName").ConfigureAwait(false);

// Get the next value of the counter
LoopCounter nextLoopCounter = await processInstance
.GetLoopNextCounterValueAsync("LoopName").ConfigureAwait(false);

Or, using the parameter name specified in the form.

CounterType

ValueDescription
IntLoop through integers.
DateTimeLoop through dates.

StepType

ValueDescription
IncrementAt each iteration, the step will be added to the current value.
DecrementAt each iteration, the step will be subtracted from the current value.

Installation and settings

Add the following namespaces to the section of the using statements:

using OptimaJet.Workflow.Plugins;

Create a plugin object:

var loopPlugin = new LoopPlugin();

Connect the plugin to WorkflowRuntime:

var runtime = new WorkflowRuntime();
...
runtime.WithPlugin(loopPlugin);

Cases

Simple loop

3

1: Activity before loop

Activity executed before the loop.

2, 6: Auto

Transition with condition: Auto.

3: StartLoop

Activity with action from the list:

At the first transition into the activity, the loop is initialized. At the next transitions, the loop counter is updated.

If re-entering after the end of the loop, the loop starts over again.

4: Loop is not completed and broken

Transition with condition: LoopIsNotCompletedAndBroken.

The transition occurs if the loop is not completed or is interrupted.

5: Activity in loop

Activity executed in each iteration of the loop.

7: Otherwise

Transition with condition: Otherwise.

The transition occurs if the loop is completed (completely done or broken).

8: Activity after loop

Activity executed after the loop.

Loop with break and continue

4

1: Activity before loop

Activity executed before the loop.

2, 10: Auto

Transition with condition: Auto.

3: StartLoop

Activity with action from the list:

At the first transition into the activity, the loop is initialized. At the next transitions, the loop counter is updated.

If re-entering after the end of the loop, the loop starts over again.

4: Loop is not completed and broken

Transition with condition: LoopIsNotCompletedAndBroken.

The transition occurs if the loop is not completed or is interrupted.

5: Activity in loop

Activity executed in each iteration of the loop.

6: Otherwise

Transition with condition: Otherwise.

The transition occurs if the loop is completed (completely done or broken).

7: Your condition

Transition with conditions to interrupt the loop or to skip an iteration.

8: Activity if loop is default

Activity executed if the conditions specified in Your condition are not met.

9: Set loop state

Activity with action: SetLoopState when Continue or Break is executed in the loop.

11: Loop is broken

Transition with condition: LoopIsBroken.

The jump transition if the loop is not completed or is interrupted.

12: Activity after loop breaking

Activity executed after breaking the loop.

13: Loop is completed

Jump with condition: LoopIsCompleted.

The transition occurs if the loop is completed.

14: Activity after loop completion

Activity executed after the successful completion of the loop.