| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | using Elsa.Expressions.Contracts; |
| | | 5 | | using Elsa.Extensions; |
| | | 6 | | using Elsa.Workflows.Activities.StateMachine.Models; |
| | | 7 | | using Elsa.Workflows.Attributes; |
| | | 8 | | using Elsa.Workflows.Models; |
| | | 9 | | using Elsa.Workflows.Options; |
| | | 10 | | using JetBrains.Annotations; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Workflows.Activities.StateMachine.Activities; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Executes a state machine made of named states and trigger-driven transitions. |
| | | 16 | | /// </summary> |
| | | 17 | | [Activity("Elsa", "Flow", "Executes a state machine made of named states and trigger-driven transitions.")] |
| | | 18 | | [PublicAPI] |
| | | 19 | | public class StateMachine : Activity |
| | | 20 | | { |
| | | 21 | | private const string PhaseEntering = "Entering"; |
| | | 22 | | private const string CurrentStateProperty = "CurrentState"; |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | 13 | 25 | | public StateMachine([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line |
| | | 26 | | { |
| | 13 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// The states in declaration order. |
| | | 31 | | /// </summary> |
| | 134 | 32 | | public ICollection<StateMachineState> States { get; set; } = new List<StateMachineState>(); |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// The transitions in declaration order. |
| | | 36 | | /// </summary> |
| | 155 | 37 | | public ICollection<Transition> Transitions { get; set; } = new List<Transition>(); |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// The first state to enter when no current state is set. |
| | | 41 | | /// </summary> |
| | 24 | 42 | | public string? InitialState { get; set; } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// The currently active state. |
| | | 46 | | /// </summary> |
| | 31 | 47 | | public string? CurrentState { get; set; } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Exposes nested activities to the workflow graph builder. |
| | | 51 | | /// </summary> |
| | | 52 | | [JsonIgnore] |
| | | 53 | | [Browsable(false)] |
| | | 54 | | public IEnumerable<IActivity> Activities => |
| | 43 | 55 | | States.SelectMany(x => new[] { x.Entry, x.Exit }) |
| | 32 | 56 | | .Concat(Transitions.SelectMany(x => new[] { x.Trigger, x.Action })) |
| | 128 | 57 | | .Where(x => x != null) |
| | 11 | 58 | | .Cast<IActivity>(); |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | | 61 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 62 | | { |
| | 11 | 63 | | var currentState = GetCurrentState(context); |
| | 11 | 64 | | SetCurrentState(context, string.IsNullOrWhiteSpace(currentState) ? InitialState : currentState); |
| | | 65 | | |
| | 11 | 66 | | if (FindState(GetCurrentState(context)) == null) |
| | | 67 | | { |
| | 0 | 68 | | await context.CompleteActivityAsync(); |
| | 0 | 69 | | return; |
| | | 70 | | } |
| | | 71 | | |
| | 11 | 72 | | await EnterStateAsync(context); |
| | 11 | 73 | | } |
| | | 74 | | |
| | | 75 | | private async ValueTask EnterStateAsync(ActivityExecutionContext context, ActivityExecutionContext? schedulingContex |
| | | 76 | | { |
| | 16 | 77 | | var state = FindState(GetCurrentState(context)); |
| | | 78 | | |
| | 16 | 79 | | if (state == null) |
| | | 80 | | { |
| | 0 | 81 | | await context.CompleteActivityAsync(); |
| | 0 | 82 | | return; |
| | | 83 | | } |
| | | 84 | | |
| | 16 | 85 | | if (state.Entry != null) |
| | | 86 | | { |
| | 10 | 87 | | await ScheduleAsync(context, state.Entry, OnStateEntryCompletedAsync, PhaseEntering, schedulingContext); |
| | 10 | 88 | | return; |
| | | 89 | | } |
| | | 90 | | |
| | 6 | 91 | | await ScheduleOutboundTriggersAsync(context, schedulingContext); |
| | 16 | 92 | | } |
| | | 93 | | |
| | | 94 | | private async ValueTask OnStateEntryCompletedAsync(ActivityCompletedContext context) |
| | | 95 | | { |
| | 9 | 96 | | await ScheduleOutboundTriggersAsync(context.TargetContext, context.ChildContext); |
| | 9 | 97 | | } |
| | | 98 | | |
| | | 99 | | private async ValueTask ScheduleOutboundTriggersAsync(ActivityExecutionContext context, ActivityExecutionContext? sc |
| | | 100 | | { |
| | 39 | 101 | | var outboundTransitions = GetOutboundTransitions(GetCurrentState(context)).Where(x => x.Trigger != null && FindS |
| | | 102 | | |
| | 15 | 103 | | if (!outboundTransitions.Any()) |
| | | 104 | | { |
| | 1 | 105 | | await context.CompleteActivityAsync(); |
| | 1 | 106 | | return; |
| | | 107 | | } |
| | | 108 | | |
| | 76 | 109 | | foreach (var transition in outboundTransitions) |
| | 24 | 110 | | await ScheduleAsync(context, transition.Trigger!, OnTriggerCompletedAsync, GetTransitionKey(transition), sch |
| | 15 | 111 | | } |
| | | 112 | | |
| | | 113 | | private async ValueTask OnTriggerCompletedAsync(ActivityCompletedContext context) |
| | | 114 | | { |
| | 11 | 115 | | var targetContext = context.TargetContext; |
| | 11 | 116 | | var transition = FindTransitionByKey(targetContext, context.ChildContext.Tag as string) ?? FindTransitionByTrigg |
| | | 117 | | |
| | 11 | 118 | | if (transition == null || !IsCurrentSource(targetContext, transition) || FindState(transition.To) == null) |
| | 0 | 119 | | return; |
| | | 120 | | |
| | 11 | 121 | | var canTransition = transition.Condition == null || await EvaluateConditionAsync(targetContext, transition.Condi |
| | | 122 | | |
| | 11 | 123 | | if (!canTransition) |
| | | 124 | | { |
| | 2 | 125 | | if (transition.Trigger != null) |
| | 2 | 126 | | await ScheduleAsync(targetContext, transition.Trigger, OnTriggerCompletedAsync, GetTransitionKey(transit |
| | | 127 | | |
| | 2 | 128 | | return; |
| | | 129 | | } |
| | | 130 | | |
| | 9 | 131 | | await CancelCompetingTriggersAsync(targetContext, transition, context.ChildContext); |
| | | 132 | | |
| | 9 | 133 | | if (transition.Action != null) |
| | | 134 | | { |
| | 4 | 135 | | await ScheduleTransitionActivityAsync(targetContext, transition.Action, transition, OnTransitionActionComple |
| | 4 | 136 | | return; |
| | | 137 | | } |
| | | 138 | | |
| | 5 | 139 | | await ExitStateAsync(targetContext, transition, context.ChildContext); |
| | 11 | 140 | | } |
| | | 141 | | |
| | | 142 | | private async ValueTask OnTransitionActionCompletedAsync(ActivityCompletedContext context) |
| | | 143 | | { |
| | 2 | 144 | | var targetContext = context.TargetContext; |
| | 2 | 145 | | var transition = FindTransitionByKey(targetContext, context.ChildContext.Tag as string); |
| | | 146 | | |
| | 2 | 147 | | if (transition == null || !IsCurrentSource(targetContext, transition)) |
| | 1 | 148 | | return; |
| | | 149 | | |
| | 1 | 150 | | await ExitStateAsync(targetContext, transition, context.ChildContext); |
| | 2 | 151 | | } |
| | | 152 | | |
| | | 153 | | private async ValueTask ExitStateAsync(ActivityExecutionContext context, Transition transition, ActivityExecutionCon |
| | | 154 | | { |
| | 6 | 155 | | var sourceState = FindState(transition.From); |
| | | 156 | | |
| | 6 | 157 | | if (sourceState?.Exit != null) |
| | | 158 | | { |
| | 2 | 159 | | await ScheduleTransitionActivityAsync(context, sourceState.Exit, transition, OnStateExitCompletedAsync, sche |
| | 2 | 160 | | return; |
| | | 161 | | } |
| | | 162 | | |
| | 4 | 163 | | await CompleteTransitionAsync(context, transition, schedulingContext); |
| | 6 | 164 | | } |
| | | 165 | | |
| | | 166 | | private async ValueTask OnStateExitCompletedAsync(ActivityCompletedContext context) |
| | | 167 | | { |
| | 1 | 168 | | var targetContext = context.TargetContext; |
| | 1 | 169 | | var transition = FindTransitionByKey(targetContext, context.ChildContext.Tag as string); |
| | | 170 | | |
| | 1 | 171 | | if (transition == null || !IsCurrentSource(targetContext, transition)) |
| | 0 | 172 | | return; |
| | | 173 | | |
| | 1 | 174 | | await CompleteTransitionAsync(targetContext, transition, context.ChildContext); |
| | 1 | 175 | | } |
| | | 176 | | |
| | | 177 | | private async ValueTask CompleteTransitionAsync(ActivityExecutionContext context, Transition transition, ActivityExe |
| | | 178 | | { |
| | 5 | 179 | | SetCurrentState(context, transition.To); |
| | 5 | 180 | | await EnterStateAsync(context, schedulingContext); |
| | 5 | 181 | | } |
| | | 182 | | |
| | | 183 | | private async ValueTask ScheduleTransitionActivityAsync( |
| | | 184 | | ActivityExecutionContext context, |
| | | 185 | | IActivity activity, |
| | | 186 | | Transition transition, |
| | | 187 | | ActivityCompletionCallback callback, |
| | | 188 | | ActivityExecutionContext? schedulingContext) |
| | | 189 | | { |
| | 6 | 190 | | await ScheduleAsync(context, activity, callback, GetTransitionKey(transition), schedulingContext); |
| | 6 | 191 | | } |
| | | 192 | | |
| | | 193 | | private static async ValueTask ScheduleAsync( |
| | | 194 | | ActivityExecutionContext context, |
| | | 195 | | IActivity activity, |
| | | 196 | | ActivityCompletionCallback callback, |
| | | 197 | | string tag, |
| | | 198 | | ActivityExecutionContext? schedulingContext) |
| | | 199 | | { |
| | 42 | 200 | | var options = new ScheduleWorkOptions |
| | 42 | 201 | | { |
| | 42 | 202 | | CompletionCallback = callback, |
| | 42 | 203 | | Tag = tag, |
| | 42 | 204 | | SchedulingActivityExecutionId = schedulingContext?.Id |
| | 42 | 205 | | }; |
| | | 206 | | |
| | 42 | 207 | | await context.ScheduleActivityAsync(activity, options); |
| | 42 | 208 | | } |
| | | 209 | | |
| | | 210 | | private async Task CancelCompetingTriggersAsync(ActivityExecutionContext context, Transition winningTransition, Acti |
| | | 211 | | { |
| | 9 | 212 | | var competingTriggerIds = GetOutboundTransitions(winningTransition.From) |
| | 17 | 213 | | .Where(x => !ReferenceEquals(x, winningTransition)) |
| | 8 | 214 | | .Select(x => x.Trigger?.Id) |
| | 8 | 215 | | .Where(x => !string.IsNullOrWhiteSpace(x)) |
| | 8 | 216 | | .Select(x => x!) |
| | 9 | 217 | | .ToHashSet(); |
| | | 218 | | |
| | 9 | 219 | | var competingTriggerContexts = context.WorkflowExecutionContext.ActivityExecutionContexts |
| | 18 | 220 | | .Where(x => x.ParentActivityExecutionContext == context && !ReferenceEquals(x, winningTriggerContext) && com |
| | 9 | 221 | | .ToList(); |
| | | 222 | | |
| | 22 | 223 | | foreach (var competingTriggerContext in competingTriggerContexts) |
| | 2 | 224 | | await competingTriggerContext.CancelActivityAsync(); |
| | | 225 | | |
| | 9 | 226 | | RemoveScheduledCompetingTriggers(context, competingTriggerIds); |
| | 9 | 227 | | RemoveCompetingTriggerCallbacks(context, competingTriggerIds); |
| | 9 | 228 | | } |
| | | 229 | | |
| | | 230 | | private static void RemoveScheduledCompetingTriggers(ActivityExecutionContext context, HashSet<string> competingTrig |
| | | 231 | | { |
| | 9 | 232 | | var scheduler = context.WorkflowExecutionContext.Scheduler; |
| | 9 | 233 | | var scheduledWorkItems = scheduler.List().ToList(); |
| | | 234 | | |
| | 30 | 235 | | if (!scheduledWorkItems.Any(x => IsCompetingTriggerWorkItem(context, competingTriggerIds, x))) |
| | 1 | 236 | | return; |
| | | 237 | | |
| | 8 | 238 | | scheduler.Clear(); |
| | | 239 | | |
| | 67 | 240 | | foreach (var workItem in scheduledWorkItems.Where(x => !IsCompetingTriggerWorkItem(context, competingTriggerIds, |
| | 14 | 241 | | scheduler.Schedule(workItem); |
| | 8 | 242 | | } |
| | | 243 | | |
| | | 244 | | private static bool IsCompetingTriggerWorkItem(ActivityExecutionContext context, HashSet<string> competingTriggerIds |
| | 44 | 245 | | workItem.Owner == context && competingTriggerIds.Contains(workItem.Activity.Id); |
| | | 246 | | |
| | | 247 | | private static void RemoveCompetingTriggerCallbacks(ActivityExecutionContext context, HashSet<string> competingTrigg |
| | | 248 | | { |
| | 9 | 249 | | var competingTriggerCallbacks = context.WorkflowExecutionContext.CompletionCallbacks |
| | 8 | 250 | | .Where(x => x.Owner == context && competingTriggerIds.Contains(x.Child.Activity.Id)) |
| | 9 | 251 | | .ToList(); |
| | | 252 | | |
| | 9 | 253 | | context.WorkflowExecutionContext.RemoveCompletionCallbacks(competingTriggerCallbacks); |
| | 9 | 254 | | } |
| | | 255 | | |
| | 81 | 256 | | private string? GetCurrentState(ActivityExecutionContext context) => context.GetProperty<string>(CurrentStatePropert |
| | | 257 | | |
| | | 258 | | private void SetCurrentState(ActivityExecutionContext context, string? state) |
| | | 259 | | { |
| | 16 | 260 | | CurrentState = state; |
| | | 261 | | |
| | 16 | 262 | | if (state == null) |
| | 0 | 263 | | context.RemoveProperty(CurrentStateProperty); |
| | | 264 | | else |
| | 16 | 265 | | context.SetProperty(CurrentStateProperty, state); |
| | 16 | 266 | | } |
| | | 267 | | |
| | 14 | 268 | | private bool IsCurrentSource(ActivityExecutionContext context, Transition transition) => string.Equals(transition.Fr |
| | | 269 | | |
| | | 270 | | private static async Task<bool> EvaluateConditionAsync(ActivityExecutionContext context, Input<bool> condition) |
| | | 271 | | { |
| | 7 | 272 | | var evaluator = context.GetRequiredService<IExpressionEvaluator>(); |
| | 7 | 273 | | return await evaluator.EvaluateAsync(condition, context.ExpressionExecutionContext); |
| | 7 | 274 | | } |
| | | 275 | | |
| | | 276 | | private StateMachineState? FindState(string? name) => |
| | 68 | 277 | | string.IsNullOrWhiteSpace(name) |
| | 68 | 278 | | ? null |
| | 193 | 279 | | : States.FirstOrDefault(x => string.Equals(x.Name, name, StringComparison.Ordinal)); |
| | | 280 | | |
| | | 281 | | private IEnumerable<Transition> GetOutboundTransitions(string? sourceState) => |
| | 38 | 282 | | string.IsNullOrWhiteSpace(sourceState) |
| | 38 | 283 | | ? [] |
| | 129 | 284 | | : Transitions.Where(x => string.Equals(x.From, sourceState, StringComparison.Ordinal)); |
| | | 285 | | |
| | | 286 | | private Transition? FindTransitionByTrigger(ActivityExecutionContext context, IActivity trigger) => |
| | 0 | 287 | | GetOutboundTransitions(GetCurrentState(context)).FirstOrDefault(x => ReferenceEquals(x.Trigger, trigger)); |
| | | 288 | | |
| | | 289 | | private Transition? FindTransitionByKey(ActivityExecutionContext context, string? key) |
| | | 290 | | { |
| | 14 | 291 | | if (string.IsNullOrWhiteSpace(key)) |
| | 0 | 292 | | return null; |
| | | 293 | | |
| | 14 | 294 | | var currentState = GetCurrentState(context); |
| | 31 | 295 | | return GetOutboundTransitions(currentState).FirstOrDefault(x => string.Equals(GetTransitionKey(x), key, StringCo |
| | 15 | 296 | | ?? Transitions.FirstOrDefault(x => string.Equals(GetTransitionKey(x), key, StringComparison.Ordinal)); |
| | | 297 | | } |
| | | 298 | | |
| | | 299 | | private string GetTransitionKey(Transition transition) |
| | | 300 | | { |
| | 50 | 301 | | var index = 0; |
| | 204 | 302 | | foreach (var current in Transitions) |
| | | 303 | | { |
| | 77 | 304 | | if (ReferenceEquals(current, transition)) |
| | 50 | 305 | | return $"{index}:{GetTransitionDisplayKey(transition)}"; |
| | | 306 | | |
| | 27 | 307 | | index++; |
| | | 308 | | } |
| | | 309 | | |
| | 0 | 310 | | return GetTransitionDisplayKey(transition); |
| | 50 | 311 | | } |
| | | 312 | | |
| | | 313 | | private static string GetTransitionDisplayKey(Transition transition) => |
| | 50 | 314 | | string.IsNullOrWhiteSpace(transition.Name) |
| | 50 | 315 | | ? $"{transition.From}->{transition.To}" |
| | 50 | 316 | | : transition.Name; |
| | | 317 | | } |