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

#4007 - Model objects returned in inconsistent order #4010

Merged
merged 4 commits into from
Jul 10, 2020
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
5 changes: 4 additions & 1 deletion src/model/Model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ class MODEL_API Model : public openstudio::Workspace {
* speed up the search. This method will only work for concrete model objects (leaves in the
* ModelObject inheritance tree), hence the name. */
template <typename T>
std::vector<T> getConcreteModelObjects() const
std::vector<T> getConcreteModelObjects(bool sorted=false) const
{
std::vector<T> result;
std::vector<WorkspaceObject> objects = this->getObjectsByType(T::iddObjectType());
Expand All @@ -311,6 +311,9 @@ class MODEL_API Model : public openstudio::Workspace {
std::shared_ptr<typename T::ImplType> p = it->getImpl<typename T::ImplType>();
if (p) { result.push_back(T(p)); }
}
if (sorted) {
std::sort(result.begin(), result.end());
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

add an optional argument to sort stuff in getConcreteModelObjects, defaults to false like it was before.

return result;
}

Expand Down
10 changes: 5 additions & 5 deletions src/model/Model_Common_Include.i
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
%init %{
rb_eval_string("OpenStudio::IdfObject.class_eval { define_method(:to_" #_name ") { OpenStudio::Model::to" #_name "(self); } }");
rb_eval_string("OpenStudio::Model::Model.class_eval { define_method(:get" #_name ") { |handle| OpenStudio::Model::get" #_name "(self, handle); } }");
rb_eval_string("OpenStudio::Model::Model.class_eval { define_method(:get" #_name "s) { OpenStudio::Model::get" #_name "s(self); } }");
rb_eval_string("OpenStudio::Model::Model.class_eval { define_method(:get" #_name "s) { | sorted = false | OpenStudio::Model::get" #_name "s(self, sorted); } }");
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Modify the ruby wrapper to provide getXXXXs(sorted=false), so that if you call getXXXs it sill does what it used to (unsorted for ConcreteModelObjects and ModelObjects alike)

(These code changes are very simple to see, but it took me a surprising amount of effort to get right)

rb_eval_string("OpenStudio::Model::Model.class_eval { define_method(:get" #_name "ByName) { |name| OpenStudio::Model::get" #_name "ByName(self, name); } }");
rb_eval_string("OpenStudio::Model::Model.class_eval { define_method(:get" #_name "sByName) { |name, exactMatch| OpenStudio::Model::get" #_name "sByName(self, name, exactMatch); } }");
%}
Expand Down Expand Up @@ -202,7 +202,7 @@

boost::optional<_name> to##_name(const openstudio::IdfObject& idfObject);
boost::optional<_name> get##_name(const Model &t_model, const openstudio::Handle &t_handle);
std::vector<_name> get##_name##s(const Model &t_model);
std::vector<_name> get##_name##s(const Model &t_model, bool sorted);
boost::optional<_name> get##_name##ByName(const Model &t_model, const std::string &t_name);
std::vector<_name> get##_name##sByName(const Model &t_model, const std::string &t_name, bool t_exactMatch);
}
Expand All @@ -216,11 +216,11 @@
boost::optional<_name> get##_name(const Model &t_model, const openstudio::Handle &t_handle) {
return t_model.getModelObject<_name>(t_handle);
}
std::vector<_name> get##_name##s(const Model &t_model) {
std::vector<_name> get##_name##s(const Model &t_model, bool sorted) {
%#if _isConcrete
return t_model.getConcreteModelObjects<_name>();
return t_model.getConcreteModelObjects<_name>(sorted);
%#else
return t_model.getModelObjects<_name>();
return t_model.getModelObjects<_name>(sorted);
%#endif
}
boost::optional<_name> get##_name##ByName(const Model &t_model, const std::string &t_name) {
Expand Down