Skip to content

Commit 9b51671

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

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

src/systems/model_parsing.jl

Lines changed: 60 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,71 @@ 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+
var_dict = get!(varclass_dict, varname) do
199+
Dict{Symbol, Any}()
200+
end
201+
202+
for (type, key) in metatypes
203+
if (mt = get(meta, key, nothing)) !== nothing
204+
key == VariableConnectType && (mt = nameof(mt))
205+
var_dict[type] = mt
206+
end
207+
end
208+
end
209+
210+
function push_array_kwargs_and_metadata!(
211+
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[],
218+
Dict(varname => Dict(
219+
:size => tuple([index_arg.args[end] for index_arg in indices]...),
220+
:value => varval,
221+
:type => type
222+
)))
223+
224+
# Useful keys for kwargs entry are: value, type and size.
225+
dict[:kwargs][varname] = varclass_dict[][varname]
226+
227+
meta !== nothing && update_readable_metadata!(varclass_dict[], meta, varname)
228+
end
229+
230+
function unit_handled_variable_value(meta, varname)
231+
varval = if meta isa Nothing || get(meta, VariableUnit, nothing) isa Nothing
232+
varname
233+
else
234+
:($convert_units($(meta[VariableUnit]), $varname))
235+
end
236+
return varval
237+
end
238+
183239
function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
184240
def = nothing, indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing,
185241
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)