| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Elsa.Expressions.Models; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Workflows.Activities.Flowchart.Models; |
| | | 6 | | using Elsa.Workflows.Attributes; |
| | | 7 | | using Elsa.Workflows.Models; |
| | | 8 | | using Elsa.Workflows.Signals; |
| | | 9 | | using Elsa.Workflows.UIHints; |
| | | 10 | | using JetBrains.Annotations; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Workflows.Activities; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Signals the current composite activity to complete itself as a whole. |
| | | 16 | | /// </summary> |
| | | 17 | | [Activity("Elsa", "Composition", "Signals the current composite activity to complete itself as a whole.")] |
| | | 18 | | [PublicAPI] |
| | | 19 | | public class Complete : Activity, ITerminalNode |
| | | 20 | | { |
| | | 21 | | /// <inheritdoc /> |
| | 20 | 22 | | public Complete([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) |
| | | 23 | | { |
| | 20 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public Complete(IEnumerable<string> outcomes, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = |
| | 0 | 28 | | : this(new Input<object>(outcomes.ToList()), source, line) |
| | | 29 | | { |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public Complete(Func<ExpressionExecutionContext, ICollection<string>> outcomes, [CallerFilePath] string? source = nu |
| | 0 | 34 | | : this(new Input<object>(outcomes), source, line) |
| | | 35 | | { |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | public Complete(Func<ExpressionExecutionContext, string> outcome, [CallerFilePath] string? source = null, [CallerLin |
| | 0 | 40 | | : this(context => [outcome(context)], source, line) |
| | | 41 | | { |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | 0 | 45 | | public Complete(Input<object> outcomes, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) |
| | | 46 | | { |
| | 0 | 47 | | Outcomes = outcomes; |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// The outcome or set of outcomes to complete this activity with. |
| | | 52 | | /// </summary> |
| | | 53 | | [Input( |
| | | 54 | | Description = "The outcome or set of outcomes to complete this activity with.", |
| | | 55 | | UIHint = InputUIHints.OutcomePicker, |
| | | 56 | | DefaultSyntax = "Object" |
| | | 57 | | )] |
| | 43 | 58 | | public Input<object> Outcomes { get; set; } = null!; |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | | 61 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 62 | | { |
| | 3 | 63 | | var outcomesValue = Outcomes.GetOrDefault(context); |
| | 3 | 64 | | var outcomes = InterpretOutcomes(outcomesValue).ToArray(); |
| | | 65 | | |
| | 3 | 66 | | await context.SendSignalAsync(new CompleteCompositeSignal(new Outcomes(outcomes))); |
| | 3 | 67 | | await context.CompleteActivityAsync(); |
| | 3 | 68 | | } |
| | | 69 | | |
| | | 70 | | private static IEnumerable<string> InterpretOutcomes(object? outcomesValue) |
| | | 71 | | { |
| | | 72 | | switch (outcomesValue) |
| | | 73 | | { |
| | | 74 | | case string singleOutcome: |
| | 0 | 75 | | yield return singleOutcome; |
| | 0 | 76 | | break; |
| | | 77 | | case IEnumerable<string> outcomeStrings: |
| | 0 | 78 | | foreach (var outcome in outcomeStrings) |
| | 0 | 79 | | yield return outcome; |
| | 0 | 80 | | break; |
| | | 81 | | case IEnumerable<object> outcomeObjects: |
| | | 82 | | { |
| | 0 | 83 | | foreach (var outcome in outcomeObjects) |
| | 0 | 84 | | yield return outcome.ToString()!; |
| | 0 | 85 | | break; |
| | | 86 | | } |
| | | 87 | | case JsonElement jsonElement: |
| | | 88 | | { |
| | 0 | 89 | | if (jsonElement.ValueKind == JsonValueKind.Array) |
| | | 90 | | { |
| | 0 | 91 | | var outcomeArray = jsonElement.EnumerateArray().ToList(); |
| | 0 | 92 | | foreach (var element in outcomeArray) |
| | 0 | 93 | | yield return element.ToString(); |
| | | 94 | | } |
| | | 95 | | else |
| | 0 | 96 | | yield return jsonElement.ToString(); |
| | | 97 | | |
| | 0 | 98 | | break; |
| | | 99 | | } |
| | | 100 | | default: |
| | 3 | 101 | | yield return "Done"; |
| | | 102 | | break; |
| | | 103 | | } |
| | 3 | 104 | | } |
| | | 105 | | } |