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

Add module getting objects with no set ID from loads #330

6 changes: 5 additions & 1 deletion .ci/code/BHoM_Adapter_Tests/Objects/StructuralAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ public StructuralAdapter()
{typeof(RigidLink), new List<Type> { typeof(LinkConstraint), typeof(Node) } },
{typeof(FEMesh), new List<Type> { typeof(Node), typeof(ISurfaceProperty)} },
{ typeof(IElementLoad<Bar>), new List<Type>{ typeof(Bar)} },
{ typeof(IElementLoad<Node>), new List<Type>{ typeof(Node)} }
{ typeof(IElementLoad<Node>), new List<Type>{ typeof(Node)} },
{ typeof(GravityLoad), new List<Type>{ typeof(Bar), typeof(Panel), typeof(FEMesh)} }
};

AdapterIdFragmentType = typeof(StructuralAdapterId);
BH.Adapter.Modules.Structure.ModuleLoader.LoadModules(this);
}

protected override bool ICreate<T>(IEnumerable<T> objects, ActionConfig actionConfig = null)
Expand Down
46 changes: 46 additions & 0 deletions .ci/code/BHoM_Adapter_Tests/Objects/StructuralAdapterId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.oM.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace BH.Tests.Adapter
{
[Description("")]
public class StructuralAdapterId : IAdapterId
{
/***************************************************/
/**** Properties ****/
/***************************************************/

[Description("")]
public virtual object Id { get; set; }


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

}
}


83 changes: 83 additions & 0 deletions .ci/code/BHoM_Adapter_Tests/PushTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,88 @@ public void DependencyOrder_UpdateAndFullPush()
Assert.IsTrue(orderedObjects.Where(t => t.Item1 == typeof(Node)).First().Item2 == PushType.UpdateOnly,
"For Node objects, UpdateOnly should have come before FullPush.");
}

[Test]
public void DependencyOrder_CreateLoadAllObjectsWithIds()
{
List<Bar> bars = Create.RandomObjects<Bar>(10);
for (int i = 0; i < bars.Count; i++)
{
Engine.Adapter.Modify.SetAdapterId(bars[i], new StructuralAdapterId { Id = i + 1 });
}
BarUniformlyDistributedLoad load = Create.RandomObject<BarUniformlyDistributedLoad>();
load.Objects.Elements = bars;

sa.Push(new List<object> { load });

string correctOrder = "BH.oM.Structure.Loads.Loadcase, BH.oM.Structure.Loads.BarUniformlyDistributedLoad"; //All bars contain Ids, hence no bars should be created even if there is a dependency on the bars
string createdOrder = string.Join(", ", sa.Created.Select(c => c.Item1.FullName));

Assert.AreEqual(correctOrder, createdOrder);
}

[Test]
public void DependencyOrder_CreateLoadHalfObjectsWithIds()
{
int objectCount = 10;
List<Bar> bars = Create.RandomObjects<Bar>(objectCount);
int withIdCount = objectCount / 2;
int withoutIdCount = objectCount - withIdCount;
for (int i = 0; i < withIdCount; i++)
{
Engine.Adapter.Modify.SetAdapterId(bars[i], new StructuralAdapterId { Id = i + 1 });
}

//Shuffle the order fo the bars.
//Doing this to test that the order of bars with and without Id does not matter
Random random = new Random(2);
bars = bars.OrderBy(x => random.Next()).ToList();

BarUniformlyDistributedLoad load = Create.RandomObject<BarUniformlyDistributedLoad>();
load.Objects.Elements = bars;

sa.Push(new List<object> { load });

Assert.IsTrue(sa.Created.Any(x => x.Item1 == typeof(Bar)), "No bars created.");
int barCreationCount = sa.Created.First(x => x.Item1 == typeof(Bar)).Item2.Count();

Assert.AreEqual(withoutIdCount, barCreationCount, "Wrong number of bars created.");
}

[Test]
public void DependencyOrder_CreateGravityLoadHalfObjectsWithIds()
{
int objectCount = 10;
List<Bar> bars = Create.RandomObjects<Bar>(objectCount);
List<Panel> panels = Create.RandomObjects<Panel>(objectCount);
int withIdCount = objectCount / 2;
int withoutIdCount = objectCount - withIdCount;
for (int i = 0; i < withIdCount; i++)
{
Engine.Adapter.Modify.SetAdapterId(bars[i], new StructuralAdapterId { Id = i + 1 });
Engine.Adapter.Modify.SetAdapterId(panels[i], new StructuralAdapterId { Id = i + 1 });
}

//Shuffle the order fo the bars.
//Doing this to test that the order of bars with and without Id does not matter
Random random = new Random(2);
bars = bars.OrderBy(x => random.Next()).ToList();
panels = panels.OrderBy(x => random.Next()).ToList();

GravityLoad load = Create.RandomObject<GravityLoad>();
load.Objects.Elements = bars.Cast<BHoMObject>().Concat(panels).ToList();

sa.Push(new List<object> { load });

Assert.IsTrue(sa.Created.Any(x => x.Item1 == typeof(Bar)), "No bars created.");
int barCreationCount = sa.Created.First(x => x.Item1 == typeof(Bar)).Item2.Count();

Assert.AreEqual(withoutIdCount, barCreationCount, "Wrong number of bars created.");

Assert.IsTrue(sa.Created.Any(x => x.Item1 == typeof(Panel)), "No Panels created.");
int panelsCreationCount = sa.Created.First(x => x.Item1 == typeof(Panel)).Item2.Count();

Assert.AreEqual(withoutIdCount, panelsCreationCount, "Wrong number of Panels created.");
}
}
}
5 changes: 3 additions & 2 deletions Adapter_Engine/Query/GetDependencyObjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ public static Dictionary<Type, IEnumerable> GetDependencyObjects<T>(this IEnumer
foreach (Type t in dependencyTypes)
{
MethodInfo generic = method.MakeGenericMethod(new Type[] { typeof(T), t });
var list = generic.Invoke(null, new object[] { objects, adapter });
IList list = generic.Invoke(null, new object[] { objects, adapter }) as IList;

dict.Add(t, list as IEnumerable);
if (list != null && list.Count != 0)
dict.Add(t, list);
}

return dict;
Expand Down
74 changes: 74 additions & 0 deletions Structure_AdapterModules/GetGravityLoadElementsWithoutID.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.oM.Base;
using BH.oM.Adapter;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.ComponentModel;
using BH.oM.Structure.Elements;
using System.Collections;
using BH.oM.Geometry;
using BH.oM.Structure.Constraints;
using BH.oM.Structure.Loads;


namespace BH.Adapter.Modules
{
[Description("Get all elements of type T that do not contain an adapter ID of the expected type from the provided GravityLoads. Avoids the need to again read and check against elements already in the model.")]
public class GetGravityLoadElementsWithoutID<T> : IGetDependencyModule<GravityLoad, T> where T : IBHoMObject
{
/***************************************************/
/**** Interface method ****/
/***************************************************/

public IEnumerable<T> GetDependencies(IEnumerable<GravityLoad> objects)
{
List<T> noIdLoadObjects = new List<T>();
foreach (GravityLoad load in objects)
{
if(load?.Objects?.Elements != null)
noIdLoadObjects.AddRange(load.Objects.Elements.OfType<T>().Where(x => x != null && !x.Fragments.Contains(m_adapterIdType)));
}
return noIdLoadObjects;
}

/***************************************************/
/**** Constructors ****/
/***************************************************/

public GetGravityLoadElementsWithoutID(IBHoMAdapter adapter)
{
m_adapterIdType = adapter.AdapterIdFragmentType;
}

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

private Type m_adapterIdType;

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


74 changes: 74 additions & 0 deletions Structure_AdapterModules/GetLoadElementsWithoutID.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2023, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.oM.Base;
using BH.oM.Adapter;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.ComponentModel;
using BH.oM.Structure.Elements;
using System.Collections;
using BH.oM.Geometry;
using BH.oM.Structure.Constraints;
using BH.oM.Structure.Loads;


namespace BH.Adapter.Modules
{
[Description("Get all elements that do not contain an adapter ID of the expected type from the provided IElementLoads. Avoids the need to again read and check against elements already in the model.")]
public class GetLoadElementsWithoutID<T> : IGetDependencyModule<IElementLoad<T>, T> where T : IBHoMObject
{
/***************************************************/
/**** Interface method ****/
/***************************************************/

public IEnumerable<T> GetDependencies(IEnumerable<IElementLoad<T>> objects)
{
List<T> noIdLoadObjects = new List<T>();
foreach (IElementLoad<T> load in objects)
{
if(load?.Objects?.Elements != null)
noIdLoadObjects.AddRange(load.Objects.Elements.Where(x => x != null && !x.Fragments.Contains(m_adapterIdType)));
}
return noIdLoadObjects;
}

/***************************************************/
/**** Constructors ****/
/***************************************************/

public GetLoadElementsWithoutID(IBHoMAdapter adapter)
{
m_adapterIdType = adapter.AdapterIdFragmentType;
}

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

private Type m_adapterIdType;

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


7 changes: 7 additions & 0 deletions Structure_AdapterModules/ModuleLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using BH.Adapter;
using BH.oM.Adapter;
using BH.oM.Base;
using BH.oM.Structure.Elements;

namespace BH.Adapter.Modules.Structure
{
Expand All @@ -40,6 +41,12 @@ public static void LoadModules(this BHoMAdapter adapter)
adapter.AdapterModules.Add(new CopyNodeProperties());
adapter.AdapterModules.Add(new GetCasesFromCombinations());
adapter.AdapterModules.Add(new GetCombinationsFromCombinations());
adapter.AdapterModules.Add(new GetLoadElementsWithoutID<Bar>(adapter));
adapter.AdapterModules.Add(new GetLoadElementsWithoutID<Node>(adapter));
adapter.AdapterModules.Add(new GetLoadElementsWithoutID<IAreaElement>(adapter));
adapter.AdapterModules.Add(new GetGravityLoadElementsWithoutID<Bar>(adapter));
adapter.AdapterModules.Add(new GetGravityLoadElementsWithoutID<Panel>(adapter));
adapter.AdapterModules.Add(new GetGravityLoadElementsWithoutID<FEMesh>(adapter));
}
}
}
Expand Down