Skip to content

Commit 20eb7ca

Browse files
committed
feat: add helpers to update symbolic array metadata
1 parent c614856 commit 20eb7ca

File tree

2 files changed

+63
-6
lines changed

2 files changed

+63
-6
lines changed

src/systems/model_parsing.jl

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ function update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var,
158158
else
159159
push!(kwargs, Expr(:kw, :($a::Union{Nothing, $type}), nothing))
160160
end
161-
dict[:kwargs][getname(var)] = Dict(:value => def, :type => type)
161+
dict[:kwargs][a] = Dict(:value => def, :type => type)
162162
else
163163
vartype = gensym(:T)
164164
push!(kwargs,
@@ -171,15 +171,70 @@ function update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var,
171171
else
172172
push!(where_types, :($vartype <: $type))
173173
end
174-
dict[:kwargs][getname(var)] = Dict(:value => def, :type => AbstractArray{type})
174+
dict[:kwargs][a] = Dict(:value => def, :type => AbstractArray{type})
175175
end
176176
if dict[varclass] isa Vector
177-
dict[varclass][1][getname(var)][:type] = AbstractArray{type}
177+
dict[varclass][1][a][:type] = AbstractArray{type}
178178
else
179-
dict[varclass][getname(var)][:type] = type
179+
dict[varclass][a][:type] = type
180180
end
181181
end
182182

183+
function update_readable_metadata!(varclass_dict, meta::Dict, varname)
184+
metatypes = [(:connection_type, VariableConnectType),
185+
(:description, VariableDescription),
186+
(:unit, VariableUnit),
187+
(:bounds, VariableBounds),
188+
(:noise, VariableNoiseType),
189+
(:input, VariableInput),
190+
(:output, VariableOutput),
191+
(:irreducible, VariableIrreducible),
192+
(:state_priority, VariableStatePriority),
193+
(:misc, VariableMisc),
194+
(:disturbance, VariableDisturbance),
195+
(:tunable, VariableTunable),
196+
(:dist, VariableDistribution)]
197+
198+
199+
var_dict = get!(varclass_dict, varname) do
200+
Dict{Symbol, Any}()
201+
end
202+
203+
for (type, key) in metatypes
204+
if (mt = get(meta, key, nothing)) !== nothing
205+
key == VariableConnectType && (mt = nameof(mt))
206+
var_dict[type] = mt
207+
end
208+
end
209+
end
210+
211+
function push_array_kwargs_and_metadata!(dict, indices, meta, type, varclass, varname, varval)
212+
dict[varclass] = get!(dict, varclass) do
213+
Dict{Symbol, Dict{Symbol, Any}}()
214+
end
215+
varclass_dict = dict[varclass] isa Vector ? Ref(dict[varclass][1]) : Ref(dict[varclass])
216+
217+
merge!(varclass_dict[], Dict(varname => Dict(
218+
:size => tuple([index_arg.args[end] for index_arg in indices]...),
219+
:value => varval,
220+
:type => type
221+
)))
222+
223+
# Useful keys for kwargs entry are: value, type and size.
224+
dict[:kwargs][varname] = varclass_dict[][varname]
225+
226+
meta !== nothing && update_readable_metadata!(varclass_dict[], meta, varname)
227+
end
228+
229+
function unit_handled_variable_value(meta, varname)
230+
varval = if meta isa Nothing || get(meta, VariableUnit, nothing) isa Nothing
231+
varname
232+
else
233+
:($convert_units($(meta[VariableUnit]), $varname))
234+
end
235+
return varval
236+
end
237+
183238
function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
184239
def = nothing, indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing,
185240
type::Type = Real, meta = Dict{DataType, Expr}())

test/model_parsing.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ end
259259
@test all(collect(hasmetadata.(model.l, ModelingToolkit.VariableDescription)))
260260

261261
@test all(lastindex.([model.a2, model.b2, model.d2, model.e2, model.h2]) .== 2)
262-
@test size(model.l) == MockModel.structure[:parameters][:l][:size] == (2, 3)
262+
@test size(model.l) == (2, 3)
263+
@test MockModel.structure[:parameters][:l][:size] == (2, 3)
263264

264265
model = complete(model)
265266
@test getdefault(model.cval) == 1
@@ -474,7 +475,8 @@ using ModelingToolkit: getdefault, scalarize
474475

475476
@named model_with_component_array = ModelWithComponentArray()
476477

477-
@test eval(ModelWithComponentArray.structure[:parameters][:r][:unit]) == eval(u"")
478+
@test eval(ModelWithComponentArray.structure[:parameters][:r][:unit]) ==
479+
eval(u"")
478480
@test lastindex(parameters(model_with_component_array)) == 3
479481

480482
# Test the constant `k`. Manually k's value should be kept in sync here

0 commit comments

Comments
 (0)