Fisheye: Tag 50003dafe2f6dca20a5cb36b2c437cffe91670e1 refers to a dead (removed) revision in file `Riskeer/Common/src/Riskeer.Common.Forms/Helpers/CalculationsDisplayNameHelper.cs'. Fisheye: No comparison available. Pass `N' to diff? Index: Riskeer/Common/src/Riskeer.Common.Forms/Helpers/TargetProbabilityCalculationsDisplayNameHelper.cs =================================================================== diff -u --- Riskeer/Common/src/Riskeer.Common.Forms/Helpers/TargetProbabilityCalculationsDisplayNameHelper.cs (revision 0) +++ Riskeer/Common/src/Riskeer.Common.Forms/Helpers/TargetProbabilityCalculationsDisplayNameHelper.cs (revision 50003dafe2f6dca20a5cb36b2c437cffe91670e1) @@ -0,0 +1,148 @@ +// Copyright (C) Stichting Deltares 2021. All rights reserved. +// +// This file is part of Riskeer. +// +// Riskeer is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.Collections.Generic; +using System.Linq; +using Riskeer.Common.Data.AssessmentSection; +using Riskeer.Common.Data.Hydraulics; +using Riskeer.Common.Forms.TypeConverters; + +namespace Riskeer.Common.Forms.Helpers +{ + /// + /// This class helps in obtaining unique display names for different types of calculations that can be identified + /// by their target probability. + /// + public static class TargetProbabilityCalculationsDisplayNameHelper + { + private static readonly NoProbabilityValueDoubleConverter noProbabilityValueDoubleConverter = new NoProbabilityValueDoubleConverter(); + + /// + /// Gets a unique water level calculations display name for the provided . + /// + /// The assessment section the belong to. + /// The water level calculations to get the unique display name for. + /// A unique water level calculations display name. + /// Thrown when any input parameter is null. + /// Thrown when is not part of the water level + /// calculations within . + public static string GetUniqueDisplayNameForWaterLevelCalculations(IAssessmentSection assessmentSection, IEnumerable calculations) + { + if (assessmentSection == null) + { + throw new ArgumentNullException(nameof(assessmentSection)); + } + + if (calculations == null) + { + throw new ArgumentNullException(nameof(calculations)); + } + + var nonUniqueWaterLevelCalculationsDisplayNameLookup = new Dictionary + { + { + assessmentSection.WaterLevelCalculationsForLowerLimitNorm, + noProbabilityValueDoubleConverter.ConvertToString(assessmentSection.FailureMechanismContribution.LowerLimitNorm) + }, + { + assessmentSection.WaterLevelCalculationsForSignalingNorm, + noProbabilityValueDoubleConverter.ConvertToString(assessmentSection.FailureMechanismContribution.SignalingNorm) + } + }; + + foreach (HydraulicBoundaryLocationCalculationsForTargetProbability calculationsForTargetProbability in assessmentSection.WaterLevelCalculationsForUserDefinedTargetProbabilities) + { + nonUniqueWaterLevelCalculationsDisplayNameLookup.Add(calculationsForTargetProbability.HydraulicBoundaryLocationCalculations, + noProbabilityValueDoubleConverter.ConvertToString(calculationsForTargetProbability.TargetProbability)); + } + + if (!nonUniqueWaterLevelCalculationsDisplayNameLookup.ContainsKey(calculations)) + { + throw new InvalidOperationException("The provided calculations object is not part of the water level calculations within the assessment section."); + } + + return GetUniqueDisplayNameLookup(nonUniqueWaterLevelCalculationsDisplayNameLookup)[calculations]; + } + + /// + /// Gets a unique display name for the provided . + /// + /// The enumeration of all calculations (containing ). + /// The calculations to get the unique display name for. + /// The function to obtain the target probability for elements within . + /// A unique calculations display name. + /// Thrown when any input parameter is null. + /// Thrown when is not part of . + public static string GetUniqueDisplayNameForCalculations(T calculations, IEnumerable allCalculations, Func getTargetProbabilityFunc) + { + if (allCalculations == null) + { + throw new ArgumentNullException(nameof(allCalculations)); + } + + if (calculations == null) + { + throw new ArgumentNullException(nameof(calculations)); + } + + Dictionary nonUniqueCalculationsDisplayNameLookup = + allCalculations.ToDictionary(c => (object) c, + c => noProbabilityValueDoubleConverter.ConvertToString(getTargetProbabilityFunc(c))); + + if (!nonUniqueCalculationsDisplayNameLookup.ContainsKey(calculations)) + { + throw new InvalidOperationException("The provided calculations object is not part of the enumeration."); + } + + return GetUniqueDisplayNameLookup(nonUniqueCalculationsDisplayNameLookup)[calculations]; + } + + private static Dictionary GetUniqueDisplayNameLookup(IDictionary nonUniqueDisplayNameLookup) + { + var uniqueDisplayNameLookup = new Dictionary(); + + while (nonUniqueDisplayNameLookup.Any()) + { + KeyValuePair firstElement = nonUniqueDisplayNameLookup.First(); + + IList> elementsWithSameDisplayNameAsFirstElement = nonUniqueDisplayNameLookup.Where(e => e.Value.Equals(firstElement.Value)) + .ToList(); + + uniqueDisplayNameLookup.Add(firstElement.Key, firstElement.Value); + nonUniqueDisplayNameLookup.Remove(firstElement.Key); + + if (elementsWithSameDisplayNameAsFirstElement.Count > 1) + { + for (var i = 1; i < elementsWithSameDisplayNameAsFirstElement.Count; i++) + { + KeyValuePair elementWithNonUniqueDisplayName = elementsWithSameDisplayNameAsFirstElement.ElementAt(i); + + uniqueDisplayNameLookup.Add(elementWithNonUniqueDisplayName.Key, elementWithNonUniqueDisplayName.Value + $" ({i})"); + nonUniqueDisplayNameLookup.Remove(elementWithNonUniqueDisplayName.Key); + } + } + } + + return uniqueDisplayNameLookup; + } + } +} \ No newline at end of file Index: Riskeer/Integration/src/Riskeer.Integration.Plugin/RiskeerPlugin.cs =================================================================== diff -u -rd47871932be00b85193481e18b55d97f50824fed -r50003dafe2f6dca20a5cb36b2c437cffe91670e1 --- Riskeer/Integration/src/Riskeer.Integration.Plugin/RiskeerPlugin.cs (.../RiskeerPlugin.cs) (revision d47871932be00b85193481e18b55d97f50824fed) +++ Riskeer/Integration/src/Riskeer.Integration.Plugin/RiskeerPlugin.cs (.../RiskeerPlugin.cs) (revision 50003dafe2f6dca20a5cb36b2c437cffe91670e1) @@ -1018,8 +1018,8 @@ yield return new TreeNodeInfo { - Text = context => CalculationsDisplayNameHelper.GetUniqueDisplayNameForWaterLevelCalculations(context.AssessmentSection, - context.WrappedData), + Text = context => TargetProbabilityCalculationsDisplayNameHelper.GetUniqueDisplayNameForWaterLevelCalculations(context.AssessmentSection, + context.WrappedData), Image = context => RiskeerCommonFormsResources.GenericInputOutputIcon, ContextMenuStrip = WaterLevelCalculationsForNormTargetProbabilityContextMenuStrip }; @@ -1038,8 +1038,8 @@ yield return new TreeNodeInfo { - Text = context => CalculationsDisplayNameHelper.GetUniqueDisplayNameForWaterLevelCalculations(context.AssessmentSection, - context.WrappedData.HydraulicBoundaryLocationCalculations), + Text = context => TargetProbabilityCalculationsDisplayNameHelper.GetUniqueDisplayNameForWaterLevelCalculations(context.AssessmentSection, + context.WrappedData.HydraulicBoundaryLocationCalculations), Image = context => RiskeerCommonFormsResources.GenericInputOutputIcon, EnsureVisibleOnCreate = (context, o) => true, OnRemoveConfirmationText = context => Resources.RiskeerPlugin_GetTreeNodeInfos_Confirm_remove_TargetProbability, @@ -1063,9 +1063,9 @@ yield return new TreeNodeInfo { - Text = context => CalculationsDisplayNameHelper.GetUniqueDisplayNameForCalculations(context.WrappedData, - context.AssessmentSection.WaveHeightCalculationsForUserDefinedTargetProbabilities, - probability => probability.TargetProbability), + Text = context => TargetProbabilityCalculationsDisplayNameHelper.GetUniqueDisplayNameForCalculations(context.WrappedData, + context.AssessmentSection.WaveHeightCalculationsForUserDefinedTargetProbabilities, + probability => probability.TargetProbability), Image = context => RiskeerCommonFormsResources.GenericInputOutputIcon, EnsureVisibleOnCreate = (context, o) => true, OnRemoveConfirmationText = context => Resources.RiskeerPlugin_GetTreeNodeInfos_Confirm_remove_TargetProbability,