1- abstract type OptimizationContainerKey end
1+ abstract type OptimizationContainerKey{
2+ T <: OptimizationKeyType ,
3+ U <: InfrastructureSystemsType ,
4+ } end
25
36# These functions define the column names of DataFrames in all read-result functions.
47# The function get_second_dimension_result_column_name is only used if the result data
@@ -9,204 +12,121 @@ abstract type OptimizationContainerKey end
912get_first_dimension_result_column_name (:: OptimizationContainerKey ) = " name"
1013get_second_dimension_result_column_name (:: OptimizationContainerKey ) = " name2"
1114
12- const CONTAINER_KEY_EMPTY_META = " "
13-
14- function make_key (:: Type{T} , args... ) where {T <: OptimizationContainerKey }
15- return T (args... )
16- end
17-
18- function encode_key (key:: OptimizationContainerKey )
19- return encode_symbol (get_component_type (key), get_entry_type (key), key. meta)
20- end
21-
22- encode_key_as_string (key:: OptimizationContainerKey ) = string (encode_key (key))
23- encode_keys_as_strings (container_keys) = [encode_key_as_string (k) for k in container_keys]
24-
25- function encode_symbol (
26- :: Type{T} ,
27- :: Type{U} ,
28- meta:: String = CONTAINER_KEY_EMPTY_META,
29- ) where {T <: InfrastructureSystemsType , U}
30- meta_ = isempty (meta) ? meta : COMPONENT_NAME_DELIMITER * meta
31- T_ = replace (replace (strip_module_name (T), " {" => COMPONENT_NAME_DELIMITER), " }" => " " )
32- return Symbol (" $(strip_module_name (string (U)))$(COMPONENT_NAME_DELIMITER)$(T_) " * meta_)
33- end
34-
35- function check_meta_chars (meta:: String )
36- # Underscores in this field will prevent us from being able to decode keys.
37- if occursin (COMPONENT_NAME_DELIMITER, meta)
38- throw (InvalidValue (" '$COMPONENT_NAME_DELIMITER ' is not allowed in meta" ))
39- end
40- end
41-
42- function should_write_resulting_value (key_val:: OptimizationContainerKey )
43- value_type = get_entry_type (key_val)
44- return should_write_resulting_value (value_type)
15+ struct VariableKey{T <: VariableType , U <: InfrastructureSystemsType } < :
16+ OptimizationContainerKey{T, U}
17+ meta:: String
4518end
4619
47- function convert_result_to_natural_units (key:: OptimizationContainerKey )
48- return convert_result_to_natural_units (get_entry_type (key))
20+ struct ConstraintKey{T <: ConstraintType , U <: InfrastructureSystemsType } < :
21+ OptimizationContainerKey{T, U}
22+ meta:: String
4923end
5024
51- # ### VariableKeys ####
52-
53- struct VariableKey{T <: VariableType , U <: InfrastructureSystemsType } < :
54- OptimizationContainerKey
25+ struct AuxVarKey{T <: AuxVariableType , U <: InfrastructureSystemsType } < :
26+ OptimizationContainerKey{T, U}
5527 meta:: String
5628end
5729
58- function VariableKey (
59- :: Type{T} ,
60- :: Type{U} ,
61- meta = CONTAINER_KEY_EMPTY_META,
62- ) where {T <: VariableType , U <: InfrastructureSystemsType }
63- if isabstracttype (U)
64- throw (ArgumentError (" Type $U can't be abstract" ))
65- end
66- check_meta_chars (meta)
67- return VariableKey {T, U} (meta)
30+ struct ParameterKey{T <: ParameterType , U <: InfrastructureSystemsType } < :
31+ OptimizationContainerKey{T, U}
32+ meta:: String
6833end
6934
70- get_entry_type (
71- :: VariableKey{T, U} ,
72- ) where {T <: VariableType , U <: InfrastructureSystemsType } = T
73- get_component_type (
74- :: VariableKey{T, U} ,
75- ) where {T <: VariableType , U <: InfrastructureSystemsType } = U
76-
77- # ### ConstraintKey ####
78-
79- struct ConstraintKey{T <: ConstraintType , U <: InfrastructureSystemsType } < :
80- OptimizationContainerKey
35+ struct InitialConditionKey{T <: InitialConditionType , U <: InfrastructureSystemsType } < :
36+ OptimizationContainerKey{T, U}
8137 meta:: String
8238end
8339
84- function ConstraintKey (
85- :: Type{T} ,
86- :: Type{U} ,
87- meta = CONTAINER_KEY_EMPTY_META,
88- ) where {T <: ConstraintType , U <: InfrastructureSystemsType }
89- check_meta_chars (meta)
90- return ConstraintKey {T, U} (meta)
40+ struct ExpressionKey{T <: ExpressionType , U <: InfrastructureSystemsType } < :
41+ OptimizationContainerKey{T, U}
42+ meta:: String
9143end
9244
9345get_entry_type (
94- :: ConstraintKey {T, U } ,
95- ) where {T <: ConstraintType , U <: InfrastructureSystemsType } = T
46+ :: OptimizationContainerKey {T, <:InfrastructureSystemsType } ,
47+ ) where {T <: OptimizationKeyType } = T
9648get_component_type (
97- :: ConstraintKey{T , U} ,
98- ) where {T <: ConstraintType , U <: InfrastructureSystemsType } = U
49+ :: OptimizationContainerKey{<:OptimizationKeyType , U} ,
50+ ) where {U <: InfrastructureSystemsType } = U
9951
100- function encode_key (key :: ConstraintKey )
101- return encode_symbol ( get_component_type (key), get_entry_type (key), key . meta)
102- end
52+ # okay to construct AuxVarKey with abstract component type, but not others.
53+ maybe_throw_if_abstract ( :: Type{T} , :: Type{U} ) where {T <: OptimizationKeyType , U} =
54+ isabstracttype (U) && throw ( ArgumentError ( " Type $U can't be abstract " ))
10355
104- Base . convert (:: Type{ConstraintKey } , name :: Symbol ) = ConstraintKey ( decode_symbol (name) ... )
56+ maybe_throw_if_abstract (:: Type{AuxVariableType } , :: Type{U} ) where {U} = nothing
10557
106- # ### ExpressionKeys ####
58+ const CONTAINER_KEY_EMPTY_META = " "
10759
108- struct ExpressionKey{T <: ExpressionType , U <: InfrastructureSystemsType } < :
109- OptimizationContainerKey
110- meta:: String
60+ # see https://discourse.julialang.org/t/parametric-constructor-where-type-being-constructed-is-parameter/129866/3
61+ function (M:: Type{S} where {S <: OptimizationContainerKey })(
62+ :: Type{T} ,
63+ :: Type{U} ,
64+ ) where {T <: OptimizationKeyType , U <: InfrastructureSystemsType }
65+ maybe_throw_if_abstract (T, U)
66+ M {T, U} (CONTAINER_KEY_EMPTY_META)
11167end
11268
113- function ExpressionKey (
69+ function (M :: Type{S} where {S <: OptimizationContainerKey }) (
11470 :: Type{T} ,
11571 :: Type{U} ,
116- meta = CONTAINER_KEY_EMPTY_META,
117- ) where {T <: ExpressionType , U <: InfrastructureSystemsType }
118- if isabstracttype (U)
119- throw (ArgumentError (" Type $U can't be abstract" ))
120- end
121- check_meta_chars (meta)
122- return ExpressionKey {T, U} (meta)
72+ meta:: String ,
73+ ) where {T <: OptimizationKeyType , U <: InfrastructureSystemsType }
74+ maybe_throw_if_abstract (S, U)
75+ return M {T, U} (meta)
12376end
12477
125- get_entry_type (
126- :: ExpressionKey{T, U} ,
127- ) where {T <: ExpressionType , U <: InfrastructureSystemsType } = T
128-
129- get_component_type (
130- :: ExpressionKey{T, U} ,
131- ) where {T <: ExpressionType , U <: InfrastructureSystemsType } = U
132-
133- function encode_key (key:: ExpressionKey )
134- return encode_symbol (get_component_type (key), get_entry_type (key), key. meta)
78+ function make_key (
79+ :: Type{S} ,
80+ :: Type{T} ,
81+ :: Type{U} ,
82+ meta:: String = CONTAINER_KEY_EMPTY_META,
83+ ) where {
84+ S <: OptimizationContainerKey ,
85+ T <: OptimizationKeyType ,
86+ U <: InfrastructureSystemsType ,
87+ }
88+ return S {T, U} (meta)
13589end
13690
137- Base. convert (:: Type{ExpressionKey} , name:: Symbol ) = ExpressionKey (decode_symbol (name)... )
138-
139- # ### AuxVariableKeys ####
140-
141- struct AuxVarKey{T <: AuxVariableType , U <: InfrastructureSystemsType } < :
142- OptimizationContainerKey
143- meta:: String
144- end
91+ # ## Encoding keys ###
14592
146- function AuxVarKey (
93+ function encode_symbol (
14794 :: Type{T} ,
14895 :: Type{U} ,
149- meta = CONTAINER_KEY_EMPTY_META,
150- ) where {T <: AuxVariableType , U <: InfrastructureSystemsType }
151- if isabstracttype (U)
152- throw (ArgumentError (" Type $U can't be abstract" ))
153- end
154- return AuxVarKey {T, U} (meta)
96+ meta:: String = CONTAINER_KEY_EMPTY_META,
97+ ) where {T <: OptimizationKeyType , U <: InfrastructureSystemsType }
98+ meta_ = isempty (meta) ? meta : COMPONENT_NAME_DELIMITER * meta
99+ U_ = replace (replace (strip_module_name (U), " {" => COMPONENT_NAME_DELIMITER), " }" => " " )
100+ return Symbol (" $(strip_module_name (string (T)))$(COMPONENT_NAME_DELIMITER)$(U_) " * meta_)
155101end
156102
157- get_entry_type (
158- :: AuxVarKey{T, U} ,
159- ) where {T <: AuxVariableType , U <: InfrastructureSystemsType } = T
160- get_component_type (
161- :: AuxVarKey{T, U} ,
162- ) where {T <: AuxVariableType , U <: InfrastructureSystemsType } = U
163-
164- # ### Initial Conditions Keys ####
165-
166- struct InitialConditionKey{T <: InitialConditionType , U <: InfrastructureSystemsType } < :
167- OptimizationContainerKey
168- meta:: String
103+ function encode_key (
104+ key:: OptimizationContainerKey{T, U} ,
105+ ) where {T <: OptimizationKeyType , U <: InfrastructureSystemsType }
106+ return encode_symbol (T, U, key. meta)
169107end
170108
171- function InitialConditionKey (
172- :: Type{T} ,
173- :: Type{U} ,
174- meta = CONTAINER_KEY_EMPTY_META,
175- ) where {T <: InitialConditionType , U <: InfrastructureSystemsType }
176- if isabstracttype (U )
177- throw (ArgumentError ( " Type $U can't be abstract " ))
109+ encode_key_as_string (key :: OptimizationContainerKey ) = string ( encode_key (key))
110+ encode_keys_as_strings (container_keys) = [ encode_key_as_string (k) for k in container_keys]
111+
112+ function check_meta_chars ( meta:: String )
113+ # Underscores in this field will prevent us from being able to decode keys.
114+ if occursin (COMPONENT_NAME_DELIMITER, meta )
115+ throw (InvalidValue ( " ' $COMPONENT_NAME_DELIMITER ' is not allowed in meta " ))
178116 end
179- return InitialConditionKey {T, U} (meta)
180117end
181118
182- get_entry_type (
183- :: InitialConditionKey{T, U} ,
184- ) where {T <: InitialConditionType , U <: InfrastructureSystemsType } = T
185- get_component_type (
186- :: InitialConditionKey{T, U} ,
187- ) where {T <: InitialConditionType , U <: InfrastructureSystemsType } = U
188-
189- # ### Parameter Keys #####
190- struct ParameterKey{T <: ParameterType , U <: InfrastructureSystemsType } < :
191- OptimizationContainerKey
192- meta:: String
119+ function should_write_resulting_value (
120+ :: OptimizationContainerKey{T, <:InfrastructureSystemsType} ,
121+ ) where {T <: OptimizationKeyType }
122+ return should_write_resulting_value (T)
193123end
194124
195- function ParameterKey (
196- :: Type{T} ,
197- :: Type{U} ,
198- meta = CONTAINER_KEY_EMPTY_META,
199- ) where {T <: ParameterType , U <: InfrastructureSystemsType }
200- if isabstracttype (U)
201- throw (ArgumentError (" Type $U can't be abstract" ))
202- end
203- check_meta_chars (meta)
204- return ParameterKey {T, U} (meta)
125+ function convert_result_to_natural_units (
126+ :: OptimizationContainerKey{T, <:InfrastructureSystemsType} ,
127+ ) where {T <: OptimizationKeyType }
128+ return convert_result_to_natural_units (T)
205129end
206130
207- get_entry_type (
208- :: ParameterKey{T, U} ,
209- ) where {T <: ParameterType , U <: InfrastructureSystemsType } = T
210- get_component_type (
211- :: ParameterKey{T, U} ,
212- ) where {T <: ParameterType , U <: InfrastructureSystemsType } = U
131+ Base. convert (:: Type{ExpressionKey} , name:: Symbol ) = ExpressionKey (decode_symbol (name)... )
132+ Base. convert (:: Type{ConstraintKey} , name:: Symbol ) = ConstraintKey (decode_symbol (name)... )
0 commit comments