Skip to content

Commit ae22c71

Browse files
committed
feat: add helpers to update symbolic array metadata
1 parent 816fde7 commit ae22c71

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
@@ -163,7 +163,7 @@ function update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var,
163163
push!(kwargs,
164164
Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $type}), NO_VALUE))
165165
end
166-
dict[:kwargs][getname(var)] = Dict(:value => def, :type => type)
166+
dict[:kwargs][a] = Dict(:value => def, :type => type)
167167
else
168168
vartype = gensym(:T)
169169
push!(kwargs,
@@ -177,15 +177,71 @@ function update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var,
177177
else
178178
push!(where_types, :($vartype <: $type))
179179
end
180-
dict[:kwargs][getname(var)] = Dict(:value => def, :type => AbstractArray{type})
180+
dict[:kwargs][a] = Dict(:value => def, :type => AbstractArray{type})
181181
end
182182
if dict[varclass] isa Vector
183-
dict[varclass][1][getname(var)][:type] = AbstractArray{type}
183+
dict[varclass][1][a][:type] = AbstractArray{type}
184184
else
185-
dict[varclass][getname(var)][:type] = type
185+
dict[varclass][a][:type] = type
186186
end
187187
end
188188

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