Skip to content

Commit

Permalink
refactor: moved classes to separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
psarras committed Oct 31, 2024
1 parent c681165 commit f87b3aa
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@

using Oasys.Business;

using OasysGH;
using OasysGH.Components;

using Attribute = Oasys.Business.Attribute;

namespace Oasys.GH.Helpers {

public static class Populator {
public static class BusinessExtensions {

private static readonly Dictionary<Type, Func<Attribute, IGH_Param>> ToGhParam
= new Dictionary<Type, Func<Attribute, IGH_Param>> {
Expand Down Expand Up @@ -91,75 +88,6 @@ public static void PopulateOutputParams(this IBusinessComponent businessComponen
}
}

public abstract class BusinessOasysDropdownGlue<T> : GH_OasysDropDownComponent, IDefaultValues
where T : IBusinessComponent {
private readonly T _businessComponent = Activator.CreateInstance<T>();

public BusinessOasysDropdownGlue() : base("", "", "", "", "") {
Name = _businessComponent.Metadata.Name;
NickName = _businessComponent.Metadata.NickName;
Description = _businessComponent.Metadata.Description;
Category = _businessComponent.Organisation.Category;
SubCategory = _businessComponent.Organisation.SubCategory;
}

public override Guid ComponentGuid { get; }

public override OasysPluginInfo PluginInfo { get; }

public void SetDefaultValues() { _businessComponent.SetDefaultValues(this); }

protected override void RegisterInputParams(GH_InputParamManager pManager) {
_businessComponent.PopulateInputParams(this);
}

protected override void RegisterOutputParams(GH_OutputParamManager pManager) {
_businessComponent.PopulateOutputParams(this);
}

public override void SetSelected(int i, int j) { }

protected override void SolveInternal(IGH_DataAccess da) {
_businessComponent.Compute();
_businessComponent.SetOutputValues(this, da);
}

protected override void InitialiseDropdowns() {
_spacerDescriptions = new List<string>();
_dropDownItems = new List<List<string>>();
_selectedItems = new List<string>();
_isInitialised = true;
}
}

public abstract class BusinessOasysGlue<T> : GH_OasysComponent, IDefaultValues where T : IBusinessComponent {

private readonly T _businessComponent = Activator.CreateInstance<T>();

public BusinessOasysGlue(string name, string nickname, string description, string category, string subCategory) :
base(name, nickname, description, category, subCategory) { }

public override OasysPluginInfo PluginInfo { get; } = AdSecGH.PluginInfo.Instance;
public void SetDefaultValues() { _businessComponent.SetDefaultValues(this); }

protected override void RegisterInputParams(GH_InputParamManager pManager) {
_businessComponent.PopulateInputParams(this);
}

protected override void RegisterOutputParams(GH_OutputParamManager pManager) {
_businessComponent.PopulateOutputParams(this);
}

protected override void SolveInstance(IGH_DataAccess DA) {
_businessComponent.Compute();
_businessComponent.SetOutputValues(this, DA);
}
}

public interface IDefaultValues {
void SetDefaultValues();
}

public class DummyBusiness : IBusinessComponent {

public DoubleParameter Alpha { get; set; } = new DoubleParameter {
Expand Down Expand Up @@ -221,7 +149,7 @@ public DummyOasysComponent() : base("Oasys Component Glue", "OCG", Test.thisIsFo
protected override void SolveInstance(IGH_DataAccess DA) { }
}

public class DummyOasysDropdown : BusinessOasysDropdownGlue<DummyBusiness> {
public class Dummy : BusinessGlue<DummyBusiness> {
public override GH_Exposure Exposure { get; } = GH_Exposure.hidden;
public override Guid ComponentGuid => new Guid("CAA08C9E-417C-42AE-B704-91F214C8C871");
}
Expand Down
80 changes: 80 additions & 0 deletions AdSecGH/Helpers/BusinessGlue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;

using Grasshopper.Kernel;

using Oasys.Business;

using OasysGH;
using OasysGH.Components;

namespace Oasys.GH.Helpers {

public abstract class BusinessOasysGlue<T> : GH_OasysComponent, IDefaultValues where T : IBusinessComponent {

private readonly T _businessComponent = Activator.CreateInstance<T>();

public BusinessOasysGlue(string name, string nickname, string description, string category, string subCategory) :
base(name, nickname, description, category, subCategory) { }

public override OasysPluginInfo PluginInfo { get; } = AdSecGH.PluginInfo.Instance;
public void SetDefaultValues() { _businessComponent.SetDefaultValues(this); }

protected override void RegisterInputParams(GH_InputParamManager pManager) {
_businessComponent.PopulateInputParams(this);
}

protected override void RegisterOutputParams(GH_OutputParamManager pManager) {
_businessComponent.PopulateOutputParams(this);
}

protected override void SolveInstance(IGH_DataAccess DA) {
_businessComponent.Compute();
_businessComponent.SetOutputValues(this, DA);
}
}

public interface IDefaultValues {
void SetDefaultValues();
}

public abstract class BusinessGlue<T> : GH_OasysDropDownComponent, IDefaultValues where T : IBusinessComponent {
private readonly T _businessComponent = Activator.CreateInstance<T>();

public BusinessGlue() : base("", "", "", "", "") {
Name = _businessComponent.Metadata.Name;
NickName = _businessComponent.Metadata.NickName;
Description = _businessComponent.Metadata.Description;
Category = _businessComponent.Organisation.Category;
SubCategory = _businessComponent.Organisation.SubCategory;
}

public override Guid ComponentGuid { get; }

public override OasysPluginInfo PluginInfo { get; }

public void SetDefaultValues() { _businessComponent.SetDefaultValues(this); }

protected override void RegisterInputParams(GH_InputParamManager pManager) {
_businessComponent.PopulateInputParams(this);
}

protected override void RegisterOutputParams(GH_OutputParamManager pManager) {
_businessComponent.PopulateOutputParams(this);
}

public override void SetSelected(int i, int j) { }

protected override void SolveInternal(IGH_DataAccess da) {
_businessComponent.Compute();
_businessComponent.SetOutputValues(this, da);
}

protected override void InitialiseDropdowns() {
_spacerDescriptions = new List<string>();
_dropDownItems = new List<List<string>>();
_selectedItems = new List<string>();
_isInitialised = true;
}
}
}
41 changes: 41 additions & 0 deletions AdSecGHTests/Helpers/BusinessComponentNameTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Oasys.GH.Helpers;

using Xunit;

namespace AdSecGHTests.Helpers {
[Collection("GrasshopperFixture collection")]
public class BusinessComponentNameTest {
private readonly Dummy component;
private readonly DummyBusiness dummyBusiness;

public BusinessComponentNameTest() {
dummyBusiness = new DummyBusiness();
component = new Dummy();
}

[Fact]
public void ShouldHaveNameFromBusiness() {
Assert.Equal(dummyBusiness.Metadata.Name, component.Name);
}

[Fact]
public void ShouldHaveNicknameFromBusiness() {
Assert.Equal(dummyBusiness.Metadata.NickName, component.NickName);
}

[Fact]
public void ShouldHaveDescriptionFromBusiness() {
Assert.Equal(dummyBusiness.Metadata.Description, component.Description);
}

[Fact]
public void ShouldBeAtTheRightCategory() {
Assert.Equal(dummyBusiness.Organisation.Category, component.Category);
}

[Fact]
public void ShouldBeAtTheRightSubCategory() {
Assert.Equal(dummyBusiness.Organisation.SubCategory, component.SubCategory);
}
}
}
40 changes: 2 additions & 38 deletions AdSecGHTests/Helpers/DataConvertorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,14 @@

namespace AdSecGHTests.Helpers {

[Collection("GrasshopperFixture collection")]
public class BusinessComponentNameTest {
private readonly DummyOasysDropdown component;
private readonly DummyBusiness dummyBusiness;

public BusinessComponentNameTest() {
dummyBusiness = new DummyBusiness();
component = new DummyOasysDropdown();
}

[Fact]
public void ShouldHaveNameFromBusiness() {
Assert.Equal(dummyBusiness.Metadata.Name, component.Name);
}

[Fact]
public void ShouldHaveNicknameFromBusiness() {
Assert.Equal(dummyBusiness.Metadata.NickName, component.NickName);
}

[Fact]
public void ShouldHaveDescriptionFromBusiness() {
Assert.Equal(dummyBusiness.Metadata.Description, component.Description);
}

[Fact]
public void ShouldBeAtTheRightCategory() {
Assert.Equal(dummyBusiness.Organisation.Category, component.Category);
}

[Fact]
public void ShouldBeAtTheRightSubCategory() {
Assert.Equal(dummyBusiness.Organisation.SubCategory, component.SubCategory);
}
}

[Collection("GrasshopperFixture collection")]
public class DataConvertorTest {
private readonly DummyBusiness dummyBusiness;
private readonly DummyOasysDropdown oasysDropdown;
private readonly Dummy oasysDropdown;

public DataConvertorTest() {
dummyBusiness = new DummyBusiness();
oasysDropdown = new DummyOasysDropdown();
oasysDropdown = new Dummy();
}

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions AdSecGHTests/Helpers/DropIntoGhDocumentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ namespace AdSecGHTests.Helpers {
[Collection("GrasshopperFixture collection")]
public class DropIntoGhDocumentTest {
private readonly GH_Document _document;
private readonly DummyOasysDropdown oasysDropdown;
private readonly Dummy oasysDropdown;

public DropIntoGhDocumentTest() {
string tempPath = Path.Combine(Path.GetTempPath(), "AdSecGHTests", "DropIntoGhDocumentTest.gh");

oasysDropdown = new DummyOasysDropdown();
oasysDropdown = new Dummy();
_document = new GH_Document();
_document.AddObject(oasysDropdown, true);
_document.NewSolution(true);
Expand Down

0 comments on commit f87b3aa

Please sign in to comment.