| | | 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 |
| | 4028 | 50 | | new Dictionary<object, object> |
| | 4028 | 51 | | { |
| | 4028 | 52 | | [WorkflowExecutionContextKey] = workflowExecutionContext, |
| | 4028 | 53 | | [InputKey] = input, |
| | 4028 | 54 | | [WorkflowKey] = workflowExecutionContext.Workflow, |
| | 4028 | 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, |
| | 304 | 61 | | new Dictionary<object, object> |
| | 304 | 62 | | { |
| | 304 | 63 | | [WorkflowKey] = workflow, |
| | 304 | 64 | | [InputKey] = input |
| | 304 | 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> |
| | 75 | 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 | | { |
| | 103 | 80 | | return context.TransientProperties.TryGetValue(WorkflowExecutionContextKey, out var value) |
| | 103 | 81 | | ? (WorkflowExecutionContext)value |
| | 103 | 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 | | { |
| | 2105 | 90 | | return context.TransientProperties.TryGetValue(ActivityExecutionContextKey, out var value) |
| | 2105 | 91 | | ? (ActivityExecutionContext)value |
| | 2105 | 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> |
| | 352 | 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> |
| | 1678 | 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 | | { |
| | 1710 | 213 | | if (output != null) |
| | | 214 | | { |
| | | 215 | | // Set the value on the output. |
| | 81 | 216 | | var outputMemoryBlockReference = output.MemoryBlockReference(); |
| | 81 | 217 | | var parsedValue = output.ParseValue(value); |
| | 81 | 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 |
| | 81 | 221 | | var workflowExecutionContext = context.GetWorkflowExecutionContext(); |
| | 81 | 222 | | var workflow = workflowExecutionContext.Workflow; |
| | 84 | 223 | | var workflowOutputDefinition = workflow.Outputs.FirstOrDefault(x => x.Name == outputMemoryBlockReference |
| | | 224 | | |
| | 81 | 225 | | if (workflowOutputDefinition != null) |
| | 1 | 226 | | workflowExecutionContext.Output[workflowOutputDefinition.Name] = value!; |
| | | 227 | | } |
| | 1710 | 228 | | } |
| | | 229 | | |
| | | 230 | | /// <summary> |
| | | 231 | | /// Sets an already converted value on the destination referenced by the specified output. |
| | | 232 | | /// </summary> |
| | | 233 | | public void SetBoundValue(Output output, object? value) |
| | | 234 | | { |
| | 5 | 235 | | var outputMemoryBlockReference = output.MemoryBlockReference(); |
| | 5 | 236 | | context.Set(outputMemoryBlockReference, value); |
| | | 237 | | |
| | 5 | 238 | | var workflowExecutionContext = context.GetWorkflowExecutionContext(); |
| | 8 | 239 | | var workflowOutputDefinition = workflowExecutionContext.Workflow.Outputs.FirstOrDefault(x => x.Name == outpu |
| | | 240 | | |
| | 5 | 241 | | if (workflowOutputDefinition != null) |
| | 2 | 242 | | workflowExecutionContext.Output[workflowOutputDefinition.Name] = value!; |
| | 5 | 243 | | } |
| | | 244 | | |
| | | 245 | | /// <summary> |
| | | 246 | | /// Returns a dictionary of memory block keys and values across scopes. |
| | | 247 | | /// </summary> |
| | | 248 | | public IDictionary<string, object> ReadAndFlattenMemoryBlocks() => |
| | 5 | 249 | | context.FlattenMemoryBlocks().ToDictionary(x => x.Key, x => x.Value.Value!); |
| | | 250 | | |
| | | 251 | | /// <summary> |
| | | 252 | | /// Returns a dictionary of memory blocks across scopes. |
| | | 253 | | /// </summary> |
| | | 254 | | public IDictionary<string, MemoryBlock> FlattenMemoryBlocks() |
| | | 255 | | { |
| | 1 | 256 | | var currentContext = context; |
| | 1 | 257 | | var memoryBlocks = new Dictionary<string, MemoryBlock>(); |
| | | 258 | | |
| | 3 | 259 | | while (currentContext != null) |
| | | 260 | | { |
| | 2 | 261 | | var register = currentContext.Memory; |
| | | 262 | | |
| | 8 | 263 | | foreach (var entry in register.Blocks) |
| | 2 | 264 | | memoryBlocks.TryAdd(entry.Key, entry.Value); |
| | | 265 | | |
| | 2 | 266 | | currentContext = currentContext.ParentContext; |
| | | 267 | | } |
| | | 268 | | |
| | 1 | 269 | | return memoryBlocks; |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | /// <summary> |
| | | 273 | | /// Returns a the first context that contains a memory block with the specified ID. |
| | | 274 | | /// </summary> |
| | | 275 | | public ExpressionExecutionContext? FindContextContainingBlock(string blockId) |
| | | 276 | | { |
| | 1085 | 277 | | return context.FindParent(x => x.Memory.HasBlock(blockId)); |
| | | 278 | | } |
| | | 279 | | |
| | | 280 | | /// <summary> |
| | | 281 | | /// Returns the first context in the hierarchy that matches the specified predicate. |
| | | 282 | | /// </summary> |
| | | 283 | | /// <param name="predicate">The predicate to match.</param> |
| | | 284 | | /// <returns>The first context that matches the predicate or <c>null</c> if no match was found.</returns> |
| | | 285 | | public ExpressionExecutionContext? FindParent(Func<ExpressionExecutionContext, bool> predicate) |
| | | 286 | | { |
| | 446 | 287 | | var currentContext = context; |
| | | 288 | | |
| | 1104 | 289 | | while (currentContext != null) |
| | | 290 | | { |
| | 925 | 291 | | if (predicate(currentContext)) |
| | 267 | 292 | | return currentContext; |
| | | 293 | | |
| | 658 | 294 | | currentContext = currentContext.ParentContext; |
| | | 295 | | } |
| | | 296 | | |
| | 179 | 297 | | return null; |
| | | 298 | | } |
| | | 299 | | |
| | | 300 | | /// <summary> |
| | | 301 | | /// Returns the value of the specified variable. |
| | | 302 | | /// </summary> |
| | | 303 | | public object GetVariableInScope(string variableName) |
| | | 304 | | { |
| | 2 | 305 | | var variable = context.GetVariable(variableName); |
| | 2 | 306 | | var value = variable?.Get(context); |
| | | 307 | | |
| | 2 | 308 | | return ConvertIEnumerableToArray(value); |
| | | 309 | | } |
| | | 310 | | |
| | | 311 | | /// <summary> |
| | | 312 | | /// Gets all variables names in scope. |
| | | 313 | | /// </summary> |
| | | 314 | | public IEnumerable<string> GetVariableNamesInScope() => |
| | 38 | 315 | | context.EnumerateVariablesInScope() |
| | 36 | 316 | | .Select(x => x.Name) |
| | 36 | 317 | | .Where(x => !string.IsNullOrWhiteSpace(x)) |
| | 38 | 318 | | .Distinct(); |
| | | 319 | | |
| | | 320 | | /// <summary> |
| | | 321 | | /// Gets all variables in scope. |
| | | 322 | | /// </summary> |
| | | 323 | | public IEnumerable<Variable> GetVariablesInScope() => |
| | 0 | 324 | | context.EnumerateVariablesInScope() |
| | 0 | 325 | | .Where(x => !string.IsNullOrWhiteSpace(x.Name)) |
| | 0 | 326 | | .DistinctBy(x => x.Name); |
| | | 327 | | |
| | | 328 | | /// <summary> |
| | | 329 | | /// Sets the value of a named variable in the context. |
| | | 330 | | /// </summary> |
| | | 331 | | public void SetVariableInScope(string variableName, object? value) |
| | | 332 | | { |
| | 2 | 333 | | var q = from v in context.EnumerateVariablesInScope() |
| | 2 | 334 | | where v.Name == variableName |
| | 2 | 335 | | where v.TryGet(context, out _) |
| | 2 | 336 | | select v; |
| | | 337 | | |
| | 2 | 338 | | var variable = q.FirstOrDefault(); |
| | | 339 | | |
| | 2 | 340 | | if (variable != null) |
| | 2 | 341 | | variable.Set(context, value); |
| | | 342 | | |
| | 2 | 343 | | if (variable == null) |
| | 0 | 344 | | context.CreateVariable(variableName, value); |
| | 2 | 345 | | } |
| | | 346 | | |
| | | 347 | | /// <summary> |
| | | 348 | | /// Enumerates all variables in scope. |
| | | 349 | | /// </summary> |
| | | 350 | | public IEnumerable<Variable> EnumerateVariablesInScope() |
| | | 351 | | { |
| | 51 | 352 | | var currentScope = context; |
| | | 353 | | |
| | 161 | 354 | | while (currentScope != null) |
| | | 355 | | { |
| | 122 | 356 | | if (!currentScope.TryGetActivityExecutionContext(out var activityExecutionContext)) |
| | | 357 | | { |
| | 41 | 358 | | var variables = currentScope.Memory.Blocks.Values |
| | | 359 | | .Where(x => x.Metadata is VariableBlockMetadata) |
| | | 360 | | .Select(x => x.Metadata as VariableBlockMetadata) |
| | | 361 | | .Select(x => x!.Variable) |
| | 41 | 362 | | .ToList(); |
| | | 363 | | |
| | 108 | 364 | | foreach (var variable in variables) |
| | 14 | 365 | | yield return variable; |
| | | 366 | | } |
| | | 367 | | else |
| | | 368 | | { |
| | 81 | 369 | | var variables = activityExecutionContext.Variables; |
| | | 370 | | |
| | 208 | 371 | | foreach (var variable in variables) |
| | 28 | 372 | | yield return variable; |
| | | 373 | | } |
| | | 374 | | |
| | 110 | 375 | | currentScope = currentScope.ParentContext; |
| | | 376 | | } |
| | | 377 | | |
| | 39 | 378 | | if (context.TryGetWorkflowExecutionContext(out var workflowExecutionContext)) |
| | | 379 | | { |
| | 19 | 380 | | if (workflowExecutionContext.Workflow.ResultVariable != null) |
| | 12 | 381 | | yield return workflowExecutionContext.Workflow.ResultVariable; |
| | | 382 | | } |
| | 39 | 383 | | } |
| | | 384 | | |
| | | 385 | | /// <summary> |
| | | 386 | | /// Returns the input value associated with the specified <see cref="InputDefinition"/> in the given <see cref=" |
| | | 387 | | /// </summary> |
| | | 388 | | /// <typeparam name="T">The type of the input value.</typeparam> |
| | | 389 | | /// <param name="inputDefinition">The <see cref="InputDefinition"/> specifying the input to retrieve.</param> |
| | | 390 | | /// <returns>The input value associated with the specified <see cref="InputDefinition"/> in the <see cref="Expre |
| | | 391 | | public T? GetInput<T>(InputDefinition inputDefinition) |
| | | 392 | | { |
| | 15 | 393 | | return context.GetInput<T>(inputDefinition.Name); |
| | | 394 | | } |
| | | 395 | | } |
| | | 396 | | |
| | | 397 | | private static JsonSerializerOptions? _serializerOptions; |
| | | 398 | | |
| | | 399 | | private static JsonSerializerOptions GetSerializerOptions(ExpressionExecutionContext context) |
| | | 400 | | { |
| | 15 | 401 | | if (_serializerOptions != null) |
| | 12 | 402 | | return _serializerOptions; |
| | | 403 | | |
| | 3 | 404 | | var serializerOptions = context.GetRequiredService<IJsonSerializer>().GetOptions().Clone(); |
| | 3 | 405 | | serializerOptions.ReferenceHandler = ReferenceHandler.Preserve; |
| | 3 | 406 | | _serializerOptions = serializerOptions; |
| | 3 | 407 | | return serializerOptions; |
| | | 408 | | } |
| | | 409 | | |
| | | 410 | | extension(ExpressionExecutionContext context) |
| | | 411 | | { |
| | | 412 | | /// <summary> |
| | | 413 | | /// Returns the value of the specified input. |
| | | 414 | | /// </summary> |
| | | 415 | | /// <param name="name">The name of the input.</param> |
| | | 416 | | /// <typeparam name="T">The type of the input.</typeparam> |
| | | 417 | | /// <returns>The value of the specified input.</returns> |
| | | 418 | | public T? GetInput<T>(string name) |
| | | 419 | | { |
| | 15 | 420 | | var value = context.GetInput(name); |
| | 15 | 421 | | var serializerOptions = GetSerializerOptions(context); |
| | 15 | 422 | | var converterOptions = new ObjectConverterOptions(serializerOptions); |
| | 15 | 423 | | return value.ConvertTo<T>(converterOptions); |
| | | 424 | | } |
| | | 425 | | |
| | | 426 | | /// <summary> |
| | | 427 | | /// Returns the value of the specified input. |
| | | 428 | | /// </summary> |
| | | 429 | | /// <param name="name">The name of the input.</param> |
| | | 430 | | /// <returns>The value of the specified input.</returns> |
| | | 431 | | public object? GetInput(string name) |
| | | 432 | | { |
| | 15 | 433 | | if (context.IsContainedWithinCompositeActivity()) |
| | | 434 | | { |
| | | 435 | | // If there's a variable in the current scope with the specified name, return that. |
| | 0 | 436 | | var variable = context.GetVariable(name); |
| | | 437 | | |
| | 0 | 438 | | if (variable != null) |
| | 0 | 439 | | return variable.Get(context); |
| | | 440 | | } |
| | | 441 | | |
| | | 442 | | // Otherwise, return the input. |
| | 15 | 443 | | var workflowExecutionContext = context.GetWorkflowExecutionContext(); |
| | 15 | 444 | | var input = workflowExecutionContext.Input; |
| | 15 | 445 | | return input.TryGetValue(name, out var value) ? value : null; |
| | | 446 | | } |
| | | 447 | | |
| | | 448 | | /// <summary> |
| | | 449 | | /// Returns the value of the specified output. |
| | | 450 | | /// </summary> |
| | | 451 | | /// <param name="activityIdOrName">The ID or name of the activity.</param> |
| | | 452 | | /// <param name="outputName">The name of the output.</param> |
| | | 453 | | /// <returns>The value of the specified output.</returns> |
| | | 454 | | /// <exception cref="InvalidOperationException">Thrown when the activity is not found.</exception> |
| | | 455 | | public object? GetOutput(string activityIdOrName, string? outputName) |
| | | 456 | | { |
| | 0 | 457 | | var workflowExecutionContext = context.GetWorkflowExecutionContext(); |
| | 0 | 458 | | var activityExecutionContext = context.GetActivityExecutionContext(); |
| | 0 | 459 | | var activity = activityExecutionContext.FindActivityByIdOrName(activityIdOrName); |
| | | 460 | | |
| | 0 | 461 | | if (activity == null) |
| | 0 | 462 | | throw new InvalidOperationException("Activity not found."); |
| | | 463 | | |
| | 0 | 464 | | var outputRegister = workflowExecutionContext.GetActivityOutputRegister(); |
| | 0 | 465 | | var outputRecordCandidates = outputRegister.FindMany(activity.Id, outputName); |
| | 0 | 466 | | var containerIds = activityExecutionContext.GetAncestors().Select(x => x.Id).ToList(); |
| | 0 | 467 | | var filteredOutputRecordCandidates = outputRecordCandidates.Where(x => containerIds.Contains(x.ContainerId)) |
| | 0 | 468 | | var outputRecord = filteredOutputRecordCandidates.FirstOrDefault(); |
| | 0 | 469 | | return outputRecord?.Value; |
| | | 470 | | } |
| | | 471 | | |
| | | 472 | | /// <summary> |
| | | 473 | | /// Returns all activity outputs. |
| | | 474 | | /// </summary> |
| | | 475 | | public async IAsyncEnumerable<ActivityOutputs> GetActivityOutputs() |
| | | 476 | | { |
| | 0 | 477 | | if (!context.TryGetActivityExecutionContext(out var activityExecutionContext)) |
| | 0 | 478 | | yield break; |
| | | 479 | | |
| | 0 | 480 | | var useActivityName = activityExecutionContext.WorkflowExecutionContext.Workflow.CreatedWithModernTooling(); |
| | 0 | 481 | | var activitiesWithOutputs = activityExecutionContext.GetActivitiesWithOutputs(); |
| | | 482 | | |
| | 0 | 483 | | if (useActivityName) |
| | | 484 | | activitiesWithOutputs = activitiesWithOutputs.Where(x => !string.IsNullOrWhiteSpace(x.Activity.Name)); |
| | | 485 | | |
| | 0 | 486 | | await foreach (var activityWithOutput in activitiesWithOutputs) |
| | | 487 | | { |
| | 0 | 488 | | var activity = activityWithOutput.Activity; |
| | 0 | 489 | | var activityDescriptor = activityWithOutput.ActivityDescriptor; |
| | 0 | 490 | | var activityIdentifier = useActivityName ? activity.Name! : activity.Id; |
| | 0 | 491 | | var activityIdPascalName = activityIdentifier.Pascalize(); |
| | | 492 | | |
| | 0 | 493 | | foreach (var output in activityDescriptor.Outputs) |
| | | 494 | | { |
| | 0 | 495 | | var outputPascalName = output.Name.Pascalize(); |
| | 0 | 496 | | yield return new(activity.Id, activityIdPascalName, [ |
| | 0 | 497 | | outputPascalName |
| | 0 | 498 | | ]); |
| | | 499 | | } |
| | 0 | 500 | | } |
| | 0 | 501 | | } |
| | | 502 | | |
| | | 503 | | /// <summary> |
| | | 504 | | /// Returns a value indicating whether the current activity is inside a composite activity. |
| | | 505 | | /// </summary> |
| | | 506 | | public bool IsContainedWithinCompositeActivity() |
| | | 507 | | { |
| | 34 | 508 | | if (!context.TryGetActivityExecutionContext(out var activityExecutionContext)) |
| | 10 | 509 | | return false; |
| | | 510 | | |
| | | 511 | | // If the first workflow definition in the ancestor hierarchy and that workflow definition has a parent, the |
| | 62 | 512 | | var firstWorkflowContext = activityExecutionContext.GetAncestors().FirstOrDefault(x => x.Activity is Workflo |
| | | 513 | | |
| | 24 | 514 | | return firstWorkflowContext?.ParentActivityExecutionContext != null; |
| | | 515 | | } |
| | | 516 | | |
| | | 517 | | /// <summary> |
| | | 518 | | /// Returns the result of the activity that was executed before the current activity. |
| | | 519 | | /// </summary> |
| | | 520 | | public object? GetLastResult() |
| | | 521 | | { |
| | 0 | 522 | | var workflowExecutionContext = context.GetWorkflowExecutionContext(); |
| | 0 | 523 | | return workflowExecutionContext.GetLastActivityResult(); |
| | | 524 | | } |
| | | 525 | | |
| | | 526 | | /// <summary> |
| | | 527 | | /// Returns all activity inputs. |
| | | 528 | | /// </summary> |
| | | 529 | | public IEnumerable<WorkflowInput> GetWorkflowInputs() |
| | | 530 | | { |
| | | 531 | | // Check if we are evaluating an expression during workflow execution. |
| | 18 | 532 | | if (context.TryGetWorkflowExecutionContext(out var workflowExecutionContext)) |
| | | 533 | | { |
| | 8 | 534 | | var input = workflowExecutionContext.Input; |
| | | 535 | | |
| | 24 | 536 | | foreach (var inputEntry in input) |
| | | 537 | | { |
| | 4 | 538 | | var inputPascalName = inputEntry.Key.Pascalize(); |
| | 4 | 539 | | var inputValue = inputEntry.Value; |
| | 4 | 540 | | yield return new(inputPascalName, inputValue); |
| | | 541 | | } |
| | | 542 | | } |
| | | 543 | | else |
| | | 544 | | { |
| | | 545 | | // We end up here when we are evaluating an expression during trigger indexing. |
| | | 546 | | // The scenario being that a workflow definition might have variables declared, that we want to be able |
| | 20 | 547 | | foreach (var block in context.Memory.Blocks.Values) |
| | | 548 | | { |
| | 0 | 549 | | if (block.Metadata is not VariableBlockMetadata variableBlockMetadata) |
| | | 550 | | continue; |
| | | 551 | | |
| | 0 | 552 | | var variable = variableBlockMetadata.Variable; |
| | 0 | 553 | | var variablePascalName = variable.Name.Pascalize(); |
| | 0 | 554 | | yield return new(variablePascalName, block.Value); |
| | | 555 | | } |
| | | 556 | | } |
| | 18 | 557 | | } |
| | | 558 | | } |
| | | 559 | | |
| | | 560 | | private static object ConvertIEnumerableToArray(object? obj) |
| | | 561 | | { |
| | 2 | 562 | | if (obj == null) |
| | 0 | 563 | | return null!; |
| | | 564 | | |
| | | 565 | | // If it's not an IEnumerable or it's a string or dictionary, return the original object. |
| | 2 | 566 | | if (obj is not IEnumerable enumerable || obj is string || obj is IDictionary) |
| | 2 | 567 | | return obj; |
| | | 568 | | |
| | | 569 | | // If this is an async enumerable, return as-is. |
| | 0 | 570 | | if (obj.GetType().Name == "AsyncIListEnumerableAdapter`1") |
| | 0 | 571 | | return obj; |
| | | 572 | | |
| | | 573 | | // Use LINQ to convert the IEnumerable to an array. |
| | | 574 | | // For projection operators like Select, the element type is the LAST generic argument |
| | | 575 | | // (e.g., ListSelectIterator<TSource, TResult> where TResult is the element type) |
| | 0 | 576 | | var elementType = obj.GetType().GetGenericArguments().LastOrDefault(); |
| | | 577 | | |
| | 0 | 578 | | if (elementType == null) |
| | 0 | 579 | | return obj; |
| | | 580 | | |
| | 0 | 581 | | var toArrayMethod = typeof(Enumerable).GetMethod("ToArray")!.MakeGenericMethod(elementType); |
| | 0 | 582 | | return toArrayMethod.Invoke(null, [ |
| | 0 | 583 | | enumerable |
| | 0 | 584 | | ])!; |
| | | 585 | | } |
| | | 586 | | } |