| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Workflows.Activities; |
| | | 3 | | using Elsa.Workflows.Models; |
| | | 4 | | using Humanizer; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows; |
| | | 8 | | |
| | | 9 | | /// <inheritdoc /> |
| | 851 | 10 | | public class IdentityGraphService(IActivityVisitor activityVisitor, IActivityRegistryLookupService activityRegistryLooku |
| | | 11 | | { |
| | | 12 | | /// <inheritdoc /> |
| | | 13 | | public async Task AssignIdentitiesAsync(Workflow workflow, CancellationToken cancellationToken = default) |
| | | 14 | | { |
| | 644 | 15 | | await AssignIdentitiesAsync((IActivity)workflow, cancellationToken); |
| | 644 | 16 | | } |
| | | 17 | | |
| | | 18 | | /// <inheritdoc /> |
| | | 19 | | public async Task AssignIdentitiesAsync(IActivity root, CancellationToken cancellationToken = default) |
| | | 20 | | { |
| | 644 | 21 | | var graph = await activityVisitor.VisitAsync(root, cancellationToken); |
| | 644 | 22 | | await AssignIdentitiesAsync(graph); |
| | 644 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | 644 | 26 | | public Task AssignIdentitiesAsync(ActivityNode root) => AssignIdentitiesAsync(root.Flatten().ToList()); |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | public async Task AssignIdentitiesAsync(ICollection<ActivityNode> flattenedList) |
| | | 30 | | { |
| | 3658 | 31 | | var identityCounters = new Dictionary<string, int>(); |
| | | 32 | | |
| | 38870 | 33 | | foreach (var node in flattenedList) |
| | | 34 | | { |
| | 15777 | 35 | | node.Activity.Id = CreateId(node, identityCounters, flattenedList); |
| | 15777 | 36 | | node.Activity.NodeId = node.NodeId; |
| | 15777 | 37 | | await AssignInputOutputsAsync(node.Activity); |
| | | 38 | | |
| | 15777 | 39 | | if (node.Activity is IVariableContainer variableContainer) |
| | 8824 | 40 | | AssignVariables(variableContainer); |
| | 15777 | 41 | | } |
| | 3658 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public async Task AssignInputOutputsAsync(IActivity activity) |
| | | 46 | | { |
| | 15777 | 47 | | var activityDescriptor = await activityRegistryLookup.FindAsync(activity.Type, activity.Version); |
| | | 48 | | |
| | 15777 | 49 | | if (activityDescriptor == null!) |
| | | 50 | | { |
| | 642 | 51 | | logger.LogWarning("Activity descriptor not found for activity type {ActivityType}. Skipping identity assignm |
| | 642 | 52 | | return; |
| | | 53 | | } |
| | | 54 | | |
| | 15135 | 55 | | var inputDictionary = activityDescriptor.GetWrappedInputProperties(activity); |
| | | 56 | | |
| | 59820 | 57 | | foreach (var (inputName, input) in inputDictionary) |
| | | 58 | | { |
| | 14775 | 59 | | var blockReference = input?.MemoryBlockReference(); |
| | | 60 | | |
| | 14775 | 61 | | if (blockReference == null!) |
| | | 62 | | continue; |
| | | 63 | | |
| | 10069 | 64 | | if (string.IsNullOrEmpty(blockReference.Id)) |
| | 6163 | 65 | | blockReference.Id = $"{activity.Id}:input-{inputName.Humanize().Kebaberize()}"; |
| | | 66 | | } |
| | | 67 | | |
| | 15135 | 68 | | var outputs = activity.GetOutputs(); |
| | | 69 | | |
| | 34854 | 70 | | foreach (var output in outputs) |
| | | 71 | | { |
| | 2292 | 72 | | var blockReference = output.Value.MemoryBlockReference(); |
| | | 73 | | |
| | 2292 | 74 | | if (blockReference == null!) |
| | | 75 | | continue; |
| | | 76 | | |
| | 2292 | 77 | | if (string.IsNullOrEmpty(blockReference.Id)) |
| | 1080 | 78 | | blockReference.Id = $"{activity.Id}:output-{output.Name.Humanize().Kebaberize()}"; |
| | | 79 | | } |
| | 15777 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <inheritdoc /> |
| | | 83 | | public void AssignVariables(IVariableContainer activity) |
| | | 84 | | { |
| | 8824 | 85 | | var variables = activity.Variables; |
| | 8824 | 86 | | var seed = 0; |
| | | 87 | | |
| | 18744 | 88 | | foreach (var variable in variables) |
| | 548 | 89 | | variable.Id = variable.Id != null! ? variable.Id : $"{activity.Id}:variable-{++seed}"; |
| | 8824 | 90 | | } |
| | | 91 | | |
| | | 92 | | private string CreateId(ActivityNode activityNode, IDictionary<string, int> identityCounters, ICollection<ActivityNo |
| | | 93 | | { |
| | 15777 | 94 | | if (!string.IsNullOrWhiteSpace(activityNode.Activity.Id)) |
| | 9153 | 95 | | return activityNode.Activity.Id; |
| | | 96 | | |
| | | 97 | | while (true) |
| | | 98 | | { |
| | 7340 | 99 | | var fullTypeName = activityNode.Activity.Type; |
| | 7340 | 100 | | var shortTypeName = fullTypeName.Split('.').Last(); |
| | 7340 | 101 | | var index = GetNextIndexFor(shortTypeName, identityCounters); |
| | 7340 | 102 | | var name = $"{shortTypeName}{index + 1}"; |
| | | 103 | | |
| | 43064 | 104 | | if (allNodes.All(x => x.Activity.Id != name)) |
| | 6624 | 105 | | return name; |
| | | 106 | | } |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | private int GetNextIndexFor(string activityType, IDictionary<string, int> identityCounters) |
| | | 110 | | { |
| | 7340 | 111 | | if (!identityCounters.TryGetValue(activityType, out var index)) |
| | | 112 | | { |
| | 5961 | 113 | | identityCounters[activityType] = index; |
| | | 114 | | } |
| | | 115 | | else |
| | | 116 | | { |
| | 1379 | 117 | | index = identityCounters[activityType] + 1; |
| | 1379 | 118 | | identityCounters[activityType] = index; |
| | | 119 | | } |
| | | 120 | | |
| | 7340 | 121 | | return index; |
| | | 122 | | } |
| | | 123 | | } |