| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | using System.ComponentModel; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Workflows.Attributes; |
| | | 6 | | using Elsa.Workflows.Signals; |
| | | 7 | | using Elsa.Workflows.UIHints; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Workflows.Activities; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Branch execution into multiple branches. |
| | | 14 | | /// </summary> |
| | | 15 | | [Activity("Elsa", "Control Flow", "Branch execution into multiple branches.")] |
| | | 16 | | [PublicAPI] |
| | | 17 | | [Browsable(false)] |
| | | 18 | | public class Fork : Activity |
| | | 19 | | { |
| | | 20 | | /// <inheritdoc /> |
| | 4 | 21 | | public Fork([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) |
| | | 22 | | { |
| | | 23 | | // Handle break signals directly instead of using the BreakBehavior. The behavior stops propagation of the signa |
| | 4 | 24 | | OnSignalReceived<BreakSignal>(OnBreakSignalReceived); |
| | 4 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Controls when this activity yields control back to its parent activity. |
| | | 29 | | /// </summary> |
| | | 30 | | [Input( |
| | | 31 | | Description = "Controls when this activity yields control back to its parent activity.", |
| | | 32 | | UIHint = InputUIHints.DropDown |
| | | 33 | | )] |
| | 10 | 34 | | public ForkJoinMode JoinMode { get; set; } = ForkJoinMode.WaitAll; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// The branches to schedule. |
| | | 38 | | /// </summary> |
| | 18 | 39 | | public ICollection<IActivity> Branches { get; set; } = new List<IActivity>(); |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | | 42 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 43 | | { |
| | | 44 | | // If there are no branches, complete immediately |
| | 4 | 45 | | if (Branches.Count == 0) |
| | | 46 | | { |
| | 1 | 47 | | await context.CompleteActivityAsync(); |
| | 1 | 48 | | return; |
| | | 49 | | } |
| | | 50 | | |
| | 3 | 51 | | await context.ScheduleActivities(Branches, CompleteChildAsync); |
| | 4 | 52 | | } |
| | | 53 | | |
| | | 54 | | private async ValueTask CompleteChildAsync(ActivityCompletedContext context) |
| | | 55 | | { |
| | 0 | 56 | | var targetContext = context.TargetContext; |
| | 0 | 57 | | var isBreaking = targetContext.GetIsBreaking(); |
| | | 58 | | |
| | 0 | 59 | | if (isBreaking) |
| | | 60 | | { |
| | 0 | 61 | | await CompleteAsync(targetContext); |
| | 0 | 62 | | return; |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | var childContext = context.ChildContext; |
| | 0 | 66 | | var completedChildActivityId = childContext.Activity.Id; |
| | | 67 | | |
| | | 68 | | // Append activity to the set of completed activities. |
| | 0 | 69 | | var completedActivityIds = targetContext.UpdateProperty<HashSet<string>>("Completed", set => |
| | 0 | 70 | | { |
| | 0 | 71 | | set ??= new(); |
| | 0 | 72 | | set.Add(completedChildActivityId); |
| | 0 | 73 | | return set; |
| | 0 | 74 | | }); |
| | | 75 | | |
| | 0 | 76 | | var allChildActivityIds = Branches.Select(x => x.Id).ToImmutableHashSet(); |
| | 0 | 77 | | var joinMode = JoinMode; |
| | | 78 | | |
| | | 79 | | switch (joinMode) |
| | | 80 | | { |
| | | 81 | | case ForkJoinMode.WaitAny: |
| | 0 | 82 | | await CompleteAsync(targetContext); |
| | 0 | 83 | | break; |
| | | 84 | | case ForkJoinMode.WaitAll: |
| | 0 | 85 | | var allSet = allChildActivityIds.All(x => completedActivityIds.Contains(x)); |
| | 0 | 86 | | if (allSet) await CompleteAsync(targetContext); |
| | | 87 | | break; |
| | | 88 | | } |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | private void OnBreakSignalReceived(BreakSignal signal, SignalContext signalContext) |
| | | 92 | | { |
| | 0 | 93 | | signalContext.ReceiverActivityExecutionContext.SetIsBreaking(); |
| | 0 | 94 | | } |
| | | 95 | | } |