| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | using Elsa.Workflows.Memory; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Workflows.Activities; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// A base class for activities that control a collection of activities. |
| | | 8 | | /// </summary> |
| | | 9 | | public abstract class Container : Activity, IVariableContainer |
| | | 10 | | { |
| | | 11 | | /// <inheritdoc /> |
| | 5244 | 12 | | protected Container(string? source = null, int? line = null) : base(source, line) |
| | | 13 | | { |
| | 5244 | 14 | | } |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// The <see cref="IActivity"/>s to execute. |
| | | 18 | | /// </summary> |
| | 18551 | 19 | | public ICollection<IActivity> Activities { get; set; } = new List<IActivity>(); |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// The variables available to this scope. |
| | | 23 | | /// </summary> |
| | 14733 | 24 | | public ICollection<Variable> Variables { get; set; } = new Collection<Variable>(); |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 28 | | { |
| | | 29 | | // Ensure variables have names. |
| | 832 | 30 | | EnsureNames(Variables); |
| | | 31 | | |
| | | 32 | | // Register variables. |
| | 832 | 33 | | context.ExpressionExecutionContext.Memory.Declare(Variables); |
| | | 34 | | |
| | | 35 | | // Schedule children. |
| | 832 | 36 | | await ScheduleChildrenAsync(context); |
| | 832 | 37 | | } |
| | | 38 | | |
| | | 39 | | private void EnsureNames(IEnumerable<Variable> variables) |
| | | 40 | | { |
| | 832 | 41 | | var count = 0; |
| | | 42 | | |
| | 1728 | 43 | | foreach (var variable in variables) |
| | 32 | 44 | | if (string.IsNullOrWhiteSpace(variable.Name)) |
| | 22 | 45 | | variable.Name = $"Variable{++count}"; |
| | 832 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Schedule the <see cref="Activities"/> for execution. |
| | | 50 | | /// </summary> |
| | | 51 | | protected virtual ValueTask ScheduleChildrenAsync(ActivityExecutionContext context) |
| | | 52 | | { |
| | 0 | 53 | | ScheduleChildren(context); |
| | 0 | 54 | | return ValueTask.CompletedTask; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Schedule the <see cref="Activities"/> for execution. |
| | | 59 | | /// </summary> |
| | | 60 | | protected virtual void ScheduleChildren(ActivityExecutionContext context) |
| | | 61 | | { |
| | 0 | 62 | | } |
| | | 63 | | } |