Skip to content

Commit

Permalink
Merge pull request #5298 from NREL/alfalfa_advanced_usage_fix
Browse files Browse the repository at this point in the history
Fix Ruby/Python SWIG API when type is AlfalfaComponent
  • Loading branch information
jmarrec authored Nov 12, 2024
2 parents a000d0e + 614d667 commit 4f0f8de
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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")
Expand Down
22 changes: 22 additions & 0 deletions src/alfalfa/Alfalfa.i
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,26 @@
%template(OptionalAlfalfaPoint) boost::optional<openstudio::alfalfa::AlfalfaPoint>;
%template(OptionalAlfalfaComponent) boost::optional<openstudio::alfalfa::AlfalfaComponent>;

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<AlfalfaPoint> AlfalfaJSON::exposeFromComponent(const AlfalfaComponentBase& component, const std::string& display_name = std::string()) {
AlfalfaComponent alfalfa_component(component);
return $self->exposeFromComponent(alfalfa_component, display_name);
}
}
}


#endif
3 changes: 1 addition & 2 deletions src/alfalfa/AlfalfaComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ namespace alfalfa {
class ALFALFA_API AlfalfaComponent
{
public:
template <typename T, std::enable_if_t<std::is_base_of<AlfalfaComponentBase, T>::value, bool> = true>
AlfalfaComponent(T component) : m_component(std::make_unique<T>(std::move(component))) {}
AlfalfaComponent(const AlfalfaComponentBase& component) : m_component(component.clone()) {}

AlfalfaComponent(const AlfalfaComponent& other) : m_component(other.m_component->clone()) {}

Expand Down
36 changes: 36 additions & 0 deletions src/alfalfa/test/AlfalfaJSON_GTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

0 comments on commit 4f0f8de

Please sign in to comment.