| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Workflows.Activities.Flowchart.Extensions; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Workflows.Activities.Flowchart.Models; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Represents a directed graph structure for managing workflow connections. |
| | | 8 | | /// Caches forward and backward connections to optimize graph traversal. |
| | | 9 | | /// </summary> |
| | 692 | 10 | | public class FlowGraph(ICollection<Connection> connections, IActivity? rootActivity) |
| | | 11 | | { |
| | | 12 | | private List<Connection>? _cachedForwardConnections; |
| | 692 | 13 | | private readonly Dictionary<IActivity, List<Connection>> _cachedInboundForwardConnections = new(); |
| | 692 | 14 | | private readonly Dictionary<IActivity, List<Connection>> _cachedInboundConnections = new(); |
| | 692 | 15 | | private readonly Dictionary<IActivity, List<Connection>> _cachedOutboundConnections = new(); |
| | 692 | 16 | | private readonly Dictionary<Connection, (bool IsBackwardConnection, bool IsValid)> _cachedIsBackwardConnection = new |
| | 692 | 17 | | private readonly Dictionary<IActivity, bool> _cachedIsDanglingActivity = new(); |
| | 692 | 18 | | private readonly Dictionary<IActivity, List<IActivity>> _cachedAncestors = new(); |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Gets the list of forward connections, computing them if not already cached. |
| | | 22 | | /// </summary> |
| | 775 | 23 | | private List<Connection> ForwardConnections => _cachedForwardConnections ??= rootActivity == null ? new() : GetForwa |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Retrieves all inbound forward connections for a given activity. |
| | | 27 | | /// </summary> |
| | 1161 | 28 | | public List<Connection> GetForwardInboundConnections(IActivity activity) => _cachedInboundForwardConnections.GetOrAd |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Retrieves all outbound connections for a given activity. |
| | | 32 | | /// </summary> |
| | 2306 | 33 | | public List<Connection> GetOutboundConnections(IActivity activity) => _cachedOutboundConnections.GetOrAdd(activity, |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Retrieves all inbound connections for a given activity. |
| | | 37 | | /// </summary> |
| | 20 | 38 | | public List<Connection> GetInboundConnections(IActivity activity) => _cachedInboundConnections.GetOrAdd(activity, () |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Determines if a given activity is "dangling," meaning it does not exist as a target in any forward connection. |
| | | 42 | | /// </summary> |
| | 685 | 43 | | public bool IsDanglingActivity(IActivity activity) => _cachedIsDanglingActivity.GetOrAdd(activity, () => activity != |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Determines if a given connection is a backward connection (i.e., not part of the forward traversal) and whether |
| | | 47 | | /// </summary> |
| | | 48 | | public bool IsBackwardConnection(Connection connection, out bool isValid) |
| | | 49 | | { |
| | | 50 | | // Check if result is already cached |
| | 122 | 51 | | if (_cachedIsBackwardConnection.TryGetValue(connection, out var result)) |
| | | 52 | | { |
| | 0 | 53 | | isValid = result.IsValid; |
| | 0 | 54 | | return result.IsBackwardConnection; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | // Compute if the connection is backward |
| | 122 | 58 | | bool isBackwardConnection = !GetForwardInboundConnections(connection.Target.Activity).Contains(connection); |
| | | 59 | | |
| | | 60 | | // Compute if the backward connection is valid |
| | 122 | 61 | | isValid = isBackwardConnection && IsValidBackwardConnection(ForwardConnections, rootActivity, connection); |
| | | 62 | | |
| | | 63 | | // Cache the result |
| | 122 | 64 | | _cachedIsBackwardConnection[connection] = (isBackwardConnection, isValid); |
| | | 65 | | |
| | 122 | 66 | | return isBackwardConnection; |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Retrieves all ancestor activities for a given activity by traversing ForwardConnections in reverse. |
| | | 71 | | /// </summary> |
| | | 72 | | public List<IActivity> GetAncestorActivities(IActivity activity) |
| | | 73 | | { |
| | 110 | 74 | | return _cachedAncestors.GetOrAdd(activity, () => ComputeAncestors(activity)); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Computes the list of ancestors by following Source activities in ForwardConnections. |
| | | 79 | | /// </summary> |
| | | 80 | | private List<IActivity> ComputeAncestors(IActivity activity) |
| | | 81 | | { |
| | 53 | 82 | | HashSet<IActivity> ancestors = new(); |
| | 53 | 83 | | Queue<IActivity> queue = new(); |
| | | 84 | | |
| | | 85 | | // Find all connections where this activity is the target |
| | 628 | 86 | | foreach (var connection in ForwardConnections.Where(c => c.Target.Activity == activity)) |
| | | 87 | | { |
| | 73 | 88 | | if (ancestors.Add(connection.Source.Activity)) |
| | 71 | 89 | | queue.Enqueue(connection.Source.Activity); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | // Traverse upwards through the graph |
| | 198 | 93 | | while (queue.Count > 0) |
| | | 94 | | { |
| | 145 | 95 | | var current = queue.Dequeue(); |
| | | 96 | | |
| | 1683 | 97 | | foreach (var connection in ForwardConnections.Where(c => c.Target.Activity == current)) |
| | | 98 | | { |
| | 108 | 99 | | if (ancestors.Add(connection.Source.Activity)) |
| | 74 | 100 | | queue.Enqueue(connection.Source.Activity); |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | |
| | 53 | 104 | | return ancestors.ToList(); |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Computes the list of forward connections in the graph, excluding cyclic connections. |
| | | 109 | | /// </summary> |
| | | 110 | | private static List<Connection> GetForwardConnections(ICollection<Connection> connections, IActivity root) |
| | | 111 | | { |
| | 271 | 112 | | Dictionary<IActivity, List<IActivity>> adjList = new(); |
| | | 113 | | |
| | 1608 | 114 | | foreach (var conn in connections) |
| | | 115 | | { |
| | 533 | 116 | | if (!adjList.ContainsKey(conn.Source.Activity)) |
| | 461 | 117 | | adjList[conn.Source.Activity] = new(); |
| | | 118 | | |
| | 533 | 119 | | adjList[conn.Source.Activity].Add(conn.Target.Activity); |
| | | 120 | | } |
| | | 121 | | |
| | 271 | 122 | | HashSet<IActivity> visited = new(); |
| | 271 | 123 | | HashSet<(IActivity, IActivity)> visitedEdges = new(); |
| | 271 | 124 | | List<(IActivity Source, IActivity Target)> validEdges = new(); |
| | 271 | 125 | | Queue<IActivity> queue = new(); |
| | | 126 | | |
| | 271 | 127 | | queue.Enqueue(root); |
| | | 128 | | |
| | 1059 | 129 | | while (queue.Count > 0) |
| | | 130 | | { |
| | 788 | 131 | | var source = queue.Dequeue(); |
| | 788 | 132 | | visited.Add(source); |
| | | 133 | | |
| | 788 | 134 | | if (!adjList.ContainsKey(source)) continue; |
| | | 135 | | |
| | 2100 | 136 | | foreach (var target in adjList[source]) |
| | | 137 | | { |
| | 564 | 138 | | var edge = (source, target); |
| | 564 | 139 | | if (visitedEdges.Contains(edge)) |
| | | 140 | | continue; |
| | | 141 | | |
| | 525 | 142 | | if (HasPathToActivity(validEdges, target, source)) |
| | | 143 | | continue; |
| | | 144 | | |
| | 519 | 145 | | visitedEdges.Add(edge); |
| | 519 | 146 | | validEdges.Add((source, target)); |
| | | 147 | | |
| | 519 | 148 | | if (!visited.Contains(target)) |
| | 517 | 149 | | queue.Enqueue(target); |
| | | 150 | | } |
| | | 151 | | } |
| | | 152 | | |
| | 271 | 153 | | return validEdges |
| | 2758 | 154 | | .SelectMany(e => connections.Where(c => c.Source.Activity == e.Source && c.Target.Activity == e.Target)) |
| | 271 | 155 | | .Distinct() |
| | 271 | 156 | | .ToList(); |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Determines if there is an existing path from the source activity to the target activity. |
| | | 161 | | /// Helps in detecting cyclic connections. |
| | | 162 | | /// </summary> |
| | | 163 | | private static bool HasPathToActivity(ICollection<(IActivity Source, IActivity Target)> edges, IActivity source, IAc |
| | | 164 | | { |
| | 525 | 165 | | if (source == target) |
| | 0 | 166 | | return true; |
| | | 167 | | |
| | 525 | 168 | | HashSet<IActivity> visited = new(); |
| | 525 | 169 | | Stack<IActivity> stack = new(); |
| | 525 | 170 | | stack.Push(source); |
| | | 171 | | |
| | 1055 | 172 | | while (stack.Count > 0) |
| | | 173 | | { |
| | 536 | 174 | | var current = stack.Pop(); |
| | | 175 | | |
| | 536 | 176 | | if (current == target) |
| | 6 | 177 | | return true; |
| | | 178 | | |
| | 530 | 179 | | if (visited.Contains(current)) |
| | | 180 | | continue; |
| | | 181 | | |
| | 530 | 182 | | visited.Add(current); |
| | | 183 | | |
| | 1988 | 184 | | foreach (var next in edges.Where(x => x.Source == current).Select(e => e.Target)) |
| | | 185 | | { |
| | 15 | 186 | | if (!visited.Contains(next)) |
| | 15 | 187 | | stack.Push(next); |
| | | 188 | | } |
| | | 189 | | } |
| | | 190 | | |
| | 519 | 191 | | return false; |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | /// <summary> |
| | | 195 | | /// Determines whether a backward connection is valid by ensuring all paths from source to root pass through target. |
| | | 196 | | /// </summary> |
| | | 197 | | private static bool IsValidBackwardConnection(List<Connection> forwardConnections, IActivity? root, Connection conne |
| | | 198 | | { |
| | 3 | 199 | | if (root == null) return false; |
| | | 200 | | |
| | 3 | 201 | | var pathsToRoot = GetPathsToRoot(forwardConnections, root, connection.Source.Activity); |
| | | 202 | | |
| | 12 | 203 | | foreach (var path in pathsToRoot) |
| | | 204 | | { |
| | 4 | 205 | | if (!path.Contains(connection.Target.Activity)) |
| | 2 | 206 | | return false; |
| | | 207 | | } |
| | | 208 | | |
| | 1 | 209 | | return true; |
| | 2 | 210 | | } |
| | | 211 | | |
| | | 212 | | /// <summary> |
| | | 213 | | /// Finds all paths from a given start activity to the root using BFS. |
| | | 214 | | /// </summary> |
| | | 215 | | private static List<List<IActivity>> GetPathsToRoot(List<Connection> forwardConnections, IActivity root, IActivity s |
| | | 216 | | { |
| | 3 | 217 | | List<List<IActivity>> paths = new(); |
| | 3 | 218 | | Queue<List<IActivity>> queue = new(); |
| | 3 | 219 | | queue.Enqueue(new() |
| | 3 | 220 | | { start }); |
| | | 221 | | |
| | 28 | 222 | | while (queue.Count > 0) |
| | | 223 | | { |
| | 25 | 224 | | var path = queue.Dequeue(); |
| | 25 | 225 | | var lastNode = path.Last(); |
| | | 226 | | |
| | 25 | 227 | | if (lastNode == root) |
| | | 228 | | { |
| | 8 | 229 | | paths.Add([..path]); |
| | 8 | 230 | | continue; |
| | | 231 | | } |
| | | 232 | | |
| | 17 | 233 | | var previousNodes = forwardConnections |
| | 109 | 234 | | .Where(c => c.Target.Activity == lastNode) |
| | 39 | 235 | | .Select(c => c.Source.Activity); |
| | | 236 | | |
| | 78 | 237 | | foreach (var prev in previousNodes) |
| | | 238 | | { |
| | 22 | 239 | | if (!path.Contains(prev)) |
| | | 240 | | { |
| | 22 | 241 | | var newPath = new List<IActivity>(path) { prev }; |
| | 22 | 242 | | queue.Enqueue(newPath); |
| | | 243 | | } |
| | | 244 | | } |
| | | 245 | | } |
| | | 246 | | |
| | 3 | 247 | | return paths; |
| | | 248 | | } |
| | | 249 | | } |