Skip to content

Commit

Permalink
Implements new StringArray SettingType class + refactors some fields …
Browse files Browse the repository at this point in the history
…as lists of strings

For example, the Authors field can have multiple values separated by a comma
  • Loading branch information
samuelduchesne committed Sep 10, 2020
1 parent b82fab7 commit 30506ea
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 18 deletions.
28 changes: 15 additions & 13 deletions Controls/InterfaceModels/BuildingTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Linq;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;

Expand Down Expand Up @@ -39,17 +39,17 @@ public class BuildingTemplate : LibraryComponent
[SimulationSetting(Description = "End year for range")]
public int YearTo { get; set; }

[SimulationSetting(Description = "alpha-3 Country Code")]
public string Country { get; set; }
[SimulationSetting(Description = "alpha-3 Country Code; comma separated for multiple values")]
public string[] Country { get; set; }

[SimulationSetting(Description = "ANSI/ASHRAE/IESNA Standard 90.1 International Climatic Zone")]
public string ClimateZone { get; set; }
[SimulationSetting(Description = "ANSI/ASHRAE/IESNA Standard 90.1 International Climatic Zone; comma separated for multiple values")]
public string[] ClimateZone { get; set; }

[SimulationSetting(Description = "Author of this template")]
public string Author { get; set; }
[SimulationSetting(Description = "Authors of this template; comma separated for multiple values")]
public string[] Authors { get; set; }

[SimulationSetting(Description = "Contact information")]
public string AuthorEmail { get; set; }
[SimulationSetting(Description = "Contact information; comma separated for multiple values")]
public string[] AuthorEmails { get; set; }

[SimulationSetting(Description = "Version number")]
public string Version { get; set; }
Expand Down Expand Up @@ -93,9 +93,10 @@ public override LibraryComponent Duplicate()
DefaultWindowToWallRatio = DefaultWindowToWallRatio,
YearFrom = YearFrom,
YearTo = YearTo,
Country = Country,
ClimateZone = ClimateZone,
Author = Author,
AuthorEmail = AuthorEmail,
Authors = Authors,
AuthorEmails = AuthorEmails,
Version = Version
};
res.CopyBasePropertiesFrom(this);
Expand All @@ -114,9 +115,10 @@ public override void OverwriteWith(LibraryComponent other, ComponentCoordinator
DefaultWindowToWallRatio = c.DefaultWindowToWallRatio;
YearFrom = c.YearFrom;
YearTo = c.YearTo;
Country = c.Country;
ClimateZone = c.ClimateZone;
Author = c.Author;
AuthorEmail = c.AuthorEmail;
Authors = c.Authors;
AuthorEmails = c.AuthorEmails;
Version = c.Version;
CopyBasePropertiesFrom(c);
}
Expand Down
1 change: 1 addition & 0 deletions Controls/SettingType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public enum SettingType
String,
Enum,
RealArray,
StringArray,
Reference
}
}
4 changes: 4 additions & 0 deletions Controls/SettingValueBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public override object ChangeType(object value, Type type, System.Globalization.
{
return entry.Split(',').Select(Double.Parse).ToArray();
}
else if (type == typeof(string[]))
{
return entry.Split(',').Select(a=>a.Trim()).ToArray();
}
else
{
throw new NotSupportedException($"Unsupported simulation setting type '{type}'");
Expand Down
23 changes: 23 additions & 0 deletions Controls/SimulationSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public SimulationSetting(
{
type = SettingType.RealArray;
}
else if (prop.PropertyType == typeof(string[]))
{
type = SettingType.StringArray;
}
else
{
throw new ArgumentException("Unknown setting type", nameof(type));
Expand Down Expand Up @@ -110,6 +114,11 @@ public object SingleValue
if (distinct[0] == null) { return null; }
return String.Join(",", (double[])distinct.Single());
}
else if (prop.PropertyType == typeof(string[]) && distinct.Count() == 1)
{
if (distinct[0] == null) { return null; }
return String.Join(", ", (string[])distinct.Single());
}
return distinct.Count() == 1 ? distinct.Single() : null;
}
set
Expand Down Expand Up @@ -171,6 +180,20 @@ public string MultipleValueDescriptionText
return "<multiple values>";
}
}
else if (prop.PropertyType == typeof(string[]))
{
var cmp = new EqualityComparerGenericWrapper(StructuralComparisons.StructuralEqualityComparer);
var vals = components.Select(prop.GetValue).Distinct(cmp);
if (vals.Count() == 1)
{
var val = (string[])vals.Single();
return String.Join(",", val);
}
else
{
return "<multiple values>";
}
}
else if (!prop.PropertyType.IsValueType)
{
var vals =
Expand Down
10 changes: 5 additions & 5 deletions Core/BuildingTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
Expand Down Expand Up @@ -39,16 +39,16 @@ public class BuildingTemplate : LibraryComponent
public int? YearTo { get; set; }

[DataMember]
public string Country { get; set; }
public string[] Country { get; set; }

[DataMember]
public string ClimateZone { get; set; }
public string[] ClimateZone { get; set; }

[DataMember]
public string Author { get; set; }
public string[] Authors { get; set; }

[DataMember]
public string AuthorEmail { get; set; }
public string[] AuthorEmails { get; set; }

[DataMember]
public string Version { get; set; } = "v1.0";
Expand Down

0 comments on commit 30506ea

Please sign in to comment.