|
| 1 | +/* |
| 2 | + * This file is part of the Buildings and Habitats object Model (BHoM) |
| 3 | + * Copyright (c) 2015 - 2024, the respective contributors. All rights reserved. |
| 4 | + * |
| 5 | + * Each contributor holds copyright over their respective contributions. |
| 6 | + * The project versioning (Git) records all such contribution source information. |
| 7 | + * |
| 8 | + * |
| 9 | + * The BHoM is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Lesser General Public License as published by |
| 11 | + * the Free Software Foundation, either version 3.0 of the License, or |
| 12 | + * (at your option) any later version. |
| 13 | + * |
| 14 | + * The BHoM is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Lesser General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Lesser General Public License |
| 20 | + * along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. |
| 21 | + */ |
| 22 | + |
| 23 | +using BH.Engine.Base.Objects; |
| 24 | +using BH.oM.Base; |
| 25 | +using BH.oM.Base.Attributes; |
| 26 | +using BH.oM.Verification; |
| 27 | +using BH.oM.Verification.Conditions; |
| 28 | +using System; |
| 29 | +using System.ComponentModel; |
| 30 | + |
| 31 | +namespace BH.Engine.Verification |
| 32 | +{ |
| 33 | + public static partial class Compute |
| 34 | + { |
| 35 | + /***************************************************/ |
| 36 | + /**** Public Methods ****/ |
| 37 | + /***************************************************/ |
| 38 | + |
| 39 | + [Description("Compares two values using the provided comparison requirement and tolerance.")] |
| 40 | + [Input("value", "Value to compare against the reference value.")] |
| 41 | + [Input("referenceValue", "Reference value to compare the value against.")] |
| 42 | + [Input("comparisonType", "Comparison requirement, i.e. whether the value should be equal, greater, less than reference value etc.")] |
| 43 | + [Input("tolerance", "Tolerance to apply in the comparison.")] |
| 44 | + [Output("result", "True if comparison of the input values meets the comparison requirement, otherwise false. Null in case of inconclusive comparison.")] |
| 45 | + public static bool? CompareValues(this object value, object referenceValue, ValueComparisonType comparisonType, object tolerance) |
| 46 | + { |
| 47 | + // Basic cases (check for nullity) |
| 48 | + if (referenceValue == null && value == null) |
| 49 | + return true; |
| 50 | + else if (referenceValue == null || value == null) |
| 51 | + return false; |
| 52 | + |
| 53 | + if (value is Type && referenceValue is Type) |
| 54 | + return value == referenceValue; |
| 55 | + |
| 56 | + // Try enum comparison |
| 57 | + if (value is Enum || referenceValue is Enum) |
| 58 | + return value.GetType() == referenceValue.GetType() && (int)value == (int)referenceValue; |
| 59 | + |
| 60 | + // Try a numerical comparison |
| 61 | + double numericalValue, referenceNumValue; |
| 62 | + if (double.TryParse(value?.ToString(), out numericalValue) && double.TryParse(referenceValue?.ToString(), out referenceNumValue)) |
| 63 | + { |
| 64 | + double numTolerance; |
| 65 | + if (!double.TryParse(tolerance?.ToString(), out numTolerance)) |
| 66 | + numTolerance = 1e-03; |
| 67 | + |
| 68 | + return NumericalComparison(numericalValue, referenceNumValue, numTolerance, comparisonType); |
| 69 | + } |
| 70 | + |
| 71 | + // Try string comparison |
| 72 | + if (value is string && referenceValue is string) |
| 73 | + return StringComparison((string)value, (string)referenceValue, comparisonType); |
| 74 | + |
| 75 | + // Consider some other way to compare objects. |
| 76 | + if (comparisonType == ValueComparisonType.EqualTo || comparisonType == ValueComparisonType.NotEqualTo) |
| 77 | + { |
| 78 | + bool? passed; |
| 79 | + |
| 80 | + // If the referenceValue is a Type, convert this ValueCondition to a IsOfType condition. |
| 81 | + if (referenceValue is Type) |
| 82 | + { |
| 83 | + IsOfType typeCondition = new IsOfType() { Type = referenceValue as Type }; |
| 84 | + passed = value.IPasses(typeCondition); |
| 85 | + } |
| 86 | + else |
| 87 | + passed = CompareObjectEquality(value, referenceValue, tolerance); |
| 88 | + |
| 89 | + if (passed != null && comparisonType == ValueComparisonType.NotEqualTo) |
| 90 | + passed = !passed; |
| 91 | + |
| 92 | + return passed; |
| 93 | + } |
| 94 | + |
| 95 | + BH.Engine.Base.Compute.RecordWarning("Objects could not be compared because no meaningful comparison method has been found."); |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + /***************************************************/ |
| 101 | + /**** Private Methods ****/ |
| 102 | + /***************************************************/ |
| 103 | + |
| 104 | + private static bool CompareObjectEquality(object value, object refValue, object tolerance) |
| 105 | + { |
| 106 | + if (value == null || refValue == null) |
| 107 | + return value == refValue; |
| 108 | + |
| 109 | + if (value.GetType() != refValue.GetType()) |
| 110 | + return false; |
| 111 | + |
| 112 | + var cc = tolerance as ComparisonConfig; |
| 113 | + if (cc != null) |
| 114 | + { |
| 115 | + HashComparer<object> hc = new HashComparer<object>(cc); |
| 116 | + return hc.Equals(value, refValue); |
| 117 | + } |
| 118 | + |
| 119 | + return value.Equals(refValue); |
| 120 | + } |
| 121 | + |
| 122 | + /***************************************************/ |
| 123 | + |
| 124 | + private static bool? NumericalComparison(double value, double referenceValue, double tolerance, ValueComparisonType condition) |
| 125 | + { |
| 126 | + switch (condition) |
| 127 | + { |
| 128 | + case ValueComparisonType.EqualTo: |
| 129 | + return (Math.Abs(value - referenceValue) <= tolerance); |
| 130 | + case ValueComparisonType.NotEqualTo: |
| 131 | + return (Math.Abs(value - referenceValue) > tolerance); |
| 132 | + case ValueComparisonType.GreaterThan: |
| 133 | + return (value - referenceValue > tolerance); |
| 134 | + case ValueComparisonType.GreaterThanOrEqualTo: |
| 135 | + return (value - referenceValue >= -tolerance); |
| 136 | + case ValueComparisonType.LessThan: |
| 137 | + return (value - referenceValue < -tolerance); |
| 138 | + case ValueComparisonType.LessThanOrEqualTo: |
| 139 | + return (value - referenceValue <= tolerance); |
| 140 | + default: |
| 141 | + BH.Engine.Base.Compute.RecordWarning($"Comparison of type {condition} is not supported for numbers."); |
| 142 | + return null; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /***************************************************/ |
| 147 | + |
| 148 | + private static bool? StringComparison(string value, string referenceValue, ValueComparisonType condition) |
| 149 | + { |
| 150 | + switch (condition) |
| 151 | + { |
| 152 | + case ValueComparisonType.EqualTo: |
| 153 | + return value == referenceValue; |
| 154 | + case ValueComparisonType.NotEqualTo: |
| 155 | + return value != referenceValue; |
| 156 | + case ValueComparisonType.Contains: |
| 157 | + return value.Contains(referenceValue); |
| 158 | + case ValueComparisonType.StartsWith: |
| 159 | + return value.StartsWith(referenceValue); |
| 160 | + case ValueComparisonType.EndsWith: |
| 161 | + return value.EndsWith(referenceValue); |
| 162 | + default: |
| 163 | + BH.Engine.Base.Compute.RecordWarning($"Comparison of type {condition} is not supported for strings."); |
| 164 | + return null; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + /***************************************************/ |
| 169 | + } |
| 170 | +} |
0 commit comments