| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using Elsa.Expressions.Contracts; |
| | | 4 | | using Elsa.Expressions.Models; |
| | | 5 | | using Elsa.Workflows.Attributes; |
| | | 6 | | using Elsa.Workflows.Models; |
| | | 7 | | using Elsa.Workflows.UIHints; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Workflows.Activities; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// The Switch activity is an approximation of the `switch` construct in C#. |
| | | 14 | | /// When a case evaluates to true, the associated activity is then scheduled for execution. |
| | | 15 | | /// </summary> |
| | | 16 | | [Activity("Elsa", "Branching", "Evaluate a set of case conditions and schedule the activity for a matching case.")] |
| | | 17 | | [PublicAPI] |
| | | 18 | | public class Switch : Activity |
| | | 19 | | { |
| | | 20 | | /// <inheritdoc /> |
| | 15 | 21 | | public Switch([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) |
| | | 22 | | { |
| | 15 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// The value to switch on, made available as output for capturing. |
| | | 27 | | /// </summary> |
| | 13 | 28 | | public Output<object>? Output { get; set; } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// The cases to evaluate. |
| | | 32 | | /// </summary> |
| | | 33 | | [Input( |
| | | 34 | | Description = "The cases to evaluate.", |
| | | 35 | | UIHint = "switch-editor" |
| | | 36 | | )] |
| | 71 | 37 | | public ICollection<SwitchCase> Cases { get; set; } = new List<SwitchCase>(); |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// The switch mode determines whether the first match should be scheduled, or all matches. |
| | | 41 | | /// </summary> |
| | | 42 | | [Input( |
| | | 43 | | Description = "The switch mode determines whether the first match should be scheduled, or all matches.", |
| | | 44 | | UIHint = InputUIHints.DropDown |
| | | 45 | | )] |
| | 67 | 46 | | public Input<SwitchMode> Mode { get; set; } = new(SwitchMode.MatchFirst); |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// The default activity to schedule when no case matches. |
| | | 50 | | /// </summary> |
| | 39 | 51 | | [Port] public IActivity? Default { get; set; } |
| | | 52 | | |
| | | 53 | | /// <inheritdoc /> |
| | | 54 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 55 | | { |
| | 13 | 56 | | var matchingCases = (await FindMatchingCasesAsync(context.ExpressionExecutionContext)).ToList(); |
| | 13 | 57 | | var hasAnyMatches = matchingCases.Any(); |
| | 13 | 58 | | var mode = context.Get(Mode); |
| | 13 | 59 | | var results = mode == SwitchMode.MatchFirst ? hasAnyMatches ? new[] { matchingCases.First() } : Array.Empty<Swit |
| | | 60 | | |
| | 13 | 61 | | if (hasAnyMatches) |
| | | 62 | | { |
| | 14 | 63 | | foreach (var result in results) |
| | | 64 | | { |
| | 4 | 65 | | await context.ScheduleActivityAsync(result.Activity, OnChildActivityCompletedAsync); |
| | | 66 | | } |
| | | 67 | | |
| | 3 | 68 | | return; |
| | | 69 | | } |
| | | 70 | | |
| | 10 | 71 | | await context.ScheduleActivityAsync(Default, OnChildActivityCompletedAsync); |
| | 13 | 72 | | } |
| | | 73 | | |
| | | 74 | | private async Task<IEnumerable<SwitchCase>> FindMatchingCasesAsync(ExpressionExecutionContext context) |
| | | 75 | | { |
| | 13 | 76 | | var matchingCases = new List<SwitchCase>(); |
| | 13 | 77 | | var expressionEvaluator = context.GetRequiredService<IExpressionEvaluator>(); |
| | | 78 | | |
| | 54 | 79 | | foreach (var switchCase in Cases) |
| | | 80 | | { |
| | 14 | 81 | | var result = await expressionEvaluator.EvaluateAsync<bool?>(switchCase.Condition, context); |
| | | 82 | | |
| | 14 | 83 | | if (result == true) |
| | | 84 | | { |
| | 6 | 85 | | matchingCases.Add(switchCase); |
| | | 86 | | } |
| | 14 | 87 | | } |
| | | 88 | | |
| | 13 | 89 | | return matchingCases; |
| | 13 | 90 | | } |
| | | 91 | | |
| | | 92 | | private async ValueTask OnChildActivityCompletedAsync(ActivityCompletedContext context) |
| | | 93 | | { |
| | 0 | 94 | | await context.TargetContext.CompleteActivityAsync(); |
| | 0 | 95 | | } |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Represents an individual case of the <see cref="Switch"/> activity. |
| | | 100 | | /// </summary> |
| | | 101 | | public class SwitchCase |
| | | 102 | | { |
| | | 103 | | /// <summary> |
| | | 104 | | /// Initializes a new instance of the <see cref="SwitchCase"/> class. |
| | | 105 | | /// </summary> |
| | | 106 | | [JsonConstructor] |
| | | 107 | | public SwitchCase() |
| | | 108 | | { |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Initializes a new instance of the <see cref="SwitchCase"/> class. |
| | | 113 | | /// </summary> |
| | | 114 | | /// <param name="label">The label of the case.</param> |
| | | 115 | | /// <param name="condition">The condition to evaluate.</param> |
| | | 116 | | /// <param name="activity">The activity to schedule when the condition evaluates to true.</param> |
| | | 117 | | public SwitchCase(string label, Expression condition, IActivity activity) |
| | | 118 | | { |
| | | 119 | | Label = label; |
| | | 120 | | Condition = condition; |
| | | 121 | | Activity = activity; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <inheritdoc /> |
| | | 125 | | public SwitchCase(string label, Func<ExpressionExecutionContext, ValueTask<bool>> condition, IActivity activity) : t |
| | | 126 | | { |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <inheritdoc /> |
| | | 130 | | public SwitchCase(string label, Func<ValueTask<bool>> condition, IActivity activity) : this(label, Expression.Delega |
| | | 131 | | { |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | /// <inheritdoc /> |
| | | 135 | | public SwitchCase(string label, Func<ExpressionExecutionContext, bool> condition, IActivity activity) : this(label, |
| | | 136 | | { |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <inheritdoc /> |
| | | 140 | | public SwitchCase(string label, Func<bool> condition, IActivity activity) : this(label, Expression.DelegateExpressio |
| | | 141 | | { |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// The label of the case. |
| | | 146 | | /// </summary> |
| | | 147 | | public string Label { get; set; } = null!; |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// The condition to evaluate. |
| | | 151 | | /// </summary> |
| | | 152 | | public Expression Condition { get; set; } = Expression.LiteralExpression(false); |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// The activity to schedule when the condition evaluates to true. |
| | | 156 | | /// </summary> |
| | | 157 | | public IActivity? Activity { get; set; } |
| | | 158 | | } |