Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
63 changes: 7 additions & 56 deletions CarbonQueryDatabase_Adapter/CRUD/Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ protected override IEnumerable<IBHoMObject> IRead(Type type, IList ids, ActionCo
{
dynamic elems = null;
CQDConfig config = null;

if (actionConfig is CQDConfig)
config = actionConfig as CQDConfig;
//Choose what to pull out depending on the type.

if (type == typeof(EnvironmentalProductDeclaration))
elems = ReadEnvironmentalProductDeclaration(ids as dynamic, config);
else if (type == typeof(SectorEnvironmentalProductDeclaration))
elems = ReadSectorEnvironmentalProductDeclaration(ids as dynamic, config);

return elems;
}
/***************************************************/
Expand All @@ -76,9 +76,11 @@ private List<EnvironmentalProductDeclaration> ReadEnvironmentalProductDeclaratio
{
epdGetRequest = BH.Engine.Adapters.CarbonQueryDatabase.Create.CarbonQueryDatabaseRequest("epds/" + id, m_bearerToken, config);
}

string reqString = epdGetRequest.ToUrlString();
string response = BH.Engine.Adapters.HTTP.Compute.MakeRequest(epdGetRequest);
List<object> responseObjs = null;

if (response == null)
{
BH.Engine.Reflection.Compute.RecordWarning("No response received, check bearer token and connection.");
Expand All @@ -99,6 +101,7 @@ private List<EnvironmentalProductDeclaration> ReadEnvironmentalProductDeclaratio
BH.Engine.Reflection.Compute.RecordWarning("Response is not a valid JSON. How'd that happen?");
return null;
}

//Convert nested customObject from serialization to list of epdData objects
List<EnvironmentalProductDeclaration> epdDataFromRequest = new List<EnvironmentalProductDeclaration>();
object epdObjects = responseObjs[0];
Expand All @@ -107,65 +110,13 @@ private List<EnvironmentalProductDeclaration> ReadEnvironmentalProductDeclaratio
{
foreach (CustomObject co in objList)
{
EnvironmentalProductDeclaration epdData = Adapter.CarbonQueryDatabase.Convert.ToEnvironmentalProductDeclaration(co);
EnvironmentalProductDeclaration epdData = Adapter.CarbonQueryDatabase.Convert.ToEnvironmentalProductDeclaration(co, config);
epdDataFromRequest.Add(epdData);
}
}
return epdDataFromRequest;
}
/***************************************************/
private List<SectorEnvironmentalProductDeclaration> ReadSectorEnvironmentalProductDeclaration(List<string> ids = null, CQDConfig config = null)
{
int count = config.Count;
string name = config.NameLike;
string id = config.Id;

//Create GET Request
GetRequest epdGetRequest;
if (id == null)
{
epdGetRequest = BH.Engine.Adapters.CarbonQueryDatabase.Create.CarbonQueryDatabaseRequest("industry_epds", m_bearerToken, config);
}
else
{
epdGetRequest = BH.Engine.Adapters.CarbonQueryDatabase.Create.CarbonQueryDatabaseRequest("industry_epds" + id, m_bearerToken, config);
}
string response = BH.Engine.Adapters.HTTP.Compute.MakeRequest(epdGetRequest);
List<object> responseObjs = null;
if (response == null)
{
BH.Engine.Reflection.Compute.RecordWarning("No response received, check bearer token and connection.");
return null;
}
//Check if the response is a valid json
else if (response.StartsWith("{"))
{
response = "[" + response + "]";
responseObjs = new List<object>() { Engine.Serialiser.Convert.FromJsonArray(response) };
}
else if (response.StartsWith("["))
{
responseObjs = new List<object>() { Engine.Serialiser.Convert.FromJsonArray(response) };
}
else
{
BH.Engine.Reflection.Compute.RecordWarning("Response is not a valid JSON. How'd that happen?");
return null;
}
//Convert nested customObject from serialization to list of epdData objects
List<SectorEnvironmentalProductDeclaration> epdDataFromRequest = new List<SectorEnvironmentalProductDeclaration>();
object epdObjects = responseObjs[0];
IEnumerable objList = epdObjects as IEnumerable;
if (objList != null)
{
foreach (CustomObject co in objList)
{
SectorEnvironmentalProductDeclaration epdData = Adapter.CarbonQueryDatabase.Convert.ToSectorEnvironmentalProductDeclaration(co);
epdDataFromRequest.Add(epdData);
}
}
return epdDataFromRequest;
}
/***************************************************/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
<HintPath>C:\ProgramData\BHoM\Assemblies\BHoM_Adapter.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="BHoM_Engine">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\ProgramData\BHoM\Assemblies\BHoM_Engine.dll</HintPath>
</Reference>
<Reference Include="Data_oM">
<HintPath>C:\ProgramData\BHoM\Assemblies\Data_oM.dll</HintPath>
<Private>False</Private>
Expand All @@ -60,6 +64,10 @@
<HintPath>C:\ProgramData\BHoM\Assemblies\LifeCycleAssessment_oM.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Physical_oM">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\ProgramData\BHoM\Assemblies\Physical_oM.dll</HintPath>
</Reference>
<Reference Include="Reflection_Engine">
<HintPath>C:\ProgramData\BHoM\Assemblies\Reflection_Engine.dll</HintPath>
<Private>False</Private>
Expand Down
122 changes: 60 additions & 62 deletions CarbonQueryDatabase_Adapter/Convert/ToBHoM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,19 @@
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.ComponentModel;
using BH.oM.Reflection.Attributes;
using BH.Engine.Reflection;
using BH.Engine.Units;
using BH.oM.Base;
using BH.oM.LifeCycleAssessment;
using BH.oM.LifeCycleAssessment.Fragments;
using BH.oM.LifeCycleAssessment.MaterialFragments;
using BH.Engine.Reflection;
using System;
using System.Collections;
using BH.Engine.Adapters.CarbonQueryDatabase;
using BH.Engine.Units;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

using BH.oM.Adapters.CarbonQueryDatabase;

namespace BH.Adapter.CarbonQueryDatabase
{
Expand All @@ -46,7 +42,7 @@ public static partial class Convert
/**** Public Methods ****/
/***************************************************/

public static EnvironmentalProductDeclaration ToEnvironmentalProductDeclaration(this CustomObject obj)
public static EnvironmentalProductDeclaration ToEnvironmentalProductDeclaration(this CustomObject obj, CQDConfig config)
{
int result = 0;

Expand All @@ -66,81 +62,83 @@ public static EnvironmentalProductDeclaration ToEnvironmentalProductDeclaration(
double density = ConvertToSI(densityVal, densityUnits);
string gwp = obj.PropertyValue("gwp")?.ToString() ?? "";
double gwpVal = (gwp == "") ? double.NaN : System.Convert.ToDouble(gwp.Substring(0, gwp.IndexOf(" "))) * epdUnitMult;
int lifespan = (int)(obj.PropertyValue("reference_service_life") ?? 0);
int referenceYear = int.TryParse(obj.PropertyValue("date_of_issue")?.ToString() ?? "", out result) ? result : 0;

EnvironmentalProductDeclaration epd = new EnvironmentalProductDeclaration
{
QuantityType = QuantityType.Volume,
Id = obj.PropertyValue("id")?.ToString() ?? "",
Name = obj.PropertyValue("name")?.ToString() ?? "",
Manufacturer = obj.PropertyValue("manufacturer.name")?.ToString() ?? "",
Plant = obj.PropertyValue("plant.name")?.ToString() ?? "",
PostalCode = int.TryParse(obj.PropertyValue("plant.postal_code")?.ToString() ?? "", out result) ? result : 0,
Density = density,
GlobalWarmingPotential = gwpVal,
BiogenicCarbon = obj.PropertyValue("biogenic_embodied_carbon") != null ? System.Convert.ToDouble(obj.PropertyValue("biogenic_embodied_carbon_z")) * epdUnitMult : double.NaN,
Description = obj.PropertyValue("description")?.ToString() ?? "",
IndustryStandards = standards != null ? standards.ToList() : new List<string>(),
};

return epd;
}

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

public static SectorEnvironmentalProductDeclaration ToSectorEnvironmentalProductDeclaration(this CustomObject obj)
{
List<string> publisherNames = new List<string>();
string publisherNames = "";
if (obj.PropertyValue("publishers") != null)
{
IEnumerable pubs = (IEnumerable)obj.PropertyValue("publishers");
foreach (CustomObject pub in pubs)
{
publisherNames.Add(pub.PropertyValue("name").ToString());
publisherNames += pub.ToString() + " ";
}
publisherNames = publisherNames.Trim();
}

List<string> jurisdictionNames = new List<string>();
string jurisdictionNames = "";
if (obj.PropertyValue("geography") != null)
{
IEnumerable jurs = (IEnumerable)obj.PropertyValue("geography.country_codes");
foreach (object jur in jurs)
{
jurisdictionNames.Add(jur.ToString());
jurisdictionNames += jur.ToString() + " ";
}
jurisdictionNames = jurisdictionNames.Trim();
}

string declaredUnit = obj.PropertyValue("declared_unit")?.ToString() ?? "";
string epdUnit = GetUnitsFromString(declaredUnit);
double declaredVal = GetValFromString(declaredUnit);
QuantityType quantityType = GetQuantityTypeFromString(epdUnit);
double epdUnitMult = ConvertToSI(1/declaredVal, epdUnit);

string densityString = obj.PropertyValue("density_max")?.ToString() ?? "";
double densityVal = GetValFromString(densityString);
string densityUnits = GetUnitsFromString(densityString);
double density = ConvertToSI(densityVal, densityUnits);
string gwp = obj.PropertyValue("gwp")?.ToString() ?? "";
double gwpVal = (gwp == "") ? double.NaN : System.Convert.ToDouble(gwp.Substring(0, gwp.IndexOf(" "))) * epdUnitMult;
EnvironmentalMetric metric = new EnvironmentalMetric
{
Field = EnvironmentalProductDeclarationField.GlobalWarmingPotential,
Phases = new List<LifeCycleAssessmentPhases>() { LifeCycleAssessmentPhases.A1, LifeCycleAssessmentPhases.A2, LifeCycleAssessmentPhases.A3},
Quantity = gwpVal,
};

SectorEnvironmentalProductDeclaration epd = new SectorEnvironmentalProductDeclaration
AdditionalEPDData data = new AdditionalEPDData
{
QuantityType = quantityType,
Id = obj.PropertyValue("id")?.ToString() ?? "",
Name = obj.PropertyValue("name")?.ToString() ?? "",
Density = density,
GlobalWarmingPotential = gwpVal,
BiogenicCarbon = obj.PropertyValue("biogenic_embodied_carbon") != null ? System.Convert.ToDouble(obj.PropertyValue("biogenic_embodied_carbon_z")) * epdUnitMult : double.NaN,
Description = obj.PropertyValue("description")?.ToString() ?? "",
EndOfLifeTreatment = "",
Id = obj.PropertyValue("id")?.ToString() ?? "",
IndustryStandards = standards != null ? standards.ToList() : new List<string>(),
Jurisdiction = jurisdictionNames,
LifeSpan = lifespan,
Manufacturer = obj.PropertyValue("manufacturer.name")?.ToString() ?? "",
PlantName = obj.PropertyValue("plant.name")?.ToString() ?? "",
PostalCode = int.TryParse(obj.PropertyValue("plant.postal_code")?.ToString() ?? "", out result) ? result : 0,
Publisher = publisherNames,
ReferenceYear = referenceYear,
};

return epd;
}
EPDDensity densityFragment = new EPDDensity
{
Density = density,
};

/***************************************************/
EnvironmentalProductDeclaration epd = new EnvironmentalProductDeclaration
{
Type = config.Type,
EnvironmentalMetric = new List<EnvironmentalMetric> { metric },
QuantityType = quantityType,
QuantityTypeValue = 1,
Name = obj.PropertyValue("name")?.ToString() ?? "",
};

// Add Additional Data Fragment
EnvironmentalProductDeclaration epdData = (EnvironmentalProductDeclaration)Engine.Base.Modify.AddFragment(epd, data);

// Add Density Fragment
if (density != 0)
{
EnvironmentalProductDeclaration epdDataDensity = (EnvironmentalProductDeclaration)Engine.Base.Modify.AddFragment(epdData, densityFragment);
return epdDataDensity;
}
else
{
return epdData;
}
}

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

public static QuantityType GetQuantityTypeFromString(string unitFrom)
{
Expand Down
4 changes: 4 additions & 0 deletions CarbonQueryDatabase_oM/Adapter/CQDConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using BH.oM.Adapter;
using System.Collections.Generic;
using System.ComponentModel;
using BH.oM.LifeCycleAssessment;

namespace BH.oM.Adapters.CarbonQueryDatabase
{
Expand All @@ -35,6 +36,9 @@ public class CQDConfig : ActionConfig
/**** Public Properties ****/
/***************************************************/

[Description("The Type of Environmental Product Declaration")]
public virtual EPDType Type { get; set; } = EPDType.Product;

[Description("Specifies ID to search and return objects for in CarbonQueryDatabase. If this is specified it supersedes other input parameters.")]
public virtual string Id { get; set; } = null;

Expand Down
5 changes: 5 additions & 0 deletions CarbonQueryDatabase_oM/CarbonQueryDatabase_oM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<HintPath>C:\ProgramData\BHoM\Assemblies\Data_oM.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="LifeCycleAssessment_oML">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\ProgramData\BHoM\Assemblies\LifeCycleAssessment_oM.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand Down