Skip to content

Commit 7b9a234

Browse files
committed
feat: update the vartype of kwargs rel. to symbolic-array
While here, removes passing `indices` around to generate_var, update_kwargs_and_metadata! and parse_variable_def! as these no longer handles symbolic-arrays.
1 parent 84bf015 commit 7b9a234

File tree

1 file changed

+36
-48
lines changed

1 file changed

+36
-48
lines changed

src/systems/model_parsing.jl

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -151,34 +151,18 @@ pop_structure_dict!(dict, key) = length(dict[key]) == 0 && pop!(dict, key)
151151
struct NoValue end
152152
const NO_VALUE = NoValue()
153153

154-
function update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var,
154+
function update_kwargs_and_metadata!(dict, kwargs, a, def, type,
155155
varclass, where_types, meta)
156-
if indices isa Nothing
157-
if !isnothing(meta) && haskey(meta, VariableUnit)
158-
uvar = gensym()
159-
push!(where_types, uvar)
160-
push!(kwargs,
161-
Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $uvar}), NO_VALUE))
162-
else
163-
push!(kwargs,
164-
Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $type}), NO_VALUE))
165-
end
166-
dict[:kwargs][a] = Dict(:value => def, :type => type)
156+
if !isnothing(meta) && haskey(meta, VariableUnit)
157+
uvar = gensym()
158+
push!(where_types, uvar)
159+
push!(kwargs,
160+
Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $uvar}), NO_VALUE))
167161
else
168-
vartype = gensym(:T)
169162
push!(kwargs,
170-
Expr(:kw,
171-
Expr(:(::), a,
172-
Expr(:curly, :Union, :Nothing, :Missing, NoValue,
173-
Expr(:curly, :AbstractArray, vartype))),
174-
NO_VALUE))
175-
if !isnothing(meta) && haskey(meta, VariableUnit)
176-
push!(where_types, vartype)
177-
else
178-
push!(where_types, :($vartype <: $type))
179-
end
180-
dict[:kwargs][a] = Dict(:value => def, :type => AbstractArray{type})
163+
Expr(:kw, :($a::Union{Nothing, Missing, $NoValue, $type}), NO_VALUE))
181164
end
165+
dict[:kwargs][a] = Dict(:value => def, :type => type)
182166
if dict[varclass] isa Vector
183167
dict[varclass][1][a][:type] = AbstractArray{type}
184168
else
@@ -213,8 +197,8 @@ function update_readable_metadata!(varclass_dict, meta::Dict, varname)
213197
end
214198
end
215199

216-
function push_array_kwargs_and_metadata!(
217-
dict, indices, meta, type, varclass, varname, varval)
200+
function update_array_kwargs_and_metadata!(
201+
dict, indices, kwargs, meta, type, varclass, varname, varval, where_types)
218202
dict[varclass] = get!(dict, varclass) do
219203
Dict{Symbol, Dict{Symbol, Any}}()
220204
end
@@ -227,6 +211,18 @@ function push_array_kwargs_and_metadata!(
227211
:type => type
228212
)))
229213

214+
vartype = gensym(:T)
215+
push!(kwargs,
216+
Expr(:kw,
217+
Expr(:(::), varname,
218+
Expr(:curly, :Union, :Nothing, Expr(:curly, :AbstractArray, vartype))),
219+
nothing))
220+
if !isnothing(meta) && haskey(meta, VariableUnit)
221+
push!(where_types, vartype)
222+
else
223+
push!(where_types, :($vartype <: $type))
224+
end
225+
230226
# Useful keys for kwargs entry are: value, type and size.
231227
dict[:kwargs][varname] = varclass_dict[][varname]
232228

@@ -243,13 +239,12 @@ function unit_handled_variable_value(meta, varname)
243239
end
244240

245241
function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
246-
def = nothing, indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing,
247-
type::Type = Real, meta = Dict{DataType, Expr}())
242+
def = nothing, type::Type = Real, meta = Dict{DataType, Expr}())
248243
arg isa LineNumberNode && return
249244
MLStyle.@match arg begin
250245
a::Symbol => begin
251-
var = generate_var!(dict, a, varclass; indices, type)
252-
update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var,
246+
var = generate_var!(dict, a, varclass; type)
247+
update_kwargs_and_metadata!(dict, kwargs, a, def, type,
253248
varclass, where_types, meta)
254249
return var, def, Dict()
255250
end
@@ -265,8 +260,8 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
265260
dict, mod, a, varclass, kwargs, where_types; def, type, meta)
266261
end
267262
Expr(:call, a, b) => begin
268-
var = generate_var!(dict, a, b, varclass, mod; indices, type)
269-
update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var,
263+
var = generate_var!(dict, a, b, varclass, mod; type)
264+
update_kwargs_and_metadata!(dict, kwargs, a, def, type,
270265
varclass, where_types, meta)
271266
return var, def, Dict()
272267
end
@@ -276,7 +271,6 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
276271
Expr(:tuple, Expr(:ref, a, indices...), meta_val) => begin
277272
(@isdefined type) || (type = Real)
278273
varname = Meta.isexpr(a, :call) ? a.args[1] : a
279-
push!(kwargs, Expr(:kw, varname, nothing))
280274
meta = parse_metadata(mod, meta_val)
281275
varval = (@isdefined default_val) ? default_val :
282276
unit_handled_variable_value(meta, varname)
@@ -291,8 +285,8 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
291285
var = :($varname = $first(@variables ($a[$(indices)]::$type = $varval),
292286
$meta_val))
293287
end
294-
push_array_kwargs_and_metadata!(
295-
dict, indices, meta, type, varclass, varname, varval)
288+
update_array_kwargs_and_metadata!(
289+
dict, indices, kwargs, meta, type, varclass, varname, varval, where_types)
296290
(:($varname...), var), nothing, Dict()
297291
end
298292
Expr(:(=), Expr(:(::), Expr(:ref, a, indices...), type), def_n_meta) ||
@@ -303,7 +297,6 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
303297
meta = parse_metadata(mod, def_n_meta)
304298
varval = unit_handled_variable_value(meta, varname)
305299
val, def_n_meta = (def_n_meta.args[1], def_n_meta.args[2:end])
306-
push!(kwargs, Expr(:kw, varname, nothing))
307300
if varclass == :parameters
308301
Meta.isexpr(a, :call) &&
309302
assert_unique_independent_var(dict, a.args[end])
@@ -320,7 +313,6 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
320313
$(def_n_meta...)))
321314
end
322315
else
323-
push!(kwargs, Expr(:kw, varname, nothing))
324316
if varclass == :parameters
325317
Meta.isexpr(a, :call) &&
326318
assert_unique_independent_var(dict, a.args[end])
@@ -335,15 +327,14 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
335327
end
336328
varval, meta = def_n_meta, nothing
337329
end
338-
push_array_kwargs_and_metadata!(
339-
dict, indices, meta, type, varclass, varname, varval)
330+
update_array_kwargs_and_metadata!(
331+
dict, indices, kwargs, meta, type, varclass, varname, varval, where_types)
340332
(:($varname...), var), nothing, Dict()
341333
end
342334
Expr(:(::), Expr(:ref, a, indices...), type) ||
343335
Expr(:ref, a, indices...) => begin
344336
(@isdefined type) || (type = Real)
345337
varname = a isa Expr && a.head == :call ? a.args[1] : a
346-
push!(kwargs, Expr(:kw, varname, nothing))
347338
if varclass == :parameters
348339
Meta.isexpr(a, :call) && assert_unique_independent_var(dict, a.args[end])
349340
var = :($varname = $first(@parameters $a[$(indices...)]::$type = $varname))
@@ -356,8 +347,8 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
356347
throw("Symbolic array with arbitrary length is not handled for $varclass.
357348
Please open an issue with an example.")
358349
end
359-
push_array_kwargs_and_metadata!(
360-
dict, indices, nothing, type, varclass, varname, nothing)
350+
update_array_kwargs_and_metadata!(
351+
dict, indices, kwargs, nothing, type, varclass, varname, nothing, where_types)
361352
(:($varname...), var), nothing, Dict()
362353
end
363354
Expr(:(=), a, b) => begin
@@ -392,11 +383,8 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
392383
end
393384
end
394385

395-
function generate_var(a, varclass;
396-
indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing,
397-
type = Real)
398-
var = indices === nothing ? Symbolics.variable(a; T = type) :
399-
first(@variables $a[indices...]::type)
386+
function generate_var(a, varclass; type = Real)
387+
var = Symbolics.variable(a; T = type)
400388
if varclass == :parameters
401389
var = toparam(var)
402390
elseif varclass == :independent_variables
@@ -426,7 +414,7 @@ function generate_var!(dict, a, varclass;
426414
vd isa Vector && (vd = first(vd))
427415
vd[a] = Dict{Symbol, Any}()
428416
indices !== nothing && (vd[a][:size] = Tuple(lastindex.(indices)))
429-
generate_var(a, varclass; indices, type)
417+
generate_var(a, varclass; type)
430418
end
431419

432420
function assert_unique_independent_var(dict, iv::Num)

0 commit comments

Comments
 (0)