| | | 1 | | namespace Elsa.Workflows.Models; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents an activity in the context of an hierarchical tree structure, providing access to its siblings, parents a |
| | | 5 | | /// </summary> |
| | | 6 | | public class ActivityNode |
| | | 7 | | { |
| | 17580 | 8 | | private readonly List<ActivityNode> _parents = new(); |
| | 17580 | 9 | | private readonly List<ActivityNode> _children = new(); |
| | | 10 | | private string? _nodeId; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Initializes a new instance of the <see cref="ActivityNode"/> class. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="activity">The activity.</param> |
| | | 16 | | /// <param name="port">The port to which the activity belongs.</param> |
| | 17580 | 17 | | public ActivityNode(IActivity activity, string port) |
| | | 18 | | { |
| | 17580 | 19 | | Activity = activity; |
| | 17580 | 20 | | Port = port; |
| | 17580 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets the node ID. |
| | | 25 | | /// </summary> |
| | | 26 | | public string NodeId |
| | | 27 | | { |
| | | 28 | | get |
| | | 29 | | { |
| | 44119 | 30 | | if (_nodeId == null) |
| | | 31 | | { |
| | 44086 | 32 | | var ancestorIds = Ancestors().Reverse().Select(x => x.Activity.Id).ToList(); |
| | 15790 | 33 | | _nodeId = ancestorIds.Any() ? $"{string.Join(":", ancestorIds)}:{Activity.Id}" : Activity.Id; |
| | | 34 | | } |
| | | 35 | | |
| | 44119 | 36 | | return _nodeId; |
| | | 37 | | } |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets the activity. |
| | | 42 | | /// </summary> |
| | 253335 | 43 | | public IActivity Activity { get; } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets the port to which the activity belongs. |
| | | 47 | | /// </summary> |
| | 1 | 48 | | public string Port { get; } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets the parents of this node. |
| | | 52 | | /// </summary> |
| | 45564 | 53 | | public IReadOnlyCollection<ActivityNode> Parents => _parents.AsReadOnly(); |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets the children of this node. |
| | | 57 | | /// </summary> |
| | 18241 | 58 | | public ICollection<ActivityNode> Children => _children.AsReadOnly(); |
| | | 59 | | |
| | | 60 | | public void AddParent(ActivityNode parent) |
| | | 61 | | { |
| | 13258 | 62 | | _parents.Add(parent); |
| | 13258 | 63 | | _nodeId = null; |
| | 13258 | 64 | | } |
| | | 65 | | |
| | | 66 | | public void AddChild(ActivityNode child) |
| | | 67 | | { |
| | 13258 | 68 | | _children.Add(child); |
| | 13258 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets the descendants of this node. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <returns></returns> |
| | | 75 | | public IEnumerable<ActivityNode> Descendants() |
| | | 76 | | { |
| | 0 | 77 | | foreach (var child in Children) |
| | | 78 | | { |
| | 0 | 79 | | yield return child; |
| | | 80 | | |
| | 0 | 81 | | var descendants = child.Descendants(); |
| | | 82 | | |
| | 0 | 83 | | foreach (var descendant in descendants) |
| | 0 | 84 | | yield return descendant; |
| | 0 | 85 | | } |
| | 0 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Gets the ancestors of this node. |
| | | 90 | | /// </summary> |
| | | 91 | | public IEnumerable<ActivityNode> Ancestors() |
| | | 92 | | { |
| | 149126 | 93 | | foreach (var parent in Parents) |
| | | 94 | | { |
| | 29750 | 95 | | yield return parent; |
| | | 96 | | |
| | 28331 | 97 | | var ancestors = parent.Ancestors(); |
| | | 98 | | |
| | 123549 | 99 | | foreach (var ancestor in ancestors) |
| | 33461 | 100 | | yield return ancestor; |
| | 28296 | 101 | | } |
| | 44086 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Gets the siblings of this node. |
| | | 106 | | /// </summary> |
| | 0 | 107 | | public IEnumerable<ActivityNode> Siblings() => Parents.SelectMany(parent => parent.Children); |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Gets the siblings and cousins of this node. |
| | | 111 | | /// </summary> |
| | | 112 | | /// <returns></returns> |
| | 0 | 113 | | public IEnumerable<ActivityNode> SiblingsAndCousins() => Parents.SelectMany(parent => parent.Descendants()); |
| | | 114 | | } |