Added modes to the switch activities (#4067)

This commit is contained in:
MariusVuscanNx 2023-05-25 23:55:17 +03:00 committed by GitHub
parent 758a5a4b62
commit 5703584ea2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 14 deletions

View file

@ -1,5 +1,3 @@
using System.Runtime.CompilerServices;
using System.Text.Json.Serialization;
using Elsa.Expressions;
using Elsa.Expressions.Contracts;
using Elsa.Expressions.Models;
@ -9,6 +7,8 @@ using Elsa.Workflows.Core.Activities.Flowchart.Models;
using Elsa.Workflows.Core.Attributes;
using Elsa.Workflows.Core.Models;
using JetBrains.Annotations;
using System.Runtime.CompilerServices;
using System.Text.Json.Serialization;
namespace Elsa.Workflows.Core.Activities.Flowchart.Activities;
@ -33,17 +33,29 @@ public class FlowSwitch : Activity
[Input(UIHint = "flow-switch-editor")] public ICollection<FlowSwitchCase> Cases { get; set; } = new List<FlowSwitchCase>();
/// <summary>
/// The switch mode determines whether the first match should be scheduled, or all matches.
/// </summary>
[Input(Description = "The switch mode determines whether the first match should be scheduled, or all matches.")]
public Input<SwitchMode> Mode { get; set; } = new(SwitchMode.MatchFirst);
/// <inheritdoc />
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
{
var matchingCase = await FindMatchingCaseAsync(context.ExpressionExecutionContext);
var outcome = matchingCase?.Label ?? "Default";
var matchingCases = await FindMatchingCasesAsync(context.ExpressionExecutionContext);
var hasAnyMatches = matchingCases.Any();
await context.CompleteActivityAsync(new Outcomes(outcome));
var mode = context.Get(Mode);
var results = mode == SwitchMode.MatchFirst ? hasAnyMatches ? new[] { matchingCases.First() } : Array.Empty<FlowSwitchCase>() : matchingCases.ToArray();
var outcomes = hasAnyMatches ? results.Select(r => r.Label).ToArray() : new[] { "Default" };
await context.CompleteActivityAsync(new Outcomes(outcomes));
}
private async Task<FlowSwitchCase?> FindMatchingCaseAsync(ExpressionExecutionContext context)
private async Task<IEnumerable<FlowSwitchCase>> FindMatchingCasesAsync(ExpressionExecutionContext context)
{
var matchingCases = new List<FlowSwitchCase>();
var expressionEvaluator = context.GetRequiredService<IExpressionEvaluator>();
foreach (var switchCase in Cases)
@ -51,10 +63,12 @@ public class FlowSwitch : Activity
var result = await expressionEvaluator.EvaluateAsync<bool?>(switchCase.Condition, context);
if (result == true)
return switchCase;
{
matchingCases.Add(switchCase);
}
}
return null;
return matchingCases;
}
}

View file

@ -50,6 +50,12 @@ public class Switch : Activity
)]
public ICollection<SwitchCase> Cases { get; set; } = new List<SwitchCase>();
/// <summary>
/// The switch mode determines whether the first match should be scheduled, or all matches.
/// </summary>
[Input(Description = "The switch mode determines whether the first match should be scheduled, or all matches.")]
public Input<SwitchMode> Mode { get; set; } = new(SwitchMode.MatchFirst);
/// <summary>
/// The default activity to schedule when no case matches.
/// </summary>
@ -59,19 +65,28 @@ public class Switch : Activity
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
{
context.Set(Output, Expression);
var matchingCase = await FindMatchingCaseAsync(context.ExpressionExecutionContext);
var matchingCases = await FindMatchingCasesAsync(context.ExpressionExecutionContext);
var hasAnyMatches = matchingCases.Any();
if (matchingCase != null)
var mode = context.Get(Mode);
var results = mode == SwitchMode.MatchFirst ? hasAnyMatches ? new[] { matchingCases.First() } : Array.Empty<SwitchCase>() : matchingCases.ToArray();
if (hasAnyMatches)
{
await context.ScheduleActivityAsync(matchingCase.Activity, OnChildActivityCompletedAsync);
foreach (var result in results)
{
await context.ScheduleActivityAsync(result.Activity, OnChildActivityCompletedAsync);
}
return;
}
await context.ScheduleActivityAsync(Default, OnChildActivityCompletedAsync);
}
private async Task<SwitchCase?> FindMatchingCaseAsync(ExpressionExecutionContext context)
private async Task<IEnumerable<SwitchCase>> FindMatchingCasesAsync(ExpressionExecutionContext context)
{
var matchingCases = new List<SwitchCase>();
var expressionEvaluator = context.GetRequiredService<IExpressionEvaluator>();
foreach (var switchCase in Cases)
@ -79,10 +94,12 @@ public class Switch : Activity
var result = await expressionEvaluator.EvaluateAsync<bool?>(switchCase.Condition, context);
if (result == true)
return switchCase;
{
matchingCases.Add(switchCase);
}
}
return null;
return matchingCases;
}
private async ValueTask OnChildActivityCompletedAsync(ActivityExecutionContext context, ActivityExecutionContext childContext)

View file

@ -0,0 +1,15 @@
namespace Elsa.Workflows.Core.Models
{
public enum SwitchMode
{
/// <summary>
/// Yields the outcome of the first condition evaluating to true.
/// </summary>
MatchFirst,
/// <summary>
/// Yields the outcome of all conditions evaluating to true.
/// </summary>
MatchAny
}
}