< Summary

Information
Class: Elsa.Workflows.Models.WorkflowGraph
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Models/WorkflowGraph.cs
Line coverage
51%
Covered lines: 16
Uncovered lines: 15
Coverable lines: 31
Total lines: 109
Line coverage: 51.6%
Branch coverage
0%
Covered branches: 0
Total branches: 18
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Workflow()100%11100%
get_Root()100%11100%
get_Nodes()100%11100%
get_NodeActivityLookup()100%11100%
get_NodeHashLookup()100%210%
get_NodeIdLookup()100%11100%
FindActivity(...)0%4260%
FindNodeById(...)0%620%
FindNodeByHash(...)0%620%
FindNodeByActivity(...)0%620%
FindNodeByActivityId(...)100%210%
FindActivityByNodeId(...)0%620%
FindActivityById(...)0%620%
FindActivityByHash(...)0%620%
Hash(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Models/WorkflowGraph.cs

#LineLine coverage
 1using System.Security.Cryptography;
 2using System.Text;
 3using Elsa.Workflows.Activities;
 4
 5namespace 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>
 10public record WorkflowGraph
 11{
 12    /// <summary>
 13    /// Initializes a new instance of the <see cref="WorkflowGraph"/> class.
 14    /// </summary>
 199515    public WorkflowGraph(Workflow workflow, ActivityNode root, IEnumerable<ActivityNode> nodes)
 16    {
 199517        using var hashAlgorithm = SHA256.Create();
 199518        Workflow = workflow;
 199519        Root = root;
 199520        Nodes = nodes.ToList();
 1095721        NodeIdLookup = Nodes.ToDictionary(x => x.NodeId);
 1095722        NodeHashLookup = Nodes.ToDictionary(x => Hash(hashAlgorithm, x.NodeId));
 1095723        NodeActivityLookup = Nodes.ToDictionary(x => x.Activity);
 399024    }
 25
 26    /// <summary>
 27    /// Gets the workflow.
 28    /// </summary>
 3487529    public Workflow Workflow { get; }
 30
 31    /// <summary>
 32    /// Gets the root node.
 33    /// </summary>
 22234    public ActivityNode Root { get; }
 35
 36    /// <summary>
 37    /// Gets a flat collection of all nodes in the workflow.
 38    /// </summary>
 684939    public ICollection<ActivityNode> Nodes { get; }
 40
 41    /// <summary>
 42    /// Gets a lookup of nodes by their activity.
 43    /// </summary>
 3472544    public IDictionary<IActivity, ActivityNode> NodeActivityLookup { get; }
 45
 46    /// <summary>
 47    /// Gets a lookup of nodes by their hash.
 48    /// </summary>
 049    public IDictionary<string, ActivityNode> NodeHashLookup { get; }
 50
 51    /// <summary>
 52    /// Gets a lookup of nodes by their ID.
 53    /// </summary>
 22654    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    {
 063        return handle.ActivityId != null
 064            ? FindActivityById(handle.ActivityId)
 065            : handle.ActivityNodeId != null
 066                ? FindActivityByNodeId(handle.ActivityNodeId)
 067                : handle.ActivityHash != null
 068                    ? FindActivityByHash(handle.ActivityHash)
 069                    : null;
 70    }
 71
 72    /// <summary>
 73    /// Returns the <see cref="ActivityNode"/> with the specified activity ID from the workflow graph.
 74    /// </summary>
 075    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>
 082    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    {
 087        return NodeActivityLookup.TryGetValue(activity, out var node) ? node : null;
 88    }
 89
 90    /// Returns the <see cref="ActivityNode"/> associated with the specified activity ID.
 091    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.
 094    public IActivity? FindActivityByNodeId(string nodeId) => FindNodeById(nodeId)?.Activity;
 95
 96    /// Returns the <see cref="IActivity"/> with the specified ID from the workflow graph.
 097    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>
 0102    public IActivity? FindActivityByHash(string hash) => FindNodeByHash(hash)?.Activity;
 103
 104    private static string Hash(HashAlgorithm hashAlgorithm, string input)
 105    {
 8962106        var data = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(input));
 8962107        return Convert.ToHexString(data);
 108    }
 109}