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

Make use of caching system in base adapter #504

Merged
17 changes: 12 additions & 5 deletions Robot_Adapter/CRUD/Create/Elements/Panels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ private RobotGeoObject CreateRobotContour(List<Edge> edges, out List<Edge> subEd

private void SetRobotPanelEdgeConstraints(RobotObjObject panel, List<Edge> edges)
{
if (edges.All(x => x.Support == null && x.Release == null)) //All supports and releases are null can just return without making any com calls
return;

IRobotCollection panelEdges = panel.Main.Edges;

if (panelEdges.Count != edges.Count)
Expand All @@ -207,14 +210,18 @@ private void SetRobotPanelEdgeConstraints(RobotObjObject panel, List<Edge> edges

for (int i = 1; i <= panelEdges.Count; i++)
{
IRobotObjEdge panelEdge = panelEdges.Get(i);
if (panelEdge == null)
continue;

Constraint6DOF support = edges[i - 1].Support;
Constraint4DOF release = edges[i - 1].Release;

if (support != null)
{
IRobotObjEdge panelEdge = panelEdges.Get(i);
if (panelEdge == null)
continue;

panelEdge.SetLabel(IRobotLabelType.I_LT_SUPPORT, support.DescriptionOrName());
Constraint4DOF release = edges[i - 1].Release;
}

if (release != null)
m_RobotApplication.Project.Structure.Objects.LinearReleases.Set(panel.Number, i, panel.Number, 1, release.DescriptionOrName());
}
Expand Down
18 changes: 9 additions & 9 deletions Robot_Adapter/CRUD/Read/Elements/Bars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,25 @@ private List<Bar> ReadBars(IList ids = null)

m_RobotApplication.Project.Structure.Bars.EndMultiOperation();

List<string> nodeIds = bhomBars.SelectMany(x => new string[] { x.StartNode.Name, x.EndNode.Name }).Distinct().ToList();
Dictionary<string, Node> bhomNodes = ReadNodes(nodeIds).ToDictionaryDistinctCheck(x => GetAdapterId<int>(x).ToString());
List<int> nodeIds = bhomBars.SelectMany(x => new int[] { int.Parse(x.StartNode.Name), int.Parse(x.EndNode.Name) }).Distinct().ToList();
Dictionary<int, Node> bhomNodes = GetCachedOrReadAsDictionary<int, Node>(nodeIds);
List<string> releaseIds = bhomBars.Select(x => x.Release?.Name).Where(x => x != null).Distinct().ToList();
Dictionary<string, BarRelease> bhombarReleases = releaseIds.Count == 0 ? new Dictionary<string, BarRelease>() : ReadBarRelease(releaseIds).ToDictionaryDistinctCheck(x => x.Name.ToString());
Dictionary<string, BarRelease> bhombarReleases = releaseIds.Count == 0 ? new Dictionary<string, BarRelease>() : GetCachedOrReadAsDictionary<string, BarRelease>(releaseIds);
List<string> materialIds = bhomBars.Select(x => x.SectionProperty?.Material?.Name).Where(x => x != null).Distinct().ToList();
Dictionary<string, IMaterialFragment> bhomMaterials = materialIds.Count == 0 ? new Dictionary<string, IMaterialFragment>() : ReadMaterials(materialIds).ToDictionaryDistinctCheck(x => x.Name.ToString());
Dictionary<string, IMaterialFragment> bhomMaterials = materialIds.Count == 0 ? new Dictionary<string, IMaterialFragment>() : GetCachedOrReadAsDictionary<string, IMaterialFragment>(materialIds);
List<string> sectionIds = bhomBars.Select(x => x.SectionProperty?.Name).Where(x => x != null).Distinct().ToList();
Dictionary<string, ISectionProperty> bhomSections = sectionIds.Count == 0 ? new Dictionary<string, ISectionProperty>() : ReadSectionProperties(sectionIds, bhomMaterials).ToDictionaryDistinctCheck(x => x.Name.ToString());
Dictionary<string, ISectionProperty> bhomSections = sectionIds.Count == 0 ? new Dictionary<string, ISectionProperty>() : GetCachedOrReadAsDictionary<string, ISectionProperty>(sectionIds);
List<string> offsetIds = bhomBars.Select(x => x.Offset?.Name).Where(x => x != null).Distinct().ToList();
Dictionary<string, Offset> offsets = offsetIds.Count == 0 ? new Dictionary<string, Offset>() : ReadOffsets(offsetIds).ToDictionaryDistinctCheck(x => x.Name.ToString());
Dictionary<string, Offset> offsets = offsetIds.Count == 0 ? new Dictionary<string, Offset>() : GetCachedOrReadAsDictionary<string, Offset>(offsetIds);
List<string> framingElemIds = bhomBars.Select(x => x.FindFragment<FramingElementDesignProperties>()?.Name).Where(x => x != null).Distinct().ToList();
Dictionary<string, FramingElementDesignProperties> bhomFramEleDesProps = framingElemIds.Count == 0 ? new Dictionary<string, FramingElementDesignProperties>() : ReadFramingElementDesignProperties(framingElemIds).ToDictionaryDistinctCheck(x => x.Name.ToString());
Dictionary<string, FramingElementDesignProperties> bhomFramEleDesProps = framingElemIds.Count == 0 ? new Dictionary<string, FramingElementDesignProperties>() : GetCachedOrReadAsDictionary<string, FramingElementDesignProperties>(framingElemIds);
Dictionary<string, Dictionary<string, ISectionProperty>> sectionWithMaterial = new Dictionary<string, Dictionary<string, ISectionProperty>>(); //Used to store sections where the material differs from the default

foreach (Bar bar in bhomBars)
{
bool nodesExtracted = true;
Node startNode;
if (bhomNodes.TryGetValue(bar.StartNode.Name, out startNode))
if (bhomNodes.TryGetValue(int.Parse(bar.StartNode.Name), out startNode))
bar.StartNode = startNode;
else
{
Expand All @@ -127,7 +127,7 @@ private List<Bar> ReadBars(IList ids = null)
}

Node endNode;
if (bhomNodes.TryGetValue(bar.EndNode.Name, out endNode))
if (bhomNodes.TryGetValue(int.Parse(bar.EndNode.Name), out endNode))
bar.EndNode = endNode;
else
{
Expand Down
6 changes: 3 additions & 3 deletions Robot_Adapter/CRUD/Read/Elements/Groups.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public partial class RobotAdapter
RobotGroup rgroup = m_RobotApplication.Project.Structure.Groups.Get(IRobotObjectType.I_OT_BAR, i);
if (rgroup != null)
{
List<Bar> obj = ReadBars(Convert.FromRobotSelectionString(rgroup.SelList));
List<Bar> obj = GetCachedOrRead<Bar>(Convert.FromRobotSelectionString(rgroup.SelList));
groups.Add(BH.Engine.Base.Create.BHoMGroup(obj, false, rgroup.Name));
}
}
Expand All @@ -51,7 +51,7 @@ public partial class RobotAdapter
RobotGroup rgroup = m_RobotApplication.Project.Structure.Groups.Get(IRobotObjectType.I_OT_NODE, i);
if (rgroup != null)
{
List<Node> obj = ReadNodes(Convert.FromRobotSelectionString(rgroup.SelList));
List<Node> obj = GetCachedOrRead<Node>(Convert.FromRobotSelectionString(rgroup.SelList));
groups.Add(BH.Engine.Base.Create.BHoMGroup(obj, false, rgroup.Name));
}
}
Expand All @@ -60,7 +60,7 @@ public partial class RobotAdapter
RobotGroup rgroup = m_RobotApplication.Project.Structure.Groups.Get(IRobotObjectType.I_OT_PANEL, i);
if (rgroup != null)
{
List<Panel> obj = ReadPanels(Convert.FromRobotSelectionString(rgroup.SelList));
List<Panel> obj = GetCachedOrRead<Panel>(Convert.FromRobotSelectionString(rgroup.SelList));
groups.Add(BH.Engine.Base.Create.BHoMGroup(obj, false, rgroup.Name));
}
}
Expand Down
2 changes: 1 addition & 1 deletion Robot_Adapter/CRUD/Read/Elements/Nodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private List<Node> ReadNodes(IList ids = null)
bhomNodes.Add(bhomNode);
}
List<string> contraintIds = bhomNodes.Select(x => x.Support?.Name).Where(x => x != null).Distinct().ToList();
Dictionary<string, Constraint6DOF> supports = contraintIds.Count == 0 ? new Dictionary<string, Constraint6DOF>() : ReadConstraints6DOF(contraintIds).ToDictionaryDistinctCheck(x => x.Name.ToString());
Dictionary<string, Constraint6DOF> supports = contraintIds.Count == 0 ? new Dictionary<string, Constraint6DOF>() : GetCachedOrReadAsDictionary<string, Constraint6DOF>(contraintIds);

foreach (Node node in bhomNodes)
{
Expand Down
5 changes: 2 additions & 3 deletions Robot_Adapter/CRUD/Read/Elements/RigidLinks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ private List<RigidLink> ReadRigidLinks(IList ids = null)
List<int> linksIds = CheckAndGetIds<RigidLink>(ids);
List<RigidLink> bhomRigidLinks = new List<RigidLink>();

IEnumerable<Node> bhomNodesList = ReadNodes();
Dictionary<int, Node> bhomNodes = bhomNodesList.ToDictionary(x => GetAdapterId<int>(x));
Dictionary<string, LinkConstraint> bhomLinkConstraints = ReadLinkConstraint().ToDictionary(x => x.Name.ToString());
Dictionary<int, Node> bhomNodes = GetCachedOrReadAsDictionary<int, Node>();
Dictionary<string, LinkConstraint> bhomLinkConstraints = GetCachedOrReadAsDictionary<string, LinkConstraint>();

if (linksIds == null || linksIds.Count == 0)
{
Expand Down
6 changes: 3 additions & 3 deletions Robot_Adapter/CRUD/Read/Loads/ControurLoads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private List<ILoad> ReadContourLoads(List<string> ids = null)
{

List<ILoad> bhomLoads = new List<ILoad>();
Dictionary<string, Loadcase> bhomLoadCases = ReadLoadCase().ToDictionaryDistinctCheck(x => x.Name);
Dictionary<int, Loadcase> bhomLoadCases = GetCachedOrReadAsDictionary<int, Loadcase>();
IRobotCaseCollection loadCollection = m_RobotApplication.Project.Structure.Cases.GetAll();

for (int i = 1; i <= loadCollection.Count; i++)
Expand All @@ -54,7 +54,7 @@ private List<ILoad> ReadContourLoads(List<string> ids = null)
if (lCase.Type == IRobotCaseType.I_CT_SIMPLE)
{
IRobotSimpleCase sCase = lCase as IRobotSimpleCase;
if (bhomLoadCases.ContainsKey(sCase.Name))
if (bhomLoadCases.ContainsKey(sCase.Number))
{
for (int j = 1; j <= sCase.Records.Count; j++)
{
Expand Down Expand Up @@ -88,7 +88,7 @@ private List<ILoad> ReadContourLoads(List<string> ids = null)
{
Force = new Vector { X = fx, Y = fy, Z = fz },
Contour = new Polyline { ControlPoints = contourPoints },
Loadcase = bhomLoadCases[sCase.Name],
Loadcase = bhomLoadCases[sCase.Number],
Axis = localAxis == 1 ? LoadAxis.Local : LoadAxis.Global,
Projected = projectedLoad == 1
};
Expand Down
6 changes: 3 additions & 3 deletions Robot_Adapter/CRUD/Read/Loads/GeometricalLineLoads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private List<ILoad> ReadGeometricalLineLoads(List<string> ids = null)
{

List<ILoad> bhomLoads = new List<ILoad>();
Dictionary<string, Loadcase> bhomLoadCases = ReadLoadCase().ToDictionaryDistinctCheck(x => x.Name);
Dictionary<int, Loadcase> bhomLoadCases = GetCachedOrReadAsDictionary<int, Loadcase>();
IRobotCaseCollection loadCollection = m_RobotApplication.Project.Structure.Cases.GetAll();

for (int i = 1; i <= loadCollection.Count; i++)
Expand All @@ -55,7 +55,7 @@ private List<ILoad> ReadGeometricalLineLoads(List<string> ids = null)
if (lCase.Type == IRobotCaseType.I_CT_SIMPLE)
{
IRobotSimpleCase sCase = lCase as IRobotSimpleCase;
if (bhomLoadCases.ContainsKey(sCase.Name))
if (bhomLoadCases.ContainsKey(sCase.Number))
{
for (int j = 1; j <= sCase.Records.Count; j++)
{
Expand Down Expand Up @@ -94,7 +94,7 @@ private List<ILoad> ReadGeometricalLineLoads(List<string> ids = null)
MomentB = new Vector { X = mxb, Y = myb, Z = mzb },
Location = new Line { Start = new Point { X = xa, Y = ya, Z = za }, End = new Point { X = xb, Y = yb, Z = zb } },
Axis = local.FromRobotLoadAxis(),
Loadcase = bhomLoadCases[sCase.Name]
Loadcase = bhomLoadCases[sCase.Number]
};
bhomLoads.Add(contourLoad);
break;
Expand Down
55 changes: 22 additions & 33 deletions Robot_Adapter/CRUD/Read/Loads/Loads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public partial class RobotAdapter
private List<ILoad> ReadLoads(Type type, List<string> ids = null)
{
//Ensure previous object caches have been cleared
ClearLoadObjectCache();

List<ILoad> loads = new List<ILoad>();

Expand Down Expand Up @@ -96,7 +95,6 @@ private List<ILoad> ReadLoads(Type type, List<string> ids = null)
loads.AddRange(ReadGeometricalLineLoads(ids));

//Clean up the object cache
ClearLoadObjectCache();

return loads;
}
Expand All @@ -107,12 +105,12 @@ private List<ILoad> ReadLoads(Type type, List<string> ids = null)
[Input("convertMethod", "Method used for turning a load record into a BHoM load, setting all properties except for objects and case. For example `record => record.FromRobotAreaUDL()` for Areaload.")]
[Input("loadType","Robot loadtype enum corresponding to the type of load being pulled.")]
[Input("ids", "Not yet in use.")]
private List<ILoad> ReadObjectLoads<T>(Func<IRobotLoadRecord, IElementLoad<T>> convertMethod, IRobotLoadRecordType loadType, List<string> ids = null) where T : IBHoMObject
private List<ILoad> ReadObjectLoads<T>(Func<IRobotLoadRecord, IElementLoad<T>> convertMethod, IRobotLoadRecordType loadType, List<string> ids = null) where T : class, IBHoMObject
{
//Main method looping through all loadcases and extracting the picked up load type
List<ILoad> bhomLoads = new List<ILoad>();
Dictionary<int, T> loadObjects = ReadLoadCacheObjects<T>().ToDictionary(x => GetAdapterId<int>(x));
Dictionary<string, Loadcase> bhomLoadCases = ReadLoadCase().ToDictionaryDistinctCheck(x => x.Name);
Dictionary<int, T> loadObjects = GetCachedOrReadLoadObjects<T>();
Dictionary<int, Loadcase> bhomLoadCases = GetCachedOrReadAsDictionary<int, Loadcase>();

IRobotCaseCollection loadCollection = m_RobotApplication.Project.Structure.Cases.GetAll();

Expand All @@ -136,7 +134,7 @@ private List<ILoad> ReadObjectLoads<T>(Func<IRobotLoadRecord, IElementLoad<T>> c
continue;
}

if (bhomLoadCases.ContainsKey(sCase.Name))
if (bhomLoadCases.ContainsKey(sCase.Number))
{
for (int j = 1; j <= sCase.Records.Count; j++)
{
Expand All @@ -152,7 +150,7 @@ private List<ILoad> ReadObjectLoads<T>(Func<IRobotLoadRecord, IElementLoad<T>> c
if (load != null)
{
SetLoadGroup(load, objects.Cast<IBHoMObject>());
load.Loadcase = bhomLoadCases[sCase.Name];
load.Loadcase = bhomLoadCases[sCase.Number];
bhomLoads.Add(load);
}
else
Expand Down Expand Up @@ -217,48 +215,39 @@ private static void SetLoadGroup<T>(IElementLoad<T> load, IEnumerable<IBHoMObjec

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

private IEnumerable<T> ReadLoadCacheObjects<T>()
private Dictionary<int, T> GetCachedOrReadLoadObjects<T>() where T : class, IBHoMObject
{
//Reads elements of a particular type.

if (m_LoadObjectCache == null)
m_LoadObjectCache = new Dictionary<Type, IEnumerable<IBHoMObject>>();

Type type = typeof(T);

if (type == typeof(BHoMObject))
{
var bars = ReadLoadCacheObjects<Bar>();
var panels = ReadLoadCacheObjects<Panel>();
return bars.Cast<T>().Concat(panels.Cast<T>());
}
var bars = GetCachedOrReadAsDictionary<int, Bar>();
var panels = GetCachedOrReadAsDictionary<int, Panel>();
Dictionary<int, T> dict = new Dictionary<int, T>();

foreach (var kvp in bars)
{
dict[kvp.Key] = kvp.Value as T;
}
foreach (var kvp in panels)
{
dict[kvp.Key] = kvp.Value as T;
}

if (m_LoadObjectCache.ContainsKey(type))
return m_LoadObjectCache[type].Cast<T>();
return dict;
}

Type readType = type;
if (type == typeof(IAreaElement))
readType = typeof(Panel);
return GetCachedOrReadAsDictionary<int, Panel>().ToDictionary(x => x.Key, x => x.Value as T);

IEnumerable<IBHoMObject> readObjs = IRead(readType, null).Where(x => x != null).ToList();
m_LoadObjectCache[type] = readObjs;
return readObjs.Cast<T>();
return GetCachedOrReadAsDictionary<int, T>();
}

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

private void ClearLoadObjectCache()
{
m_LoadObjectCache = new Dictionary<Type, IEnumerable<IBHoMObject>>();
}

/***************************************************/
/**** Private Fields ****/
/***************************************************/

private Dictionary<Type, IEnumerable<IBHoMObject>> m_LoadObjectCache = new Dictionary<Type, IEnumerable<IBHoMObject>>();

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

}
Expand Down
8 changes: 6 additions & 2 deletions Robot_Adapter/CRUD/Read/Properties/Labels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using BH.oM.Structure.SurfaceProperties;
using BH.oM.Structure.SectionProperties;
using BH.oM.Structure.Offsets;
using System.Linq;

namespace BH.Adapter.Robot
{
Expand All @@ -54,7 +55,6 @@ public List<IBHoMObject> ReadLabels(IRobotLabelType robotLabelType, List<string>
if (obj != null)
objects.Add(obj);
}

}
}
else
Expand Down Expand Up @@ -114,7 +114,11 @@ private IBHoMObject ReadLabel(IRobotLabelType robotLabelType, string robotLabelN
string materialName = secData.MaterialName;
if (!bhomMaterials.TryGetValue(materialName, out sectionMaterial))
{
sectionMaterial = ReadMaterialByLabelName(materialName);
sectionMaterial = GetCachedOrRead<IMaterialFragment, string>(materialName);
if (sectionMaterial == null)
{
sectionMaterial = ReadMaterialByLabelName(materialName);
}
bhomMaterials[materialName] = sectionMaterial;
}
ISectionProperty section = secData.FromRobot(sectionMaterial, robotLabelName);
Expand Down
2 changes: 1 addition & 1 deletion Robot_Adapter/CRUD/Read/Properties/SurfaceProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public partial class RobotAdapter

private List<ISurfaceProperty> ReadSurfaceProperties(List<string> ids = null)
{
Dictionary<string, IMaterialFragment> bhomMaterials = ReadMaterials().ToDictionary(x => x.Name);
Dictionary<string, IMaterialFragment> bhomMaterials = GetCachedOrReadAsDictionary<string, IMaterialFragment>();

List<ISurfaceProperty> bhomProps = ReadLabels(IRobotLabelType.I_LT_PANEL_THICKNESS, ids, bhomMaterials).Select(x => x as ISurfaceProperty).Where(x => x != null).ToList();
bhomProps.AddRange(ReadLabels(IRobotLabelType.I_LT_CLADDING, ids, bhomMaterials).Select(x => x as ISurfaceProperty).Where(x => x != null).ToList());
Expand Down
Loading