Skip to content

Refactor and consolidate generator set functionality in the domain layer #866

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

Merged
merged 14 commits into from
Apr 22, 2025

Conversation

frodehk
Copy link
Contributor

@frodehk frodehk commented Apr 11, 2025

Why is this pull request needed?

This refactoring introduces a more structured and modular approach to the generator set functionality by leveraging the GeneratorSetEnergyComponent (extending EnergyComponent) in domain.infrastructure.generator_set and GeneratorSetProcessUnit (extending ProcessUnit) in domain.process.generator_set.

What does this pull request change?

  • Restructuring: The functionality previously in Genset (deleted from infrastructure.generator_set) has been moved to GeneratorSetEnergyComponent and GeneratorSetProcessUnit
  • Consolidation: The logic from domain.process.core.generator.GeneratorModelSampled and domain.process.dto.generator_set.GeneratorSetSampled has been unified and relocated to GeneratorSetProcessUnit in domain.process.generator_set.
  • Validation: Data validation has been extracted into a dedicated module, generator_set_validator.py, under domain.process.generator_set.

DTOs (for generator sets) are removed because their roles have become ambiguous and redundant over time. The responsibilities previously handled by the DTO layer have been distributed more appropriately:

  • Direct input handling: Data that was previously passed through DTOs is now provided directly to the relevant classes, improving clarity and reducing unnecessary abstraction.

  • Role reassignment: The GeneratorSetEnergyComponent and GeneratorSetProcessUnit classes have taken over the responsibilities of the DTOs. These classes now encapsulate the logic and data handling, ensuring a more cohesive and modular design.

This refactoring eliminates ambiguity, improves separation of concerns, and aligns the code with a cleaner architecture.

@frodehk frodehk self-assigned this Apr 11, 2025
@frodehk frodehk requested a review from a team as a code owner April 11, 2025 06:59
@frodehk frodehk force-pushed the refactor/move-generator-set-dto-core-models branch from aaac32e to 2127830 Compare April 11, 2025 07:26
@frodehk frodehk changed the title Refactor/move generator set dto core models Refactor and consolidate generator set functionality in the domain layer Apr 14, 2025
Comment on lines +113 to +124
def __eq__(self, other):
if not isinstance(other, GeneratorSetProcessUnit):
return False
return (
self.typ == other.typ
and self.headers == other.headers
and self.data == other.data
and self.get_name() == other.get_name()
and self.get_id() == other.get_id()
and self.energy_usage_adjustment_constant == other.energy_usage_adjustment_constant
and self.energy_usage_adjustment_factor == other.energy_usage_adjustment_factor
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The __eq__ method is being used indirectly in the test TestGeneratorSet.test_valid. It compares two GeneratorSetProcessUnit objects within a dictionary using assert. When the __eq__ method is commented out, Python defaults to comparing object references, which causes the test to fail because the two objects are different instances, even though their attributes are identical.

But it is only one test failing, so it is not widely used! If we remove it, we should rewrite the test to compare the attributes directly I guess (instead of relying on equality).

Comment on lines +28 to +29
model_data.setdefault("name", "generator_set_sampled_default_name")
return GeneratorSetProcessUnit(**model_data)
Copy link
Contributor

@jsolaas jsolaas Apr 15, 2025

Choose a reason for hiding this comment

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

This is what I missed, so ignore the other comment about something missing.

I think we should be able to improve this mapping a bit, instead of using _default_facility_to_dto_model_data and EnergyModelFactory we could add a mapper for GeneratorSetProcessUnit explicitly. Instead of reading headers and data out of a resource we could pass resource in. That would require us to move the resource interface into domain. This is where I think we should have two domains, one for the full context of all periods, and one for the core of ecalc (single timestep process simulation). A resource makes sense in one, but not the other IMO.

Further down the line we could move the mapping into ReferenceService. That gives us the option to validate References 'on-demand' instead of instantly.

Copy link
Collaborator

Choose a reason for hiding this comment

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

almost felt like I wrote a part of this myself :p tbc

Copy link
Contributor Author

@frodehk frodehk Apr 15, 2025

Choose a reason for hiding this comment

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

A new mapper for the GeneratorSetProcessUnit is created, and Resource is moved to domain (requires update of import in api/api/domain/resource.py). Should we continue to identify the boundaries between the two suggested domains (full context vs single time step)? And then move relevant classes and logic into their respective domains? I guess we should do this separately from this PR, as we will get a lot of changes?

Should we move the mapping into ReferenceService right away, or is it better to wait until the domain boundaries are more clear?

@frodehk frodehk merged commit 9ba4721 into main Apr 22, 2025
8 checks passed
@frodehk frodehk deleted the refactor/move-generator-set-dto-core-models branch April 22, 2025 09:30
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.

3 participants