Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Ruby/Python SWIG API when type is AlfalfaComponent #5298

Merged
merged 2 commits into from
Nov 12, 2024
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
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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These could be moved into the main source if desired.

void setOutput(const AlfalfaComponentBase& component) {
AlfalfaComponent alfalfa_component(component);
$self->setOutput(alfalfa_component);
Comment on lines +63 to +64
Copy link
Member Author

@TShapinsky TShapinsky Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
AlfalfaComponent alfalfa_component(component);
$self->setOutput(alfalfa_component);
$self->setOutput(AlfalfaComponent {component});

}

void setInput(const AlfalfaComponentBase& component) {
AlfalfaComponent alfalfa_component(component);
$self->setInput(alfalfa_component);
Comment on lines +68 to +69
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
AlfalfaComponent alfalfa_component(component);
$self->setInput(alfalfa_component);
$self->setInput(AlfalfaComponent {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);
Comment on lines +75 to +76
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
AlfalfaComponent alfalfa_component(component);
return $self->exposeFromComponent(alfalfa_component, display_name);
return $self->exposeFromComponent(AlfalfaComponent {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()) {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why you added this one in the main source but the others just in the Alfalfa.i

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want the ctor exposed to the bindings so i added it here and ignored the ctors for the class in the .i .


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);
}