From f332d4f661d95b41d7496fc8c162cd3a44eccdf0 Mon Sep 17 00:00:00 2001 From: TShapinsky Date: Mon, 11 Nov 2024 11:42:59 -0700 Subject: [PATCH] Fix Ruby/Python SWIG API when type is AlfalfaComponent --- .../measures/AlfalfaEPlusPython/measure.py | 11 ++++++ .../measures/AlfalfaEPlusRuby/measure.rb | 13 ++++++- src/alfalfa/Alfalfa.i | 22 ++++++++++++ src/alfalfa/AlfalfaComponent.hpp | 3 +- src/alfalfa/test/AlfalfaJSON_GTest.cpp | 36 +++++++++++++++++++ 5 files changed, 82 insertions(+), 3 deletions(-) diff --git a/resources/Examples/compact_osw/measures/AlfalfaEPlusPython/measure.py b/resources/Examples/compact_osw/measures/AlfalfaEPlusPython/measure.py index e80d09789e5..a21d61e0dc1 100644 --- a/resources/Examples/compact_osw/measures/AlfalfaEPlusPython/measure.py +++ b/resources/Examples/compact_osw/measures/AlfalfaEPlusPython/measure.py @@ -69,6 +69,17 @@ def run( meter_object = openstudio.IdfObject.load("Output:Meter, Electricity:Facility;").get() alfalfa.exposeFromObject(meter_object, "Electricity Meter IDF:Eplus:Python") + # Test Composite Point + meter_component = openstudio.alfalfa.AlfalfaMeter("Electricity:Facility") + actuator_component = openstudio.alfalfa.AlfalfaActuator("component_name", "component_type", "control_type") + composite_point = openstudio.alfalfa.AlfalfaPoint("Compound Point:Ruby") + composite_point.setOutput(meter_component) + composite_point.setInput(actuator_component) + alfalfa.exposePoint(composite_point) + + # Test Expose from Component + alfalfa.exposeFromComponent(actuator_component, "From Component Actuator") + # Test Output Variables alfalfa.exposeOutputVariable("EMS", "my_var", "Output Variable String:EPlus:Python") diff --git a/resources/Examples/compact_osw/measures/AlfalfaEPlusRuby/measure.rb b/resources/Examples/compact_osw/measures/AlfalfaEPlusRuby/measure.rb index ddb2ba33364..76d3af7a8b6 100644 --- a/resources/Examples/compact_osw/measures/AlfalfaEPlusRuby/measure.rb +++ b/resources/Examples/compact_osw/measures/AlfalfaEPlusRuby/measure.rb @@ -45,6 +45,17 @@ def run(workspace, runner, user_arguments) meter_object = OpenStudio::IdfObject.load("Output:Meter, Electricity:Facility;").get() alfalfa.exposeFromObject(meter_object, "Electricity Meter IDF:Eplus:Ruby") + # Test Composite Point + meter_component = OpenStudio::Alfalfa::AlfalfaMeter.new("Electricity:Facility") + actuator_component = OpenStudio::Alfalfa::AlfalfaActuator.new("component_name", "component_type", "control_type") + composite_point = OpenStudio::Alfalfa::AlfalfaPoint.new("Compound Point:Ruby") + composite_point.setOutput(meter_component) + composite_point.setInput(actuator_component) + alfalfa.exposePoint(composite_point) + + # Test Expose from Component + alfalfa.exposeFromComponent(actuator_component, "From Component Actuator") + # Test Output Variables alfalfa.exposeOutputVariable("EMS", "my_var", "Output Variable String:EPlus:Ruby") @@ -61,7 +72,7 @@ def run(workspace, runner, user_arguments) alfalfa.exposeFromObject(global_variable_object, "Global Variable IDF:EPlus:Ruby") # Test Actuators - alfalfa.exposeActuator("component_name", "componen_type", "control_type", "Actuator String:EPlus:Ruby") + alfalfa.exposeActuator("component_name", "component_type", "control_type", "Actuator String:EPlus:Ruby") actuator_object = OpenStudio::IdfObject.load("EnergyManagementSystem:Actuator,MyActuator,component_name,component_type,control_type;").get() alfalfa.exposeFromObject(actuator_object, "Actuator IDF:EPlus:Ruby") diff --git a/src/alfalfa/Alfalfa.i b/src/alfalfa/Alfalfa.i index ebe639c05a7..37530704fc0 100644 --- a/src/alfalfa/Alfalfa.i +++ b/src/alfalfa/Alfalfa.i @@ -57,4 +57,26 @@ %template(OptionalAlfalfaPoint) boost::optional; %template(OptionalAlfalfaComponent) boost::optional; +namespace openstudio::alfalfa { + %extend AlfalfaPoint { + void setOutput(const AlfalfaComponentBase& component) { + AlfalfaComponent alfalfa_component(component); + $self->setOutput(alfalfa_component); + } + + void setInput(const AlfalfaComponentBase& component) { + AlfalfaComponent alfalfa_component(component); + $self->setInput(alfalfa_component); + } + } + + %extend AlfalfaJSON { + boost::optional AlfalfaJSON::exposeFromComponent(const AlfalfaComponentBase& component, const std::string& display_name = std::string()) { + AlfalfaComponent alfalfa_component(component); + return $self->exposeFromComponent(alfalfa_component, display_name); + } + } +} + + #endif diff --git a/src/alfalfa/AlfalfaComponent.hpp b/src/alfalfa/AlfalfaComponent.hpp index 951ed4516f9..994bd306e6d 100644 --- a/src/alfalfa/AlfalfaComponent.hpp +++ b/src/alfalfa/AlfalfaComponent.hpp @@ -12,8 +12,7 @@ namespace alfalfa { class ALFALFA_API AlfalfaComponent { public: - template ::value, bool> = true> - AlfalfaComponent(T component) : m_component(std::make_unique(std::move(component))) {} + AlfalfaComponent(const AlfalfaComponentBase& component) : m_component(component.clone()) {} AlfalfaComponent(const AlfalfaComponent& other) : m_component(other.m_component->clone()) {} diff --git a/src/alfalfa/test/AlfalfaJSON_GTest.cpp b/src/alfalfa/test/AlfalfaJSON_GTest.cpp index 6a623d7c409..a336777308b 100644 --- a/src/alfalfa/test/AlfalfaJSON_GTest.cpp +++ b/src/alfalfa/test/AlfalfaJSON_GTest.cpp @@ -391,6 +391,15 @@ TEST(AlfalfaJSON, expose_meter) { EXPECT_FALSE(str_point.input().is_initialized()); EXPECT_TRUE(str_point.output().is_initialized()); EXPECT_EQ(str_point.displayName(), display_name); + + // Test Other Point Construction Methods + const AlfalfaPoint component_point = alfalfa.exposeFromComponent(str_component, display_name).get(); + AlfalfaPoint custom_point(display_name); + EXPECT_THROW({ custom_point.setInput(str_component); }, std::runtime_error); + custom_point.setOutput(str_component); + + EXPECT_EQ(str_point, custom_point); + EXPECT_EQ(str_point, component_point); } TEST(AlfalfaJSON, expose_output_variable) { @@ -461,6 +470,15 @@ TEST(AlfalfaJSON, expose_output_variable) { EXPECT_TRUE(str_point.output().is_initialized()); EXPECT_FALSE(str_point.input().is_initialized()); EXPECT_EQ(str_point.displayName(), display_name); + + // Test Other Point Construction Methods + const AlfalfaPoint component_point = alfalfa.exposeFromComponent(str_component, display_name).get(); + AlfalfaPoint custom_point(display_name); + EXPECT_THROW({ custom_point.setInput(str_component); }, std::runtime_error); + custom_point.setOutput(str_component); + + EXPECT_EQ(str_point, custom_point); + EXPECT_EQ(str_point, component_point); } TEST(AlfalfaJSON, expose_global_variable) { @@ -501,6 +519,15 @@ TEST(AlfalfaJSON, expose_global_variable) { EXPECT_TRUE(str_point.input().is_initialized()); EXPECT_TRUE(str_point.output().is_initialized()); EXPECT_EQ(str_point.displayName(), display_name); + + // Test Other Point Construction Methods + const AlfalfaPoint component_point = alfalfa.exposeFromComponent(str_component, display_name).get(); + AlfalfaPoint custom_point(display_name); + custom_point.setInput(str_component); + custom_point.setOutput(str_component); + + EXPECT_EQ(str_point, custom_point); + EXPECT_EQ(str_point, component_point); } TEST(AlfalfaJSON, expose_actuator) { @@ -550,4 +577,13 @@ TEST(AlfalfaJSON, expose_actuator) { EXPECT_TRUE(str_point.input().is_initialized()); EXPECT_TRUE(str_point.output().is_initialized()); EXPECT_EQ(str_point.displayName(), display_name); + + // Test Other Point Construction Methods + const AlfalfaPoint component_point = alfalfa.exposeFromComponent(str_component, display_name).get(); + AlfalfaPoint custom_point(display_name); + custom_point.setInput(str_component); + custom_point.setOutput(str_component); + + EXPECT_EQ(str_point, custom_point); + EXPECT_EQ(str_point, component_point); }