| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using Elsa.Extensions; |
| | | 3 | | using Elsa.Workflows.Attributes; |
| | | 4 | | using Elsa.Workflows.Behaviors; |
| | | 5 | | using Elsa.Workflows.Memory; |
| | | 6 | | using Elsa.Workflows.Models; |
| | | 7 | | using JetBrains.Annotations; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows.Activities; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Iterate over a sequence of steps between a start and an end number. |
| | | 13 | | /// </summary> |
| | | 14 | | [Activity("Elsa", "Looping", "Iterate over a sequence of steps between a start and an end number.")] |
| | | 15 | | [PublicAPI] |
| | | 16 | | public class For : Activity |
| | | 17 | | { |
| | | 18 | | private const string CurrentStepProperty = "CurrentStep"; |
| | | 19 | | |
| | | 20 | | /// <inheritdoc /> |
| | 50 | 21 | | public For([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) |
| | | 22 | | { |
| | 50 | 23 | | Behaviors.Add<BreakBehavior>(this); |
| | 50 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | 17 | 27 | | public For(int start, int end, int step, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null |
| | | 28 | | { |
| | 17 | 29 | | Start = new(start); |
| | 17 | 30 | | End = new(end); |
| | 17 | 31 | | Step = new(step); |
| | 17 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// The start step. |
| | | 36 | | /// </summary> |
| | | 37 | | [Input(Description = "The start step.")] |
| | 235 | 38 | | public Input<int> Start { get; set; } = new(0); |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// The end step. |
| | | 42 | | /// </summary> |
| | | 43 | | [Input(Description = "The end step.")] |
| | 234 | 44 | | public Input<int> End { get; set; } = new(0); |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// The step size. To count down, enter a negative number. |
| | | 48 | | /// </summary> |
| | | 49 | | [Input(Description = "The step size. To count down, enter a negative number.")] |
| | 233 | 50 | | public Input<int> Step { get; set; } = new(1); |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Controls whether the end step is upper/lowerbound inclusive or exclusive. True (inclusive) by default. |
| | | 54 | | /// </summary> |
| | | 55 | | [Input(Description = "Controls whether the end step is upper/lowerbound inclusive or exclusive. True (inclusive) by |
| | 208 | 56 | | public Input<bool> OuterBoundInclusive { get; set; } = new(true); |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// The activity to execute for each iteration. |
| | | 60 | | /// </summary> |
| | | 61 | | [Port] |
| | 181 | 62 | | public IActivity? Body { get; set; } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Stores the current value for each iteration. |
| | | 66 | | /// </summary> |
| | | 67 | | [Output] |
| | 120 | 68 | | public Output<object?> CurrentValue { get; set; } = new(); |
| | | 69 | | |
| | | 70 | | /// <inheritdoc /> |
| | | 71 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 72 | | { |
| | 44 | 73 | | var iterateNode = Body; |
| | | 74 | | |
| | 44 | 75 | | if (iterateNode == null) |
| | 1 | 76 | | return; |
| | | 77 | | |
| | 43 | 78 | | await HandleIteration(context); |
| | 44 | 79 | | } |
| | | 80 | | |
| | | 81 | | private async ValueTask HandleIteration(ActivityExecutionContext context) |
| | | 82 | | { |
| | 43 | 83 | | var iterateNode = Body; |
| | 43 | 84 | | var end = context.Get(End); |
| | 43 | 85 | | var currentValue = context.GetProperty<int?>(CurrentStepProperty); |
| | 43 | 86 | | var start = context.Get(Start); |
| | 43 | 87 | | var step = context.Get(Step); |
| | 43 | 88 | | var inclusive = context.Get(OuterBoundInclusive); |
| | 43 | 89 | | var increment = step >= 0; |
| | | 90 | | |
| | 43 | 91 | | currentValue = currentValue == null ? start : currentValue + step; |
| | | 92 | | |
| | 43 | 93 | | var isBreaking = context.GetIsBreaking(); |
| | | 94 | | |
| | 43 | 95 | | var loop = |
| | 43 | 96 | | !isBreaking && (increment && inclusive ? currentValue <= end |
| | 43 | 97 | | : increment && !inclusive ? currentValue < end |
| | 43 | 98 | | : !increment && inclusive ? currentValue >= end |
| | 43 | 99 | | : !increment && !inclusive && currentValue > end); |
| | | 100 | | |
| | 43 | 101 | | if (loop) |
| | | 102 | | { |
| | 23 | 103 | | if (iterateNode != null) |
| | | 104 | | { |
| | 23 | 105 | | var variables = new[] |
| | 23 | 106 | | { |
| | 23 | 107 | | new Variable("CurrentValue", currentValue) |
| | 23 | 108 | | }; |
| | 23 | 109 | | await context.ScheduleActivityAsync(iterateNode, OnChildComplete, variables: variables); |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | |
| | | 113 | | // Update internal step. |
| | 23 | 114 | | context.SetProperty(CurrentStepProperty, currentValue); |
| | | 115 | | |
| | | 116 | | // Update loop variable. |
| | 23 | 117 | | context.Set(CurrentValue, currentValue); |
| | | 118 | | } |
| | | 119 | | else |
| | | 120 | | { |
| | | 121 | | // Report activity completion. |
| | 20 | 122 | | await context.CompleteActivityAsync(); |
| | | 123 | | } |
| | 43 | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | private async ValueTask OnChildComplete(ActivityCompletedContext context) => await HandleIteration(context.TargetCon |
| | | 127 | | } |