Skip to content

Commit

Permalink
split geenric and regular datatypes
Browse files Browse the repository at this point in the history
  • Loading branch information
tg359 committed Nov 24, 2023
1 parent c8e1875 commit 0c2acb7
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 63 deletions.
2 changes: 0 additions & 2 deletions LadybugTools_Adapter/Convert/Collections/EPW.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
using BH.oM.LadybugTools;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace BH.Adapter.LadybugTools
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.Engine.Base;
using BH.Engine.Serialiser;
using BH.oM.Base;
using BH.oM.LadybugTools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BH.Adapter.LadybugTools
{
Expand Down
28 changes: 10 additions & 18 deletions LadybugTools_Adapter/Convert/FromBHoM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,19 @@
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.oM.LadybugTools;
using System;
using BH.Engine.Serialiser;
using System.Collections.Generic;
using System.Text;
using BH.oM.Base;
using System.IO;
using BH.oM.Adapter;
using BH.Engine.Adapter;
using BH.oM.Base.Debugging;

namespace BH.Adapter.LadybugTools
{
public static partial class Convert
{
public static string FromBHoM(this ILadybugTools input)
public static string FromBHoM(this BH.oM.LadybugTools.ILadybugTools input)
{
return ICustomify(input);
}

public static string ICustomify(this ILadybugTools lbtObject)
public static string ICustomify(this BH.oM.LadybugTools.ILadybugTools lbtObject)
{
if (lbtObject == null)
{
Expand All @@ -50,22 +42,22 @@ public static string ICustomify(this ILadybugTools lbtObject)
return Jsonify(lbtObject as dynamic);
}

private static string Jsonify(this oM.LadybugTools.AnalysisPeriod analysisPeriod)
private static string Jsonify(this BH.oM.LadybugTools.AnalysisPeriod analysisPeriod)
{
return FromAnalysisPeriod(analysisPeriod).ToJson();
}

private static string Jsonify(this oM.LadybugTools.DataType dataType)
private static string Jsonify(this BH.oM.LadybugTools.IDataType dataType)
{
return FromDataType(dataType).ToJson();
}

private static string Jsonify(this oM.LadybugTools.EnergyMaterial energyMaterial)
private static string Jsonify(this BH.oM.LadybugTools.EnergyMaterial energyMaterial)
{
return FromEnergyMaterial(energyMaterial).ToJson();
}

private static string Jsonify(this oM.LadybugTools.EnergyMaterialVegetation energyMaterial)
private static string Jsonify(this BH.oM.LadybugTools.EnergyMaterialVegetation energyMaterial)
{
return FromEnergyMaterialVegetation(energyMaterial).ToJson();
}
Expand All @@ -75,22 +67,22 @@ private static string Jsonify(this oM.LadybugTools.EPW epw)
return FromEPW(epw);
}

private static string Jsonify(this oM.LadybugTools.Header header)
private static string Jsonify(this BH.oM.LadybugTools.Header header)
{
return FromHeader(header).ToJson();
}

private static string Jsonify(this oM.LadybugTools.HourlyContinuousCollection collection)
private static string Jsonify(this BH.oM.LadybugTools.HourlyContinuousCollection collection)
{
return FromHourlyContinuousCollection(collection);
}

private static string Jsonify(this oM.LadybugTools.Location location)
private static string Jsonify(this BH.oM.LadybugTools.Location location)
{
return FromLocation(location).ToJson();
}

private static Dictionary<string, object> Jsonify(this ILadybugTools obj)
private static Dictionary<string, object> Jsonify(this BH.oM.LadybugTools.ILadybugTools obj)
{
BH.Engine.Base.Compute.RecordError($"The type: {obj.GetType()} is not convertible to ladybug serialisable json yet.");
return null;
Expand Down
3 changes: 0 additions & 3 deletions LadybugTools_Adapter/Convert/MetaData/AnalysisPeriod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.oM.Base;
using BH.oM.LadybugTools;
using System;
using System.Collections.Generic;
using System.Text;

namespace BH.Adapter.LadybugTools
{
Expand Down
67 changes: 41 additions & 26 deletions LadybugTools_Adapter/Convert/MetaData/DataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,28 @@
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.oM.Base;
using BH.oM.LadybugTools;
using System;
using System.Collections.Generic;
using System.Text;

namespace BH.Adapter.LadybugTools
{
public static partial class Convert
{
public static BH.oM.LadybugTools.DataType ToDataType(Dictionary<string, object> oldObject)
public static BH.oM.LadybugTools.IDataType ToDataType(Dictionary<string, object> oldObject)
{
string baseUnit;
string dataType = "GenericType";
string name = "";
string dataType = "";
string unit = "";

//base_unit only occurs when Data_Type is "GenericType".
if (oldObject.ContainsKey("base_unit"))
baseUnit = (string)oldObject["base_unit"];
else
baseUnit = "";
try
{
name = (string)oldObject["name"];
}
catch (Exception ex)
{
BH.Engine.Base.Compute.RecordError($"An error occurred when reading the name of the DataType. returning name as default (\"\").\n The error: {ex}");
}

try
{
Expand All @@ -54,32 +55,46 @@ public static BH.oM.LadybugTools.DataType ToDataType(Dictionary<string, object>

try
{
name = (string)oldObject["name"];
unit = (string)oldObject["unit"];
}
catch (Exception ex)
catch (Exception _)
{
BH.Engine.Base.Compute.RecordError($"An error occurred when reading the name of the DataType. returning name as default (\"\").\n The error: {ex}");
try
{
unit = (string)oldObject["base_unit"];
}
catch (Exception ex)
{
//This DataType object won't work properly in ladybug python when serialised again as it requires either a base_unit or unit.
BH.Engine.Base.Compute.RecordError($"An error occurred when reading the unit of the DataType. returning unit as default ({unit}).\n The error: {ex}");
}
}

return new oM.LadybugTools.DataType()
if (dataType == "GenericType")
{
return new oM.LadybugTools.GenericDataType() { BaseUnit = unit, Data_Type = dataType, Name = name };
}
else
{
Data_Type = dataType,
Name = name,
BaseUnit = baseUnit
};
return new oM.LadybugTools.DataType() { Unit = unit, Data_Type = dataType, Name = name };
}
BH.Engine.Base.Compute.RecordError($"The data type being converted is borked.");
}

public static Dictionary<string, object> FromDataType(BH.oM.LadybugTools.DataType dataType)
public static Dictionary<string, object> FromDataType(IDataType dataType)
{
Dictionary<string, object> returnDict = new Dictionary<string, object>
Dictionary<string, object> returnDict = new Dictionary<string, object>();
if (dataType.GetType().GetProperty("BaseUnit") != null)
{
{ "type", "GenericDataType" },
{ "name", dataType.Name },
{ "data_type", dataType.Data_Type }
};
returnDict.Add("base_unit", ((GenericDataType)dataType).BaseUnit);
}
else
{
returnDict.Add("unit", ((DataType)dataType).Unit);
}

if (dataType.BaseUnit != "")
returnDict.Add("base_unit", dataType.BaseUnit);
returnDict.Add("name", ((DataType)dataType).Name);
returnDict.Add("data_type", dataType.Data_Type);

return returnDict;
}
Expand Down
2 changes: 1 addition & 1 deletion LadybugTools_Adapter/Convert/MetaData/Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static partial class Convert
public static BH.oM.LadybugTools.Header ToHeader(Dictionary<string, object> oldObject)
{
string unit = "";
DataType dataType = new DataType();
IDataType dataType = null;
AnalysisPeriod analysisPeriod = new AnalysisPeriod();
Dictionary<string, object> metaData = new Dictionary<string, object>();

Expand Down
2 changes: 0 additions & 2 deletions LadybugTools_Adapter/Convert/MetaData/Location.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.oM.LadybugTools;
using System;
using System.Collections.Generic;
using System.Text;

namespace BH.Adapter.LadybugTools
{
Expand Down
3 changes: 3 additions & 0 deletions LadybugTools_Adapter/Convert/ToBHoM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public static List<IBHoMObject> ToBHoM(this FileSettings jsonFile)
case "GenericDataType":
returnObjects.Add(ToDataType(lbtObject));
break;
case "DataType":
returnObjects.Add(ToDataType(lbtObject));
break;
case "EnergyMaterial":
returnObjects.Add(ToEnergyMaterial(lbtObject));
break;
Expand Down
10 changes: 3 additions & 7 deletions LadybugTools_oM/MetaData/DataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,12 @@

namespace BH.oM.LadybugTools
{
public class DataType : BHoMObject, ILadybugTools
public class DataType : BHoMObject, IDataType
{

[Description("The name of this data type.")]
public override string Name { get; set; } = string.Empty;

[Description("The data type of this data type.")]
public virtual string Data_Type { get; set; } = string.Empty;

[Description(@"The base type of this data type. This is used if Data_Type is set to ""GenericDataType"".")]
public virtual string BaseUnit { get; set; } = string.Empty;
[Description(@"The unit of this data type.")]
public virtual string Unit { get; set; } = string.Empty;
}
}
37 changes: 37 additions & 0 deletions LadybugTools_oM/MetaData/GenericDataType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.ComponentModel;

namespace BH.oM.LadybugTools
{
public class GenericDataType : BHoMObject, IDataType
{
[Description("The data type of this data type.")]
public virtual string Data_Type { get; set; } = string.Empty;

[Description("The base unit of this data type.")]
public virtual string BaseUnit { get; set; } = string.Empty;
}
}
4 changes: 2 additions & 2 deletions LadybugTools_oM/MetaData/Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ namespace BH.oM.LadybugTools
public class Header : BHoMObject, ILadybugTools
{
[Description("The data type the data associated with this header object represents.")]
public virtual DataType DataType { get; set; } = new DataType();
public virtual IDataType DataType { get; set; }

[Description("The unit for this header object.")]
[Description("The unit for this header object. Should be the same, or convertible from the unit given for the DataType.")]
public virtual string Unit { get; set; } = string.Empty;

[Description("The analysis period associated with this header object.")]
Expand Down
30 changes: 30 additions & 0 deletions LadybugTools_oM/MetaData/IDataType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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>.
*/


namespace BH.oM.LadybugTools
{
public interface IDataType : ILadybugTools
{
string Data_Type { get; set; }
}
}

0 comments on commit 0c2acb7

Please sign in to comment.