LinerSortStrategy

This commit is contained in:
naninunenoy 2020-11-17 01:37:08 +09:00
parent 5ec5d749c3
commit 2cba3e7e95
15 changed files with 216 additions and 22 deletions

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d7b935eca30945f5bff132d6a7645320
timeCreated: 1605459681

View File

@ -0,0 +1,22 @@
using System.Linq;
namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Sort {
public static class DependencyNodeExtensions {
public static bool IsSourceEmpty(this IDependencyNode node) {
return !node.Sources.Any();
}
public static bool IsDestinationEmpty(this IDependencyNode node) {
return !node.Destinations.Any();
}
public static bool IsEmptyDependency(this IDependencyNode node) {
return node.IsSourceEmpty() && node.IsDestinationEmpty();
}
public static int CountDependencies(this IDependencyNode node) {
return node.Sources.Count + node.Destinations.Count;
}
public static bool ContainsSelfReference(this IDependencyNode node) {
return node.Sources.Any(x => x == node.Profile) ||
node.Destinations.Any(x => x == node.Profile);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8d0cee1bf7d4c8c47a98a23879a257cb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,7 @@
using System.Collections.Generic;
namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Sort {
public interface ISortStrategy {
IEnumerable<SortedNode> Sort(IEnumerable<IDependencyNode> nodes);
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3e2c63741aae4306aa49389ffdcb2e13
timeCreated: 1605459681

View File

@ -0,0 +1,31 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Sort {
public class LinerSortStrategy : ISortStrategy {
readonly Vector2 originPosition;
readonly float basicDistance;
readonly float nodeWidth;
readonly float nodeHeight;
public LinerSortStrategy(Vector2 originPosition, float basicDistance, float nodeWidth, float nodeHeight) {
this.originPosition = originPosition;
this.basicDistance = basicDistance;
this.nodeWidth = nodeWidth;
this.nodeHeight = nodeHeight;
}
IEnumerable<SortedNode> ISortStrategy.Sort(IEnumerable<IDependencyNode> nodes) {
var arr = nodes.ToArray();
var posDict = arr
.ToDictionary(x => x.Profile, _ => originPosition);
// 参照元がないNodeを原点に
var left = arr.FirstOrDefault(x => x.IsSourceEmpty());
posDict[left.Profile] = originPosition;
var right = arr.FirstOrDefault(x => x.Profile != left.Profile);
posDict[right.Profile] = new Vector2(nodeWidth / 2.0F + basicDistance, 0.0F) + originPosition;
return posDict.Select(x => new SortedNode { Profile = x.Key, Position = x.Value });
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7d00de9a7b3041be922c457f9f53b1c0
timeCreated: 1605459871

View File

@ -0,0 +1,8 @@
using UnityEngine;
namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Sort {
public class SortedNode {
public NodeProfile Profile { set; get; }
public Vector2 Position { set; get; }
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c995f86b2ebe4c83ac65ec6727431ddb
timeCreated: 1605459738

View File

@ -0,0 +1,56 @@
using System.Collections;
using AsmdefHelper.DependencyGraph.Editor.DependencyNode.Sort;
using NUnit.Framework;
using UnityEditor;
using UnityEngine.TestTools;
namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Tests {
public class DependencyNodeExtensionsTest {
[SetUp]
public void SetUpBeforeEveryTest() {
Nodes.Init();
// [0]--->[1]--->[2]<---[3] [4]
Nodes._0.SetRequireNodes(new[] { Profiles._1 });
Nodes._1.SetRequireNodes(new[] { Profiles._2 });
Nodes._3.SetRequireNodes(new[] { Profiles._2 });
NodeProcessor.SetBeRequiredNodes(new[] { Nodes._0, Nodes._1, Nodes._2, Nodes._3, Nodes._4 });
}
[Test]
public void TestIsSourceEmpty() {
Assert.That(Nodes._0.IsSourceEmpty(), Is.True);
Assert.That(Nodes._1.IsSourceEmpty(), Is.False);
Assert.That(Nodes._2.IsSourceEmpty(), Is.False);
Assert.That(Nodes._3.IsSourceEmpty(), Is.True);
Assert.That(Nodes._4.IsSourceEmpty(), Is.True);
}
[Test]
public void TestIsDestinationEmpty() {
Assert.That(Nodes._0.IsDestinationEmpty(), Is.False);
Assert.That(Nodes._1.IsDestinationEmpty(), Is.False);
Assert.That(Nodes._2.IsDestinationEmpty(), Is.True);
Assert.That(Nodes._3.IsDestinationEmpty(), Is.False);
Assert.That(Nodes._4.IsDestinationEmpty(), Is.True);
}
[Test]
public void TestIsEmptyDependency() {
Assert.That(Nodes._0.IsEmptyDependency(), Is.False);
Assert.That(Nodes._1.IsEmptyDependency(), Is.False);
Assert.That(Nodes._2.IsEmptyDependency(), Is.False);
Assert.That(Nodes._3.IsEmptyDependency(), Is.False);
Assert.That(Nodes._4.IsEmptyDependency(), Is.True);
}
[Test]
public void TestCountDependencies() {
Assert.That(Nodes._0.CountDependencies(), Is.EqualTo(1));
Assert.That(Nodes._1.CountDependencies(), Is.EqualTo(2));
Assert.That(Nodes._2.CountDependencies(), Is.EqualTo(2));
Assert.That(Nodes._3.CountDependencies(), Is.EqualTo(1));
Assert.That(Nodes._4.CountDependencies(), Is.EqualTo(0));
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 95cc92d669584f4bacd5faf7b1e78320
timeCreated: 1605543283

View File

@ -0,0 +1,43 @@
using System.Collections;
using System.Linq;
using AsmdefHelper.DependencyGraph.Editor.DependencyNode.Sort;
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
using UnityEngine.TestTools;
namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Tests {
public class LinerSortTest {
const float d = 10.0F;
const float w = 10.0F;
const float h = 10.0F;
const float e = 0.0001F;
ISortStrategy sortStrategy;
[SetUp]
public void SetUpBeforeEveryTest() {
Nodes.Init();
sortStrategy = new LinerSortStrategy(Vector2.zero, d, w, h);
}
[Test]
public void TestLinerNodeDependency() {
var nodes = new[] { Nodes._0, Nodes._1 };
// [0]--->[1]
Nodes._0.SetRequireNodes(new[] { Profiles._1});
NodeProcessor.SetBeRequiredNodes(nodes);
var result = sortStrategy.Sort(nodes).ToArray();
var node0 = result.FirstOrDefault(x => x.Profile == Profiles._0);
var node1 = result.FirstOrDefault(x => x.Profile == Profiles._1);
Assert.That(node0, Is.Not.Null);
Assert.That(node0.Position.x, Is.Zero);
Assert.That(node0.Position.y, Is.Zero);
Assert.That(node1, Is.Not.Null);
Assert.That(node1.Position.x, Is.EqualTo(node0.Position.x + d + (w / 2.0F)).Within(e));
Assert.That(node1.Position.y, Is.EqualTo(node0.Position.y).Within(e));
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ecac0d687a574143a409b043040ce933
timeCreated: 1605460235

View File

@ -34,30 +34,9 @@ namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Tests {
Assert.That(Nodes._1.Sources.Any(x => x.Equals(Profiles._0))); 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] [Test]
public void TestSomeNodeDependency() { public void TestSomeNodeDependency() {
SetSomeNodeDependency(); Nodes.SetSomeNodeDependency();
NodeProcessor.SetBeRequiredNodes(Nodes.All); NodeProcessor.SetBeRequiredNodes(Nodes.All);
// 0 // 0
Assert.That(Nodes._0.Sources, Is.Empty); Assert.That(Nodes._0.Sources, Is.Empty);

View File

@ -48,5 +48,24 @@ namespace AsmdefHelper.DependencyGraph.Editor.DependencyNode.Tests {
n.Destinations.Clear(); n.Destinations.Clear();
} }
} }
public 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 });
}
} }
} }