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: 56 additions & 7 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,11 +76,9 @@ 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 @@ -101,7 +99,6 @@ 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 @@ -110,13 +107,65 @@ private List<EnvironmentalProductDeclaration> ReadEnvironmentalProductDeclaratio
{
foreach (CustomObject co in objList)
{
EnvironmentalProductDeclaration epdData = Adapter.CarbonQueryDatabase.Convert.ToEnvironmentalProductDeclaration(co, config);
EnvironmentalProductDeclaration epdData = Adapter.CarbonQueryDatabase.Convert.ToEnvironmentalProductDeclaration(co);
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,10 +44,6 @@
<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 @@ -64,10 +60,6 @@
<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: 62 additions & 60 deletions CarbonQueryDatabase_Adapter/Convert/ToBHoM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

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 System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

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

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

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

Expand All @@ -62,84 +66,82 @@ 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;

string publisherNames = "";
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>();
if (obj.PropertyValue("publishers") != null)
{
IEnumerable pubs = (IEnumerable)obj.PropertyValue("publishers");
foreach (CustomObject pub in pubs)
{
publisherNames += pub.ToString() + " ";
publisherNames.Add(pub.PropertyValue("name").ToString());
}
publisherNames = publisherNames.Trim();
}

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

EnvironmentalMetric metric = new EnvironmentalMetric
{
Field = EnvironmentalProductDeclarationField.GlobalWarmingPotential,
Phases = new List<LifeCycleAssessmentPhases>() { LifeCycleAssessmentPhases.A1, LifeCycleAssessmentPhases.A2, LifeCycleAssessmentPhases.A3},
Quantity = gwpVal,
};

AdditionalEPDData data = new AdditionalEPDData
{
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,
};
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);

EPDDensity densityFragment = new EPDDensity
{
Density = density,
};
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;

EnvironmentalProductDeclaration epd = new EnvironmentalProductDeclaration
SectorEnvironmentalProductDeclaration epd = new SectorEnvironmentalProductDeclaration
{
Type = config.Type,
EnvironmentalMetric = new List<EnvironmentalMetric> { metric },
QuantityType = quantityType,
QuantityTypeValue = 1,
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() ?? "",
Jurisdiction = jurisdictionNames,
Publisher = publisherNames,
};

// 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;
}
return epd;
}

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



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

namespace BH.oM.Adapters.CarbonQueryDatabase
{
Expand All @@ -36,9 +35,6 @@ 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: 0 additions & 5 deletions CarbonQueryDatabase_oM/CarbonQueryDatabase_oM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@
<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