Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BHoM_Engine: refactor to follow BHoM_Adapter refactoring Level1 #1092

Merged
merged 3 commits into from
Sep 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 1 addition & 69 deletions Reflection_Engine/Query/DistinctProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,100 +75,32 @@ public static IEnumerable<P> DistinctProperties<T, P>(this IEnumerable<T> object

// Collect the property objects
List<P> propertyObjects = new List<P>();
Dictionary<PropertyInfo, Action<T, P>> settersSingle = new Dictionary<PropertyInfo, Action<T, P>>();
Dictionary<PropertyInfo, Func<T, P>> gettersSingle = new Dictionary<PropertyInfo, Func<T, P>>();

//Get the distinct properties for the single value properties
foreach (PropertyInfo property in propertiesSingle)
{
// Optimisation using this article: https://blogs.msmvps.com/jonskeet/2008/08/09/making-reflection-fly-and-exploring-delegates/
Func<T, P> getProp = (Func<T, P>)Delegate.CreateDelegate(typeof(Func<T, P>), property.GetGetMethod());
Action<T, P> setProp = (Action<T, P>)Delegate.CreateDelegate(typeof(Action<T, P>), property.GetSetMethod());

// Keep those for later
settersSingle.Add(property, setProp);
gettersSingle.Add(property, getProp);

// Collect the objects from this property
propertyObjects.AddRange(objects.Select(x => getProp(x)).Where(x => x!=null));
}

Dictionary<PropertyInfo, Action<T, List<P>>> settersList = new Dictionary<PropertyInfo, Action<T, List<P>>>();
Dictionary<PropertyInfo, Func<T, List<P>>> gettersList = new Dictionary<PropertyInfo, Func<T, List<P>>>();

//Get the distinct properties for the single value properties
foreach (PropertyInfo property in propertiesList)
{
// Optimisation using this article: https://blogs.msmvps.com/jonskeet/2008/08/09/making-reflection-fly-and-exploring-delegates/
Func<T, List<P>> getProp = (Func<T, List<P>>)Delegate.CreateDelegate(typeof(Func<T, List<P>>), property.GetGetMethod());
Action<T, List<P>> setProp = (Action<T, List<P>>)Delegate.CreateDelegate(typeof(Action<T, List<P>>), property.GetSetMethod());

// Keep those for later
settersList.Add(property, setProp);
gettersList.Add(property, getProp);

// Collect the objects from this property
propertyObjects.AddRange(objects.SelectMany(x => getProp(x)));
}

// Clone the distinct property objects
Dictionary<Guid, P> cloneDictionary = CloneObjects<P>(propertyObjects.DistinctDictionary());


//Assign cloned distinct single property objects back into input objects
foreach (PropertyInfo property in propertiesSingle)
{
Action<T, P> setProp = settersSingle[property];
Func<T, P> getProp = gettersSingle[property];
foreach (T x in objects)
{
P prop = getProp(x);
if(prop != null)
setProp(x, cloneDictionary[prop.BHoM_Guid]);
}
}

//Assign cloned distinct list property objects back into input objects
foreach (PropertyInfo property in propertiesList)
{
Action<T, List<P>> setProp = settersList[property];
Func<T, List<P>> getProp = gettersList[property];

foreach (T x in objects)
setProp(x, getProp(x).Select(y => cloneDictionary[y.BHoM_Guid]).ToList());

}

//Return the disticnt property objects
return cloneDictionary.Values;
return propertyObjects;
}


/***************************************************/
/**** Private Methods ****/
/***************************************************/

private static Dictionary<Guid, T> DistinctDictionary<T>(this IEnumerable<T> list) where T : IBHoMObject
{
return list.GroupBy(x => x.BHoM_Guid).Select(x => x.First()).ToDictionary(x => x.BHoM_Guid);
}

/***************************************************/

private static Dictionary<Guid, T> CloneObjects<T>(Dictionary<Guid, T> dict) where T : IBHoMObject
{
Dictionary<Guid, T> clones = new Dictionary<Guid, T>();

foreach (KeyValuePair<Guid, T> kvp in dict)
{
T obj = (T)kvp.Value.GetShallowClone();
obj.CustomData = new Dictionary<string, object>(kvp.Value.CustomData);
clones.Add(kvp.Key, obj);
}

return clones;
}

/***************************************************/
}
}