Coverage for backend/common/config_types.py: 99%

84 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-11-06 23:27 +0000

1from typing import TypedDict, Required, Literal, Tuple, Dict, List, Any, Union 

2 

3 

4 

5from core.auxiliary.enums.unitOpGraphics import ConType 

6from core.auxiliary.enums import SimulationObjectClass 

7from pydantic import BaseModel, Field, RootModel 

8from typing import Literal, Tuple, Dict, List, Any 

9 

10 

11PropertyPackageKey = str 

12PortKey = str 

13 

14Transformer = Literal["inverse"] 

15 

16class PropertyType(BaseModel): 

17 """ 

18 - propertySetGroup: 

19 key of the property set group. If not specified, the property 

20 will be added to the default property set group. 

21 - value: 

22 default value of the property. If not specified, the value will 

23 be set to empty. 

24 - unit: 

25 default unit of the property. If not specified, the unit will 

26 be set to the default unit of the unit type. 

27 - unit_type (Required): 

28 type of the unit (temperature, pressure, etc.) or "dimensionless" for no unit 

29 - description: 

30 description of the property 

31 - type (Required): 

32 type of the property (numeric, dropdown, checkbox, segmented, text, numeric_arg) 

33 """ 

34 propertySetGroup: str = Field("default", description="Property set group") 

35 displayName: str = Field(..., description="Display name of the property") 

36 indexSets: List[str] = Field(None, description="Index sets of the property") 

37 sumToOne: bool = Field(None, description="Flag indicating whether the property should sum to one. If true, the last instance of the first index set will be calcualted from the others. See compound_propogation.py") 

38 value: Union[int, float,bool,str,List[str]] = Field(None, description="Default value of the property") 

39 unit: str = Field(None, description="Default unit of the property") 

40 unitType: str = Field(..., description="Type of the unit") 

41 description: str = Field(None, description="Description of the property") 

42 type: Literal["numeric", "dropdown", "checkbox", "segmented", "text", "numeric_arg"] = Field(..., description="Type of the property") 

43 many: bool = Field(None, description="Flag indicating whether there are multiple properties by this key") 

44 default: int = Field(None, description="Default number of properties if many=True") 

45 options: Dict[str, str] = Field(None, description="Options for the dropdown property. Key is the value, value is the display name") 

46 hasTimeIndex: bool = Field(True, description="Flag indicating whether the property has a time index.") 

47 

48 

49class PropertiesType(RootModel): 

50 root: Dict[str, PropertyType] 

51 

52 def __iter__(self): 

53 return iter(self.root) 

54 

55 def __getitem__(self, key): 

56 return self.root[key] 

57 

58 def get(self, key, default=None): 

59 return self.root.get(key, default) 

60 

61 def items(self): 

62 return self.root.items() 

63 

64 def keys(self): 

65 return self.root.keys() 

66 

67 

68class PropertySetType(BaseModel): 

69 type: Literal["All", "None", "composition", "stateVars", "exceptLast"] 

70 displayName: str = Field(..., description="Display name of the property set") 

71 stateVars: Tuple[str, ...] = Field(None, description="State variables of the property set") 

72 toggle: str = Field(None, description="Key of property that is used to enable/disable the property set. Must be within the property set group.") 

73 

74 

75class PortType(BaseModel): 

76 displayName: str = Field(..., description="Display name of the port") 

77 type: ConType = Field(..., description="Type of the port") 

78 streamType: str = Field("stream", description="Type of the stream e.g stream, energy_stream") 

79 many: bool = Field(None, description="Flag indicating whether the port can have multiple connections") 

80 default: int = Field(None, description="Amount of ports to create if many=True") 

81 minimum: int = Field(None, description="Minimum amount of ports if many=True") 

82 makeStream: bool = Field(True, description="Flag indicating whether the port is a power stream port") 

83 streamOffset: float = Field(0.75, description="Offset in grid units from unit opp to stream") 

84 streamName: str = Field("Stream", description="default name of the stream") 

85 

86class ArgType(BaseModel): 

87 PropertyPackage: str = Field(None, description="Property Package") 

88 SteamPropertyPackage: str = Field(None, description="Property Package") 

89 InletPropertyPackage: str = Field(None, description="Property Package") 

90 OutletPropertyPackage: str = Field(None, description="Property Package") 

91 Dynamic: str = Field(None, description="Is Dynamic or not") 

92 HasHoldUp: str = Field(None, description="Has holdup") 

93 HasPressureChange: str = Field(None, description="Pressure change") 

94 NumInlets: str = Field(None, description="Number of inlets") 

95 NumOutlets: str = Field(None, description="Number of outlets") 

96 HotSide: str = Field(None, description="Hot side properties") 

97 ColdSide: str = Field(None, description="Cold side properties") 

98 EnableCoefficients: str = Field(None, description="Are coefficients enabled?") 

99 ValveFunction: str = Field(None, description="Valve Function") 

100 Model: str = Field(None, description="Machine Learning model") 

101 IDs: str = Field(None, description="Machine Learning IDs") 

102 UnitOpNames: str = Field(None, description="ML unitOp names") 

103 

104 

105class GraphicObjectType(BaseModel): 

106 width: float = Field(..., description="Width of the object") 

107 height: float = Field(..., description="Height of the object") 

108 autoHeight: bool = Field(False, description="Flag indicating whether the height should be automatically adjusted based on number of ports") 

109 

110 

111class ObjectType(BaseModel): 

112 displayType: str = Field(..., description="Display type of the object") 

113 displayName: str | None = Field( 

114 default=None, description="Display name of the object" 

115 ) 

116 ports: Dict[PortKey, PortType] = Field(None, description="Ports contained in the object") 

117 graphicObject: GraphicObjectType = Field(..., description="Graphic object of the object") 

118 indexSets: List[str] = Field([], description="Index sets of the object") 

119 properties: PropertiesType = Field({}, description="Properties of the object") 

120 propertySetGroups: Dict[str, PropertySetType] = Field({}, description="Property set groups of the object") 

121 propertyPackagePorts: Dict[str, List[PortKey]] = Field(None, description="Which ports are used by which property packages") 

122 is_stream: bool = Field(False, description="Flag indicating whether the object is a stream") 

123 info: str = Field(None, description="Info about the object") 

124 keyProperties: List[str] | Dict[str, List[str]] = Field(None, description="Key properties of the object") 

125 splitter_fraction_name: str | None = Field(None, description="Property for configuring outlet names for indexed items") 

126 

127 

128# each key (eg. pump, compressor) should match with SimulationObjectClass.values 

129# there is no way to enforce this in the type system other than hardcoding 

130# all the keys in the SimulationObjectClass.values as Literal types 

131# see https://github.com/python/typing/issues/781 

132ConfigBaseType = Dict[SimulationObjectClass.values, ObjectType]