generated from BHoM/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 2
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
FraserGreenroyd
merged 51 commits into
develop
from
LadybugTools_Toolkit-#136-SerialisationRefactor
Nov 23, 2023
Merged
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 3907c90
end of day commit - working on doing a deserialise method
Tom-Kingstone 3d3f964
moved a bunch of objects to the adapter, and made some adapter conver…
Tom-Kingstone cae521f
end of day commit - working on doing a deserialise method
Tom-Kingstone ceeeae0
Merge branch 'LadybugTools_Toolkit-#136-SerialisationRefactor' of htt…
Tom-Kingstone c183192
finished ToBHoM methods, weird error occurring (missing Microsoft.CSh…
Tom-Kingstone 78d8993
added push adapter action, started work on changing the output of the…
Tom-Kingstone 460ddda
structured folder so it's easier to find a specific object, and fixed…
Tom-Kingstone c073fbc
trying serialisation of HourlyContinuous objects, and removed Type fr…
Tom-Kingstone 5da6318
removed unnecessary objects, finished writing the From and To methods…
Tom-Kingstone 9ad8f5e
fixed FromHourlyContinuousCollection to use the new IHourly interface.
Tom-Kingstone 49e68ee
can store these values without quotes
Tom-Kingstone fee7049
It does need the quotes after all :P
Tom-Kingstone 0f795b1
fixed issue converting hourlystrings to json not containing start and…
Tom-Kingstone 180aec5
moving oM files into categories
Tom-Kingstone ea04331
moved some validation around
Tom-Kingstone e421ace
small changes to Read and Create
Tom-Kingstone 7733c5e
fixing some problems that occur python-side
Tom-Kingstone d256e40
fixed some more bugs causing python errors, and changed some of the n…
Tom-Kingstone 9ad288b
added a type to EnergyMaterial serialisation, and fixed some styling
Tom-Kingstone f3bd565
removed reference to System.Extensions.Web.dll
Tom-Kingstone 72fab55
removed unnecessary and unused interface
Tom-Kingstone 5c600b2
changed some object properties to remove unnecessary properties (and …
Tom-Kingstone 8bb08b9
removed reference to deleted interface
Tom-Kingstone 19de837
added descriptions for some objects and attributes
Tom-Kingstone fb59feb
Resolve copyright compliance
BHoMBot 246fbde
Update project compliance
BHoMBot d520eac
Apply suggestions from code review
Tom-Kingstone 23edbc3
added multiple changes from suggestions
Tom-Kingstone 54861b1
some more changes
Tom-Kingstone eb18f8f
Merge branch 'LadybugTools_Toolkit-#136-SerialisationRefactor' of htt…
Tom-Kingstone fe37cb0
moving out of python land
Tom-Kingstone b5cd780
changed convert method to reflect discussion
Tom-Kingstone ed8beee
begone, objects!
Tom-Kingstone f825f4f
removed an empty line
Tom-Kingstone 94b1f81
fixed compile
Tom-Kingstone 317ec0f
added error handling when casting volatile data
Tom-Kingstone 299abf4
fixed spelling mistake :P
Tom-Kingstone 60e88c2
Apply suggestions from code review
Tom-Kingstone e37d53a
try/catch for EnergyMaterial, and added check for if the actionConfig…
Tom-Kingstone f0417eb
playing catch with EnergyMaterialVegetation, and also catch for 'type…
Tom-Kingstone 6bcf936
I caught the ball from AnalysisPeriod
Tom-Kingstone a521e5c
and now DataType has a ball to catch
Tom-Kingstone 0cfad5e
the Header did a header! then it caught the ball
Tom-Kingstone 445f5bf
we found the Location that we're playing catch
Tom-Kingstone 72b92f9
HCC and EPW are now in the game of catch.
Tom-Kingstone ea6644d
added returning BHoM objects if the json serialised into one.
Tom-Kingstone 603a25f
added ability to push lists of objects and pull JSON arrays
Tom-Kingstone 82ce9a8
fixed issue when deserialising objects that are already BHoM serialis…
Tom-Kingstone fb47bcd
removed an empty line that @FraserGreenroyd would have removed anyway :P
Tom-Kingstone 350813d
Update LadybugTools_Adapter/Convert/Constructions/EnergyMaterialVeget…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
Tom-Kingstone marked this conversation as resolved.
Show resolved
Hide resolved
|
||
CreateLadybug(lbtObjects, config); | ||
return objects.Where(x => typeof(ILadybugTools).IsAssignableFrom(x.GetType())).ToList(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
|
||
Tom-Kingstone marked this conversation as resolved.
Show resolved
Hide resolved
|
||
foreach (ILadybugTools lbtObject in objects) | ||
{ | ||
jsonObjects.Add(lbtObject.FromBHoM()); | ||
} | ||
|
||
string json = "[" + string.Join(", ", jsonObjects) + "]"; | ||
File.WriteAllText(config.JsonFile.GetFullFileName(), json); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
Tom-Kingstone marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.