Skip to content

Commit 6c77f89

Browse files
committed
feat: update metadata dict and kwargs for symbolic arrays
1 parent 20eb7ca commit 6c77f89

File tree

1 file changed

+70
-40
lines changed

1 file changed

+70
-40
lines changed

src/systems/model_parsing.jl

Lines changed: 70 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -236,21 +236,8 @@ function unit_handled_variable_value(meta, varname)
236236
end
237237

238238
function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
239-
def = nothing, indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing,
240-
type::Type = Real, meta = Dict{DataType, Expr}())
241-
metatypes = [(:connection_type, VariableConnectType),
242-
(:description, VariableDescription),
243-
(:unit, VariableUnit),
244-
(:bounds, VariableBounds),
245-
(:noise, VariableNoiseType),
246-
(:input, VariableInput),
247-
(:output, VariableOutput),
248-
(:irreducible, VariableIrreducible),
249-
(:state_priority, VariableStatePriority),
250-
(:misc, VariableMisc),
251-
(:disturbance, VariableDisturbance),
252-
(:tunable, VariableTunable),
253-
(:dist, VariableDistribution)]
239+
def = nothing, indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing,
240+
type::Type = Real, meta = Dict{DataType, Expr}())
254241

255242
arg isa LineNumberNode && return
256243
MLStyle.@match arg begin
@@ -277,27 +264,78 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
277264
varclass, where_types, meta)
278265
return var, def, Dict()
279266
end
267+
Expr(:tuple, Expr(:(::), Expr(:ref, a, indices...), type), meta_val) ||
268+
Expr(:tuple, Expr(:ref, a, indices...), meta_val) => begin
269+
(@isdefined type) || (type = Real)
270+
varname = Meta.isexpr(a, :call) ? a.args[1] : a
271+
push!(kwargs, Expr(:kw, varname, nothing))
272+
meta = parse_metadata(mod, meta_val)
273+
varval = (@isdefined default_val) ? default_val : unit_handled_variable_value(meta, varname)
274+
if varclass == :parameters
275+
var = :($varname = $first(@parameters ($a[$(indices...)]::$type = $varval), $meta_val))
276+
else
277+
var = :($varname = $first(@variables ($a[$(indices)]::$type = $varval), $meta_val))
278+
end
279+
push_array_kwargs_and_metadata!(dict, indices, meta, type, varclass, varname, varval)
280+
(:($varname...), var), nothing, Dict()
281+
end
282+
Expr(:(=), Expr(:(::), Expr(:ref, a, indices...), type), def_n_meta) ||
283+
Expr(:(=), Expr(:ref, a, indices...), def_n_meta) => begin
284+
(@isdefined type) || (type = Real)
285+
varname = Meta.isexpr(a, :call) ? a.args[1] : a
286+
if Meta.isexpr(def_n_meta, :tuple)
287+
meta = parse_metadata(mod, def_n_meta)
288+
varval = unit_handled_variable_value(meta, varname)
289+
val, def_n_meta = (def_n_meta.args[1], def_n_meta.args[2:end])
290+
push!(kwargs, Expr(:kw, varname, nothing))
291+
if varclass == :parameters
292+
var = :($varname = $varname === nothing ? $val : $varname;
293+
$varname = $first(@parameters ($a[$(indices...)]::$type =
294+
$varval), $(def_n_meta...)))
295+
else
296+
var = :($varname = $varname === nothing ? $val : $varname;
297+
$varname = $first(@variables $a[$(indices...)]::$type = (
298+
$varval), $(def_n_meta...)))
299+
end
300+
else
301+
push!(kwargs, Expr(:kw, varname, nothing))
302+
if varclass == :parameters
303+
var = :($varname = $varname === nothing ? $def_n_meta : $varname;
304+
$varname = $first(@parameters $a[$(indices...)]::$type = $varname))
305+
else
306+
var = :($varname = $varname === nothing ? $def_n_meta : $varname;
307+
$varname = $first(@variables $a[$(indices...)]::$type = $varname))
308+
end
309+
varval, meta = def_n_meta, nothing
310+
end
311+
push_array_kwargs_and_metadata!(dict, indices, meta, type, varclass, varname, varval)
312+
(:($varname...), var), nothing, Dict()
313+
end
314+
Expr(:(::), Expr(:ref, a, indices...), type) ||
315+
Expr(:ref, a, indices...) => begin
316+
(@isdefined type) || (type = Real)
317+
varname = a isa Expr && a.head == :call ? a.args[1] : a
318+
push!(kwargs, Expr(:kw, varname, nothing))
319+
if varclass == :parameters
320+
var = :($varname = $first(@parameters $a[$(indices...)]::$type = $varname))
321+
elseif varclass == :variables
322+
var = :($varname = $first(@variables $a[$(indices...)]::$type = $varname))
323+
else
324+
throw("Symbolic array with arbitrary length is not handled for $varclass.
325+
Please open an issue with an example.")
326+
end
327+
push_array_kwargs_and_metadata!(dict, indices, nothing, type, varclass, varname, nothing)
328+
(:($varname...), var), nothing, Dict()
329+
end
280330
Expr(:(=), a, b) => begin
281331
Base.remove_linenums!(b)
282332
def, meta = parse_default(mod, b)
283333
var, def, _ = parse_variable_def!(
284334
dict, mod, a, varclass, kwargs, where_types; def, type, meta)
285-
if dict[varclass] isa Vector
286-
dict[varclass][1][getname(var)][:default] = def
287-
else
288-
dict[varclass][getname(var)][:default] = def
289-
end
335+
varclass_dict = dict[varclass] isa Vector ? Ref(dict[varclass][1]) : Ref(dict[varclass])
336+
varclass_dict[][getname(var)][:default] = def
290337
if meta !== nothing
291-
for (type, key) in metatypes
292-
if (mt = get(meta, key, nothing)) !== nothing
293-
key == VariableConnectType && (mt = nameof(mt))
294-
if dict[varclass] isa Vector
295-
dict[varclass][1][getname(var)][type] = mt
296-
else
297-
dict[varclass][getname(var)][type] = mt
298-
end
299-
end
300-
end
338+
update_readable_metadata!(varclass_dict[], meta, getname(var))
301339
var, metadata_with_exprs = set_var_metadata(var, meta)
302340
return var, def, metadata_with_exprs
303341
end
@@ -307,17 +345,9 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
307345
meta = parse_metadata(mod, b)
308346
var, def, _ = parse_variable_def!(
309347
dict, mod, a, varclass, kwargs, where_types; type, meta)
348+
varclass_dict = dict[varclass] isa Vector ? Ref(dict[varclass][1]) : Ref(dict[varclass])
310349
if meta !== nothing
311-
for (type, key) in metatypes
312-
if (mt = get(meta, key, nothing)) !== nothing
313-
key == VariableConnectType && (mt = nameof(mt))
314-
if dict[varclass] isa Vector
315-
dict[varclass][1][getname(var)][type] = mt
316-
else
317-
dict[varclass][getname(var)][type] = mt
318-
end
319-
end
320-
end
350+
update_readable_metadata!(varclass_dict[], meta, getname(var))
321351
var, metadata_with_exprs = set_var_metadata(var, meta)
322352
return var, def, metadata_with_exprs
323353
end

0 commit comments

Comments
 (0)