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

try singleton pattern for unpackaged #2

Merged
merged 8 commits into from
Apr 16, 2024

Conversation

gonuke
Copy link

@gonuke gonuke commented Apr 16, 2024

Here's a proposal for a more streamlined way to get a single unpackaged type for each simulation available across all contexts.

I'll add some comments inline.

I haven't tested it all yet.

@@ -207,13 +204,19 @@ Package::Ptr Context::AddPackage(std::string name, double fill_min, double fill_
}

Package::Ptr Context::GetPackageByName(std::string name) {
if (name == Package::unpackaged_name()) {
Copy link
Author

Choose a reason for hiding this comment

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

special handling of unpackaged_name

if (packages_.count(name) == 0) {
throw KeyError("Invalid package name " + name);
}
return packages_[name];
}

Package::Ptr Context::GetPackageById(int id) {
if (id == Package::unpackaged_id()) {
Copy link
Author

Choose a reason for hiding this comment

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

special handling of unpackaged_id

src/package.cc Outdated
// otherwise return the object that already exists
Package::Ptr unpackaged() {

if !unpackaged_ {
Copy link
Author

Choose a reason for hiding this comment

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

This only ever happens once per simulation.

Package::Package(std::string name, double fill_min, double fill_max, std::string strategy) : name_(name), id_(next_package_id_++), fill_min_(fill_min), fill_max_(fill_max), strategy_(strategy) {}

Package::Package(int id, std::string name) : id_(id), name_(name), fill_min_(0), fill_max_(std::numeric_limits<double>::max()) {}
Package::Package(std::string name, double fill_min, double fill_max, std::string strategy) :
Copy link
Author

Choose a reason for hiding this comment

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

This is my proposal to handle the different cases with a single constructor

  1. must always provide at least a name
  2. defaults for fill_min, fill_max, and strategy (see header)
  3. if the name is unpackaged_name_ set the id manually to the right value, otherwise use the next_id
  • This works for creating the unpackaged object
  • This works for creating any object without setting anything but the name - you wanted an unnamed package but someone can still do that by specifying "unnamed" in the Create() method.

Copy link
Author

Choose a reason for hiding this comment

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

Although, if this is only called through Create() then that probably needs defaults if we want to have defaults.

src/package.cc Outdated
Comment on lines 58 to 61
if (unpackaged_) {
throw ValueError("can't create a package with name " + unpackaged_name_);
}
id_ = unpackaged_id_;
Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure I've covered all the bases here... it's possible for the user to generate a package with this reserved name before the unpackaged object gets built, which will lead to problems.

@gonuke
Copy link
Author

gonuke commented Apr 16, 2024

Tests are all failing, but I promised some students I'd grade their interim reports so I'll come back to this

@gonuke
Copy link
Author

gonuke commented Apr 16, 2024

I think it's now passing tests...?

Copy link

github-actions bot commented Apr 16, 2024

Downstream Build Status Report - 9b95834 - 2024-04-16 02:18:29 +0000

Build FROM cyclus_20.04_apt/cyclus
  • Cycamore: Success
  • Cymetric: Success
Build FROM cyclus_20.04_conda/cyclus
  • Cycamore: Success
  • Cymetric: Success
Build FROM cyclus_22.04_apt/cyclus
  • Cycamore: Success
  • Cymetric: Success
Build FROM cyclus_22.04_conda/cyclus
  • Cycamore: Success
  • Cymetric: Success

Copy link
Owner

@nuclearkatie nuclearkatie left a comment

Choose a reason for hiding this comment

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

Thanks for this @gonuke! I will add the defaults to Create in the main PR after merging. I had only seen a pattern like this described as a way to have only one instance of an object, which obviously wasn't what we were looking for. So I appreciate your addition here!

@nuclearkatie
Copy link
Owner

@gonuke if you mark this as ready for review I'll merge and keep adding to it in #1729

@gonuke gonuke marked this pull request as ready for review April 16, 2024 17:22
@nuclearkatie
Copy link
Owner

Thanks @gonuke!

@nuclearkatie nuclearkatie merged commit df2fdf4 into nuclearkatie:packaging Apr 16, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants