| | | 1 | | using Elsa.Expressions.Contracts; |
| | | 2 | | using Elsa.Expressions.Models; |
| | | 3 | | using Elsa.Workflows; |
| | | 4 | | using Elsa.Workflows.Helpers; |
| | | 5 | | using Elsa.Workflows.Models; |
| | | 6 | | using Microsoft.Extensions.DependencyInjection; |
| | | 7 | | using Microsoft.Extensions.Logging; |
| | | 8 | | |
| | | 9 | | // ReSharper disable once CheckNamespace |
| | | 10 | | namespace Elsa.Extensions; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Extension methods for <see cref="Trigger"/>. |
| | | 14 | | /// </summary> |
| | | 15 | | public static class TriggerExtensions |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Returns a filtered list of triggers that match the specified activity type. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="triggers">The triggers to filter.</param> |
| | | 21 | | /// <typeparam name="T">The type of the activity.</typeparam> |
| | | 22 | | public static IEnumerable<Trigger> Filter<T>(this IEnumerable<Trigger> triggers) where T : IActivity |
| | | 23 | | { |
| | 0 | 24 | | var activityTypeName = TypeNameHelper.GenerateTypeName<T>(); |
| | 0 | 25 | | return triggers.Where(x => x.Type == activityTypeName); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Creates an expression execution context for the specified trigger. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="trigger">The trigger for which to create an expression execution context.</param> |
| | | 32 | | /// <param name="activityDescriptor">The activity descriptor.</param> |
| | | 33 | | /// <param name="serviceProvider">The service provider.</param> |
| | | 34 | | /// <param name="context">The workflow indexing context.</param> |
| | | 35 | | /// <param name="expressionEvaluator">The expression evaluator.</param> |
| | | 36 | | /// <param name="logger">The logger.</param> |
| | | 37 | | /// <returns>An expression execution context.</returns> |
| | | 38 | | public static async Task<ExpressionExecutionContext> CreateExpressionExecutionContextAsync( |
| | | 39 | | this ITrigger trigger, |
| | | 40 | | ActivityDescriptor activityDescriptor, |
| | | 41 | | IServiceProvider serviceProvider, |
| | | 42 | | WorkflowIndexingContext context, |
| | | 43 | | IExpressionEvaluator expressionEvaluator, |
| | | 44 | | ILogger logger) |
| | | 45 | | { |
| | 61 | 46 | | var namedInputs = trigger.GetNamedInputs(); |
| | 263 | 47 | | var assignedInputs = namedInputs.Where(x => x.Value.MemoryBlockReference != null!).ToList(); |
| | 61 | 48 | | var register = context.GetOrCreateRegister(trigger); |
| | 61 | 49 | | var cancellationToken = context.CancellationToken; |
| | 61 | 50 | | var expressionInput = new Dictionary<string, object>(); |
| | 61 | 51 | | var applicationProperties = ExpressionExecutionContextExtensions.CreateTriggerIndexingPropertiesFrom(context.Wor |
| | 61 | 52 | | applicationProperties[ExpressionExecutionContextExtensions.ActivityKey] = trigger; |
| | 61 | 53 | | var expressionExecutionContext = new ExpressionExecutionContext(serviceProvider, register, null, applicationProp |
| | | 54 | | |
| | | 55 | | // Evaluate activity inputs before requesting trigger data. |
| | 526 | 56 | | foreach (var namedInput in assignedInputs) |
| | | 57 | | { |
| | 686 | 58 | | var inputDescriptor = activityDescriptor.Inputs.FirstOrDefault(x => x.Name == namedInput.Key); |
| | | 59 | | |
| | 202 | 60 | | if (inputDescriptor == null) |
| | | 61 | | { |
| | 0 | 62 | | logger.LogWarning("Input descriptor not found for input '{InputName}'", namedInput.Key); |
| | 0 | 63 | | continue; |
| | | 64 | | } |
| | | 65 | | |
| | 202 | 66 | | if (!inputDescriptor.AutoEvaluate) |
| | | 67 | | { |
| | 0 | 68 | | logger.LogDebug("Skipping input '{InputName}' because it is not set to auto-evaluate.", namedInput.Key); |
| | 0 | 69 | | continue; |
| | | 70 | | } |
| | | 71 | | |
| | 202 | 72 | | var input = namedInput.Value; |
| | 202 | 73 | | var locationReference = input.MemoryBlockReference(); |
| | | 74 | | |
| | 202 | 75 | | if (locationReference.Id == null!) |
| | | 76 | | continue; |
| | | 77 | | |
| | | 78 | | try |
| | | 79 | | { |
| | 202 | 80 | | var value = await expressionEvaluator.EvaluateAsync(input, expressionExecutionContext); |
| | 202 | 81 | | locationReference.Set(expressionExecutionContext, value); |
| | 202 | 82 | | } |
| | 0 | 83 | | catch (Exception e) |
| | | 84 | | { |
| | 0 | 85 | | logger.LogWarning(e, "Failed to evaluate '{@Expression}'", input.Expression); |
| | 0 | 86 | | } |
| | 202 | 87 | | } |
| | | 88 | | |
| | 61 | 89 | | return expressionExecutionContext; |
| | 61 | 90 | | } |
| | | 91 | | } |