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