| | | 1 | | using System.Security.Cryptography; |
| | | 2 | | using System.Text; |
| | | 3 | | using Elsa.Workflows.Activities; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.Models; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Hold a reference to a <see cref="Workflow"/> and a collection of <see cref="ActivityNode"/> instances representing t |
| | | 9 | | /// </summary> |
| | | 10 | | public record WorkflowGraph |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Initializes a new instance of the <see cref="WorkflowGraph"/> class. |
| | | 14 | | /// </summary> |
| | 1995 | 15 | | public WorkflowGraph(Workflow workflow, ActivityNode root, IEnumerable<ActivityNode> nodes) |
| | | 16 | | { |
| | 1995 | 17 | | using var hashAlgorithm = SHA256.Create(); |
| | 1995 | 18 | | Workflow = workflow; |
| | 1995 | 19 | | Root = root; |
| | 1995 | 20 | | Nodes = nodes.ToList(); |
| | 10957 | 21 | | NodeIdLookup = Nodes.ToDictionary(x => x.NodeId); |
| | 10957 | 22 | | NodeHashLookup = Nodes.ToDictionary(x => Hash(hashAlgorithm, x.NodeId)); |
| | 10957 | 23 | | NodeActivityLookup = Nodes.ToDictionary(x => x.Activity); |
| | 3990 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets the workflow. |
| | | 28 | | /// </summary> |
| | 34875 | 29 | | public Workflow Workflow { get; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets the root node. |
| | | 33 | | /// </summary> |
| | 222 | 34 | | public ActivityNode Root { get; } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets a flat collection of all nodes in the workflow. |
| | | 38 | | /// </summary> |
| | 6849 | 39 | | public ICollection<ActivityNode> Nodes { get; } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets a lookup of nodes by their activity. |
| | | 43 | | /// </summary> |
| | 34725 | 44 | | public IDictionary<IActivity, ActivityNode> NodeActivityLookup { get; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets a lookup of nodes by their hash. |
| | | 48 | | /// </summary> |
| | 0 | 49 | | public IDictionary<string, ActivityNode> NodeHashLookup { get; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets a lookup of nodes by their ID. |
| | | 53 | | /// </summary> |
| | 226 | 54 | | public IDictionary<string, ActivityNode> NodeIdLookup { get; } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Finds the activity based on the provided <paramref name="handle"/>. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="handle">The handle containing the identification parameters for the activity.</param> |
| | | 60 | | /// <returns>The activity found based on the handle, or null if no activity is found.</returns> |
| | | 61 | | public IActivity? FindActivity(ActivityHandle handle) |
| | | 62 | | { |
| | 0 | 63 | | return handle.ActivityId != null |
| | 0 | 64 | | ? FindActivityById(handle.ActivityId) |
| | 0 | 65 | | : handle.ActivityNodeId != null |
| | 0 | 66 | | ? FindActivityByNodeId(handle.ActivityNodeId) |
| | 0 | 67 | | : handle.ActivityHash != null |
| | 0 | 68 | | ? FindActivityByHash(handle.ActivityHash) |
| | 0 | 69 | | : null; |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Returns the <see cref="ActivityNode"/> with the specified activity ID from the workflow graph. |
| | | 74 | | /// </summary> |
| | 0 | 75 | | public ActivityNode? FindNodeById(string nodeId) => NodeIdLookup.TryGetValue(nodeId, out var node) ? node : null; |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Returns the <see cref="ActivityNode"/> with the specified hash of the activity node ID from the workflow graph. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="hash">The hash of the activity node ID.</param> |
| | | 81 | | /// <returns>The <see cref="ActivityNode"/> with the specified hash of the activity node ID.</returns> |
| | 0 | 82 | | public ActivityNode? FindNodeByHash(string hash) => NodeHashLookup.TryGetValue(hash, out var node) ? node : null; |
| | | 83 | | |
| | | 84 | | /// Returns the <see cref="ActivityNode"/> containing the specified activity from the workflow graph. |
| | | 85 | | public ActivityNode? FindNodeByActivity(IActivity activity) |
| | | 86 | | { |
| | 0 | 87 | | return NodeActivityLookup.TryGetValue(activity, out var node) ? node : null; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | /// Returns the <see cref="ActivityNode"/> associated with the specified activity ID. |
| | 0 | 91 | | public ActivityNode? FindNodeByActivityId(string activityId) => Nodes.FirstOrDefault(x => x.Activity.Id == activityI |
| | | 92 | | |
| | | 93 | | /// Returns the <see cref="IActivity"/> with the specified ID from the workflow graph. |
| | 0 | 94 | | public IActivity? FindActivityByNodeId(string nodeId) => FindNodeById(nodeId)?.Activity; |
| | | 95 | | |
| | | 96 | | /// Returns the <see cref="IActivity"/> with the specified ID from the workflow graph. |
| | 0 | 97 | | public IActivity? FindActivityById(string activityId) => FindNodeById(NodeIdLookup.SingleOrDefault(n => n.Key.EndsWi |
| | | 98 | | |
| | | 99 | | /// Returns the <see cref="IActivity"/> with the specified hash of the activity node ID from the workflow graph. |
| | | 100 | | /// <param name="hash">The hash of the activity node ID.</param> |
| | | 101 | | /// <returns>The <see cref="IActivity"/> with the specified hash of the activity node ID.</returns> |
| | 0 | 102 | | public IActivity? FindActivityByHash(string hash) => FindNodeByHash(hash)?.Activity; |
| | | 103 | | |
| | | 104 | | private static string Hash(HashAlgorithm hashAlgorithm, string input) |
| | | 105 | | { |
| | 8962 | 106 | | var data = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(input)); |
| | 8962 | 107 | | return Convert.ToHexString(data); |
| | | 108 | | } |
| | | 109 | | } |