Add Complete activity for completing composite activities

This commit is contained in:
Sipke Schoorstra 2022-11-22 13:09:14 +01:00
parent be3e7ac707
commit 9981dc1f10
3 changed files with 64 additions and 8 deletions

View file

@ -0,0 +1,46 @@
using System.ComponentModel;
using Elsa.Expressions.Models;
using Elsa.Workflows.Core.Activities.Flowchart.Models;
using Elsa.Workflows.Core.Attributes;
using Elsa.Workflows.Core.Models;
using Elsa.Workflows.Core.Signals;
namespace Elsa.Workflows.Core.Activities;
/// <summary>
/// Signals the current composite activity to complete itself as a whole.
/// </summary>
[Activity("Elsa", "Primitives", "Signals the current composite activity to complete itself as a whole.")]
public class Complete : Activity
{
/// <inheritdoc />
public Complete(params string[] outcomes) : this(new Input<ICollection<string>>(outcomes))
{
}
/// <inheritdoc />
public Complete(Func<ExpressionExecutionContext, ICollection<string>> outcomes) : this(new Input<ICollection<string>>(outcomes))
{
}
/// <inheritdoc />
public Complete(Func<ExpressionExecutionContext, string> outcome) : this(context => new[] { outcome(context) })
{
}
/// <inheritdoc />
public Complete(Input<ICollection<string>> outcomes) => Outcomes = outcomes;
/// <summary>
/// The outcome or set of outcomes to complete this activity with.
/// </summary>
[Description("The outcome or set of outcomes to complete this activity with.")]
public Input<ICollection<string>> Outcomes { get; set; }
/// <inheritdoc />
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
{
var outcomes = Outcomes.Get(context).ToArray();
await context.SendSignalAsync(new CompleteCompositeSignal(new Outcomes(outcomes)));
}
}

View file

@ -90,9 +90,9 @@ public abstract class Composite : ActivityBase
/// <summary>
/// Represents a composite activity that has a single <see cref="Root"/> activity and returns a result.
/// </summary>
public class Composite<T> : ActivityBase<T>
public abstract class Composite<T> : ActivityBase<T>
{
public Composite()
protected Composite()
{
OnSignalReceived<CompleteCompositeSignal>(OnCompleteCompositeSignal);
}

View file

@ -1,5 +1,6 @@
using Elsa.Workflows.Core.Activities;
using Elsa.Workflows.Core.Activities.Flowchart.Attributes;
using Elsa.Workflows.Core.Models;
namespace Elsa.Samples.TelnyxIntegration;
@ -8,12 +9,21 @@ public class GetAge : Composite<int>
{
public GetAge()
{
Root = new Inline(async context =>
var ageVariable = new Variable<int>();
Root = new Sequence
{
var random = new Random();
var n = random.Next(2);
var outcome = n == 0 ? "Under 18" : "Over 18";
await CompleteAsync(context, outcome);
});
Variables = { ageVariable },
Activities =
{
From(() =>
{
var random = new Random();
return random.Next(36);
}, ageVariable),
new Complete(context => ageVariable.Get<int>(context) < 18 ? "Under 18" : "Over 18")
}
};
}
}