Index: Ringtoets/Common/src/Ringtoets.Common.Forms/GraphNodeConverter.cs =================================================================== diff -u -rd31087af5b9320ee36f3f8462cc71ed5f1e25e34 -rd76347f41ed3b186ba5414c7cef84f5a255bf35d --- Ringtoets/Common/src/Ringtoets.Common.Forms/GraphNodeConverter.cs (.../GraphNodeConverter.cs) (revision d31087af5b9320ee36f3f8462cc71ed5f1e25e34) +++ Ringtoets/Common/src/Ringtoets.Common.Forms/GraphNodeConverter.cs (.../GraphNodeConverter.cs) (revision d76347f41ed3b186ba5414c7cef84f5a255bf35d) @@ -21,7 +21,6 @@ using System; using System.Collections.Generic; -using System.Linq; using Core.Common.Base.Data; using Core.Common.Utils; using Core.Components.PointedTree.Data; @@ -39,44 +38,25 @@ internal static class GraphNodeConverter { /// - /// Creates a new instance of a based on the - /// . + /// Creates a new instance of , based on the properties of . /// - /// The to base the + /// The to base the /// to create on. - /// A . - /// Thrown when is null. - /// Thrown when no suitable conversion for - /// was found. - public static GraphNode Convert(IllustrationPointNode node) + /// The child graph nodes of the to create. + /// The created . + /// Thrown when any of the input variables is null. + public static GraphNode ConvertFaultTreeIllustrationPoint(FaultTreeIllustrationPoint illustrationPoint, + IEnumerable childGraphNodes) { - if (node == null) + if (illustrationPoint == null) { - throw new ArgumentNullException(nameof(node)); + throw new ArgumentNullException(nameof(illustrationPoint)); } - - IllustrationPointBase illustrationPoint = node.Data; - var subMechanismIllustrationPoint = illustrationPoint as SubMechanismIllustrationPoint; - if (subMechanismIllustrationPoint != null) + if (childGraphNodes == null) { - return ConvertSubMechanismIllustrationPoint(subMechanismIllustrationPoint); + throw new ArgumentNullException(nameof(childGraphNodes)); } - var faultTreeIllustrationPoint = illustrationPoint as FaultTreeIllustrationPoint; - if (faultTreeIllustrationPoint != null) - { - IEnumerable childGraphNodes = node.Children.Select(Convert); - - return ConvertFaultTreeIllustrationPoint(faultTreeIllustrationPoint, - childGraphNodes); - } - - throw new NotSupportedException($"Cannot convert {illustrationPoint.GetType()}."); - } - - private static GraphNode ConvertFaultTreeIllustrationPoint(FaultTreeIllustrationPoint illustrationPoint, - IEnumerable childGraphNodes) - { string childRelationTitle = illustrationPoint.CombinationType == CombinationType.And ? Resources.GraphNode_CombinationType_And : Resources.GraphNode_CombinationType_Or; @@ -91,8 +71,20 @@ }); } - private static GraphNode ConvertSubMechanismIllustrationPoint(SubMechanismIllustrationPoint illustrationPoint) + /// + /// Creates a new instance of , based on the properties of . + /// + /// The to base the + /// to create on. + /// The created . + /// Thrown when is null. + public static GraphNode ConvertSubMechanismIllustrationPoint(SubMechanismIllustrationPoint illustrationPoint) { + if (illustrationPoint == null) + { + throw new ArgumentNullException(nameof(illustrationPoint)); + } + return RingtoetsGraphNodeFactory.CreateEndGraphNode( illustrationPoint.Name, CreateGraphNodeContent(illustrationPoint.Beta)); Index: Ringtoets/Common/src/Ringtoets.Common.Forms/Views/IllustrationPointsFaultTreeControl.cs =================================================================== diff -u -r515adad18359253ba49f28454f2c2e9bb5cd2e92 -rd76347f41ed3b186ba5414c7cef84f5a255bf35d --- Ringtoets/Common/src/Ringtoets.Common.Forms/Views/IllustrationPointsFaultTreeControl.cs (.../IllustrationPointsFaultTreeControl.cs) (revision 515adad18359253ba49f28454f2c2e9bb5cd2e92) +++ Ringtoets/Common/src/Ringtoets.Common.Forms/Views/IllustrationPointsFaultTreeControl.cs (.../IllustrationPointsFaultTreeControl.cs) (revision d76347f41ed3b186ba5414c7cef84f5a255bf35d) @@ -20,8 +20,12 @@ // All rights reserved. using System; +using System.Collections.Generic; +using System.Linq; using System.Windows.Forms; using Core.Common.Controls.Views; +using Core.Components.PointedTree.Data; +using Ringtoets.Common.Data.IllustrationPoints; namespace Ringtoets.Common.Forms.Views { @@ -30,6 +34,9 @@ /// public partial class IllustrationPointsFaultTreeControl : UserControl, ISelectionProvider { + private readonly List drawnNodes = new List(); + private IllustrationPointNode data; + public event EventHandler SelectionChanged; /// @@ -44,16 +51,85 @@ /// /// Gets or sets the data of the control. /// - public object Data { get; set; } + public IllustrationPointNode Data + { + get + { + return data; + } + set + { + data = value; + if (value == null) + { + drawnNodes.Clear(); + pointedTreeGraphControl.Data = null; + return; + } + + RegisterNode(data); + + pointedTreeGraphControl.Data = drawnNodes + .Where(d => d.IllustrationPointNode == data) + .Select(d => d.GraphNode) + .FirstOrDefault(); + } + } + public object Selection { get { - return null; + GraphNode selectedGraphNode = pointedTreeGraphControl.Selection; + + return drawnNodes.Where(l => l.GraphNode == selectedGraphNode).Select(l => l.IllustrationPointNode).FirstOrDefault(); } } + private void RegisterNode(IllustrationPointNode node) + { + var childNodes = new List(); + + foreach (IllustrationPointNode childNode in node.Children) + { + RegisterNode(childNode); + + foreach (DrawnIllustrationPointNode drawnIllustrationPointNode in drawnNodes) + { + if (drawnIllustrationPointNode.IllustrationPointNode == childNode) + { + childNodes.Add(drawnIllustrationPointNode.GraphNode); + break; + } + } + } + + drawnNodes.Add(new DrawnIllustrationPointNode + { + IllustrationPointNode = node, + GraphNode = CreateGraphNodes(node.Data, childNodes) + }); + } + + private static GraphNode CreateGraphNodes(IllustrationPointBase illustrationPoint, IEnumerable childNodes) + { + var subMechanismIllustrationPoint = illustrationPoint as SubMechanismIllustrationPoint; + if (subMechanismIllustrationPoint != null) + { + return GraphNodeConverter.ConvertSubMechanismIllustrationPoint(subMechanismIllustrationPoint); + } + + var faultTreeIllustrationPoint = illustrationPoint as FaultTreeIllustrationPoint; + if (faultTreeIllustrationPoint != null) + { + return GraphNodeConverter.ConvertFaultTreeIllustrationPoint(faultTreeIllustrationPoint, + childNodes); + } + + return null; + } + private void InitializeEventHandlers() { pointedTreeGraphControl.SelectionChanged += FaultTreeIllustrationPointsControlOnSelectionChanged; @@ -68,5 +144,12 @@ { SelectionChanged?.Invoke(this, e); } + + private class DrawnIllustrationPointNode + { + public IllustrationPointNode IllustrationPointNode { get; set; } + + public GraphNode GraphNode { get; set; } + } } } \ No newline at end of file Index: Ringtoets/Common/test/Ringtoets.Common.Forms.Test/GraphNodeConverterTest.cs =================================================================== diff -u -rd31087af5b9320ee36f3f8462cc71ed5f1e25e34 -rd76347f41ed3b186ba5414c7cef84f5a255bf35d --- Ringtoets/Common/test/Ringtoets.Common.Forms.Test/GraphNodeConverterTest.cs (.../GraphNodeConverterTest.cs) (revision d31087af5b9320ee36f3f8462cc71ed5f1e25e34) +++ Ringtoets/Common/test/Ringtoets.Common.Forms.Test/GraphNodeConverterTest.cs (.../GraphNodeConverterTest.cs) (revision d76347f41ed3b186ba5414c7cef84f5a255bf35d) @@ -20,6 +20,7 @@ // All rights reserved. using System; +using System.Collections.Generic; using System.Linq; using Core.Common.Base.Data; using Core.Common.TestUtil; @@ -36,67 +37,54 @@ public class GraphNodeConverterTest { [Test] - public void Convert_NodeNull_ThrowsArgumentNullException() + public void ConvertFaultTreeIllustrationPoint_IllustrationNodeNull_ThrowsArgumentNullException() { // Call - TestDelegate test = () => GraphNodeConverter.Convert(null); + TestDelegate test = () => GraphNodeConverter.ConvertFaultTreeIllustrationPoint(null, + Enumerable.Empty()); // Assert var exception = Assert.Throws(test); - Assert.AreEqual("node", exception.ParamName); + Assert.AreEqual("illustrationPoint", exception.ParamName); } [Test] - public void Convert_InvalidTypeNodeData_ThrowsNotSupportedException() + public void ConvertFaultTreeIllustrationPoint_ChildrenNull_ThrowsArgumentNullException() { // Setup - var node = new IllustrationPointNode(new TestIllustrationPoint()); + var illustrationPoint = new TestFaultTreeIllustrationPoint(); // Call - TestDelegate test = () => GraphNodeConverter.Convert(node); + TestDelegate test = () => GraphNodeConverter.ConvertFaultTreeIllustrationPoint(illustrationPoint, + null); // Assert - var exception = Assert.Throws(test); - Assert.AreEqual($"Cannot convert {node.Data.GetType()}.", exception.Message); + var exception = Assert.Throws(test); + Assert.AreEqual("childGraphNodes", exception.ParamName); } [Test] - public void Convert_SubMechanismIllustrationPointNodeData_ReturnsExpected() - { - // Setup - const string name = "Illustration Point"; - RoundedDouble beta = new Random(7).NextRoundedDouble(); - var node = new IllustrationPointNode(new SubMechanismIllustrationPoint( - name, - beta, - Enumerable.Empty(), - Enumerable.Empty())); - - // Call - GraphNode graphNode = GraphNodeConverter.Convert(node); - - // Assert - Assert.AreEqual(CreateExpectedGraphNodeContent(name, beta), graphNode.Content); - Assert.IsTrue(graphNode.IsSelectable); - CollectionAssert.IsEmpty(graphNode.ChildNodes); - } - - [Test] [TestCase(CombinationType.And)] [TestCase(CombinationType.Or)] public void Convert_FaultTreeIllustrationPointNodeDataWithoutChildren_ReturnsExpected(CombinationType combinationType) { // Setup const string name = "Illustration Point"; RoundedDouble beta = new Random(7).NextRoundedDouble(); - var node = new IllustrationPointNode(new FaultTreeIllustrationPoint( - name, - beta, - Enumerable.Empty(), - combinationType)); + var illustrationPoint = new FaultTreeIllustrationPoint( + name, + beta, + Enumerable.Empty(), + combinationType); + IEnumerable childGraphNodes = new[] + { + GraphNodeConverter.ConvertSubMechanismIllustrationPoint(new TestSubMechanismIllustrationPoint()) + }; + // Call - GraphNode graphNode = GraphNodeConverter.Convert(node); + GraphNode graphNode = GraphNodeConverter.ConvertFaultTreeIllustrationPoint(illustrationPoint, + childGraphNodes); // Assert Assert.AreEqual(CreateExpectedGraphNodeContent(name, beta), graphNode.Content); @@ -106,89 +94,40 @@ GraphNode childNode = graphNode.ChildNodes.First(); Assert.AreEqual(CreateExpectedGraphConnectingNodeContent(combinationType), childNode.Content); Assert.IsFalse(childNode.IsSelectable); - CollectionAssert.IsEmpty(childNode.ChildNodes); + + CollectionAssert.AreEqual(childGraphNodes, childNode.ChildNodes); } [Test] - public void Convert_FaultTreeIllustrationPointNodeDataWithChildren_ReturnsExpected() + public void ConvertSubMechanismIllustrationPoint_IllustrationNodeNull_ThrowsArgumentNullException() { - // Setup - var random = new Random(70); - - var childFaultTreeNode = new IllustrationPointNode(new FaultTreeIllustrationPoint( - "ChildFaultTreeIllustrationPoint", - random.NextRoundedDouble(), - Enumerable.Empty(), - CombinationType.And)); - childFaultTreeNode.SetChildren(new[] - { - new IllustrationPointNode(new SubMechanismIllustrationPoint( - "ChildChildSubMechanismIllustrationPoint1", - random.NextRoundedDouble(), - Enumerable.Empty(), - Enumerable.Empty())), - new IllustrationPointNode(new SubMechanismIllustrationPoint( - "ChildChildSubMechanismIllustrationPoint2", - random.NextRoundedDouble(), - Enumerable.Empty(), - Enumerable.Empty())) - }); - - var node = new IllustrationPointNode(new FaultTreeIllustrationPoint( - "FaultTreeIllustrationPoint", - random.NextRoundedDouble(), - Enumerable.Empty(), - CombinationType.Or)); - node.SetChildren(new[] - { - new IllustrationPointNode(new SubMechanismIllustrationPoint( - "SubMechanismIllustrationPoint", - random.NextRoundedDouble(), - Enumerable.Empty(), - Enumerable.Empty())), - childFaultTreeNode - }); - // Call - GraphNode graphNode = GraphNodeConverter.Convert(node); + TestDelegate test = () => GraphNodeConverter.ConvertSubMechanismIllustrationPoint(null); // Assert - Assert.AreEqual(CreateExpectedGraphNodeContent(node.Data.Name, node.Data.Beta), graphNode.Content); + var exception = Assert.Throws(test); + Assert.AreEqual("illustrationPoint", exception.ParamName); + } - Assert.AreEqual(1, graphNode.ChildNodes.Count()); - GraphNode connectingNode1 = graphNode.ChildNodes.First(); - Assert.AreEqual(CreateExpectedGraphConnectingNodeContent(CombinationType.Or), connectingNode1.Content); + [Test] + public void Convert_ConvertSubMechanismIllustrationPoint_ReturnsExpected() + { + // Setup + const string name = "Illustration Point"; + RoundedDouble beta = new Random(7).NextRoundedDouble(); + var illustrationPoint = new SubMechanismIllustrationPoint( + name, + beta, + Enumerable.Empty(), + Enumerable.Empty()); - Assert.AreEqual(2, connectingNode1.ChildNodes.Count()); + // Call + GraphNode graphNode = GraphNodeConverter.ConvertSubMechanismIllustrationPoint(illustrationPoint); - IllustrationPointNode childIllustrationPointNode1 = node.Children.ElementAt(0); - GraphNode childGraphNode1 = connectingNode1.ChildNodes.ElementAt(0); - Assert.AreEqual(CreateExpectedGraphNodeContent(childIllustrationPointNode1.Data.Name, childIllustrationPointNode1.Data.Beta), - childGraphNode1.Content); - CollectionAssert.IsEmpty(childGraphNode1.ChildNodes); - - IllustrationPointNode childIllustrationPointNode2 = node.Children.ElementAt(1); - GraphNode childGraphNode2 = connectingNode1.ChildNodes.ElementAt(1); - Assert.AreEqual(CreateExpectedGraphNodeContent(childIllustrationPointNode2.Data.Name, childIllustrationPointNode2.Data.Beta), - childGraphNode2.Content); - - Assert.AreEqual(1, childGraphNode2.ChildNodes.Count()); - GraphNode connectingNode2 = childGraphNode2.ChildNodes.First(); - Assert.AreEqual(CreateExpectedGraphConnectingNodeContent(CombinationType.And), connectingNode2.Content); - - Assert.AreEqual(2, connectingNode2.ChildNodes.Count()); - - IllustrationPointNode childIllustrationPointNode21 = childIllustrationPointNode2.Children.ElementAt(0); - GraphNode childGraphNode11 = connectingNode2.ChildNodes.ElementAt(0); - Assert.AreEqual(CreateExpectedGraphNodeContent(childIllustrationPointNode21.Data.Name, childIllustrationPointNode21.Data.Beta), - childGraphNode11.Content); - CollectionAssert.IsEmpty(childGraphNode11.ChildNodes); - - IllustrationPointNode childIllustrationPointNode22 = childIllustrationPointNode2.Children.ElementAt(0); - GraphNode childGraphNode12 = connectingNode2.ChildNodes.ElementAt(0); - Assert.AreEqual(CreateExpectedGraphNodeContent(childIllustrationPointNode22.Data.Name, childIllustrationPointNode22.Data.Beta), - childGraphNode12.Content); - CollectionAssert.IsEmpty(childGraphNode12.ChildNodes); + // Assert + Assert.AreEqual(CreateExpectedGraphNodeContent(name, beta), graphNode.Content); + Assert.IsTrue(graphNode.IsSelectable); + CollectionAssert.IsEmpty(graphNode.ChildNodes); } private static string CreateExpectedGraphNodeContent(string name, RoundedDouble beta)