| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using Elsa.Expressions.Models; |
| | | 3 | | using Elsa.Workflows.Attributes; |
| | | 4 | | using Elsa.Workflows.Models; |
| | | 5 | | using Elsa.Workflows.UIHints; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Workflows.Activities; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Evaluate a Boolean condition to determine which activity to execute next. |
| | | 12 | | /// </summary> |
| | | 13 | | [Activity("Elsa", "Branching", "Evaluate a Boolean condition to determine which activity to execute next.")] |
| | | 14 | | [PublicAPI] |
| | | 15 | | public class If : Activity<bool> |
| | | 16 | | { |
| | | 17 | | /// <inheritdoc /> |
| | 48 | 18 | | public If([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) |
| | | 19 | | { |
| | 48 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <inheritdoc /> |
| | 0 | 23 | | public If(Input<bool> condition, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : this |
| | | 24 | | { |
| | 0 | 25 | | Condition = condition; |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | 21 | 29 | | public If(Func<ExpressionExecutionContext, bool> condition, [CallerFilePath] string? source = null, [CallerLineNumbe |
| | | 30 | | { |
| | 21 | 31 | | Condition = new Input<bool>(condition); |
| | 21 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | 23 | 35 | | public If(Func<bool> condition, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : this( |
| | | 36 | | { |
| | 23 | 37 | | Condition = new Input<bool>(condition); |
| | 23 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// The condition to evaluate. |
| | | 42 | | /// </summary> |
| | | 43 | | [Input(UIHint = InputUIHints.SingleLine)] |
| | 244 | 44 | | public Input<bool> Condition { get; set; } = new(new Literal<bool>(false)); |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// The activity to execute when the condition evaluates to true. |
| | | 48 | | /// </summary> |
| | | 49 | | [Port] |
| | 139 | 50 | | public IActivity? Then { get; set; } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// The activity to execute when the condition evaluates to false. |
| | | 54 | | /// </summary> |
| | | 55 | | [Port] |
| | 134 | 56 | | public IActivity? Else { get; set; } |
| | | 57 | | |
| | | 58 | | /// <inheritdoc /> |
| | | 59 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 60 | | { |
| | 38 | 61 | | var result = context.Get(Condition); |
| | 38 | 62 | | var nextActivity = result ? Then : Else; |
| | | 63 | | |
| | 38 | 64 | | context.Set(Result, result); |
| | 38 | 65 | | await context.ScheduleActivityAsync(nextActivity, OnChildCompleted); |
| | 38 | 66 | | } |
| | | 67 | | |
| | | 68 | | private async ValueTask OnChildCompleted(ActivityCompletedContext context) |
| | | 69 | | { |
| | 17 | 70 | | await context.TargetContext.CompleteActivityAsync(); |
| | 17 | 71 | | } |
| | | 72 | | } |