| | | 1 | | using System.Text.Json.Nodes; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using Elsa.Api.Client.Extensions; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Api.Client.Shared.Models; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Represents an activity in the context of an hierarchical tree structure, providing access to its siblings, parents a |
| | | 9 | | /// </summary> |
| | | 10 | | public class ActivityNode |
| | | 11 | | { |
| | | 12 | | [JsonConstructor] |
| | 1 | 13 | | public ActivityNode() |
| | | 14 | | { |
| | 1 | 15 | | } |
| | | 16 | | |
| | 0 | 17 | | public ActivityNode(JsonObject activity) |
| | | 18 | | { |
| | 0 | 19 | | Activity = activity; |
| | 0 | 20 | | NodeId = activity.GetNodeId(); |
| | 0 | 21 | | } |
| | | 22 | | |
| | 0 | 23 | | public ActivityNode(JsonObject activity, string port) : this(activity) |
| | | 24 | | { |
| | 0 | 25 | | Port = port; |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Gets the activity. |
| | | 30 | | /// </summary> |
| | 3 | 31 | | public JsonObject Activity { get; set; } = default!; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets the property name that contains the activity. |
| | | 35 | | /// </summary> |
| | 1 | 36 | | public string? Port { get; set; } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets the node ID. |
| | | 40 | | /// </summary> |
| | 1 | 41 | | public string NodeId { get; set; } = default!; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets the parents of this node. |
| | | 45 | | /// </summary> |
| | 1 | 46 | | public ICollection<ActivityNode> Parents { get; set; } = new List<ActivityNode>(); |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets the children of this node. |
| | | 50 | | /// </summary> |
| | 1 | 51 | | public ICollection<ActivityNode> Children { get; set; } = new List<ActivityNode>(); |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Gets the descendants of this node. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <returns></returns> |
| | | 57 | | public IEnumerable<ActivityNode> Descendants() |
| | | 58 | | { |
| | 0 | 59 | | foreach (var child in Children) |
| | | 60 | | { |
| | 0 | 61 | | yield return child; |
| | | 62 | | |
| | 0 | 63 | | var descendants = child.Descendants(); |
| | | 64 | | |
| | 0 | 65 | | foreach (var descendant in descendants) |
| | 0 | 66 | | yield return descendant; |
| | 0 | 67 | | } |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets the ancestors of this node. |
| | | 72 | | /// </summary> |
| | | 73 | | public IEnumerable<ActivityNode> Ancestors() |
| | | 74 | | { |
| | 0 | 75 | | foreach (var parent in Parents) |
| | | 76 | | { |
| | 0 | 77 | | yield return parent; |
| | | 78 | | |
| | 0 | 79 | | var ancestors = parent.Ancestors(); |
| | | 80 | | |
| | 0 | 81 | | foreach (var ancestor in ancestors) |
| | 0 | 82 | | yield return ancestor; |
| | 0 | 83 | | } |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Gets the siblings of this node. |
| | | 88 | | /// </summary> |
| | 0 | 89 | | public IEnumerable<ActivityNode> Siblings() => Parents.SelectMany(parent => parent.Children); |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Gets the siblings and cousins of this node. |
| | | 93 | | /// </summary> |
| | | 94 | | /// <returns></returns> |
| | 0 | 95 | | public IEnumerable<ActivityNode> SiblingsAndCousins() => Parents.SelectMany(parent => parent.Descendants()); |
| | | 96 | | } |