From 029ee6515dde245f7928508abb01cdb32099a936 Mon Sep 17 00:00:00 2001 From: naninunenoy Date: Mon, 16 Nov 2020 01:17:52 +0900 Subject: [PATCH] NodeProcessor --- .../DependencyNode/HashSetDependencyNode.cs | 11 +- .../Editor/DependencyNode/NodeId.cs | 8 ++ .../Editor/DependencyNode/NodeProcessor.cs | 25 ++++ .../DependencyNode/NodeProcessor.cs.meta | 3 + .../Editor/DependencyNode/NodeProfile.cs | 8 ++ .../Tests/HashSetDependencyNodeTest.cs | 2 +- .../DependencyNode/Tests/NodeProcessorTest.cs | 112 ++++++++++++++++++ .../Tests/NodeProcessorTest.cs.meta | 3 + .../Editor/DependencyNode/Tests/Nodes.cs | 52 ++++++++ .../Editor/DependencyNode/Tests/Nodes.cs.meta | 3 + 10 files changed, 222 insertions(+), 5 deletions(-) create mode 100644 Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/NodeProcessor.cs create mode 100644 Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/NodeProcessor.cs.meta create mode 100644 Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/NodeProcessorTest.cs create mode 100644 Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/NodeProcessorTest.cs.meta create mode 100644 Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/Nodes.cs create mode 100644 Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/Nodes.cs.meta diff --git a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/HashSetDependencyNode.cs b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/HashSetDependencyNode.cs index 7b82076..7368efb 100644 --- a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/HashSetDependencyNode.cs +++ b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/HashSetDependencyNode.cs @@ -3,13 +3,16 @@ using System.Collections.Generic; namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode { public class HashSetDependencyNode : IDependencyNode { public NodeProfile Profile { get; } - public ICollection Sources { get; } - public ICollection Destinations { get; } + public ICollection Sources => sources; + public ICollection Destinations => destinations; + + readonly HashSet sources; + readonly HashSet destinations; public HashSetDependencyNode(NodeProfile profile) { Profile = profile; - Sources = new HashSet(); - Destinations = new HashSet(); + sources = new HashSet(); + destinations = new HashSet(); } } } diff --git a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/NodeId.cs b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/NodeId.cs index c901d19..85bb52b 100644 --- a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/NodeId.cs +++ b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/NodeId.cs @@ -21,5 +21,13 @@ namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode { public override string ToString() { return value.ToString(); } + + public static bool operator ==(NodeId x, NodeId y) { + return x.Equals(y); + } + + public static bool operator !=(NodeId x, NodeId y) { + return !(x == y); + } } } diff --git a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/NodeProcessor.cs b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/NodeProcessor.cs new file mode 100644 index 0000000..cda38d4 --- /dev/null +++ b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/NodeProcessor.cs @@ -0,0 +1,25 @@ +using System.Collections.Generic; +using System.Linq; + +namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode { + public static class NodeProcessor { + + public static void SetBeRequiredNodes(IEnumerable nodes) { + var array = nodes as IDependencyNode[] ?? nodes.ToArray(); + var dict = array.ToDictionary(x => x.Profile.Id); + foreach (var n in array) { + foreach (var d in n.Destinations) { + dict[d.Id].Sources.Add(n.Profile); + } + } + } + + public static void SetRequireNodes(this IDependencyNode node, IEnumerable requireNodes) { + node.Sources.Clear(); + node.Destinations.Clear(); + foreach (var d in requireNodes) { + node.Destinations.Add(d); + } + } + } +} diff --git a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/NodeProcessor.cs.meta b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/NodeProcessor.cs.meta new file mode 100644 index 0000000..7a5ecf7 --- /dev/null +++ b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/NodeProcessor.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6febd7e408df42d89d561ffe371c602f +timeCreated: 1605454341 \ No newline at end of file diff --git a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/NodeProfile.cs b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/NodeProfile.cs index 8d60374..fe71ff6 100644 --- a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/NodeProfile.cs +++ b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/NodeProfile.cs @@ -32,5 +32,13 @@ namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode { return (Id.GetHashCode() * 397) ^ (Name != null ? Name.GetHashCode() : 0); } } + + public static bool operator ==(NodeProfile x, NodeProfile y) { + return x.Equals(y); + } + + public static bool operator !=(NodeProfile x, NodeProfile y) { + return !(x == y); + } } } diff --git a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/HashSetDependencyNodeTest.cs b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/HashSetDependencyNodeTest.cs index d588ad8..cd670cd 100644 --- a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/HashSetDependencyNodeTest.cs +++ b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/HashSetDependencyNodeTest.cs @@ -10,7 +10,7 @@ namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Tests { public void TestHashSetDependencyNode() { var node = new HashSetDependencyNode(new NodeProfile(new NodeId(1), "node")); Assert.That(node, Is.InstanceOf()); - Assert.That(node.Profile.Id, Is.EqualTo(1)); + Assert.That(node.Profile.Id.value, Is.EqualTo(1)); Assert.That(node.Profile.Name, Is.EqualTo("node")); Assert.That(node.Sources, Is.Empty); Assert.That(node.Destinations, Is.Empty); diff --git a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/NodeProcessorTest.cs b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/NodeProcessorTest.cs new file mode 100644 index 0000000..f78592b --- /dev/null +++ b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/NodeProcessorTest.cs @@ -0,0 +1,112 @@ +using System.Collections; +using System.Linq; +using NUnit.Framework; +using UnityEditor; +using UnityEngine.TestTools; + +namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Tests { + public class NodeProcessorTest { + + [SetUp] + public void SetUpBeforeEveryTest() { + Nodes.Init(); + } + + [Test] + public void TestSetRequireNodes() { + var node = new HashSetDependencyNode(new NodeProfile(new NodeId(0), "node0")); + var p1 = new NodeProfile(new NodeId(1), "node1"); + var p2 = new NodeProfile(new NodeId(2), "node2"); + var p3 = new NodeProfile(new NodeId(3), "node3"); + node.Sources.Add(p3); + node.SetRequireNodes(new[] { p1, p2 }); + Assert.That(node.Destinations.Count, Is.EqualTo(2)); + Assert.That(node.Destinations.Any(x => x.Equals(p1))); + Assert.That(node.Destinations.Any(x => x.Equals(p2))); + Assert.That(node.Sources.Count, Is.Zero); + } + + [Test] + public void TestLinerNodeDependency() { + // [0]--->[1] + Nodes._0.SetRequireNodes(new[] { Profiles._1}); + NodeProcessor.SetBeRequiredNodes(new[] { Nodes._0, Nodes._1 }); + Assert.That(Nodes._1.Sources.Any(x => x.Equals(Profiles._0))); + } + + + static void SetSomeNodeDependency() { + /* + * ┌->[1]-┐ + * [0]-┴->[2]-┴->[4]--->[5]--->[6] + * │ │ + * V │ + * [3]<-----------┘ + * + * [7]--->[8] [9] + * + * + */ + Nodes._0.SetRequireNodes(new[] { Profiles._1, Profiles._2 }); + Nodes._1.SetRequireNodes(new[] { Profiles._4 }); + Nodes._2.SetRequireNodes(new[] { Profiles._3, Profiles._4 }); + Nodes._4.SetRequireNodes(new[] { Profiles._5 }); + Nodes._5.SetRequireNodes(new[] { Profiles._3, Profiles._6 }); + Nodes._7.SetRequireNodes(new[] { Profiles._8 }); + } + + [Test] + public void TestSomeNodeDependency() { + SetSomeNodeDependency(); + NodeProcessor.SetBeRequiredNodes(Nodes.All); + // 0 + Assert.That(Nodes._0.Sources, Is.Empty); + Assert.That(Nodes._0.Destinations.Count, Is.EqualTo(2)); + Assert.That(Nodes._0.Destinations.Any(x => x == Profiles._1)); + Assert.That(Nodes._0.Destinations.Any(x => x == Profiles._2)); + // 1 + Assert.That(Nodes._1.Destinations.Count, Is.EqualTo(1)); + Assert.That(Nodes._1.Sources.Any(x => x == Profiles._0)); + Assert.That(Nodes._1.Destinations.Count, Is.EqualTo(1)); + Assert.That(Nodes._1.Destinations.Any(x => x == Profiles._4)); + // 2 + Assert.That(Nodes._2.Sources.Count, Is.EqualTo(1)); + Assert.That(Nodes._2.Sources.Any(x => x == Profiles._0)); + Assert.That(Nodes._2.Destinations.Count, Is.EqualTo(2)); + Assert.That(Nodes._2.Destinations.Any(x => x == Profiles._3)); + Assert.That(Nodes._2.Destinations.Any(x => x == Profiles._4)); + // 3 + Assert.That(Nodes._3.Sources.Count, Is.EqualTo(2)); + Assert.That(Nodes._3.Sources.Any(x => x == Profiles._2)); + Assert.That(Nodes._3.Sources.Any(x => x == Profiles._5)); + Assert.That(Nodes._3.Destinations, Is.Empty); + // 4 + Assert.That(Nodes._4.Sources.Count, Is.EqualTo(2)); + Assert.That(Nodes._4.Sources.Any(x => x == Profiles._1)); + Assert.That(Nodes._4.Sources.Any(x => x == Profiles._2)); + Assert.That(Nodes._4.Destinations.Count, Is.EqualTo(1)); + Assert.That(Nodes._4.Destinations.Any(x => x == Profiles._5)); + // 5 + Assert.That(Nodes._5.Sources.Count, Is.EqualTo(1)); + Assert.That(Nodes._5.Sources.Any(x => x == Profiles._4)); + Assert.That(Nodes._5.Destinations.Count, Is.EqualTo(2)); + Assert.That(Nodes._5.Destinations.Any(x => x == Profiles._3)); + Assert.That(Nodes._5.Destinations.Any(x => x == Profiles._6)); + // 6 + Assert.That(Nodes._6.Sources.Count, Is.EqualTo(1)); + Assert.That(Nodes._6.Sources.Any(x => x == Profiles._5)); + Assert.That(Nodes._6.Destinations, Is.Empty); + // 7 + Assert.That(Nodes._7.Sources, Is.Empty); + Assert.That(Nodes._7.Destinations.Count, Is.EqualTo(1)); + Assert.That(Nodes._7.Destinations.Any(x => x == Profiles._8)); + // 8 + Assert.That(Nodes._8.Sources.Count, Is.EqualTo(1)); + Assert.That(Nodes._8.Sources.Any(x => x == Profiles._7)); + Assert.That(Nodes._8.Destinations, Is.Empty); + // 9 + Assert.That(Nodes._9.Sources, Is.Empty); + Assert.That(Nodes._9.Destinations, Is.Empty); + } + } +} diff --git a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/NodeProcessorTest.cs.meta b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/NodeProcessorTest.cs.meta new file mode 100644 index 0000000..97797c0 --- /dev/null +++ b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/NodeProcessorTest.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 9e86a8d93f3a4bb5a7c6d81b775efb47 +timeCreated: 1605450647 \ No newline at end of file diff --git a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/Nodes.cs b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/Nodes.cs new file mode 100644 index 0000000..132f883 --- /dev/null +++ b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/Nodes.cs @@ -0,0 +1,52 @@ +using System.Collections.Generic; + +namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Tests { + + public static class Ids { + public static readonly NodeId _0 = new NodeId(0); + public static readonly NodeId _1 = new NodeId(1); + public static readonly NodeId _2 = new NodeId(2); + public static readonly NodeId _3 = new NodeId(3); + public static readonly NodeId _4 = new NodeId(4); + public static readonly NodeId _5 = new NodeId(5); + public static readonly NodeId _6 = new NodeId(6); + public static readonly NodeId _7 = new NodeId(7); + public static readonly NodeId _8 = new NodeId(8); + public static readonly NodeId _9 = new NodeId(9); + } + public static class Profiles { + public static readonly NodeProfile _0 = new NodeProfile(Ids._0, "node0"); + public static readonly NodeProfile _1 = new NodeProfile(Ids._1, "node1"); + public static readonly NodeProfile _2 = new NodeProfile(Ids._2, "node2"); + public static readonly NodeProfile _3 = new NodeProfile(Ids._3, "node3"); + public static readonly NodeProfile _4 = new NodeProfile(Ids._4, "node4"); + public static readonly NodeProfile _5 = new NodeProfile(Ids._5, "node5"); + public static readonly NodeProfile _6 = new NodeProfile(Ids._6, "node6"); + public static readonly NodeProfile _7 = new NodeProfile(Ids._7, "node7"); + public static readonly NodeProfile _8 = new NodeProfile(Ids._8, "node8"); + public static readonly NodeProfile _9 = new NodeProfile(Ids._9, "node9"); + } + public static class Nodes { + public static readonly IDependencyNode _0 = new HashSetDependencyNode(Profiles._0); + public static readonly IDependencyNode _1 = new HashSetDependencyNode(Profiles._1); + public static readonly IDependencyNode _2 = new HashSetDependencyNode(Profiles._2); + public static readonly IDependencyNode _3 = new HashSetDependencyNode(Profiles._3); + public static readonly IDependencyNode _4 = new HashSetDependencyNode(Profiles._4); + public static readonly IDependencyNode _5 = new HashSetDependencyNode(Profiles._5); + public static readonly IDependencyNode _6 = new HashSetDependencyNode(Profiles._6); + public static readonly IDependencyNode _7 = new HashSetDependencyNode(Profiles._7); + public static readonly IDependencyNode _8 = new HashSetDependencyNode(Profiles._8); + public static readonly IDependencyNode _9 = new HashSetDependencyNode(Profiles._9); + + public static readonly IEnumerable All = new[] { + _0, _1, _2, _3, _4, _5, _6, _7, _8, _9 + }; + + public static void Init() { + foreach (var n in All) { + n.Sources.Clear(); + n.Destinations.Clear(); + } + } + } +} diff --git a/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/Nodes.cs.meta b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/Nodes.cs.meta new file mode 100644 index 0000000..6e9b2d3 --- /dev/null +++ b/Assets/AsmdefHelper/DependencyGraph/Editor/DependencyNode/Tests/Nodes.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 55ca469364f847a4827a9f2ed13c7ef4 +timeCreated: 1605451653 \ No newline at end of file