| | | 1 | | using System.Text.Json.Serialization; |
| | | 2 | | |
| | | 3 | | namespace Elsa.Workflows.Activities.Flowchart.Models; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents a scope for tracking activity and connection visits within a flowchart execution. |
| | | 7 | | /// </summary> |
| | | 8 | | public class FlowScope |
| | | 9 | | { |
| | | 10 | | [JsonConstructor] |
| | 28 | 11 | | public FlowScope() |
| | | 12 | | { |
| | 28 | 13 | | } |
| | | 14 | | |
| | | 15 | | [JsonInclude] |
| | 564 | 16 | | private Dictionary<string, long> ActivitiesVisitCount { get; init; } = new(); |
| | | 17 | | |
| | | 18 | | [JsonInclude] |
| | 686 | 19 | | private Dictionary<string, long> ConnectionVisitCount { get; init; } = new(); |
| | | 20 | | |
| | | 21 | | [JsonInclude] |
| | 291 | 22 | | private Dictionary<string, bool> ConnectionLastVisitFollowed { get; init; } = new(); |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Registers a visit to the specified activity, incrementing its visit count. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="activity">The activity being visited.</param> |
| | | 28 | | public void RegisterActivityVisit(IActivity activity) |
| | | 29 | | { |
| | 133 | 30 | | string activityId = activity.Id; |
| | 133 | 31 | | ActivitiesVisitCount.TryAdd(activityId, 0); |
| | 133 | 32 | | ActivitiesVisitCount[activityId]++; |
| | 133 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets the number of times the specified activity has been visited. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="activity">The activity to check.</param> |
| | | 39 | | /// <returns>The visit count of the activity.</returns> |
| | 242 | 40 | | private long GetActivityVisitCount(IActivity activity) => ActivitiesVisitCount.GetValueOrDefault(activity.Id, 0); |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Registers a visit to the specified connection and records whether it was followed. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="connection">The connection being visited.</param> |
| | | 46 | | /// <param name="followed">Indicates whether the connection was followed.</param> |
| | | 47 | | public void RegisterConnectionVisit(Connection connection, bool followed) |
| | | 48 | | { |
| | 118 | 49 | | var connectionId = connection.ToString(); |
| | 118 | 50 | | ConnectionVisitCount.TryAdd(connectionId, 0); |
| | 118 | 51 | | ConnectionVisitCount[connectionId]++; |
| | 118 | 52 | | ConnectionLastVisitFollowed[connectionId] = followed; |
| | 118 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets the number of times the specified connection has been visited. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="connection">The connection to check.</param> |
| | | 59 | | /// <returns>The visit count of the connection.</returns> |
| | 394 | 60 | | private long GetConnectionVisitCount(Connection connection) => ConnectionVisitCount.GetValueOrDefault(connection.ToS |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Determines whether the last visit to the specified connection was followed. |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="connection">The connection to check.</param> |
| | | 66 | | /// <returns>True if the connection was followed on the last visit, otherwise false.</returns> |
| | 117 | 67 | | public bool GetConnectionLastVisitFollowed(Connection connection) => ConnectionLastVisitFollowed.GetValueOrDefault(c |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Determines whether all inbound connections to the specified activity have been visited. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <param name="flowGraph">The flow graph containing connections.</param> |
| | | 73 | | /// <param name="activity">The activity to check.</param> |
| | | 74 | | /// <returns>True if all inbound connections have been visited, otherwise false.</returns> |
| | | 75 | | public bool AllInboundConnectionsVisited(FlowGraph flowGraph, IActivity activity) |
| | | 76 | | { |
| | 102 | 77 | | var forwardInboundConnections = flowGraph.GetForwardInboundConnections(activity); |
| | 102 | 78 | | var outboundActivityVisitCount = GetActivityVisitCount(activity); |
| | 102 | 79 | | var minConnectionVisitCount = forwardInboundConnections.Min(GetConnectionVisitCount); |
| | 102 | 80 | | return minConnectionVisitCount > outboundActivityVisitCount; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Determines whether any inbound connection to the specified activity has been followed. |
| | | 85 | | /// </summary> |
| | | 86 | | /// <param name="flowGraph">The flow graph containing connections.</param> |
| | | 87 | | /// <param name="activity">The activity to check.</param> |
| | | 88 | | /// <returns>True if any inbound connection has been followed, otherwise false.</returns> |
| | | 89 | | public bool HasFollowedInboundConnection(FlowGraph flowGraph, IActivity activity) |
| | | 90 | | { |
| | 116 | 91 | | var forwardInboundConnections = flowGraph.GetForwardInboundConnections(activity); |
| | 116 | 92 | | var outboundActivityVisitCount = GetActivityVisitCount(activity); |
| | 116 | 93 | | var maxConnectionVisitCount = forwardInboundConnections.Max(GetConnectionVisitCount); |
| | 116 | 94 | | return maxConnectionVisitCount > outboundActivityVisitCount |
| | 233 | 95 | | && forwardInboundConnections.Any(c => GetConnectionVisitCount(c) == maxConnectionVisitCount && GetConnection |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Determines whether a connection should be ignored based on visit counts. |
| | | 100 | | /// </summary> |
| | | 101 | | /// <param name="connection">The connection to check.</param> |
| | | 102 | | /// <param name="activity">The activity associated with the connection.</param> |
| | | 103 | | /// <returns>True if the connection should be ignored, otherwise false.</returns> |
| | | 104 | | public bool ShouldIgnoreConnection(Connection connection, IActivity activity) |
| | | 105 | | { |
| | 24 | 106 | | var connectionVisitCount = GetConnectionVisitCount(connection); |
| | 24 | 107 | | var activityVisitCount = GetActivityVisitCount(activity); |
| | 24 | 108 | | return connectionVisitCount <= activityVisitCount; |
| | | 109 | | } |
| | | 110 | | } |