Skip to content

Adapter methods: Serialisation to and from ladybug objects #142

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
f9be66a
moved a bunch of objects to the adapter, and made some adapter conver…
Tom-Kingstone Nov 15, 2023
3907c90
end of day commit - working on doing a deserialise method
Tom-Kingstone Nov 15, 2023
3d3f964
moved a bunch of objects to the adapter, and made some adapter conver…
Tom-Kingstone Nov 15, 2023
cae521f
end of day commit - working on doing a deserialise method
Tom-Kingstone Nov 15, 2023
ceeeae0
Merge branch 'LadybugTools_Toolkit-#136-SerialisationRefactor' of htt…
Tom-Kingstone Nov 16, 2023
c183192
finished ToBHoM methods, weird error occurring (missing Microsoft.CSh…
Tom-Kingstone Nov 16, 2023
78d8993
added push adapter action, started work on changing the output of the…
Tom-Kingstone Nov 16, 2023
460ddda
structured folder so it's easier to find a specific object, and fixed…
Tom-Kingstone Nov 16, 2023
c073fbc
trying serialisation of HourlyContinuous objects, and removed Type fr…
Tom-Kingstone Nov 16, 2023
5da6318
removed unnecessary objects, finished writing the From and To methods…
Tom-Kingstone Nov 17, 2023
9ad8f5e
fixed FromHourlyContinuousCollection to use the new IHourly interface.
Tom-Kingstone Nov 20, 2023
49e68ee
can store these values without quotes
Tom-Kingstone Nov 20, 2023
fee7049
It does need the quotes after all :P
Tom-Kingstone Nov 21, 2023
0f795b1
fixed issue converting hourlystrings to json not containing start and…
Tom-Kingstone Nov 21, 2023
180aec5
moving oM files into categories
Tom-Kingstone Nov 21, 2023
ea04331
moved some validation around
Tom-Kingstone Nov 21, 2023
e421ace
small changes to Read and Create
Tom-Kingstone Nov 21, 2023
7733c5e
fixing some problems that occur python-side
Tom-Kingstone Nov 22, 2023
d256e40
fixed some more bugs causing python errors, and changed some of the n…
Tom-Kingstone Nov 22, 2023
9ad288b
added a type to EnergyMaterial serialisation, and fixed some styling
Tom-Kingstone Nov 22, 2023
f3bd565
removed reference to System.Extensions.Web.dll
Tom-Kingstone Nov 22, 2023
72fab55
removed unnecessary and unused interface
Tom-Kingstone Nov 22, 2023
5c600b2
changed some object properties to remove unnecessary properties (and …
Tom-Kingstone Nov 22, 2023
8bb08b9
removed reference to deleted interface
Tom-Kingstone Nov 22, 2023
19de837
added descriptions for some objects and attributes
Tom-Kingstone Nov 22, 2023
fb59feb
Resolve copyright compliance
BHoMBot Nov 22, 2023
246fbde
Update project compliance
BHoMBot Nov 22, 2023
d520eac
Apply suggestions from code review
Tom-Kingstone Nov 22, 2023
23edbc3
added multiple changes from suggestions
Tom-Kingstone Nov 22, 2023
54861b1
some more changes
Tom-Kingstone Nov 22, 2023
eb18f8f
Merge branch 'LadybugTools_Toolkit-#136-SerialisationRefactor' of htt…
Tom-Kingstone Nov 22, 2023
fe37cb0
moving out of python land
Tom-Kingstone Nov 22, 2023
b5cd780
changed convert method to reflect discussion
Tom-Kingstone Nov 22, 2023
ed8beee
begone, objects!
Tom-Kingstone Nov 22, 2023
f825f4f
removed an empty line
Tom-Kingstone Nov 22, 2023
94b1f81
fixed compile
Tom-Kingstone Nov 22, 2023
317ec0f
added error handling when casting volatile data
Tom-Kingstone Nov 22, 2023
299abf4
fixed spelling mistake :P
Tom-Kingstone Nov 22, 2023
60e88c2
Apply suggestions from code review
Tom-Kingstone Nov 23, 2023
e37d53a
try/catch for EnergyMaterial, and added check for if the actionConfig…
Tom-Kingstone Nov 23, 2023
f0417eb
playing catch with EnergyMaterialVegetation, and also catch for 'type…
Tom-Kingstone Nov 23, 2023
6bcf936
I caught the ball from AnalysisPeriod
Tom-Kingstone Nov 23, 2023
a521e5c
and now DataType has a ball to catch
Tom-Kingstone Nov 23, 2023
0cfad5e
the Header did a header! then it caught the ball
Tom-Kingstone Nov 23, 2023
445f5bf
we found the Location that we're playing catch
Tom-Kingstone Nov 23, 2023
72b92f9
HCC and EPW are now in the game of catch.
Tom-Kingstone Nov 23, 2023
ea6644d
added returning BHoM objects if the json serialised into one.
Tom-Kingstone Nov 23, 2023
603a25f
added ability to push lists of objects and pull JSON arrays
Tom-Kingstone Nov 23, 2023
82ce9a8
fixed issue when deserialising objects that are already BHoM serialis…
Tom-Kingstone Nov 23, 2023
fb47bcd
removed an empty line that @FraserGreenroyd would have removed anyway :P
Tom-Kingstone Nov 23, 2023
350813d
Update LadybugTools_Adapter/Convert/Constructions/EnergyMaterialVeget…
Nov 23, 2023
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
72 changes: 72 additions & 0 deletions LadybugTools_Adapter/AdapterActions/Pull.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.Engine.Adapter;
using BH.oM.Adapter;
using BH.oM.Base;
using BH.oM.Data.Requests;
using BH.oM.LadybugTools;
using System;
using System.Collections.Generic;
using System.Text;

namespace BH.Adapter.LadybugTools
{
public partial class LadybugToolsAdapter : BHoMAdapter
{
public override IEnumerable<object> Pull(IRequest request, PullType pullType = PullType.AdapterDefault, ActionConfig actionConfig = null)
{
if (actionConfig == null)
{
BH.Engine.Base.Compute.RecordError("Please provide a valid LadybugConfig ActionConfig.");
return new List<IBHoMObject>();
}

LadybugConfig config = actionConfig as LadybugConfig;
if (config == null)
{
BH.Engine.Base.Compute.RecordError($"The type of actionConfig provided: {actionConfig.GetType().FullName} is not valid for this adapter. Please provide a valid LadybugConfig actionConfig.");
return new List<IBHoMObject>();
}

if (config.JsonFile == null)
{
BH.Engine.Base.Compute.RecordError("Please provide a valid JsonFile FileSettings object.");
return new List<IBHoMObject>();
}

if (!System.IO.File.Exists(config.JsonFile.GetFullFileName()))
{
BH.Engine.Base.Compute.RecordError($"The file at {config.JsonFile.GetFullFileName()} does not exist to pull from.");
return new List<IBHoMObject>();
}

if (request != null)
{
FilterRequest filterRequest = request as FilterRequest;
return Read(filterRequest.Type, actionConfig: config);
}
else
return Read(null, config);
}
}
}
70 changes: 70 additions & 0 deletions LadybugTools_Adapter/AdapterActions/Push.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.Engine.Adapter;
using BH.Engine.LadybugTools;
using BH.oM.Adapter;
using BH.oM.Base;
using BH.oM.Data.Requests;
using BH.oM.LadybugTools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BH.Adapter.LadybugTools
{
public partial class LadybugToolsAdapter : BHoMAdapter
{
public override List<object> Push(IEnumerable<object> objects, string tag = "", PushType pushType = PushType.AdapterDefault, ActionConfig actionConfig = null)
{
if (actionConfig == null)
{
BH.Engine.Base.Compute.RecordError("Please provide a valid LadybugConfig ActionConfig.");
return new List<object>();
}

LadybugConfig config = actionConfig as LadybugConfig;
if (config == null)
{
BH.Engine.Base.Compute.RecordError("Please input a valid LadybugConfig.");
return new List<object>();
}

if (objects.Count() == 0)
{
BH.Engine.Base.Compute.RecordError("Please input a valid LadybugTools object.");
return new List<object>();
}

List<ILadybugTools> lbtObjects = objects.Where(x => typeof(ILadybugTools).IsAssignableFrom(x.GetType())).Cast<ILadybugTools>().ToList();

if (lbtObjects.Count() < objects.Count())
{
BH.Engine.Base.Compute.RecordWarning("The LadybugTools Toolkit adapter does not support converting non-ILadybugTools objects to json, skipping all objects that are not an ILadybugTools");
}

CreateLadybug(lbtObjects, config);
return objects.Where(x => typeof(ILadybugTools).IsAssignableFrom(x.GetType())).ToList();
}
}
}
50 changes: 50 additions & 0 deletions LadybugTools_Adapter/CRUD/CreateLadybug.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.Engine.Adapter;
using BH.oM.Adapter;
using BH.oM.Base.Debugging;
using BH.oM.LadybugTools;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace BH.Adapter.LadybugTools
{
public partial class LadybugToolsAdapter : BHoMAdapter
{
public static void CreateLadybug(List<ILadybugTools> objects, LadybugConfig config = null)
{
List<string> jsonObjects = new List<string>();

foreach (ILadybugTools lbtObject in objects)
{
jsonObjects.Add(lbtObject.FromBHoM());
}

string json = "[" + string.Join(", ", jsonObjects) + "]";
File.WriteAllText(config.JsonFile.GetFullFileName(), json);
}
}
}
43 changes: 43 additions & 0 deletions LadybugTools_Adapter/CRUD/Read.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.Engine.Adapter;
using BH.Engine.Serialiser;
using BH.oM.Adapter;
using BH.oM.Base;
using BH.oM.LadybugTools;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace BH.Adapter.LadybugTools
{
public partial class LadybugToolsAdapter : BHoMAdapter
{
protected override IEnumerable<IBHoMObject> IRead(Type type, IList indices = null, ActionConfig actionConfig = null)
{
LadybugConfig config = actionConfig as LadybugConfig;
return config.JsonFile.ToBHoM();
}
}
}
104 changes: 104 additions & 0 deletions LadybugTools_Adapter/Convert/Collections/EPW.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* 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.Engine.Serialiser;
using BH.oM.Base;
using BH.oM.LadybugTools;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace BH.Adapter.LadybugTools
{
public static partial class Convert
{
public static BH.oM.LadybugTools.EPW ToEPW(Dictionary<string, object> oldObject)
{
Location location = new Location();
Dictionary<string, object> metaData = new Dictionary<string, object>();

try
{
if (oldObject["location"].GetType() == typeof(CustomObject))
oldObject["location"] = (oldObject["location"] as CustomObject).CustomData;
location = ToLocation(oldObject["location"] as Dictionary<string, object>);
}
catch (Exception ex)
{
BH.Engine.Base.Compute.RecordError($"An error occurred when reading the Location of the EPW. returning a default Location.\n The error: {ex}");
}

try
{
if (oldObject["metadata"].GetType() == typeof(CustomObject))
oldObject["metadata"] = (oldObject["metadata"] as CustomObject).CustomData;
metaData = (Dictionary<string, object>)oldObject["metadata"];
}
catch (Exception ex)
{
BH.Engine.Base.Compute.RecordError($"An error occurred when reading the meta data of the EPW. returning an empty metadata object.\n The error: {ex}");
}

List<BH.oM.LadybugTools.HourlyContinuousCollection> collections = new List<BH.oM.LadybugTools.HourlyContinuousCollection>();

if (oldObject.ContainsKey("data_collections"))
{
foreach (var collection in oldObject["data_collections"] as List<object>)
{
if (collection.GetType() == typeof(CustomObject))
collections.Add(ToHourlyContinuousCollection((collection as CustomObject).CustomData));
else
collections.Add(ToHourlyContinuousCollection(collection as Dictionary<string, object>));
}
}
else
{
BH.Engine.Base.Compute.RecordError($"Could not find any data collections for this EPW object. Returning an empty list.");
}

return new EPW()
{
Location = location,
DataCollections = collections,
Metadata = metaData
};

}

public static string FromEPW(BH.oM.LadybugTools.EPW epw)
{
string type = "EPW";
string location = FromLocation(epw.Location).ToJson();
string dataCollections = string.Join(", ", epw.DataCollections.Select(x => FromHourlyContinuousCollection(x)));
string metadata = epw.Metadata.ToJson();

if (metadata.Length == 0)
metadata = "{}";

string json = @"{ ""type"": """ + type + @""", ""location"": " + location + @", ""data_collections"": [ " + dataCollections + @"], ""metadata"": " + metadata + "}";

return json;
}
}
}
Loading