< Summary

Information
Class: Elsa.Workflows.Activities.Flowchart.Models.FlowScope
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Activities/Flowchart/Models/FlowScope.cs
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 110
Line coverage: 100%
Branch coverage
50%
Covered branches: 2
Total branches: 4
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Activities/Flowchart/Models/FlowScope.cs

#LineLine coverage
 1using System.Text.Json.Serialization;
 2
 3namespace Elsa.Workflows.Activities.Flowchart.Models;
 4
 5/// <summary>
 6/// Represents a scope for tracking activity and connection visits within a flowchart execution.
 7/// </summary>
 8public class FlowScope
 9{
 10    [JsonConstructor]
 2811    public FlowScope()
 12    {
 2813    }
 14
 15    [JsonInclude]
 56416    private Dictionary<string, long> ActivitiesVisitCount { get; init; } = new();
 17
 18    [JsonInclude]
 68619    private Dictionary<string, long> ConnectionVisitCount { get; init; } = new();
 20
 21    [JsonInclude]
 29122    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    {
 13330        string activityId = activity.Id;
 13331        ActivitiesVisitCount.TryAdd(activityId, 0);
 13332        ActivitiesVisitCount[activityId]++;
 13333    }
 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>
 24240    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    {
 11849        var connectionId = connection.ToString();
 11850        ConnectionVisitCount.TryAdd(connectionId, 0);
 11851        ConnectionVisitCount[connectionId]++;
 11852        ConnectionLastVisitFollowed[connectionId] = followed;
 11853    }
 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>
 39460    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>
 11767    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    {
 10277        var forwardInboundConnections = flowGraph.GetForwardInboundConnections(activity);
 10278        var outboundActivityVisitCount = GetActivityVisitCount(activity);
 10279        var minConnectionVisitCount = forwardInboundConnections.Min(GetConnectionVisitCount);
 10280        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    {
 11691        var forwardInboundConnections = flowGraph.GetForwardInboundConnections(activity);
 11692        var outboundActivityVisitCount = GetActivityVisitCount(activity);
 11693        var maxConnectionVisitCount = forwardInboundConnections.Max(GetConnectionVisitCount);
 11694        return maxConnectionVisitCount > outboundActivityVisitCount
 23395            && 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    {
 24106        var connectionVisitCount = GetConnectionVisitCount(connection);
 24107        var activityVisitCount = GetActivityVisitCount(activity);
 24108        return connectionVisitCount <= activityVisitCount;
 109    }
 110}