Skip to content

Commit

Permalink
chore: remove base consumer and parents in dto
Browse files Browse the repository at this point in the history
  • Loading branch information
frodehk committed Jan 6, 2025
1 parent d8dc0d7 commit 562b01a
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 149 deletions.
1 change: 0 additions & 1 deletion src/libecalc/domain/infrastructure/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from libecalc.domain.infrastructure.energy_components.asset.asset import Asset
from libecalc.domain.infrastructure.energy_components.base.component_dto import BaseConsumer
from libecalc.domain.infrastructure.energy_components.electricity_consumer.electricity_consumer import (
ElectricityConsumer,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,106 +1,10 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Optional

from libecalc.common.component_type import ComponentType
from libecalc.common.consumption_type import ConsumptionType
from libecalc.common.string.string_utils import generate_id
from libecalc.common.time_utils import Period
from libecalc.common.units import Unit
from libecalc.common.utils.rates import RateType
from libecalc.domain.infrastructure.energy_components.component_validation_error import (
ComponentValidationException,
ModelValidationError,
)
from libecalc.domain.infrastructure.energy_components.utils import _convert_keys_in_dictionary_from_str_to_periods
from libecalc.dto.fuel_type import FuelType
from libecalc.dto.types import ConsumerUserDefinedCategoryType
from libecalc.dto.utils.validators import (
ExpressionType,
validate_temporal_model,
)
from libecalc.expression import Expression


class Component(ABC):
component_type: ComponentType

@property
@abstractmethod
def id(self) -> str: ...


class BaseComponent(Component, ABC):
def __init__(self, name: str, regularity: dict[Period, Expression]):
self.name = name
self.regularity = self.check_regularity(regularity)
validate_temporal_model(self.regularity)

@classmethod
def check_regularity(cls, regularity):
if isinstance(regularity, dict) and len(regularity.values()) > 0:
regularity = _convert_keys_in_dictionary_from_str_to_periods(regularity)
return regularity


class BaseEquipment(BaseComponent, ABC):
def __init__(
self,
name: str,
regularity: dict[Period, Expression],
user_defined_category: dict[Period, ConsumerUserDefinedCategoryType],
component_type: ComponentType,
energy_usage_model: Optional[dict[Period, Expression]] = None,
fuel: Optional[dict[Period, FuelType]] = None,
):
super().__init__(name, regularity)
self.user_defined_category = user_defined_category
self.energy_usage_model = energy_usage_model
self.component_type = component_type
self.fuel = fuel

@property
def id(self) -> str:
return generate_id(self.name)


class BaseConsumer(BaseEquipment, ABC):
"""Base class for all consumers."""

def __init__(
self,
name: str,
regularity: dict[Period, Expression],
consumes: ConsumptionType,
user_defined_category: dict[Period, ConsumerUserDefinedCategoryType],
component_type: ComponentType,
energy_usage_model: Optional[dict[Period, Expression]] = None,
fuel: Optional[dict[Period, FuelType]] = None,
):
super().__init__(name, regularity, user_defined_category, component_type, energy_usage_model, fuel)

self.fuel = self.validate_fuel_exist(name=self.name, fuel=fuel, consumes=consumes)
self.consumes = consumes

@classmethod
def validate_fuel_exist(cls, name: str, fuel: Optional[dict[Period, FuelType]], consumes: ConsumptionType):
"""
Make sure fuel is set if consumption type is FUEL.
"""
if isinstance(fuel, dict) and len(fuel.values()) > 0:
fuel = _convert_keys_in_dictionary_from_str_to_periods(fuel)
if consumes == ConsumptionType.FUEL and (fuel is None or len(fuel) < 1):
msg = "Missing fuel for fuel consumer"
raise ComponentValidationException(
errors=[
ModelValidationError(
name=name,
message=str(msg),
)
],
)
return fuel
from libecalc.dto.utils.validators import ExpressionType


class ExpressionTimeSeries:
Expand Down
43 changes: 40 additions & 3 deletions src/libecalc/domain/infrastructure/energy_components/common.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Literal, Optional, TypeVar, Union

from libecalc.common.component_type import ComponentType
from libecalc.common.consumption_type import ConsumptionType
from libecalc.common.string.string_utils import generate_id
from libecalc.common.time_utils import Period
from libecalc.domain.infrastructure.energy_components.asset.asset import Asset
from libecalc.domain.infrastructure.energy_components.base.component_dto import BaseConsumer
from libecalc.domain.infrastructure.energy_components.compressor.component_dto import CompressorComponent
from libecalc.domain.infrastructure.energy_components.consumer_system.consumer_system_dto import ConsumerSystem
from libecalc.domain.infrastructure.energy_components.electricity_consumer.electricity_consumer import (
Expand All @@ -12,6 +14,8 @@
from libecalc.domain.infrastructure.energy_components.generator_set.generator_set_dto import GeneratorSet
from libecalc.domain.infrastructure.energy_components.installation.installation import Installation
from libecalc.domain.infrastructure.energy_components.pump.component_dto import PumpComponent
from libecalc.domain.infrastructure.energy_components.utils import _convert_keys_in_dictionary_from_str_to_periods
from libecalc.dto.utils.validators import validate_temporal_model
from libecalc.expression import Expression

Consumer = Union[FuelConsumer, ElectricityConsumer]
Expand Down Expand Up @@ -55,7 +59,7 @@ def __init__(self, from_component_id: str, to_component_id: str, stream_name: Op
ConsumerComponent = TypeVar("ConsumerComponent", bound=Union[CompressorComponent, PumpComponent])


class TrainComponent(BaseConsumer):
class TrainComponent:
component_type: Literal[ComponentType.TRAIN_V2] = ComponentType.TRAIN_V2

def __init__(
Expand All @@ -68,6 +72,39 @@ def __init__(
stages: list,
streams: list,
):
super().__init__(name, regularity, consumes, user_defined_category, component_type)
self.name = name
self.regularity = self.check_regularity(regularity)
validate_temporal_model(self.regularity)
self.consumes = consumes
self.user_defined_category = user_defined_category
self.component_type = component_type
self.stages = stages
self.streams = streams

@property
def id(self) -> str:
return generate_id(self.name)

@staticmethod
def check_regularity(regularity: dict[Period, Expression]):
if isinstance(regularity, dict) and len(regularity.values()) > 0:
regularity = _convert_keys_in_dictionary_from_str_to_periods(regularity)
return regularity

def is_fuel_consumer(self) -> bool:
return self.consumes == ConsumptionType.FUEL

def is_electricity_consumer(self) -> bool:
return self.consumes == ConsumptionType.ELECTRICITY

def is_provider(self) -> bool:
return False

def is_container(self) -> bool:
return False

def get_component_process_type(self) -> ComponentType:
return self.component_type

def get_name(self) -> str:
return self.name
Original file line number Diff line number Diff line change
@@ -1,16 +1,68 @@
from typing import Literal
from typing import Literal, Optional

from libecalc.application.energy.energy_component import EnergyComponent
from libecalc.common.component_type import ComponentType
from libecalc.common.consumption_type import ConsumptionType
from libecalc.common.string.string_utils import generate_id
from libecalc.common.time_utils import Period
from libecalc.domain.infrastructure.energy_components.base.component_dto import BaseConsumer
from libecalc.dto.models.compressor import CompressorModel
from libecalc.domain.infrastructure.energy_components.component_validation_error import (
ComponentValidationException,
ModelValidationError,
)
from libecalc.domain.infrastructure.energy_components.utils import _convert_keys_in_dictionary_from_str_to_periods
from libecalc.dto import FuelType
from libecalc.dto.types import ConsumerUserDefinedCategoryType
from libecalc.dto.utils.validators import validate_temporal_model
from libecalc.expression import Expression


class CompressorComponent(BaseConsumer, EnergyComponent):
class CompressorComponent(EnergyComponent):
component_type: Literal[ComponentType.COMPRESSOR] = ComponentType.COMPRESSOR
energy_usage_model: dict[Period, CompressorModel]

def __init__(
self,
name: str,
regularity: dict[Period, Expression],
consumes: ConsumptionType,
user_defined_category: dict[Period, ConsumerUserDefinedCategoryType],
energy_usage_model: Optional[dict[Period, Expression]] = None,
fuel: Optional[dict[Period, FuelType]] = None,
component_type: Optional[ComponentType] = ComponentType.COMPRESSOR,
):
self.name = name
self.regularity = self.check_regularity(regularity)
validate_temporal_model(self.regularity)
self.user_defined_category = user_defined_category
self.energy_usage_model = energy_usage_model
self.fuel = self.validate_fuel_exist(name=self.name, fuel=fuel, consumes=consumes)
self.consumes = consumes
self.component_type = component_type

@property
def id(self) -> str:
return generate_id(self.name)

@staticmethod
def check_regularity(regularity):
if isinstance(regularity, dict) and len(regularity.values()) > 0:
regularity = _convert_keys_in_dictionary_from_str_to_periods(regularity)
return regularity

@staticmethod
def validate_fuel_exist(name: str, fuel: Optional[dict[Period, FuelType]], consumes: ConsumptionType):
if isinstance(fuel, dict) and len(fuel.values()) > 0:
fuel = _convert_keys_in_dictionary_from_str_to_periods(fuel)
if consumes == ConsumptionType.FUEL and (fuel is None or len(fuel) < 1):
msg = "Missing fuel for fuel consumer"
raise ComponentValidationException(
errors=[
ModelValidationError(
name=name,
message=str(msg),
)
],
)
return fuel

def is_fuel_consumer(self) -> bool:
return self.consumes == ConsumptionType.FUEL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from libecalc.core.result import ComponentResult, EcalcModelResult
from libecalc.core.result.emission import EmissionResult
from libecalc.domain.infrastructure.energy_components.base.component_dto import (
BaseConsumer,
ConsumerID,
PriorityID,
SystemComponentConditions,
Expand All @@ -43,13 +42,15 @@
from libecalc.domain.infrastructure.energy_components.fuel_model.fuel_model import FuelModel
from libecalc.domain.infrastructure.energy_components.pump import Pump
from libecalc.domain.infrastructure.energy_components.pump.component_dto import PumpComponent
from libecalc.domain.infrastructure.energy_components.utils import _convert_keys_in_dictionary_from_str_to_periods
from libecalc.dto import FuelType
from libecalc.dto.component_graph import ComponentGraph
from libecalc.dto.types import ConsumerUserDefinedCategoryType
from libecalc.dto.utils.validators import validate_temporal_model
from libecalc.expression import Expression


class ConsumerSystem(BaseConsumer, Emitter, EnergyComponent):
class ConsumerSystem(Emitter, EnergyComponent):
def __init__(
self,
name: str,
Expand All @@ -62,17 +63,42 @@ def __init__(
fuel: Optional[dict[Period, FuelType]] = None,
component_type: Literal[ComponentType.CONSUMER_SYSTEM_V2] = ComponentType.CONSUMER_SYSTEM_V2,
):
super().__init__(
component_type=component_type,
name=name,
user_defined_category=user_defined_category,
regularity=regularity,
consumes=consumes,
fuel=fuel,
)
self.name = name
self.user_defined_category = user_defined_category
self.regularity = self.check_regularity(regularity)
self.consumes = consumes
validate_temporal_model(self.regularity)
self.component_conditions = component_conditions
self.stream_conditions_priorities = stream_conditions_priorities
self.consumers = consumers
self.fuel = self.validate_fuel_exist(name=self.name, fuel=fuel, consumes=consumes)
self.component_type = component_type

@property
def id(self) -> str:
return generate_id(self.name)

@staticmethod
def check_regularity(regularity):
if isinstance(regularity, dict) and len(regularity.values()) > 0:
regularity = _convert_keys_in_dictionary_from_str_to_periods(regularity)
return regularity

@staticmethod
def validate_fuel_exist(name: str, fuel: Optional[dict[Period, FuelType]], consumes: ConsumptionType):
if isinstance(fuel, dict) and len(fuel.values()) > 0:
fuel = _convert_keys_in_dictionary_from_str_to_periods(fuel)
if consumes == ConsumptionType.FUEL and (fuel is None or len(fuel) < 1):
msg = "Missing fuel for fuel consumer"
raise ComponentValidationException(
errors=[
ModelValidationError(
name=name,
message=str(msg),
)
],
)
return fuel

def is_fuel_consumer(self) -> bool:
return self.consumes == ConsumptionType.FUEL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from libecalc.common.component_type import ComponentType
from libecalc.common.consumption_type import ConsumptionType
from libecalc.common.energy_usage_type import EnergyUsageType
from libecalc.common.string.string_utils import generate_id
from libecalc.common.temporal_model import TemporalModel
from libecalc.common.time_utils import Period
from libecalc.common.variables import ExpressionEvaluator
from libecalc.core.result import EcalcModelResult
from libecalc.domain.infrastructure.energy_components.base.component_dto import BaseConsumer
from libecalc.domain.infrastructure.energy_components.legacy_consumer.component import (
Consumer as ConsumerEnergyComponent,
)
Expand All @@ -24,7 +24,7 @@
from libecalc.expression import Expression


class ElectricityConsumer(BaseConsumer, EnergyComponent):
class ElectricityConsumer(EnergyComponent):
def __init__(
self,
name: str,
Expand All @@ -40,10 +40,25 @@ def __init__(
energy_usage_model: dict[Period, ElectricEnergyUsageModel],
consumes: Literal[ConsumptionType.ELECTRICITY] = ConsumptionType.ELECTRICITY,
):
super().__init__(name, regularity, consumes, user_defined_category, component_type, energy_usage_model, None)
self.name = name
self.regularity = self.check_regularity(regularity)
validate_temporal_model(self.regularity)
self.user_defined_category = user_defined_category
self.energy_usage_model = self.check_energy_usage_model(energy_usage_model)
self._validate_el_consumer_temporal_model(self.energy_usage_model)
self._check_model_energy_usage(self.energy_usage_model)
self.consumes = consumes
self.component_type = component_type

@property
def id(self) -> str:
return generate_id(self.name)

@staticmethod
def check_regularity(regularity):
if isinstance(regularity, dict) and len(regularity.values()) > 0:
regularity = _convert_keys_in_dictionary_from_str_to_periods(regularity)
return regularity

def is_fuel_consumer(self) -> bool:
return False
Expand Down
Loading

0 comments on commit 562b01a

Please sign in to comment.