Skip to content

Commit 82a1c1f

Browse files
authored
Diffing_Engine: IsEqual method added (#3338)
2 parents 35259da + 3bd2da1 commit 82a1c1f

File tree

6 files changed

+109
-46
lines changed

6 files changed

+109
-46
lines changed

Diffing_Engine/Compute/DiffOneByOne.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static Diff DiffOneByOne(IEnumerable<object> pastObjects, IEnumerable<obj
8282
{
8383
ObjectDifferences objectDifferences = Query.ObjectDifferences(pastObjects_cloned[i], currentObjects_cloned[i], diffConfigCopy.ComparisonConfig);
8484

85-
if (objectDifferences != null && (objectDifferences.Differences?.Any() ?? false))
85+
if (objectDifferences.Differences?.Any() ?? false)
8686
{
8787
modifiedObjects.Add(currentObjects_cloned[i]);
8888
anyChangeDetected = true;

Diffing_Engine/Compute/DiffRevisions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private static Diff DiffRevisionObjects(IEnumerable<IBHoMObject> pastObjects, IE
174174
// To compute differentProps in a Revision-Diffing, make sure we remove the RevisionFragment. We don't want to consider that.
175175
ObjectDifferences objectDifferences = Query.ObjectDifferences(oldBhomObj.RemoveFragment(typeof(RevisionFragment)), bhomObj.RemoveFragment(typeof(RevisionFragment)), dc.ComparisonConfig);
176176

177-
if (objectDifferences != null)
177+
if (objectDifferences.Differences.Any())
178178
modifiedObjectDifferences.Add(objectDifferences);
179179
}
180180
}

Diffing_Engine/Compute/DiffWithCustomIds.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,14 @@ private static Diff Diffing(IEnumerable<object> pastObjects, IEnumerable<string>
167167
{
168168
List<IPropertyDifference> customObjectDifferences = customComparer.Invoke(correspondingPastObj, followingObj, diffingConfig.ComparisonConfig);
169169

170-
if (objectDifferences == null)
171-
objectDifferences = new ObjectDifferences() { PastObject = correspondingPastObj, FollowingObject = followingObj };
170+
//if (objectDifferences == null)
171+
// objectDifferences = new ObjectDifferences() { PastObject = correspondingPastObj, FollowingObject = followingObj };
172172

173173
objectDifferences.Differences.AddRange(customObjectDifferences);
174174
}
175175
}
176176

177-
if (objectDifferences != null && (objectDifferences.Differences?.Any() ?? false))
177+
if (objectDifferences.Differences?.Any() ?? false)
178178
{
179179
// It's been modified
180180
modifiedObjs.Add(followingObj);

Diffing_Engine/Query/DifferentProperties.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static Dictionary<string, Tuple<object,object>> DifferentProperties(this
5454
ObjectDifferences objectDifferences = Query.ObjectDifferences(obj1, obj2, comparisonConfig);
5555

5656
if (objectDifferences == null)
57-
return null;
57+
return new Dictionary<string, Tuple<object, object>>();
5858

5959
// Group the `ObjectDifferences` in a Dictionary.
6060
Dictionary<string, Tuple<object, object>> result = objectDifferences.Differences.GroupBy(d => d.FullName)

Diffing_Engine/Query/IsEqual.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.oM.Base;
24+
using BH.oM.Base.Attributes;
25+
using System.ComponentModel;
26+
using System.Linq;
27+
28+
namespace BH.Engine.Diffing
29+
{
30+
public static partial class Query
31+
{
32+
/***************************************************/
33+
/**** Public Methods ****/
34+
/***************************************************/
35+
36+
[Description("Checks whether the two objects are the same within the scope defined by the ComparisonConfig.")]
37+
[Input("obj1", "First object to check for equality.")]
38+
[Input("obj2", "Second object to check for equality.")]
39+
[Input("comparisonConfig", "Comparison configuration to be used for the comparison.")]
40+
[Output("equal", "True if the two objects are the same within the scope defined by the provided ComparisonConfig, otherwise false.")]
41+
public static bool IsEqual(this object obj1, object obj2, BaseComparisonConfig comparisonConfig = null)
42+
{
43+
if (obj1 == null || obj2 == null)
44+
return obj1 == obj2;
45+
46+
if (obj1.GetType() != obj2.GetType())
47+
return false;
48+
49+
return !obj1.Differences(obj2, comparisonConfig).Any();
50+
}
51+
52+
/***************************************************/
53+
}
54+
}

Diffing_Engine/Query/ObjectDifferences.cs

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,16 @@
2020
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
2121
*/
2222

23+
using BH.Engine.Base;
24+
using BH.oM.Base;
25+
using BH.oM.Base.Attributes;
26+
using BH.oM.Diffing;
2327
using System;
28+
using System.Collections;
2429
using System.Collections.Generic;
25-
using System.Linq;
26-
using System.Text;
27-
using System.Threading.Tasks;
2830
using System.ComponentModel;
29-
using BH.oM.Base.Attributes;
30-
using BH.oM.Diffing;
31-
using BH.oM.Base;
31+
using System.Linq;
3232
using kellerman = KellermanSoftware.CompareNetObjects;
33-
using System.Reflection;
34-
using BH.Engine.Base;
35-
using BH.Engine.Reflection;
36-
using System.Collections;
3733

3834
namespace BH.Engine.Diffing
3935
{
@@ -51,13 +47,28 @@ public static partial class Query
5147
"\nIf no difference was found, returns null.")]
5248
public static ObjectDifferences ObjectDifferences(this object pastObject, object followingObject, BaseComparisonConfig comparisonConfig = null)
5349
{
54-
// Result object.
5550
ObjectDifferences result = new ObjectDifferences() { PastObject = pastObject, FollowingObject = followingObject };
56-
51+
5752
// Null check. At least one of the objects must be not null.
5853
if (pastObject == null && followingObject == null)
5954
return result;
6055

56+
result.Differences.AddRange(pastObject.Differences(followingObject, comparisonConfig));
57+
return result;
58+
}
59+
60+
61+
/***************************************************/
62+
/**** Private Methods ****/
63+
/***************************************************/
64+
65+
[Description("Finds the actual differences between the objects.")]
66+
private static IEnumerable<PropertyDifference> Differences(this object pastObject, object followingObject, BaseComparisonConfig comparisonConfig = null)
67+
{
68+
// Result object.
69+
List<PropertyDifference> returned = new List<PropertyDifference>();
70+
PropertyDifference toReturn;
71+
6172
// Set ComparisonConfig if null. Clone it for immutability in the UI.
6273
BaseComparisonConfig cc = comparisonConfig == null ? new ComparisonConfig() : comparisonConfig.DeepClone();
6374

@@ -122,8 +133,12 @@ public static ObjectDifferences ObjectDifferences(this object pastObject, object
122133
{
123134
ComparisonInclusion comparisonInclusion = comparisonInclusionFromExtensionMethod as ComparisonInclusion;
124135
if (comparisonInclusion != null && comparisonInclusion.Include)
136+
{
125137
// Add to the final result.
126-
result.AddPropertyDifference(comparisonInclusion.DisplayName, kellermanPropertyDifference.Object1, kellermanPropertyDifference.Object2, propertyFullName);
138+
toReturn = PropertyDifference(pastObject, followingObject, comparisonInclusion.DisplayName, kellermanPropertyDifference.Object1, kellermanPropertyDifference.Object2, propertyFullName);
139+
returned.Add(toReturn);
140+
yield return toReturn;
141+
}
127142

128143
// Because a `ComparisonInclusion()` extension method was found, we've already determined if this difference was to be considered or not. Continue.
129144
continue;
@@ -142,7 +157,7 @@ public static ObjectDifferences ObjectDifferences(this object pastObject, object
142157
propertyFullName = propertyFullName.Replace(".Value", "");
143158

144159
// Workaround for Kellerman duplicating the CustomData differences.
145-
if (result.Differences.Any(d => d.FullName == propertyFullName))
160+
if (returned.Any(d => d.FullName == propertyFullName))
146161
continue;
147162

148163
// Check if we are talking about CustomObjects.
@@ -185,40 +200,37 @@ public static ObjectDifferences ObjectDifferences(this object pastObject, object
185200
continue;
186201

187202
// Add to the final result.
188-
result.AddPropertyDifference(propertyDisplayName, kellermanPropertyDifference.Object1, kellermanPropertyDifference.Object2, propertyFullName);
203+
toReturn = PropertyDifference(pastObject, followingObject, propertyDisplayName, kellermanPropertyDifference.Object1, kellermanPropertyDifference.Object2, propertyFullName);
204+
returned.Add(toReturn);
205+
yield return toReturn;
189206
}
190-
191-
if (result.Differences.Count == 0)
192-
return null;
193-
194-
return result;
195207
}
196208

197-
/***************************************************/
198-
/**** Private Methods ****/
199209
/***************************************************/
200210

201211
[Description("Removes square bracket indexing from property paths, e.g. `Bar.Fragments[0].Something` is returned as `Bar.Fragments.Something`.")]
202-
private static void AddPropertyDifference(this ObjectDifferences objectDifferences, string propertyDiffDisplayName, object pastValue, object follValue, string fullName, string description = null)
212+
private static PropertyDifference PropertyDifference(this object pastObject, object followingObject, string propertyDiffDisplayName, object pastValue, object follValue, string fullName, string description = null)
203213
{
204214
description = string.IsNullOrWhiteSpace(description) ?
205-
PropertyDifferenceDescription(objectDifferences, propertyDiffDisplayName, pastValue, follValue)
215+
PropertyDifferenceDescription(pastObject, followingObject, propertyDiffDisplayName, pastValue, follValue)
206216
: description;
207217

208-
objectDifferences.Differences.Add(new PropertyDifference()
218+
return new PropertyDifference()
209219
{
210220
Name = propertyDiffDisplayName,
211221
Description = description,
212222
PastValue = pastValue,
213223
FollowingValue = follValue,
214224
FullName = fullName
215-
}); ;
225+
};
216226
}
217227

218-
private static string PropertyDifferenceDescription(ObjectDifferences objectDifferences, string propertyDiffDisplayName, object pastValue, object follValue, bool includeObjName = true, bool includeObjType = false, bool includeObjGuid = false)
228+
/***************************************************/
229+
230+
private static string PropertyDifferenceDescription(this object pastObject, object followingObject, string propertyDiffDisplayName, object pastValue, object follValue, bool includeObjName = true, bool includeObjType = false, bool includeObjGuid = false)
219231
{
220232
if (pastValue is IEnumerable && follValue is IEnumerable)
221-
return $"The collection stored in the property `{propertyDiffDisplayName}` of the `{objectDifferences.PastObject.GetType().FullName}` was modified.";
233+
return $"The collection stored in the property `{propertyDiffDisplayName}` of the `{pastObject.GetType().FullName}` was modified.";
222234

223235
Type t = pastValue?.GetType() ?? follValue?.GetType() ?? typeof(object);
224236

@@ -228,12 +240,12 @@ private static string PropertyDifferenceDescription(ObjectDifferences objectDiff
228240
{
229241
bool addedObjectDescription = false;
230242

231-
objectDescription = objectDifferences.PastObject is CustomObject ? "CustomObject " : "Object ";
243+
objectDescription = pastObject is CustomObject ? "CustomObject " : "Object ";
232244

233245
if (includeObjName)
234246
{
235-
string pastObjName = (objectDifferences.PastObject as IBHoMObject)?.Name;
236-
string follObjName = (objectDifferences.FollowingObject as IBHoMObject)?.Name;
247+
string pastObjName = (pastObject as IBHoMObject)?.Name;
248+
string follObjName = (followingObject as IBHoMObject)?.Name;
237249

238250
if (!string.IsNullOrWhiteSpace(pastObjName) && pastObjName == follObjName)
239251
{
@@ -242,16 +254,16 @@ private static string PropertyDifferenceDescription(ObjectDifferences objectDiff
242254
}
243255
}
244256

245-
if (includeObjType && !(objectDifferences.PastObject is CustomObject))
257+
if (includeObjType && !(pastObject is CustomObject))
246258
{
247-
objectDescription += $"of type `{objectDifferences.PastObject.GetType().FullName}` ";
259+
objectDescription += $"of type `{pastObject.GetType().FullName}` ";
248260
addedObjectDescription = true;
249261
}
250262

251263
if (includeObjGuid)
252264
{
253-
Guid? pastObjGuid = (objectDifferences.PastObject as IBHoMObject)?.BHoM_Guid;
254-
Guid? follObjGuid = (objectDifferences.FollowingObject as IBHoMObject)?.BHoM_Guid;
265+
Guid? pastObjGuid = (pastObject as IBHoMObject)?.BHoM_Guid;
266+
Guid? follObjGuid = (followingObject as IBHoMObject)?.BHoM_Guid;
255267

256268
if (pastObjGuid != null && pastObjGuid == follObjGuid)
257269
{
@@ -301,10 +313,7 @@ private static bool PropertyNameWildcardMatch(this string propertyName, string w
301313

302314
return propertyName.WildcardMatch(wildcardPattern);
303315
}
316+
317+
/***************************************************/
304318
}
305319
}
306-
307-
308-
309-
310-

0 commit comments

Comments
 (0)