| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using System.Reflection; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | using Elsa.Common; |
| | | 5 | | using Elsa.Expressions.Contracts; |
| | | 6 | | using Elsa.Expressions.Helpers; |
| | | 7 | | using Elsa.Expressions.Models; |
| | | 8 | | using Elsa.Mediator.Contracts; |
| | | 9 | | using Elsa.Workflows; |
| | | 10 | | using Elsa.Workflows.Attributes; |
| | | 11 | | using Elsa.Workflows.Memory; |
| | | 12 | | using Elsa.Workflows.Models; |
| | | 13 | | using Elsa.Workflows.Notifications; |
| | | 14 | | using Elsa.Workflows.Signals; |
| | | 15 | | using Elsa.Workflows.State; |
| | | 16 | | using JetBrains.Annotations; |
| | | 17 | | using Microsoft.Extensions.Logging; |
| | | 18 | | |
| | | 19 | | // ReSharper disable once CheckNamespace |
| | | 20 | | namespace Elsa.Extensions; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Provides extension methods for <see cref="ActivityExecutionContext"/>. |
| | | 24 | | /// </summary> |
| | | 25 | | [PublicAPI] |
| | | 26 | | public static partial class ActivityExecutionContextExtensions |
| | | 27 | | { |
| | | 28 | | private const string ExtensionsMetadataKey = "Extensions"; |
| | | 29 | | |
| | | 30 | | /// <param name="context">The <see cref="ActivityExecutionContext"/></param> |
| | | 31 | | extension(ActivityExecutionContext context) |
| | | 32 | | { |
| | | 33 | | /// <summary> |
| | | 34 | | /// Attempts to get a value from the input provided via <see cref="WorkflowExecutionContext"/>. If a value was f |
| | | 35 | | /// </summary> |
| | | 36 | | public bool TryGetWorkflowInput<T>(string key, out T value, JsonSerializerOptions? serializerOptions = null) |
| | | 37 | | { |
| | 11 | 38 | | var wellKnownTypeRegistry = context.GetRequiredService<IWellKnownTypeRegistry>(); |
| | | 39 | | |
| | 11 | 40 | | if (context.WorkflowInput.TryGetValue(key, out var v)) |
| | | 41 | | { |
| | 10 | 42 | | value = v.ConvertTo<T>(new(serializerOptions, wellKnownTypeRegistry))!; |
| | 10 | 43 | | return true; |
| | | 44 | | } |
| | | 45 | | |
| | 1 | 46 | | value = default!; |
| | 1 | 47 | | return false; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets a value from the input provided via <see cref="WorkflowExecutionContext"/>. If a value was found, an at |
| | | 52 | | /// </summary> |
| | 1 | 53 | | public T GetWorkflowInput<T>(JsonSerializerOptions? serializerOptions = null) => context.GetWorkflowInput<T>(typ |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets a value from the input provided via <see cref="WorkflowExecutionContext"/>. If a value was found, an at |
| | | 57 | | /// </summary> |
| | | 58 | | public T GetWorkflowInput<T>(string key, JsonSerializerOptions? serializerOptions = null) |
| | | 59 | | { |
| | 217 | 60 | | var wellKnownTypeRegistry = context.GetRequiredService<IWellKnownTypeRegistry>(); |
| | 217 | 61 | | return context.WorkflowInput[key].ConvertTo<T>(new(serializerOptions, wellKnownTypeRegistry))!; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Sets the Result property of the specified activity. |
| | | 66 | | /// </summary> |
| | | 67 | | /// being extended. |
| | | 68 | | /// <param name="value">The value to set.</param> |
| | | 69 | | /// <exception cref="Exception">Thrown when the specified activity does not implement <see cref="IActivityWithRe |
| | | 70 | | public void SetResult(object? value) |
| | | 71 | | { |
| | 19 | 72 | | var activity = context.Activity as IActivityWithResult ?? throw new($"Cannot set result on activity {context |
| | 18 | 73 | | context.Set(activity.Result, value, "Result"); |
| | 18 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Returns true if this activity is triggered for the first time and not being resumed. |
| | | 78 | | /// </summary> |
| | 253 | 79 | | public bool IsTriggerOfWorkflow() => context.WorkflowExecutionContext.TriggerActivityId == context.Activity.Id; |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Creates a workflow variable by name and optionally sets the value. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <param name="name">The name of the variable.</param> |
| | | 85 | | /// <param name="value">The value of the variable.</param> |
| | | 86 | | /// <param name="storageDriverType">The type of storage driver to use for the variable.</param> |
| | | 87 | | /// <param name="configure">A callback to configure the memory block.</param> |
| | | 88 | | /// <returns>The created <see cref="Variable"/>.</returns> |
| | | 89 | | public Variable CreateVariable(string name, object? value, Type? storageDriverType = null, Action<MemoryBlock>? |
| | | 90 | | { |
| | 1 | 91 | | return context.ExpressionExecutionContext.CreateVariable(name, value, storageDriverType, configure); |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Sets a workflow variable by name. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <param name="name">The name of the variable.</param> |
| | | 98 | | /// <param name="value">The value of the variable.</param> |
| | | 99 | | /// <param name="configure">A callback to configure the memory block.</param> |
| | | 100 | | /// <returns>The created <see cref="Variable"/>.</returns> |
| | | 101 | | public Variable SetVariable(string name, object? value, Action<MemoryBlock>? configure = null) => |
| | 10 | 102 | | context.ExpressionExecutionContext.SetVariable(name, value, configure); |
| | | 103 | | |
| | | 104 | | public Variable SetDynamicVariable<T>(string name, T value, Action<MemoryBlock>? configure = null) |
| | | 105 | | { |
| | | 106 | | // Check if a predefined variable already exists. |
| | 0 | 107 | | var predefinedVariable = context.ExpressionExecutionContext.GetVariable(name); |
| | | 108 | | |
| | 0 | 109 | | if (predefinedVariable != null) |
| | | 110 | | { |
| | 0 | 111 | | context.SetVariable(name, value); |
| | 0 | 112 | | return predefinedVariable; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | // No predefined variable exists, so we will add a dynamic variable to the current container in scope. |
| | 0 | 116 | | var container = context.FindParentWithVariableContainer(); |
| | | 117 | | |
| | 0 | 118 | | if (container == null) |
| | 0 | 119 | | throw new("No parent variable container found"); |
| | | 120 | | |
| | 0 | 121 | | var existingVariable = container.DynamicVariables.FirstOrDefault(x => x.Name == name); |
| | | 122 | | |
| | 0 | 123 | | if (existingVariable == null) |
| | 0 | 124 | | container.DynamicVariables.Add(new Variable<T>(name, value)); |
| | | 125 | | |
| | 0 | 126 | | return context.SetVariable(name, value); |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Gets a workflow variable by name. |
| | | 131 | | /// </summary> |
| | | 132 | | /// <param name="name">The name of the variable.</param> |
| | | 133 | | /// <typeparam name="T">The type of the variable.</typeparam> |
| | | 134 | | /// <returns>The variable if found, otherwise null.</returns> |
| | 9 | 135 | | public T? GetVariable<T>(string name) => context.ExpressionExecutionContext.GetVariable<T?>(name); |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Returns a dictionary of variable keys and their values across scopes. |
| | | 139 | | /// </summary> |
| | 1 | 140 | | public IDictionary<string, object> GetVariableValues() => context.ExpressionExecutionContext.ReadAndFlattenMemor |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Returns a set of tuples containing the activity and its descriptor for all activities with outputs. |
| | | 144 | | /// </summary> |
| | | 145 | | public async IAsyncEnumerable<(IActivity Activity, ActivityDescriptor ActivityDescriptor)> GetActivitiesWithOutp |
| | | 146 | | { |
| | | 147 | | // Get current container. |
| | 9 | 148 | | var currentContainerNode = context.FindParentWithVariableContainer()?.ActivityNode; |
| | | 149 | | |
| | 9 | 150 | | if (currentContainerNode == null) |
| | 0 | 151 | | yield break; |
| | | 152 | | |
| | | 153 | | // Get all nodes in the current container |
| | 9 | 154 | | var workflowExecutionContext = context.WorkflowExecutionContext; |
| | 33 | 155 | | var containedNodes = workflowExecutionContext.Nodes.Where(x => x.Parents.Contains(currentContainerNode)).Dis |
| | | 156 | | |
| | | 157 | | // Select activities with outputs. |
| | 9 | 158 | | var activityRegistry = workflowExecutionContext.GetRequiredService<IActivityRegistryLookupService>(); |
| | 9 | 159 | | var activitiesWithOutputs = containedNodes.GetActivitiesWithOutputs(activityRegistry); |
| | | 160 | | |
| | 28 | 161 | | await foreach (var (activity, activityDescriptor) in activitiesWithOutputs) |
| | 5 | 162 | | yield return (activity, activityDescriptor); |
| | 9 | 163 | | } |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Returns a set of tuples containing the activity and its descriptor for all activities with outputs. |
| | | 168 | | /// </summary> |
| | | 169 | | public static async IAsyncEnumerable<(IActivity activity, ActivityDescriptor activityDescriptor)> GetActivitiesWithO |
| | | 170 | | { |
| | 36 | 171 | | foreach (var node in nodes) |
| | | 172 | | { |
| | 9 | 173 | | var activity = node.Activity; |
| | 9 | 174 | | var activityDescriptor = await activityRegistryLookup.FindAsync(activity.Type, activity.Version); |
| | 9 | 175 | | if (activityDescriptor != null && activityDescriptor.Outputs.Any()) |
| | 5 | 176 | | yield return (activity, activityDescriptor); |
| | 9 | 177 | | } |
| | 9 | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <param name="activityExecutionContext">The <see cref="ActivityExecutionContext"/> being extended.</param> |
| | | 181 | | extension(ActivityExecutionContext activityExecutionContext) |
| | | 182 | | { |
| | | 183 | | /// <summary> |
| | | 184 | | /// Returns the activity with the specified ID or name. |
| | | 185 | | /// </summary> |
| | | 186 | | public IActivity? FindActivityByIdOrName(string idOrName) |
| | | 187 | | { |
| | | 188 | | // Get current container. |
| | 0 | 189 | | var currentContainerNode = activityExecutionContext.FindParentWithVariableContainer()?.ActivityNode; |
| | | 190 | | |
| | 0 | 191 | | if (currentContainerNode == null) |
| | 0 | 192 | | return null; |
| | | 193 | | |
| | | 194 | | // Get all nodes in the current container |
| | 0 | 195 | | var workflowExecutionContext = activityExecutionContext.WorkflowExecutionContext; |
| | 0 | 196 | | var containedNodes = workflowExecutionContext.Nodes.Where(x => x.Parents.Contains(currentContainerNode)).Dis |
| | 0 | 197 | | var node = containedNodes.FirstOrDefault(x => x.Activity.Name == idOrName || x.Activity.Id == idOrName); |
| | 0 | 198 | | return node?.Activity; |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | /// <summary> |
| | | 202 | | /// Returns the outcome name for the specified port property name. |
| | | 203 | | /// </summary> |
| | | 204 | | /// <param name="portPropertyName">The name of the port property.</param> |
| | | 205 | | /// <returns>The outcome name.</returns> |
| | | 206 | | public string GetOutcomeName(string portPropertyName) |
| | | 207 | | { |
| | 2 | 208 | | var owner = activityExecutionContext.Activity; |
| | 28 | 209 | | var ports = owner.GetType().GetProperties().Where(x => typeof(IActivity).IsAssignableFrom(x.PropertyType)).T |
| | | 210 | | |
| | 2 | 211 | | var portQuery = |
| | 2 | 212 | | from p in ports |
| | 2 | 213 | | where p.Name == portPropertyName |
| | 2 | 214 | | select p; |
| | | 215 | | |
| | 2 | 216 | | var portProperty = portQuery.First(); |
| | 2 | 217 | | return portProperty.GetCustomAttribute<PortAttribute>()?.Name ?? portProperty.Name; |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | /// <summary> |
| | | 221 | | /// Send a signal up the current hierarchy of ancestors. |
| | | 222 | | /// </summary> |
| | | 223 | | public async ValueTask SendSignalAsync(object signal) |
| | | 224 | | { |
| | 3130 | 225 | | var receivingContexts = new[] |
| | 3130 | 226 | | { |
| | 3130 | 227 | | activityExecutionContext |
| | 3130 | 228 | | }.Concat(activityExecutionContext.GetAncestors()).ToList(); |
| | 3130 | 229 | | var logger = activityExecutionContext.GetRequiredService<ILogger<ActivityExecutionContext>>(); |
| | 3130 | 230 | | var signalType = signal.GetType(); |
| | 3130 | 231 | | var signalTypeName = signalType.Name; |
| | | 232 | | |
| | | 233 | | // Let all ancestors receive the signal. |
| | 30621 | 234 | | foreach (var ancestorContext in receivingContexts) |
| | | 235 | | { |
| | 12182 | 236 | | var signalContext = new SignalContext(ancestorContext, activityExecutionContext, activityExecutionContex |
| | | 237 | | |
| | 12182 | 238 | | if (ancestorContext.Activity is not ISignalHandler handler) |
| | | 239 | | continue; |
| | | 240 | | |
| | 12182 | 241 | | logger.LogDebug("Receiving signal {SignalType} on activity {ActivityId} of type {ActivityType}", signalT |
| | 12182 | 242 | | await handler.ReceiveSignalAsync(signal, signalContext); |
| | | 243 | | |
| | 12182 | 244 | | if (signalContext.StopPropagationRequested) |
| | | 245 | | { |
| | 3 | 246 | | logger.LogDebug("Propagation of signal {SignalType} on activity {ActivityId} of type {ActivityType} |
| | 3 | 247 | | return; |
| | | 248 | | } |
| | 12179 | 249 | | } |
| | 3130 | 250 | | } |
| | | 251 | | |
| | | 252 | | /// <summary> |
| | | 253 | | /// Complete the current activity. This should only be called by activities that explicitly suppress automatic-c |
| | | 254 | | /// </summary> |
| | | 255 | | public async ValueTask ScheduleOutcomesAsync(params string[] outcomes) |
| | | 256 | | { |
| | | 257 | | // Record the outcomes, if any. |
| | 0 | 258 | | activityExecutionContext.JournalData["Outcomes"] = outcomes; |
| | | 259 | | |
| | | 260 | | // Record the output, if any. |
| | 0 | 261 | | var activity = activityExecutionContext.Activity; |
| | 0 | 262 | | var expressionExecutionContext = activityExecutionContext.ExpressionExecutionContext; |
| | 0 | 263 | | var activityDescriptor = activityExecutionContext.ActivityDescriptor; |
| | 0 | 264 | | var outputDescriptors = activityDescriptor.Outputs; |
| | 0 | 265 | | var outputs = outputDescriptors.ToDictionary(x => x.Name, x => activity.GetOutput(expressionExecutionContext |
| | 0 | 266 | | var serializer = activityExecutionContext.GetRequiredService<ISafeSerializer>(); |
| | | 267 | | |
| | 0 | 268 | | foreach (var output in outputs) |
| | | 269 | | { |
| | 0 | 270 | | var outputName = output.Key; |
| | 0 | 271 | | var outputValue = output.Value; |
| | | 272 | | |
| | 0 | 273 | | if (outputValue == null!) |
| | | 274 | | continue; |
| | | 275 | | |
| | 0 | 276 | | var serializedOutputValue = serializer.Serialize(outputValue); |
| | 0 | 277 | | activityExecutionContext.JournalData[outputName] = serializedOutputValue; |
| | | 278 | | } |
| | | 279 | | |
| | | 280 | | // Send a signal. |
| | 0 | 281 | | await activityExecutionContext.SendSignalAsync(new ScheduleActivityOutcomes(outcomes)); |
| | 0 | 282 | | } |
| | | 283 | | |
| | | 284 | | /// <summary> |
| | | 285 | | /// Cancel the activity. For blocking activities, it means their bookmarks will be removed. For job activities, |
| | | 286 | | /// </summary> |
| | | 287 | | public async Task CancelActivityAsync() |
| | | 288 | | { |
| | | 289 | | // If the activity is not running, do nothing. |
| | 93 | 290 | | if (activityExecutionContext.Status != ActivityStatus.Running && activityExecutionContext.Status != Activity |
| | 93 | 291 | | return; |
| | | 292 | | |
| | | 293 | | // Select all child contexts. |
| | 0 | 294 | | var childContexts = activityExecutionContext.Children.ToList(); |
| | | 295 | | |
| | 0 | 296 | | foreach (var childContext in childContexts) |
| | 0 | 297 | | await CancelActivityAsync(childContext); |
| | | 298 | | |
| | 0 | 299 | | var publisher = activityExecutionContext.GetRequiredService<INotificationSender>(); |
| | 0 | 300 | | activityExecutionContext.TransitionTo(ActivityStatus.Canceled); |
| | 0 | 301 | | activityExecutionContext.ClearBookmarks(); |
| | 0 | 302 | | activityExecutionContext.ClearCompletionCallbacks(); |
| | 0 | 303 | | activityExecutionContext.WorkflowExecutionContext.Bookmarks.RemoveWhere(x => x.ActivityNodeId == activityExe |
| | | 304 | | |
| | | 305 | | // Add an execution log entry. |
| | 0 | 306 | | activityExecutionContext.AddExecutionLogEntry("Canceled"); |
| | | 307 | | |
| | 0 | 308 | | await activityExecutionContext.SendSignalAsync(new CancelSignal()); |
| | 0 | 309 | | await publisher.SendAsync(new ActivityCancelled(activityExecutionContext)); |
| | 93 | 310 | | } |
| | | 311 | | |
| | | 312 | | /// <summary> |
| | | 313 | | /// Marks the current activity as faulted, records an exception, and transitions its status to <see cref="Activi |
| | | 314 | | /// An incident is logged, and the fault count for the activity and its ancestor activities is incremented. |
| | | 315 | | /// </summary> |
| | | 316 | | /// <param name="e">The exception to be recorded as the cause of the fault.</param> |
| | | 317 | | public void Fault(Exception e) |
| | | 318 | | { |
| | 15 | 319 | | activityExecutionContext.Exception = e; |
| | 15 | 320 | | activityExecutionContext.TransitionTo(ActivityStatus.Faulted); |
| | 15 | 321 | | var activity = activityExecutionContext.Activity; |
| | 15 | 322 | | var exceptionState = ExceptionState.FromException(e); |
| | 15 | 323 | | var systemClock = activityExecutionContext.GetRequiredService<ISystemClock>(); |
| | 15 | 324 | | var now = systemClock.UtcNow; |
| | 15 | 325 | | var incident = new ActivityIncident(activity.Id, activity.NodeId, activity.Type, e.Message, exceptionState, |
| | 15 | 326 | | activityExecutionContext.WorkflowExecutionContext.Incidents.Add(incident); |
| | 15 | 327 | | activityExecutionContext.AggregateFaultCount++; |
| | | 328 | | |
| | 15 | 329 | | var ancestors = activityExecutionContext.GetAncestors(); |
| | | 330 | | |
| | 82 | 331 | | foreach (var ancestor in ancestors) |
| | 26 | 332 | | ancestor.AggregateFaultCount++; |
| | 15 | 333 | | } |
| | | 334 | | |
| | | 335 | | /// <summary> |
| | | 336 | | /// Recovers the current activity from a faulted state by resetting fault counts and transitioning the activity |
| | | 337 | | /// </summary> |
| | | 338 | | public void RecoverFromFault() |
| | | 339 | | { |
| | 1 | 340 | | activityExecutionContext.AggregateFaultCount = 0; |
| | | 341 | | |
| | 1 | 342 | | var ancestors = activityExecutionContext.GetAncestors(); |
| | | 343 | | |
| | 2 | 344 | | foreach (var ancestor in ancestors) |
| | 0 | 345 | | ancestor.AggregateFaultCount--; |
| | | 346 | | |
| | 1 | 347 | | activityExecutionContext.TransitionTo(ActivityStatus.Running); |
| | 1 | 348 | | } |
| | | 349 | | |
| | | 350 | | /// <summary> |
| | | 351 | | /// Returns the first context that is associated with a <see cref="IVariableContainer"/>. |
| | | 352 | | /// </summary> |
| | | 353 | | public ActivityExecutionContext? FindParentWithVariableContainer() |
| | | 354 | | { |
| | 53 | 355 | | return activityExecutionContext.FindParent(x => x.Activity is IVariableContainer); |
| | | 356 | | } |
| | | 357 | | |
| | | 358 | | /// <summary> |
| | | 359 | | /// Returns the first context in the hierarchy that matches the specified predicate. |
| | | 360 | | /// </summary> |
| | | 361 | | /// <param name="predicate">The predicate to match.</param> |
| | | 362 | | /// <returns>The first context that matches the predicate or <c>null</c> if no match was found.</returns> |
| | | 363 | | public ActivityExecutionContext? FindParent(Func<ActivityExecutionContext, bool> predicate) |
| | | 364 | | { |
| | 23 | 365 | | var currentContext = activityExecutionContext; |
| | | 366 | | |
| | 33 | 367 | | while (currentContext != null) |
| | | 368 | | { |
| | 32 | 369 | | if (predicate(currentContext)) |
| | 22 | 370 | | return currentContext; |
| | | 371 | | |
| | 10 | 372 | | currentContext = currentContext.ParentActivityExecutionContext; |
| | | 373 | | } |
| | | 374 | | |
| | 1 | 375 | | return null; |
| | | 376 | | } |
| | | 377 | | |
| | | 378 | | /// <summary> |
| | | 379 | | /// Returns a flattened list of the current context's ancestors. |
| | | 380 | | /// </summary> |
| | | 381 | | public IEnumerable<ActivityExecutionContext> GetAncestors() |
| | | 382 | | { |
| | 9281 | 383 | | var current = activityExecutionContext.ParentActivityExecutionContext; |
| | | 384 | | |
| | 29977 | 385 | | while (current != null) |
| | | 386 | | { |
| | 23380 | 387 | | yield return current; |
| | 20696 | 388 | | current = current.ParentActivityExecutionContext; |
| | | 389 | | } |
| | 6597 | 390 | | } |
| | | 391 | | |
| | | 392 | | /// <summary> |
| | | 393 | | /// Returns a flattened list of the current context's descendants. |
| | | 394 | | /// </summary> |
| | | 395 | | public IEnumerable<ActivityExecutionContext> GetDescendents() |
| | | 396 | | { |
| | 0 | 397 | | var children = activityExecutionContext.Children.ToList(); |
| | | 398 | | |
| | 0 | 399 | | foreach (var child in children) |
| | | 400 | | { |
| | 0 | 401 | | yield return child; |
| | | 402 | | |
| | 0 | 403 | | foreach (var descendent in GetDescendents(child)) |
| | 0 | 404 | | yield return descendent; |
| | 0 | 405 | | } |
| | 0 | 406 | | } |
| | | 407 | | |
| | | 408 | | /// <summary> |
| | | 409 | | /// Returns a flattened list of the current context's immediate active children. |
| | | 410 | | /// </summary> |
| | | 411 | | public IEnumerable<ActivityExecutionContext> GetActiveChildren() |
| | | 412 | | { |
| | 0 | 413 | | return activityExecutionContext.Children; |
| | | 414 | | } |
| | | 415 | | |
| | | 416 | | /// <summary> |
| | | 417 | | /// Returns a flattened list of the current context's immediate children. |
| | | 418 | | /// </summary> |
| | | 419 | | public IEnumerable<ActivityExecutionContext> GetChildren() |
| | | 420 | | { |
| | 0 | 421 | | return activityExecutionContext.Children; |
| | | 422 | | } |
| | | 423 | | |
| | | 424 | | /// <summary> |
| | | 425 | | /// Returns a flattened list of the current context's descendants. |
| | | 426 | | /// </summary> |
| | | 427 | | public IEnumerable<ActivityExecutionContext> GetDescendants() |
| | | 428 | | { |
| | 1 | 429 | | var children = activityExecutionContext.Children.ToList(); |
| | | 430 | | |
| | 2 | 431 | | foreach (var child in children) |
| | | 432 | | { |
| | 0 | 433 | | yield return child; |
| | | 434 | | |
| | 0 | 435 | | foreach (var descendant in child.GetDescendants()) |
| | 0 | 436 | | yield return descendant; |
| | 0 | 437 | | } |
| | 1 | 438 | | } |
| | | 439 | | |
| | | 440 | | /// <summary> |
| | | 441 | | /// Sets extension data in the metadata. Represents specific data that is exposed generically for an activity. |
| | | 442 | | /// </summary> |
| | | 443 | | public void SetExtensionsMetadata(string key, object? value) |
| | | 444 | | { |
| | 4 | 445 | | var extensionsDictionary = activityExecutionContext.GetExtensionsMetadata() ?? new Dictionary<string, object |
| | | 446 | | |
| | 4 | 447 | | extensionsDictionary[key] = value; |
| | | 448 | | |
| | 4 | 449 | | activityExecutionContext.Metadata[ExtensionsMetadataKey] = extensionsDictionary; |
| | 4 | 450 | | } |
| | | 451 | | |
| | | 452 | | /// <summary> |
| | | 453 | | /// Retrieves the extension data from the metadata. Represents specific data that is exposed generically for an |
| | | 454 | | /// </summary> |
| | | 455 | | public Dictionary<string, object?>? GetExtensionsMetadata() |
| | | 456 | | { |
| | 7 | 457 | | return activityExecutionContext.Metadata.TryGetValue(ExtensionsMetadataKey, out var value) ? value as Dictio |
| | | 458 | | } |
| | | 459 | | |
| | | 460 | | /// <summary> |
| | | 461 | | /// Gets the output of an activity even when the context is not the same as the activity's context. |
| | | 462 | | /// This is useful when you want to get the output of a previously executed activity. |
| | | 463 | | /// </summary> |
| | | 464 | | /// <param name="property"></param> |
| | | 465 | | /// <typeparam name="TProp"></typeparam> |
| | | 466 | | /// <returns></returns> |
| | | 467 | | public object? GetActivityOutput<TProp>(Expression<Func<TProp>> property) |
| | | 468 | | { |
| | 71 | 469 | | if (property.Body is not MemberExpression memberExpr) |
| | | 470 | | { |
| | 0 | 471 | | return null; |
| | | 472 | | } |
| | | 473 | | |
| | 71 | 474 | | var propertyName = memberExpr.Member.Name; |
| | | 475 | | |
| | 71 | 476 | | var value = activityExecutionContext.Get(propertyName); |
| | | 477 | | |
| | 71 | 478 | | if (value != null) |
| | 0 | 479 | | return value; |
| | | 480 | | |
| | 71 | 481 | | var registry = activityExecutionContext.WorkflowExecutionContext.GetActivityOutputRegister(); |
| | | 482 | | |
| | 71 | 483 | | return registry.FindOutputByActivityInstanceId(activityExecutionContext.Id, propertyName); |
| | | 484 | | } |
| | | 485 | | |
| | 8393 | 486 | | public bool GetHasEvaluatedProperties() => activityExecutionContext.TransientProperties.TryGetValue<bool>("HasEv |
| | 3477 | 487 | | public void SetHasEvaluatedProperties() => activityExecutionContext.TransientProperties["HasEvaluatedProperties" |
| | | 488 | | } |
| | | 489 | | } |