From 5623d981811ec581c22af278d45270d526fd0484 Mon Sep 17 00:00:00 2001 From: Alessio Lombardi Date: Thu, 25 Jan 2024 16:57:28 +0000 Subject: [PATCH] Align to https://github.com/BHoM/BHoM/issues/1591 (Introducing NumericalApproximationConfig) --- .../Create/Graph/ComparisonConfig.cs | 3 +-- .../Modify/PropertyNamesToFullNames.cs | 21 +++++++++++-------- BHoM_Engine/Query/Hash.cs | 21 +++++++------------ BHoM_Engine/Query/NumericTolerance.cs | 8 +++---- BHoM_Engine/Query/NumericalApproximation.cs | 16 +++++++------- BHoM_Engine/Query/SignificantFigures.cs | 8 +++---- .../Query/NumericalDifferenceInclusion.cs | 8 +++---- Diffing_Engine/Query/ObjectDifferences.cs | 2 +- .../Query/HasMergeablePropertiesWith.cs | 7 +++---- Physical_Engine/Query/UniqueConstructions.cs | 2 +- 10 files changed, 45 insertions(+), 51 deletions(-) diff --git a/Analytical_Engine/Create/Graph/ComparisonConfig.cs b/Analytical_Engine/Create/Graph/ComparisonConfig.cs index 9c7ff41c2..7e47f56c1 100644 --- a/Analytical_Engine/Create/Graph/ComparisonConfig.cs +++ b/Analytical_Engine/Create/Graph/ComparisonConfig.cs @@ -49,8 +49,7 @@ public static ComparisonConfig ComparisonConfig(double numericTolerance = oM.Geo { ComparisonConfig cc = new ComparisonConfig() { - NumericTolerance = numericTolerance, - PropertiesToConsider = propertyNamesToConsider ?? new List(), + NumericalApproximationConfig = new NumericalApproximationConfig() { NumericTolerance = numericTolerance }, PropertiesToConsider = propertyNamesToConsider ?? new HashSet(), }; diff --git a/BHoM_Engine/Modify/PropertyNamesToFullNames.cs b/BHoM_Engine/Modify/PropertyNamesToFullNames.cs index 3e8dad7ca..4c6076404 100644 --- a/BHoM_Engine/Modify/PropertyNamesToFullNames.cs +++ b/BHoM_Engine/Modify/PropertyNamesToFullNames.cs @@ -66,7 +66,7 @@ public static void PropertyNamesToFullNames(this BaseComparisonConfig comparison // We need to process only those properties that have not been specified in FullName form, or that contain wildcards. List propertiesToConsiderToParse = comparisonConfig.PropertiesToConsider?.Where(p => !p.StartsWith("BH.") || p.Contains("*")).ToList() ?? new List(); List propertyExceptionsToParse = comparisonConfig.PropertyExceptions?.Where(p => !p.StartsWith("BH.") || p.Contains("*")).ToList() ?? new List(); - List propertyNumericTolerancesToParse = comparisonConfig.PropertyNumericTolerances?.Select(p => p.Name).Where(p => !p.StartsWith("BH.") || p.Contains("*")).ToList() ?? new List(); + List propertyNumericTolerancesToParse = comparisonConfig.NumericalApproximationConfig.PropertyNumericTolerances?.Select(p => p.Name).Where(p => !p.StartsWith("BH.") || p.Contains("*")).ToList() ?? new List(); // If all the property names are already in FullName form and without wildcards, return. if (!propertiesToConsiderToParse.Any() && !propertyExceptionsToParse.Any() && !propertyNumericTolerancesToParse.Any()) @@ -87,7 +87,7 @@ public static void PropertyNamesToFullNames(this BaseComparisonConfig comparison bool processPropertiesExceptions = propertyExceptionsToParse.Any() && (!cache || !m_cachedPropertyExceptions.TryGetValue(propertyExceptionsCacheKey, out propertyExceptions_fullNames)); // Check if we already have encountered and cached this same object Type and PropertyNumericTolerances. - string propertyNumericTolerancesCacheKey = $"{type.FullName}:{string.Join(",", comparisonConfig.PropertyNumericTolerances?.Select(ct => ct.Name + ct.Tolerance))}"; + string propertyNumericTolerancesCacheKey = $"{type.FullName}:{string.Join(",", comparisonConfig.NumericalApproximationConfig.PropertyNumericTolerances?.Select(ct => ct.Name + ct.Tolerance))}"; bool processPropertyNumericTolerances = propertyNumericTolerancesToParse.Any() && (!cache || !m_cachedPropertyNumericTolerances.TryGetValue(propertyNumericTolerancesCacheKey, out propertyNumericTolerances_fullNames)); // Safety clauses because C#'s Dictionary TryGetValue sets the out variable to `null` if it failed. @@ -120,16 +120,16 @@ public static void PropertyNamesToFullNames(this BaseComparisonConfig comparison { foreach (var propNumericTolerance in propertyNumericTolerancesToParse) if (IsMatchingInclusion(propertyFullName, propNumericTolerance)) - propertyNumericTolerances_fullNames.Add(new NamedNumericTolerance() { Name = propertyFullName, Tolerance = comparisonConfig.PropertyNumericTolerances.Where(pnc => pnc.Name == propNumericTolerance).First().Tolerance }); + propertyNumericTolerances_fullNames.Add(new NamedNumericTolerance() { Name = propertyFullName, Tolerance = comparisonConfig.NumericalApproximationConfig.PropertyNumericTolerances.Where(pnc => pnc.Name == propNumericTolerance).First().Tolerance }); if (cache) m_cachedPropertyNumericTolerances[propertyNumericTolerancesCacheKey] = propertyNumericTolerances_fullNames; } } // Add the results to the ComparisonConfig. - comparisonConfig.PropertyNumericTolerances.UnionWith(propertyNumericTolerances_fullNames); comparisonConfig.PropertiesToConsider.UnionWith(propertiesToConsider_fullNames); comparisonConfig.PropertyExceptions.UnionWith(propertyExceptions_fullNames); + comparisonConfig.NumericalApproximationConfig.PropertyNumericTolerances.UnionWith(propertyNumericTolerances_fullNames); m_ComparisonConfig_Type_processed[new Tuple(type, comparisonConfig)] = comparisonConfig; } @@ -150,7 +150,7 @@ public static void PropertyNamesToFullNames(this BaseComparisonConfig comparison // We need to execute this method only if we have properties in the ComparisonConfig that have NOT been specified in FullName form, or that contain wildcards. List propertiesToConsiderToParse = comparisonConfig.PropertiesToConsider?.Where(p => !p.StartsWith("BH.") || p.Contains("*")).ToList() ?? new List(); List propertyExceptionsToParse = comparisonConfig.PropertyExceptions?.Where(p => !p.StartsWith("BH.") || p.Contains("*")).ToList() ?? new List(); - List propertyNumericTolerancesToParse = comparisonConfig.PropertyNumericTolerances?.Select(p => p.Name).Where(p => !p.StartsWith("BH.") || p.Contains("*")).ToList() ?? new List(); + List propertyNumericTolerancesToParse = comparisonConfig.NumericalApproximationConfig.PropertyNumericTolerances?.Select(p => p.Name).Where(p => !p.StartsWith("BH.") || p.Contains("*")).ToList() ?? new List(); // If all the property names in the ComparisonConfig are already in FullName form and without wildcards, return. if (!propertiesToConsiderToParse.Any() && !propertyExceptionsToParse.Any() && !propertyNumericTolerancesToParse.Any()) @@ -179,13 +179,10 @@ public static void PropertyNamesToFullNames(this BaseComparisonConfig comparison foreach (var propNumericTolerance in propertyNumericTolerancesToParse) if (IsMatchingInclusion(propertyFullName, propNumericTolerance)) - propertyNumericTolerances_fullNames.Add(new NamedNumericTolerance() { Name = propertyFullName, Tolerance = comparisonConfig.PropertyNumericTolerances.Where(pnc => pnc.Name == propNumericTolerance).First().Tolerance }); + propertyNumericTolerances_fullNames.Add(new NamedNumericTolerance() { Name = propertyFullName, Tolerance = comparisonConfig.NumericalApproximationConfig.PropertyNumericTolerances.Where(pnc => pnc.Name == propNumericTolerance).First().Tolerance }); } // Add the results to the ComparisonConfig. - comparisonConfig.PropertyNumericTolerances = comparisonConfig.PropertyNumericTolerances == null ? - new HashSet(comparisonConfig.PropertyNumericTolerances?.Union(propertyNumericTolerances_fullNames)) - : propertyNumericTolerances_fullNames; if (comparisonConfig.PropertiesToConsider != null) comparisonConfig.PropertiesToConsider.UnionWith(propertiesToConsider_fullNames); else @@ -195,6 +192,12 @@ public static void PropertyNamesToFullNames(this BaseComparisonConfig comparison comparisonConfig.PropertyExceptions.UnionWith(propertyExceptions_fullNames); else comparisonConfig.PropertiesToConsider = new HashSet(propertyExceptions_fullNames); + + comparisonConfig.NumericalApproximationConfig = comparisonConfig.NumericalApproximationConfig ?? new NumericalApproximationConfig(); + if (comparisonConfig.NumericalApproximationConfig.PropertyNumericTolerances != null) + comparisonConfig.NumericalApproximationConfig.PropertyNumericTolerances.UnionWith(propertyNumericTolerances_fullNames); + else + comparisonConfig.NumericalApproximationConfig.PropertyNumericTolerances = new HashSet(propertyNumericTolerances_fullNames); } /***************************************************/ diff --git a/BHoM_Engine/Query/Hash.cs b/BHoM_Engine/Query/Hash.cs index e15ca8a7f..d42933628 100644 --- a/BHoM_Engine/Query/Hash.cs +++ b/BHoM_Engine/Query/Hash.cs @@ -75,9 +75,6 @@ public static string Hash(this IObject iObj, BaseComparisonConfig comparisonConf // Parse the ComparisonConfig's `PropertiesToConsider` and `PropertyExceptions` and get them all as Full Names. Modify.PropertyNamesToFullNames(cc, iObj); - // Verify that no numerical approximation is requested for objects belonging to Geometrical types. - m_isCompatibleWithGeometryHash = cc.IsCompatibleWithGeometryHash(); - // ----- HASH ----- // Compute the defining string. @@ -150,23 +147,23 @@ private static string HashString(object obj, BaseComparisonConfig cc, int nestin else if (type.IsNumeric(enumsAsNumbers: false)) { // If we didn't specify any custom tolerance/significant figures, just return the input. - if (cc.NumericTolerance == double.MinValue && cc.SignificantFigures == int.MaxValue - && (!cc.PropertyNumericTolerances?.Any() ?? true) && (!cc.PropertySignificantFigures?.Any() ?? true)) + if (cc.NumericalApproximationConfig.NumericTolerance == double.MinValue && cc.NumericalApproximationConfig.SignificantFigures == int.MaxValue + && (!cc.NumericalApproximationConfig.PropertyNumericTolerances?.Any() ?? true) && (!cc.NumericalApproximationConfig.PropertySignificantFigures?.Any() ?? true)) return $"\n{tabs}" + obj.ToString(); if (type == typeof(double)) - return $"\n{tabs}" + NumericalApproximation((double)obj, currentPropertyFullName, cc).ToString(); + return $"\n{tabs}" + NumericalApproximation((double)obj, currentPropertyFullName, cc.NumericalApproximationConfig).ToString(); if (type == typeof(int)) - return $"\n{tabs}" + NumericalApproximation((int)obj, currentPropertyFullName, cc).ToString(); + return $"\n{tabs}" + NumericalApproximation((int)obj, currentPropertyFullName, cc.NumericalApproximationConfig).ToString(); // Fallback for any other floating-point numeric type. if (type.IsNumericFloatingPointType()) - return $"\n{tabs}" + NumericalApproximation(double.Parse(obj.ToString()), currentPropertyFullName, cc).ToString(); + return $"\n{tabs}" + NumericalApproximation(double.Parse(obj.ToString()), currentPropertyFullName, cc.NumericalApproximationConfig).ToString(); // Fallback for any other integral numeric type. if (type.IsNumericIntegralType()) - return $"\n{tabs}" + NumericalApproximation(double.Parse(obj.ToString()), currentPropertyFullName, cc).ToString(); + return $"\n{tabs}" + NumericalApproximation(double.Parse(obj.ToString()), currentPropertyFullName, cc.NumericalApproximationConfig).ToString(); } else if (type.IsPrimitive || type == typeof(String)) @@ -237,11 +234,7 @@ private static string HashString(object obj, BaseComparisonConfig cc, int nestin return (string)hashStringFromExtensionMethod; if (cc.UseGeometryHash && typeof(IGeometry).IsAssignableFrom(type)) - { - // Verify that no numerical approximation is requested for objects belonging to Geometrical types. - if (cc.IsCompatibleWithGeometryHash()) - return GeometryHash((IGeometry)obj).ToString(); - } + return GeometryHash((IGeometry)obj).ToString(); // If the object is an IObject (= a BHoM class), let's look at its properties. // We only do this for IObjects (BHoM types) since we cannot guarantee full compatibility of the following procedure with any possible (non-BHoM) type. diff --git a/BHoM_Engine/Query/NumericTolerance.cs b/BHoM_Engine/Query/NumericTolerance.cs index 46031cfaf..59faf7d15 100644 --- a/BHoM_Engine/Query/NumericTolerance.cs +++ b/BHoM_Engine/Query/NumericTolerance.cs @@ -38,13 +38,13 @@ public static partial class Query "If a CustomTolerance match is found for this property Full Name, then return it. " + "If multiple matches are found, return the most sensistive tolerance among the matches. " + "If no match is found, return the most sensitive tolerance (double.MinValue).")] - [Input("comparisonConfig", "Comparison Config from where tolerance information should be extracted.")] + [Input("numericalApproxConfig", "Numerical approximation Config containing the settings for Numerical Tolerance.")] [Input("propertyFullName", "Full name (path) of the property for which we want to extract the numerical Tolerance.")] - public static double NumericTolerance(this BaseComparisonConfig comparisonConfig, string propertyFullName) + public static double NumericTolerance(this NumericalApproximationConfig numericalApproxConfig, string propertyFullName) { - comparisonConfig = comparisonConfig ?? new ComparisonConfig(); + numericalApproxConfig = numericalApproxConfig ?? new NumericalApproximationConfig(); - return NumericTolerance(comparisonConfig.PropertyNumericTolerances, comparisonConfig.NumericTolerance, propertyFullName, false); + return NumericTolerance(numericalApproxConfig.PropertyNumericTolerances, numericalApproxConfig.NumericTolerance, propertyFullName, false); } /***************************************************/ diff --git a/BHoM_Engine/Query/NumericalApproximation.cs b/BHoM_Engine/Query/NumericalApproximation.cs index dc2a66ff0..9bd591560 100644 --- a/BHoM_Engine/Query/NumericalApproximation.cs +++ b/BHoM_Engine/Query/NumericalApproximation.cs @@ -45,12 +45,12 @@ public static partial class Query [Description("Compute the approximation of a floating-point number for its comparison with other numbers, given specific ComparisonConfig settings.")] [Input("number", "Number to approximate.")] [Input("fullName", "Name of the number or of the property that holds this number. This name will be used to seek any matching custom tolerance/significant figure to apply for this approximation in the `comparisonConfig` input.")] - [Input("comparisonConfig", "Object that stores the settings that will used for the approximation.")] - public static double NumericalApproximation(this double number, string fullName = null, BaseComparisonConfig comparisonConfig = null) + [Input("numericalApproxConfig", "Object that stores the settings that will used for the approximation.")] + public static double NumericalApproximation(this double number, string fullName = null, NumericalApproximationConfig numericalApproxConfig = null) { - comparisonConfig = comparisonConfig ?? new ComparisonConfig(); + numericalApproxConfig = numericalApproxConfig ?? new NumericalApproximationConfig(); - return NumericalApproximation(number, fullName, comparisonConfig.PropertyNumericTolerances, comparisonConfig.NumericTolerance, comparisonConfig.PropertySignificantFigures, comparisonConfig.SignificantFigures); + return NumericalApproximation(number, fullName, numericalApproxConfig.PropertyNumericTolerances, numericalApproxConfig.NumericTolerance, numericalApproxConfig.PropertySignificantFigures, numericalApproxConfig.SignificantFigures); } /***************************************************/ @@ -58,12 +58,12 @@ public static double NumericalApproximation(this double number, string fullName [Description("Compute the approximation of an integer number for its comparison with other numbers, given specific ComparisonConfig settings.")] [Input("number", "Number to approximate.")] [Input("fullName", "Name of the number or of the property that holds this number. This name will be used to seek any matching custom tolerance/significant figure to apply for this approximation in the `comparisonConfig` input.")] - [Input("comparisonConfig", "Object that stores the settings that will used for the approximation.")] - public static double NumericalApproximation(this int number, string fullName = null, BaseComparisonConfig comparisonConfig = null) + [Input("numericalApproxConfig", "Object that stores the settings that will used for the approximation.")] + public static double NumericalApproximation(this int number, string fullName = null, NumericalApproximationConfig numericalApproxConfig = null) { - comparisonConfig = comparisonConfig ?? new ComparisonConfig(); + numericalApproxConfig = numericalApproxConfig ?? new NumericalApproximationConfig(); - return NumericalApproximation(number, fullName, comparisonConfig.PropertySignificantFigures, comparisonConfig.SignificantFigures); + return NumericalApproximation(number, fullName, numericalApproxConfig.PropertySignificantFigures, numericalApproxConfig.SignificantFigures); } /***************************************************/ diff --git a/BHoM_Engine/Query/SignificantFigures.cs b/BHoM_Engine/Query/SignificantFigures.cs index 981f4716c..838b9034f 100644 --- a/BHoM_Engine/Query/SignificantFigures.cs +++ b/BHoM_Engine/Query/SignificantFigures.cs @@ -38,13 +38,13 @@ public static partial class Query "If a CustomTolerance match is found for this property Full Name, then return it. " + "If multiple matches are found, return the most sensistive among the matches. " + "If no match is found, return `ComparisonConfig.SignificantFigures`.")] - [Input("comparisonConfig", "Comparison Config from where tolerance information should be extracted.")] + [Input("numericalApproxConfig", "Numerical approximation Config containing the settings for significant figures.")] [Input("propertyFullName", "Full name (path) of the property for which we want to extract the numerical Tolerance.")] - public static int SignificantFigures(this BaseComparisonConfig comparisonConfig, string propertyFullName) + public static int SignificantFigures(this NumericalApproximationConfig numericalApproxConfig, string propertyFullName) { - comparisonConfig = comparisonConfig ?? new ComparisonConfig(); + numericalApproxConfig = numericalApproxConfig ?? new NumericalApproximationConfig(); - return SignificantFigures(comparisonConfig.PropertySignificantFigures, comparisonConfig.SignificantFigures, propertyFullName); + return SignificantFigures(numericalApproxConfig.PropertySignificantFigures, numericalApproxConfig.SignificantFigures, propertyFullName); } /***************************************************/ diff --git a/Diffing_Engine/Query/NumericalDifferenceInclusion.cs b/Diffing_Engine/Query/NumericalDifferenceInclusion.cs index d41aeb648..be72b65d3 100644 --- a/Diffing_Engine/Query/NumericalDifferenceInclusion.cs +++ b/Diffing_Engine/Query/NumericalDifferenceInclusion.cs @@ -46,13 +46,13 @@ public static partial class Query [Input("number1", "First number to compare.")] [Input("number2", "Second number to compare.")] [Input("propertyFullName", "If the numbers are part of an object, full name of the property that owns them. This name will be used to seek matches in the ComparisoConfig named numeric tolerance/significant figures.")] - [Input("comparisonConfig", "Object containing the settings for this numerical comparison.")] + [Input("numericalApproxConfig", "Object containing the settings for this numerical comparison.")] [Output("seenAsDifferent", "Whether the input numbers are seen as different, given the numerical approximations specified in the comparisonConfig.")] - public static bool NumericalDifferenceInclusion(this object number1, object number2, string propertyFullName = null, BaseComparisonConfig comparisonConfig = null) + public static bool NumericalDifferenceInclusion(this object number1, object number2, string propertyFullName = null, NumericalApproximationConfig numericalApproxConfig = null) { - comparisonConfig = comparisonConfig ?? new ComparisonConfig(); + numericalApproxConfig = numericalApproxConfig ?? new NumericalApproximationConfig(); - return NumericalDifferenceInclusion(number1, number2, propertyFullName, comparisonConfig.PropertyNumericTolerances, comparisonConfig.NumericTolerance, comparisonConfig.PropertySignificantFigures, comparisonConfig.SignificantFigures); + return NumericalDifferenceInclusion(number1, number2, propertyFullName, numericalApproxConfig.PropertyNumericTolerances, numericalApproxConfig.NumericTolerance, numericalApproxConfig.PropertySignificantFigures, numericalApproxConfig.SignificantFigures); } /***************************************************/ diff --git a/Diffing_Engine/Query/ObjectDifferences.cs b/Diffing_Engine/Query/ObjectDifferences.cs index 7ab463cd5..e6b4e2685 100644 --- a/Diffing_Engine/Query/ObjectDifferences.cs +++ b/Diffing_Engine/Query/ObjectDifferences.cs @@ -181,7 +181,7 @@ public static ObjectDifferences ObjectDifferences(this object pastObject, object continue; // Check if this difference is numerical, and if so whether it should be included or not given the input tolerance/significant figures. - if (!NumericalDifferenceInclusion(kellermanPropertyDifference.Object1, kellermanPropertyDifference.Object2, propertyFullName_noIndexes, cc)) + if (!NumericalDifferenceInclusion(kellermanPropertyDifference.Object1, kellermanPropertyDifference.Object2, propertyFullName_noIndexes, cc.NumericalApproximationConfig)) continue; // Add to the final result. diff --git a/Environment_Engine/Query/HasMergeablePropertiesWith.cs b/Environment_Engine/Query/HasMergeablePropertiesWith.cs index e5a5783f2..5312cc11d 100644 --- a/Environment_Engine/Query/HasMergeablePropertiesWith.cs +++ b/Environment_Engine/Query/HasMergeablePropertiesWith.cs @@ -67,7 +67,7 @@ public static bool HasMergeablePropertiesWith(this Panel element, Panel other) "BHoM_Guid", "CustomData", }, - NumericTolerance = BH.oM.Geometry.Tolerance.Distance + NumericalApproximationConfig = new NumericalApproximationConfig() { NumericTolerance = BH.oM.Geometry.Tolerance.Distance } }; return !Diffing.Query.DifferentProperties(element, other, cc)?.Any() ?? true; @@ -90,7 +90,7 @@ public static bool HasMergeablePropertiesWith(this Opening element, Opening othe "BHoM_Guid", "CustomData", }, - NumericTolerance = BH.oM.Geometry.Tolerance.Distance + NumericalApproximationConfig = new NumericalApproximationConfig() { NumericTolerance = BH.oM.Geometry.Tolerance.Distance } }; return !Diffing.Query.DifferentProperties(element, other, cc)?.Any() ?? true; @@ -123,8 +123,7 @@ public static bool HasMergeablePropertiesWith(this Space element, Space other) "BHoM_Guid", "CustomData", }, - NumericTolerance = BH.oM.Geometry.Tolerance.Distance, - + NumericalApproximationConfig = new NumericalApproximationConfig() { NumericTolerance = BH.oM.Geometry.Tolerance.Distance }, }; return !Diffing.Query.DifferentProperties(element, other, cc)?.Any() ?? true; diff --git a/Physical_Engine/Query/UniqueConstructions.cs b/Physical_Engine/Query/UniqueConstructions.cs index b4cf14953..4be098648 100644 --- a/Physical_Engine/Query/UniqueConstructions.cs +++ b/Physical_Engine/Query/UniqueConstructions.cs @@ -50,7 +50,7 @@ public static List UniqueConstructions(this List con { "CustomData" }, - NumericTolerance = BH.oM.Geometry.Tolerance.Distance + NumericalApproximationConfig = new NumericalApproximationConfig() { NumericTolerance = BH.oM.Geometry.Tolerance.Distance }, }; if (!includeConstructionName)