< Summary

Information
Class: Elsa.Workflows.ActivityVisitor
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Services/ActivityVisitor.cs
Line coverage
97%
Covered lines: 44
Uncovered lines: 1
Coverable lines: 45
Total lines: 91
Line coverage: 97.7%
Branch coverage
91%
Covered branches: 11
Total branches: 12
Branch coverage: 91.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
VisitAsync()100%11100%
VisitRecursiveAsync()100%22100%
VisitPortsRecursiveAsync()90%101094.11%
get_CollectedActivities()100%11100%
get_CollectedNodes()100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Services/ActivityVisitor.cs

#LineLine coverage
 1using ActivityNode = Elsa.Workflows.Models.ActivityNode;
 2
 3namespace Elsa.Workflows;
 4
 5/// <inheritdoc />
 6public class ActivityVisitor : IActivityVisitor
 7{
 8    private readonly IServiceProvider _serviceProvider;
 9    private readonly IEnumerable<IActivityResolver> _portResolvers;
 10
 11    /// <summary>
 12    /// Initializes a new instance of the <see cref="ActivityVisitor"/> class.
 13    /// </summary>
 85114    public ActivityVisitor(IEnumerable<IActivityResolver> portResolvers, IServiceProvider serviceProvider)
 15    {
 85116        _serviceProvider = serviceProvider;
 332417        _portResolvers = portResolvers.OrderByDescending(x => x.Priority).ToList();
 85118    }
 19
 20    /// <inheritdoc />
 21    public async Task<ActivityNode> VisitAsync(IActivity activity, CancellationToken cancellationToken = default)
 22    {
 418523        var graph = new ActivityNode(activity, "Root");
 418524        var collectedNodes = new HashSet<ActivityNode>([
 418525            graph
 418526        ]);
 418527        var collectedActivities = new HashSet<IActivity>([
 418528            activity
 418529        ]);
 418530        var visitorContext = new ActivityVisitorContext
 418531        {
 418532            CollectedActivities = collectedActivities,
 418533            CollectedNodes = collectedNodes
 418534        };
 35
 418536        await VisitRecursiveAsync((graph, activity), visitorContext, cancellationToken);
 418537        return graph;
 418538    }
 39
 40    private async Task VisitRecursiveAsync((ActivityNode Node, IActivity Activity) pair, ActivityVisitorContext visitorC
 41    {
 1744342        if (pair.Activity is IInitializable initializable)
 43        {
 78544            var context = new InitializationContext(_serviceProvider, cancellationToken);
 78545            await initializable.InitializeAsync(context);
 46        }
 47
 1744348        await VisitPortsRecursiveAsync(pair, visitorContext, cancellationToken);
 1744349    }
 50
 51    private async Task VisitPortsRecursiveAsync((ActivityNode Node, IActivity Activity) pair, ActivityVisitorContext vis
 52    {
 8140053        var resolver = _portResolvers.FirstOrDefault(x => x.GetSupportsActivity(pair.Activity));
 54
 1744355        if (resolver == null)
 056            return;
 57
 1744358        var activityPorts = await resolver.GetActivityPortsAsync(pair.Activity, cancellationToken);
 1744359        var collectedActivities = visitorContext.CollectedActivities;
 1744360        var collectedNodes = visitorContext.CollectedNodes;
 61
 5492662        foreach (var activityPort in activityPorts)
 63        {
 4678864            foreach (var activity in activityPort.GetActivities())
 65            {
 66                // Continue if the specified activity was already encountered.
 1337467                if (collectedActivities.Contains(activity))
 68                    continue;
 69
 5167470                var childNode = collectedNodes.FirstOrDefault(x => x.Activity == activity);
 71
 1325872                if (childNode == null)
 73                {
 1325874                    childNode = new(activity, activityPort.PortName);
 1325875                    collectedNodes.Add(childNode);
 76                }
 77
 1325878                childNode.AddParent(pair.Node);
 1325879                pair.Node.AddChild(childNode);
 1325880                collectedActivities.Add(activity);
 1325881                await VisitRecursiveAsync((childNode, activity), visitorContext, cancellationToken);
 82            }
 1002083        }
 1744384    }
 85
 86    private class ActivityVisitorContext
 87    {
 2581388        public HashSet<IActivity> CollectedActivities { get; set; } = [];
 2581389        public HashSet<ActivityNode> CollectedNodes { get; set; } = [];
 90    }
 91}