NodeProcessor

This commit is contained in:
naninunenoy 2020-11-16 01:17:52 +09:00
parent beebf3e7c8
commit 029ee6515d
10 changed files with 222 additions and 5 deletions

View File

@ -3,13 +3,16 @@ using System.Collections.Generic;
namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode {
public class HashSetDependencyNode : IDependencyNode {
public NodeProfile Profile { get; }
public ICollection<NodeProfile> Sources { get; }
public ICollection<NodeProfile> Destinations { get; }
public ICollection<NodeProfile> Sources => sources;
public ICollection<NodeProfile> Destinations => destinations;
readonly HashSet<NodeProfile> sources;
readonly HashSet<NodeProfile> destinations;
public HashSetDependencyNode(NodeProfile profile) {
Profile = profile;
Sources = new HashSet<NodeProfile>();
Destinations = new HashSet<NodeProfile>();
sources = new HashSet<NodeProfile>();
destinations = new HashSet<NodeProfile>();
}
}
}

View File

@ -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);
}
}
}

View File

@ -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<IDependencyNode> 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<NodeProfile> requireNodes) {
node.Sources.Clear();
node.Destinations.Clear();
foreach (var d in requireNodes) {
node.Destinations.Add(d);
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6febd7e408df42d89d561ffe371c602f
timeCreated: 1605454341

View File

@ -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);
}
}
}

View File

@ -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<IDependencyNode>());
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);

View File

@ -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);
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9e86a8d93f3a4bb5a7c6d81b775efb47
timeCreated: 1605450647

View File

@ -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<IDependencyNode> 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();
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 55ca469364f847a4827a9f2ed13c7ef4
timeCreated: 1605451653