| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using Elsa.Expressions.Contracts; |
| | | 3 | | using Elsa.Expressions.Models; |
| | | 4 | | using Elsa.Workflows.Activities.Flowchart.Attributes; |
| | | 5 | | using Elsa.Workflows.Activities.Flowchart.Models; |
| | | 6 | | using Elsa.Workflows.Attributes; |
| | | 7 | | using Elsa.Workflows.Models; |
| | | 8 | | using Elsa.Workflows.UIHints; |
| | | 9 | | using JetBrains.Annotations; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Workflows.Activities.Flowchart.Activities; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Evaluates the specified case conditions and schedules the one that evaluates to <code>true</code>. |
| | | 15 | | /// </summary> |
| | | 16 | | [FlowNode("Default")] |
| | | 17 | | [Activity("Elsa", "Branching", "Evaluate a set of case conditions and schedule the activity for a matching case.", Displ |
| | | 18 | | [PublicAPI] |
| | | 19 | | public class FlowSwitch : Activity |
| | | 20 | | { |
| | | 21 | | /// <inheritdoc /> |
| | 5 | 22 | | public FlowSwitch([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) |
| | | 23 | | { |
| | 5 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// The possible cases to evaluate. |
| | | 28 | | /// </summary> |
| | | 29 | | [Input(UIHint = "flow-switch-editor")] |
| | 20 | 30 | | public ICollection<FlowSwitchCase> Cases { get; set; } = new List<FlowSwitchCase>(); |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// The switch mode determines whether the first match should be scheduled, or all matches. |
| | | 34 | | /// </summary> |
| | | 35 | | [Input( |
| | | 36 | | Description = "The switch mode determines whether the first match should be scheduled, or all matches.", |
| | | 37 | | UIHint = InputUIHints.DropDown |
| | | 38 | | )] |
| | 22 | 39 | | public Input<SwitchMode> Mode { get; set; } = new(SwitchMode.MatchFirst); |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | | 42 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 43 | | { |
| | 5 | 44 | | var matchingCases = (await FindMatchingCasesAsync(context.ExpressionExecutionContext)).ToList(); |
| | 5 | 45 | | var hasAnyMatches = matchingCases.Any(); |
| | 5 | 46 | | var mode = context.Get(Mode); |
| | 5 | 47 | | var results = mode == SwitchMode.MatchFirst ? hasAnyMatches ? [matchingCases.First()] : Array.Empty<FlowSwitchCa |
| | 9 | 48 | | var outcomes = hasAnyMatches ? results.Select(r => r.Label).ToArray() : ["Default"]; |
| | | 49 | | |
| | 5 | 50 | | await context.CompleteActivityAsync(new Outcomes(outcomes)); |
| | 5 | 51 | | } |
| | | 52 | | |
| | | 53 | | private async Task<IEnumerable<FlowSwitchCase>> FindMatchingCasesAsync(ExpressionExecutionContext context) |
| | | 54 | | { |
| | 5 | 55 | | var matchingCases = new List<FlowSwitchCase>(); |
| | 5 | 56 | | var expressionEvaluator = context.GetRequiredService<IExpressionEvaluator>(); |
| | | 57 | | |
| | 30 | 58 | | foreach (var switchCase in Cases) |
| | | 59 | | { |
| | 10 | 60 | | var result = await expressionEvaluator.EvaluateAsync<bool?>(switchCase.Condition, context); |
| | | 61 | | |
| | 10 | 62 | | if (result == true) |
| | | 63 | | { |
| | 5 | 64 | | matchingCases.Add(switchCase); |
| | | 65 | | } |
| | 10 | 66 | | } |
| | | 67 | | |
| | 5 | 68 | | return matchingCases; |
| | 5 | 69 | | } |
| | | 70 | | } |