| | | 1 | | using System.Collections; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | using Elsa.Common; |
| | | 5 | | using Elsa.Expressions.Helpers; |
| | | 6 | | using Elsa.Expressions.Models; |
| | | 7 | | using Elsa.Workflows; |
| | | 8 | | using Elsa.Workflows.Activities; |
| | | 9 | | using Elsa.Workflows.Memory; |
| | | 10 | | using Elsa.Workflows.Models; |
| | | 11 | | using Humanizer; |
| | | 12 | | |
| | | 13 | | // ReSharper disable once CheckNamespace |
| | | 14 | | namespace Elsa.Extensions; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Provides extensions on <see cref="ExpressionExecutionContext"/> |
| | | 18 | | /// </summary> |
| | | 19 | | public static class ExpressionExecutionContextExtensions |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// The key used to store the <see cref="WorkflowExecutionContext"/> in the <see cref="ExpressionExecutionContext.Tr |
| | | 23 | | /// </summary> |
| | 5 | 24 | | public static readonly object WorkflowExecutionContextKey = new(); |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// The key used to store the <see cref="ActivityExecutionContext"/> in the <see cref="ExpressionExecutionContext.Tr |
| | | 28 | | /// </summary> |
| | 5 | 29 | | public static readonly object ActivityExecutionContextKey = new(); |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// The key used to store the input in the <see cref="ExpressionExecutionContext.TransientProperties"/> dictionary. |
| | | 33 | | /// </summary> |
| | 5 | 34 | | public static readonly object InputKey = new(); |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// The key used to store the workflow in the <see cref="ExpressionExecutionContext.TransientProperties"/> dictionar |
| | | 38 | | /// </summary> |
| | 5 | 39 | | public static readonly object WorkflowKey = new(); |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// The key used to store the activity in the <see cref="ExpressionExecutionContext.TransientProperties"/> dictionar |
| | | 43 | | /// </summary> |
| | 5 | 44 | | public static readonly object ActivityKey = new(); |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Creates a dictionary for the specified <see cref="WorkflowExecutionContext"/> and <see cref="ActivityExecutionCo |
| | | 48 | | /// </summary> |
| | | 49 | | public static IDictionary<object, object> CreateActivityExecutionContextPropertiesFrom(WorkflowExecutionContext work |
| | 3879 | 50 | | new Dictionary<object, object> |
| | 3879 | 51 | | { |
| | 3879 | 52 | | [WorkflowExecutionContextKey] = workflowExecutionContext, |
| | 3879 | 53 | | [InputKey] = input, |
| | 3879 | 54 | | [WorkflowKey] = workflowExecutionContext.Workflow, |
| | 3879 | 55 | | }; |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Creates a dictionary for the specified <see cref="WorkflowExecutionContext"/> and <see cref="ActivityExecutionCo |
| | | 59 | | /// </summary> |
| | | 60 | | public static IDictionary<object, object> CreateTriggerIndexingPropertiesFrom(Workflow workflow, IDictionary<string, |
| | 265 | 61 | | new Dictionary<object, object> |
| | 265 | 62 | | { |
| | 265 | 63 | | [WorkflowKey] = workflow, |
| | 265 | 64 | | [InputKey] = input |
| | 265 | 65 | | }; |
| | | 66 | | |
| | | 67 | | /// <param name="context">The context to start searching from.</param> |
| | | 68 | | extension(ExpressionExecutionContext context) |
| | | 69 | | { |
| | | 70 | | /// <summary> |
| | | 71 | | /// Returns the <see cref="Workflow"/> of the specified <see cref="ExpressionExecutionContext"/> |
| | | 72 | | /// </summary> |
| | 91 | 73 | | public bool TryGetWorkflowExecutionContext(out WorkflowExecutionContext workflowExecutionContext) => context.Tra |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Returns the <see cref="WorkflowExecutionContext"/> of the specified <see cref="ExpressionExecutionContext"/> |
| | | 77 | | /// </summary> |
| | | 78 | | public WorkflowExecutionContext GetWorkflowExecutionContext() |
| | | 79 | | { |
| | 102 | 80 | | return context.TransientProperties.TryGetValue(WorkflowExecutionContextKey, out var value) |
| | 102 | 81 | | ? (WorkflowExecutionContext)value |
| | 102 | 82 | | : throw new InvalidOperationException("WorkflowExecutionContext not found. This value exists only on act |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Returns the <see cref="ActivityExecutionContext"/> of the specified <see cref="ExpressionExecutionContext"/> |
| | | 87 | | /// </summary> |
| | | 88 | | public ActivityExecutionContext GetActivityExecutionContext() |
| | | 89 | | { |
| | 2122 | 90 | | return context.TransientProperties.TryGetValue(ActivityExecutionContextKey, out var value) |
| | 2122 | 91 | | ? (ActivityExecutionContext)value |
| | 2122 | 92 | | : throw new InvalidOperationException("ActivityExecutionContext not found. This value exists only on act |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Returns the <see cref="ActivityExecutionContext"/> of the specified <see cref="ExpressionExecutionContext"/> |
| | | 97 | | /// </summary> |
| | 407 | 98 | | public bool TryGetActivityExecutionContext(out ActivityExecutionContext activityExecutionContext) => context.Tra |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Returns the <see cref="Activity"/> of the specified <see cref="ExpressionExecutionContext"/> |
| | | 102 | | /// </summary> |
| | 0 | 103 | | public IActivity GetActivity() => (IActivity)context.TransientProperties[ActivityKey]; |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Returns the value of the specified input. |
| | | 107 | | /// </summary> |
| | 1498 | 108 | | public T? Get<T>(Input<T>? input) => input != null ? context.GetBlock(input.MemoryBlockReference).Value.ConvertT |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Returns the value of the specified output. |
| | | 112 | | /// </summary> |
| | 0 | 113 | | public T? Get<T>(Output output) => context.GetBlock(output.MemoryBlockReference).Value.ConvertTo<T>(); |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Returns the value of the specified output. |
| | | 117 | | /// </summary> |
| | 0 | 118 | | public object? Get(Output output) => context.GetBlock(output.MemoryBlockReference).Value; |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Returns the value of the variable with the specified name. |
| | | 122 | | /// </summary> |
| | | 123 | | public T? GetVariable<T>(string name) |
| | | 124 | | { |
| | 71 | 125 | | var block = context.GetVariableBlock(name); |
| | 71 | 126 | | return (T?)block?.Value; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Returns the variable with the specified name. |
| | | 131 | | /// </summary> |
| | | 132 | | public Variable? GetVariable(string name, bool localScopeOnly = false) |
| | | 133 | | { |
| | 114 | 134 | | var block = context.GetVariableBlock(name, localScopeOnly); |
| | 114 | 135 | | return block?.Metadata is VariableBlockMetadata metadata ? metadata.Variable : null; |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | private MemoryBlock? GetVariableBlock(string name, bool localScopeOnly = false) |
| | | 139 | | { |
| | 1081 | 140 | | foreach (var block in context.Memory.Blocks.Where(b => b.Value.Metadata is VariableBlockMetadata)) |
| | | 141 | | { |
| | 148 | 142 | | var metadata = block.Value.Metadata as VariableBlockMetadata; |
| | 148 | 143 | | if (metadata!.Variable.Name == name) |
| | 86 | 144 | | return block.Value; |
| | | 145 | | } |
| | | 146 | | |
| | 250 | 147 | | return localScopeOnly ? null : context.ParentContext?.GetVariableBlock(name); |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Creates a named variable in the context. |
| | | 152 | | /// </summary> |
| | | 153 | | public Variable CreateVariable<T>(string name, T? value, Type? storageDriverType = null, Action<MemoryBlock>? co |
| | | 154 | | { |
| | 94 | 155 | | var existingVariable = context.GetVariable(name, localScopeOnly: true); |
| | | 156 | | |
| | 94 | 157 | | if (existingVariable != null) |
| | 1 | 158 | | throw new($"Variable {name} already exists in the context."); |
| | | 159 | | |
| | 93 | 160 | | var variable = new Variable(name, value) |
| | 93 | 161 | | { |
| | 93 | 162 | | StorageDriverType = storageDriverType ?? typeof(WorkflowInstanceStorageDriver) |
| | 93 | 163 | | }; |
| | | 164 | | |
| | 93 | 165 | | var parsedValue = variable.ParseValue(value); |
| | | 166 | | |
| | | 167 | | // Find the first parent context that has a variable container. |
| | | 168 | | // If not found, use the current context. |
| | 93 | 169 | | var variableContainerContext = context.GetVariableContainerContext(); |
| | | 170 | | |
| | 93 | 171 | | variableContainerContext.Set(variable, parsedValue, configure); |
| | 93 | 172 | | return variable; |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | /// <summary> |
| | | 176 | | /// Returns the first parent context that contains a variable container. |
| | | 177 | | /// </summary> |
| | | 178 | | public ExpressionExecutionContext GetVariableContainerContext() |
| | | 179 | | { |
| | 93 | 180 | | return context.FindParent(x => |
| | 93 | 181 | | { |
| | 193 | 182 | | var activityExecutionContext = x.TryGetActivityExecutionContext(out var activityExecutionContextResult) |
| | 193 | 183 | | return activityExecutionContext?.Activity is IVariableContainer; |
| | 93 | 184 | | }) ?? context; |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// Sets the value of a named variable in the context. |
| | | 189 | | /// </summary> |
| | | 190 | | public Variable SetVariable<T>(string name, T? value, Action<MemoryBlock>? configure = null) |
| | | 191 | | { |
| | 15 | 192 | | var variable = context.GetVariable(name); |
| | | 193 | | |
| | 15 | 194 | | if (variable == null) |
| | 4 | 195 | | return context.CreateVariable(name, value, configure: configure); |
| | | 196 | | |
| | | 197 | | // Get the context where the variable is defined. |
| | 11 | 198 | | var contextWithVariable = context.FindContextContainingBlock(variable.Id) ?? context; |
| | | 199 | | |
| | | 200 | | // Set the value on the variable. |
| | 11 | 201 | | var parsedValue = variable.ParseValue(value); |
| | 11 | 202 | | variable.Set(contextWithVariable, parsedValue, configure); |
| | | 203 | | |
| | | 204 | | // Return the variable. |
| | 11 | 205 | | return variable; |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | /// <summary> |
| | | 209 | | /// Sets the output to the specified value. |
| | | 210 | | /// </summary> |
| | | 211 | | public void Set(Output? output, object? value, Action<MemoryBlock>? configure = null) |
| | | 212 | | { |
| | 1722 | 213 | | if (output != null) |
| | | 214 | | { |
| | | 215 | | // Set the value on the output. |
| | 85 | 216 | | var outputMemoryBlockReference = output.MemoryBlockReference(); |
| | 85 | 217 | | var parsedValue = output.ParseValue(value); |
| | 85 | 218 | | context.Set(outputMemoryBlockReference, parsedValue, configure); |
| | | 219 | | |
| | | 220 | | // If the referenced output is a workflow output definition, set the value on the workflow execution con |
| | 85 | 221 | | var workflowExecutionContext = context.GetWorkflowExecutionContext(); |
| | 85 | 222 | | var workflow = workflowExecutionContext.Workflow; |
| | 87 | 223 | | var workflowOutputDefinition = workflow.Outputs.FirstOrDefault(x => x.Name == outputMemoryBlockReference |
| | | 224 | | |
| | 85 | 225 | | if (workflowOutputDefinition != null) |
| | 0 | 226 | | workflowExecutionContext.Output[workflowOutputDefinition.Name] = value!; |
| | | 227 | | } |
| | 1722 | 228 | | } |
| | | 229 | | |
| | | 230 | | /// <summary> |
| | | 231 | | /// Returns a dictionary of memory block keys and values across scopes. |
| | | 232 | | /// </summary> |
| | | 233 | | public IDictionary<string, object> ReadAndFlattenMemoryBlocks() => |
| | 5 | 234 | | context.FlattenMemoryBlocks().ToDictionary(x => x.Key, x => x.Value.Value!); |
| | | 235 | | |
| | | 236 | | /// <summary> |
| | | 237 | | /// Returns a dictionary of memory blocks across scopes. |
| | | 238 | | /// </summary> |
| | | 239 | | public IDictionary<string, MemoryBlock> FlattenMemoryBlocks() |
| | | 240 | | { |
| | 1 | 241 | | var currentContext = context; |
| | 1 | 242 | | var memoryBlocks = new Dictionary<string, MemoryBlock>(); |
| | | 243 | | |
| | 3 | 244 | | while (currentContext != null) |
| | | 245 | | { |
| | 2 | 246 | | var register = currentContext.Memory; |
| | | 247 | | |
| | 8 | 248 | | foreach (var entry in register.Blocks) |
| | 2 | 249 | | memoryBlocks.TryAdd(entry.Key, entry.Value); |
| | | 250 | | |
| | 2 | 251 | | currentContext = currentContext.ParentContext; |
| | | 252 | | } |
| | | 253 | | |
| | 1 | 254 | | return memoryBlocks; |
| | | 255 | | } |
| | | 256 | | |
| | | 257 | | /// <summary> |
| | | 258 | | /// Returns a the first context that contains a memory block with the specified ID. |
| | | 259 | | /// </summary> |
| | | 260 | | public ExpressionExecutionContext? FindContextContainingBlock(string blockId) |
| | | 261 | | { |
| | 1100 | 262 | | return context.FindParent(x => x.Memory.HasBlock(blockId)); |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | /// <summary> |
| | | 266 | | /// Returns the first context in the hierarchy that matches the specified predicate. |
| | | 267 | | /// </summary> |
| | | 268 | | /// <param name="predicate">The predicate to match.</param> |
| | | 269 | | /// <returns>The first context that matches the predicate or <c>null</c> if no match was found.</returns> |
| | | 270 | | public ExpressionExecutionContext? FindParent(Func<ExpressionExecutionContext, bool> predicate) |
| | | 271 | | { |
| | 452 | 272 | | var currentContext = context; |
| | | 273 | | |
| | 1116 | 274 | | while (currentContext != null) |
| | | 275 | | { |
| | 934 | 276 | | if (predicate(currentContext)) |
| | 270 | 277 | | return currentContext; |
| | | 278 | | |
| | 664 | 279 | | currentContext = currentContext.ParentContext; |
| | | 280 | | } |
| | | 281 | | |
| | 182 | 282 | | return null; |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | /// <summary> |
| | | 286 | | /// Returns the value of the specified variable. |
| | | 287 | | /// </summary> |
| | | 288 | | public object GetVariableInScope(string variableName) |
| | | 289 | | { |
| | 2 | 290 | | var variable = context.GetVariable(variableName); |
| | 2 | 291 | | var value = variable?.Get(context); |
| | | 292 | | |
| | 2 | 293 | | return ConvertIEnumerableToArray(value); |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | /// <summary> |
| | | 297 | | /// Gets all variables names in scope. |
| | | 298 | | /// </summary> |
| | | 299 | | public IEnumerable<string> GetVariableNamesInScope() => |
| | 46 | 300 | | context.EnumerateVariablesInScope() |
| | 36 | 301 | | .Select(x => x.Name) |
| | 36 | 302 | | .Where(x => !string.IsNullOrWhiteSpace(x)) |
| | 46 | 303 | | .Distinct(); |
| | | 304 | | |
| | | 305 | | /// <summary> |
| | | 306 | | /// Gets all variables in scope. |
| | | 307 | | /// </summary> |
| | | 308 | | public IEnumerable<Variable> GetVariablesInScope() => |
| | 0 | 309 | | context.EnumerateVariablesInScope() |
| | 0 | 310 | | .Where(x => !string.IsNullOrWhiteSpace(x.Name)) |
| | 0 | 311 | | .DistinctBy(x => x.Name); |
| | | 312 | | |
| | | 313 | | /// <summary> |
| | | 314 | | /// Sets the value of a named variable in the context. |
| | | 315 | | /// </summary> |
| | | 316 | | public void SetVariableInScope(string variableName, object? value) |
| | | 317 | | { |
| | 2 | 318 | | var q = from v in context.EnumerateVariablesInScope() |
| | 2 | 319 | | where v.Name == variableName |
| | 2 | 320 | | where v.TryGet(context, out _) |
| | 2 | 321 | | select v; |
| | | 322 | | |
| | 2 | 323 | | var variable = q.FirstOrDefault(); |
| | | 324 | | |
| | 2 | 325 | | if (variable != null) |
| | 2 | 326 | | variable.Set(context, value); |
| | | 327 | | |
| | 2 | 328 | | if (variable == null) |
| | 0 | 329 | | context.CreateVariable(variableName, value); |
| | 2 | 330 | | } |
| | | 331 | | |
| | | 332 | | /// <summary> |
| | | 333 | | /// Enumerates all variables in scope. |
| | | 334 | | /// </summary> |
| | | 335 | | public IEnumerable<Variable> EnumerateVariablesInScope() |
| | | 336 | | { |
| | 59 | 337 | | var currentScope = context; |
| | | 338 | | |
| | 177 | 339 | | while (currentScope != null) |
| | | 340 | | { |
| | 130 | 341 | | if (!currentScope.TryGetActivityExecutionContext(out var activityExecutionContext)) |
| | | 342 | | { |
| | 49 | 343 | | var variables = currentScope.Memory.Blocks.Values |
| | | 344 | | .Where(x => x.Metadata is VariableBlockMetadata) |
| | | 345 | | .Select(x => x.Metadata as VariableBlockMetadata) |
| | | 346 | | .Select(x => x!.Variable) |
| | 49 | 347 | | .ToList(); |
| | | 348 | | |
| | 124 | 349 | | foreach (var variable in variables) |
| | 14 | 350 | | yield return variable; |
| | | 351 | | } |
| | | 352 | | else |
| | | 353 | | { |
| | 81 | 354 | | var variables = activityExecutionContext.Variables; |
| | | 355 | | |
| | 208 | 356 | | foreach (var variable in variables) |
| | 28 | 357 | | yield return variable; |
| | | 358 | | } |
| | | 359 | | |
| | 118 | 360 | | currentScope = currentScope.ParentContext; |
| | | 361 | | } |
| | | 362 | | |
| | 47 | 363 | | if (context.TryGetWorkflowExecutionContext(out var workflowExecutionContext)) |
| | | 364 | | { |
| | 19 | 365 | | if (workflowExecutionContext.Workflow.ResultVariable != null) |
| | 12 | 366 | | yield return workflowExecutionContext.Workflow.ResultVariable; |
| | | 367 | | } |
| | 47 | 368 | | } |
| | | 369 | | |
| | | 370 | | /// <summary> |
| | | 371 | | /// Returns the input value associated with the specified <see cref="InputDefinition"/> in the given <see cref=" |
| | | 372 | | /// </summary> |
| | | 373 | | /// <typeparam name="T">The type of the input value.</typeparam> |
| | | 374 | | /// <param name="inputDefinition">The <see cref="InputDefinition"/> specifying the input to retrieve.</param> |
| | | 375 | | /// <returns>The input value associated with the specified <see cref="InputDefinition"/> in the <see cref="Expre |
| | | 376 | | public T? GetInput<T>(InputDefinition inputDefinition) |
| | | 377 | | { |
| | 15 | 378 | | return context.GetInput<T>(inputDefinition.Name); |
| | | 379 | | } |
| | | 380 | | } |
| | | 381 | | |
| | | 382 | | private static JsonSerializerOptions? _serializerOptions; |
| | | 383 | | |
| | | 384 | | private static JsonSerializerOptions GetSerializerOptions(ExpressionExecutionContext context) |
| | | 385 | | { |
| | 15 | 386 | | if (_serializerOptions != null) |
| | 14 | 387 | | return _serializerOptions; |
| | | 388 | | |
| | 1 | 389 | | var serializerOptions = context.GetRequiredService<IJsonSerializer>().GetOptions().Clone(); |
| | 1 | 390 | | serializerOptions.ReferenceHandler = ReferenceHandler.Preserve; |
| | 1 | 391 | | _serializerOptions = serializerOptions; |
| | 1 | 392 | | return serializerOptions; |
| | | 393 | | } |
| | | 394 | | |
| | | 395 | | extension(ExpressionExecutionContext context) |
| | | 396 | | { |
| | | 397 | | /// <summary> |
| | | 398 | | /// Returns the value of the specified input. |
| | | 399 | | /// </summary> |
| | | 400 | | /// <param name="name">The name of the input.</param> |
| | | 401 | | /// <typeparam name="T">The type of the input.</typeparam> |
| | | 402 | | /// <returns>The value of the specified input.</returns> |
| | | 403 | | public T? GetInput<T>(string name) |
| | | 404 | | { |
| | 15 | 405 | | var value = context.GetInput(name); |
| | 15 | 406 | | var serializerOptions = GetSerializerOptions(context); |
| | 15 | 407 | | var converterOptions = new ObjectConverterOptions(serializerOptions); |
| | 15 | 408 | | return value.ConvertTo<T>(converterOptions); |
| | | 409 | | } |
| | | 410 | | |
| | | 411 | | /// <summary> |
| | | 412 | | /// Returns the value of the specified input. |
| | | 413 | | /// </summary> |
| | | 414 | | /// <param name="name">The name of the input.</param> |
| | | 415 | | /// <returns>The value of the specified input.</returns> |
| | | 416 | | public object? GetInput(string name) |
| | | 417 | | { |
| | 15 | 418 | | if (context.IsContainedWithinCompositeActivity()) |
| | | 419 | | { |
| | | 420 | | // If there's a variable in the current scope with the specified name, return that. |
| | 0 | 421 | | var variable = context.GetVariable(name); |
| | | 422 | | |
| | 0 | 423 | | if (variable != null) |
| | 0 | 424 | | return variable.Get(context); |
| | | 425 | | } |
| | | 426 | | |
| | | 427 | | // Otherwise, return the input. |
| | 15 | 428 | | var workflowExecutionContext = context.GetWorkflowExecutionContext(); |
| | 15 | 429 | | var input = workflowExecutionContext.Input; |
| | 15 | 430 | | return input.TryGetValue(name, out var value) ? value : null; |
| | | 431 | | } |
| | | 432 | | |
| | | 433 | | /// <summary> |
| | | 434 | | /// Returns the value of the specified output. |
| | | 435 | | /// </summary> |
| | | 436 | | /// <param name="activityIdOrName">The ID or name of the activity.</param> |
| | | 437 | | /// <param name="outputName">The name of the output.</param> |
| | | 438 | | /// <returns>The value of the specified output.</returns> |
| | | 439 | | /// <exception cref="InvalidOperationException">Thrown when the activity is not found.</exception> |
| | | 440 | | public object? GetOutput(string activityIdOrName, string? outputName) |
| | | 441 | | { |
| | 0 | 442 | | var workflowExecutionContext = context.GetWorkflowExecutionContext(); |
| | 0 | 443 | | var activityExecutionContext = context.GetActivityExecutionContext(); |
| | 0 | 444 | | var activity = activityExecutionContext.FindActivityByIdOrName(activityIdOrName); |
| | | 445 | | |
| | 0 | 446 | | if (activity == null) |
| | 0 | 447 | | throw new InvalidOperationException("Activity not found."); |
| | | 448 | | |
| | 0 | 449 | | var outputRegister = workflowExecutionContext.GetActivityOutputRegister(); |
| | 0 | 450 | | var outputRecordCandidates = outputRegister.FindMany(activity.Id, outputName); |
| | 0 | 451 | | var containerIds = activityExecutionContext.GetAncestors().Select(x => x.Id).ToList(); |
| | 0 | 452 | | var filteredOutputRecordCandidates = outputRecordCandidates.Where(x => containerIds.Contains(x.ContainerId)) |
| | 0 | 453 | | var outputRecord = filteredOutputRecordCandidates.FirstOrDefault(); |
| | 0 | 454 | | return outputRecord?.Value; |
| | | 455 | | } |
| | | 456 | | |
| | | 457 | | /// <summary> |
| | | 458 | | /// Returns all activity outputs. |
| | | 459 | | /// </summary> |
| | | 460 | | public async IAsyncEnumerable<ActivityOutputs> GetActivityOutputs() |
| | | 461 | | { |
| | 23 | 462 | | if (!context.TryGetActivityExecutionContext(out var activityExecutionContext)) |
| | 14 | 463 | | yield break; |
| | | 464 | | |
| | 9 | 465 | | var useActivityName = activityExecutionContext.WorkflowExecutionContext.Workflow.CreatedWithModernTooling(); |
| | 9 | 466 | | var activitiesWithOutputs = activityExecutionContext.GetActivitiesWithOutputs(); |
| | | 467 | | |
| | 9 | 468 | | if (useActivityName) |
| | | 469 | | activitiesWithOutputs = activitiesWithOutputs.Where(x => !string.IsNullOrWhiteSpace(x.Activity.Name)); |
| | | 470 | | |
| | 28 | 471 | | await foreach (var activityWithOutput in activitiesWithOutputs) |
| | | 472 | | { |
| | 5 | 473 | | var activity = activityWithOutput.Activity; |
| | 5 | 474 | | var activityDescriptor = activityWithOutput.ActivityDescriptor; |
| | 5 | 475 | | var activityIdentifier = useActivityName ? activity.Name : activity.Id; |
| | 5 | 476 | | var activityIdPascalName = activityIdentifier.Pascalize(); |
| | | 477 | | |
| | 44 | 478 | | foreach (var output in activityDescriptor.Outputs) |
| | | 479 | | { |
| | 17 | 480 | | var outputPascalName = output.Name.Pascalize(); |
| | 17 | 481 | | yield return new(activity.Id, activityIdPascalName, [ |
| | 17 | 482 | | outputPascalName |
| | 17 | 483 | | ]); |
| | | 484 | | } |
| | 5 | 485 | | } |
| | 23 | 486 | | } |
| | | 487 | | |
| | | 488 | | /// <summary> |
| | | 489 | | /// Returns a value indicating whether the current activity is inside a composite activity. |
| | | 490 | | /// </summary> |
| | | 491 | | public bool IsContainedWithinCompositeActivity() |
| | | 492 | | { |
| | 38 | 493 | | if (!context.TryGetActivityExecutionContext(out var activityExecutionContext)) |
| | 14 | 494 | | return false; |
| | | 495 | | |
| | | 496 | | // If the first workflow definition in the ancestor hierarchy and that workflow definition has a parent, the |
| | 62 | 497 | | var firstWorkflowContext = activityExecutionContext.GetAncestors().FirstOrDefault(x => x.Activity is Workflo |
| | | 498 | | |
| | 24 | 499 | | return firstWorkflowContext?.ParentActivityExecutionContext != null; |
| | | 500 | | } |
| | | 501 | | |
| | | 502 | | /// <summary> |
| | | 503 | | /// Returns the result of the activity that was executed before the current activity. |
| | | 504 | | /// </summary> |
| | | 505 | | public object? GetLastResult() |
| | | 506 | | { |
| | 0 | 507 | | var workflowExecutionContext = context.GetWorkflowExecutionContext(); |
| | 0 | 508 | | return workflowExecutionContext.GetLastActivityResult(); |
| | | 509 | | } |
| | | 510 | | |
| | | 511 | | /// <summary> |
| | | 512 | | /// Returns all activity inputs. |
| | | 513 | | /// </summary> |
| | | 514 | | public IEnumerable<WorkflowInput> GetWorkflowInputs() |
| | | 515 | | { |
| | | 516 | | // Check if we are evaluating an expression during workflow execution. |
| | 22 | 517 | | if (context.TryGetWorkflowExecutionContext(out var workflowExecutionContext)) |
| | | 518 | | { |
| | 8 | 519 | | var input = workflowExecutionContext.Input; |
| | | 520 | | |
| | 24 | 521 | | foreach (var inputEntry in input) |
| | | 522 | | { |
| | 4 | 523 | | var inputPascalName = inputEntry.Key.Pascalize(); |
| | 4 | 524 | | var inputValue = inputEntry.Value; |
| | 4 | 525 | | yield return new(inputPascalName, inputValue); |
| | | 526 | | } |
| | | 527 | | } |
| | | 528 | | else |
| | | 529 | | { |
| | | 530 | | // We end up here when we are evaluating an expression during trigger indexing. |
| | | 531 | | // The scenario being that a workflow definition might have variables declared, that we want to be able |
| | 28 | 532 | | foreach (var block in context.Memory.Blocks.Values) |
| | | 533 | | { |
| | 0 | 534 | | if (block.Metadata is not VariableBlockMetadata variableBlockMetadata) |
| | | 535 | | continue; |
| | | 536 | | |
| | 0 | 537 | | var variable = variableBlockMetadata.Variable; |
| | 0 | 538 | | var variablePascalName = variable.Name.Pascalize(); |
| | 0 | 539 | | yield return new(variablePascalName, block.Value); |
| | | 540 | | } |
| | | 541 | | } |
| | 22 | 542 | | } |
| | | 543 | | } |
| | | 544 | | |
| | | 545 | | private static object ConvertIEnumerableToArray(object? obj) |
| | | 546 | | { |
| | 2 | 547 | | if (obj == null) |
| | 0 | 548 | | return null!; |
| | | 549 | | |
| | | 550 | | // If it's not an IEnumerable or it's a string or dictionary, return the original object. |
| | 2 | 551 | | if (obj is not IEnumerable enumerable || obj is string || obj is IDictionary) |
| | 2 | 552 | | return obj; |
| | | 553 | | |
| | | 554 | | // If this is an async enumerable, return as-is. |
| | 0 | 555 | | if (obj.GetType().Name == "AsyncIListEnumerableAdapter`1") |
| | 0 | 556 | | return obj; |
| | | 557 | | |
| | | 558 | | // Use LINQ to convert the IEnumerable to an array. |
| | 0 | 559 | | var elementType = obj.GetType().GetGenericArguments().FirstOrDefault(); |
| | | 560 | | |
| | 0 | 561 | | if (elementType == null) |
| | 0 | 562 | | return obj; |
| | | 563 | | |
| | 0 | 564 | | var toArrayMethod = typeof(Enumerable).GetMethod("ToArray")!.MakeGenericMethod(elementType); |
| | 0 | 565 | | return toArrayMethod.Invoke(null, [ |
| | 0 | 566 | | enumerable |
| | 0 | 567 | | ])!; |
| | | 568 | | } |
| | | 569 | | } |