forked from SciML/ModelingToolkit.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_parsing.jl
1209 lines (1105 loc) · 43.9 KB
/
model_parsing.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
$(TYPEDEF)
ModelingToolkit component or connector with metadata
# Fields
$(FIELDS)
"""
struct Model{F, S}
"""The constructor that returns ODESystem."""
f::F
"""
The dictionary with metadata like keyword arguments (:kwargs), base
system this Model extends (:extend), sub-components of the Model (:components),
variables (:variables), parameters (:parameters), structural parameters
(:structural_parameters) and equations (:equations).
"""
structure::S
"""
This flag is `true` when the Model is a connector and is `false` when it is
a component
"""
isconnector::Bool
end
(m::Model)(args...; kw...) = m.f(args...; kw...)
Base.parentmodule(m::Model) = parentmodule(m.f)
for f in (:connector, :mtkmodel)
isconnector = f == :connector ? true : false
@eval begin
macro $f(name::Symbol, body)
esc($(:_model_macro)(__module__, name, body, $isconnector))
end
end
end
flatten_equations(eqs::Vector{Equation}, eq::Equation) = vcat(eqs, [eq])
flatten_equations(eq::Vector{Equation}, eqs::Vector{Equation}) = vcat(eq, eqs)
function flatten_equations(eqs::Vector{Union{Equation, Vector{Equation}}})
foldl(flatten_equations, eqs; init = Equation[])
end
function _model_macro(mod, name, expr, isconnector)
exprs = Expr(:block)
dict = Dict{Symbol, Any}(
:constants => Dict{Symbol, Dict}(),
:defaults => Dict{Symbol, Any}(),
:kwargs => Dict{Symbol, Dict}(),
:structural_parameters => Dict{Symbol, Dict}()
)
comps = Union{Symbol, Expr}[]
ext = Ref{Any}(nothing)
eqs = Expr[]
icon = Ref{Union{String, URI}}()
ps, sps, vs, = [], [], []
c_evts = []
d_evts = []
kwargs = OrderedCollections.OrderedSet()
where_types = Union{Symbol, Expr}[]
push!(exprs.args, :(variables = []))
push!(exprs.args, :(parameters = []))
push!(exprs.args, :(systems = ODESystem[]))
push!(exprs.args, :(equations = Union{Equation, Vector{Equation}}[]))
push!(exprs.args, :(defaults = Dict{Num, Union{Number, Symbol, Function}}()))
Base.remove_linenums!(expr)
for arg in expr.args
if arg.head == :macrocall
parse_model!(exprs.args, comps, ext, eqs, icon, vs, ps,
sps, c_evts, d_evts, dict, mod, arg, kwargs, where_types)
elseif arg.head == :block
push!(exprs.args, arg)
elseif arg.head == :if
MLStyle.@match arg begin
Expr(:if, condition, x) => begin
parse_conditional_model_statements(comps, dict, eqs, exprs, kwargs,
mod, ps, vs, where_types,
parse_top_level_branch(condition, x.args)...)
end
Expr(:if, condition, x, y) => begin
parse_conditional_model_statements(comps, dict, eqs, exprs, kwargs,
mod, ps, vs, where_types,
parse_top_level_branch(condition, x.args, y)...)
end
_ => error("Got an invalid argument: $arg")
end
elseif isconnector
# Connectors can have variables listed without `@variables` prefix or
# begin block.
parse_variable_arg!(
exprs.args, vs, dict, mod, arg, :variables, kwargs, where_types)
else
error("$arg is not valid syntax. Expected a macro call.")
end
end
iv = get(dict, :independent_variable, nothing)
if iv === nothing
iv = dict[:independent_variable] = get_t(mod, :t)
end
push!(exprs.args, :(push!(equations, $(eqs...))))
push!(exprs.args, :(push!(parameters, $(ps...))))
push!(exprs.args, :(push!(systems, $(comps...))))
push!(exprs.args, :(push!(variables, $(vs...))))
gui_metadata = isassigned(icon) > 0 ? GUIMetadata(GlobalRef(mod, name), icon[]) :
GUIMetadata(GlobalRef(mod, name))
@inline pop_structure_dict!.(
Ref(dict), [:constants, :defaults, :kwargs, :structural_parameters])
sys = :($ODESystem($(flatten_equations)(equations), $iv, variables, parameters;
name, systems, gui_metadata = $gui_metadata, defaults))
if ext[] === nothing
push!(exprs.args, :(var"#___sys___" = $sys))
else
push!(exprs.args, :(var"#___sys___" = $extend($sys, $(ext[]))))
end
isconnector && push!(exprs.args,
:($Setfield.@set!(var"#___sys___".connector_type=$connector_type(var"#___sys___"))))
!isempty(c_evts) && push!(exprs.args,
:($Setfield.@set!(var"#___sys___".continuous_events=$SymbolicContinuousCallback.([
$(c_evts...)
]))))
!isempty(d_evts) && push!(exprs.args,
:($Setfield.@set!(var"#___sys___".discrete_events=$SymbolicDiscreteCallback.([
$(d_evts...)
]))))
f = if length(where_types) == 0
:($(Symbol(:__, name, :__))(; name, $(kwargs...)) = $exprs)
else
f_with_where = Expr(:where)
push!(f_with_where.args,
:($(Symbol(:__, name, :__))(; name, $(kwargs...))), where_types...)
:($f_with_where = $exprs)
end
:($name = $Model($f, $dict, $isconnector))
end
pop_structure_dict!(dict, key) = length(dict[key]) == 0 && pop!(dict, key)
function update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var,
varclass, where_types, meta)
if indices isa Nothing
if !isnothing(meta) && haskey(meta, VariableUnit)
uvar = gensym()
push!(where_types, uvar)
push!(kwargs, Expr(:kw, :($a::Union{Nothing, $uvar}), nothing))
else
push!(kwargs, Expr(:kw, :($a::Union{Nothing, $type}), nothing))
end
dict[:kwargs][getname(var)] = Dict(:value => def, :type => type)
else
vartype = gensym(:T)
push!(kwargs,
Expr(:kw,
Expr(:(::), a,
Expr(:curly, :Union, :Nothing, Expr(:curly, :AbstractArray, vartype))),
nothing))
if !isnothing(meta) && haskey(meta, VariableUnit)
push!(where_types, vartype)
else
push!(where_types, :($vartype <: $type))
end
dict[:kwargs][getname(var)] = Dict(:value => def, :type => AbstractArray{type})
end
if dict[varclass] isa Vector
dict[varclass][1][getname(var)][:type] = AbstractArray{type}
else
dict[varclass][getname(var)][:type] = type
end
end
function parse_variable_def!(dict, mod, arg, varclass, kwargs, where_types;
def = nothing, indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing,
type::Type = Real, meta = Dict{DataType, Expr}())
metatypes = [(:connection_type, VariableConnectType),
(:description, VariableDescription),
(:unit, VariableUnit),
(:bounds, VariableBounds),
(:noise, VariableNoiseType),
(:input, VariableInput),
(:output, VariableOutput),
(:irreducible, VariableIrreducible),
(:state_priority, VariableStatePriority),
(:misc, VariableMisc),
(:disturbance, VariableDisturbance),
(:tunable, VariableTunable),
(:dist, VariableDistribution)]
arg isa LineNumberNode && return
MLStyle.@match arg begin
a::Symbol => begin
var = generate_var!(dict, a, varclass; indices, type)
update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var,
varclass, where_types, meta)
return var, def, Dict()
end
Expr(:(::), a, type) => begin
type = getfield(mod, type)
parse_variable_def!(
dict, mod, a, varclass, kwargs, where_types; def, type, meta)
end
Expr(:(::), Expr(:call, a, b), type) => begin
type = getfield(mod, type)
def = _type_check!(def, a, type, varclass)
parse_variable_def!(
dict, mod, a, varclass, kwargs, where_types; def, type, meta)
end
Expr(:call, a, b) => begin
var = generate_var!(dict, a, b, varclass, mod; indices, type)
update_kwargs_and_metadata!(dict, kwargs, a, def, indices, type, var,
varclass, where_types, meta)
return var, def, Dict()
end
Expr(:(=), a, b) => begin
Base.remove_linenums!(b)
def, meta = parse_default(mod, b)
var, def, _ = parse_variable_def!(
dict, mod, a, varclass, kwargs, where_types; def, type, meta)
if dict[varclass] isa Vector
dict[varclass][1][getname(var)][:default] = def
else
dict[varclass][getname(var)][:default] = def
end
if meta !== nothing
for (type, key) in metatypes
if (mt = get(meta, key, nothing)) !== nothing
key == VariableConnectType && (mt = nameof(mt))
if dict[varclass] isa Vector
dict[varclass][1][getname(var)][type] = mt
else
dict[varclass][getname(var)][type] = mt
end
end
end
var, metadata_with_exprs = set_var_metadata(var, meta)
return var, def, metadata_with_exprs
end
return var, def, Dict()
end
Expr(:tuple, a, b) => begin
meta = parse_metadata(mod, b)
var, def, _ = parse_variable_def!(
dict, mod, a, varclass, kwargs, where_types; type, meta)
if meta !== nothing
for (type, key) in metatypes
if (mt = get(meta, key, nothing)) !== nothing
key == VariableConnectType && (mt = nameof(mt))
if dict[varclass] isa Vector
dict[varclass][1][getname(var)][type] = mt
else
dict[varclass][getname(var)][type] = mt
end
end
end
var, metadata_with_exprs = set_var_metadata(var, meta)
return var, def, metadata_with_exprs
end
return var, def, Dict()
end
Expr(:ref, a, b...) => begin
indices = map(i -> UnitRange(i.args[2], i.args[end]), b)
parse_variable_def!(dict, mod, a, varclass, kwargs, where_types;
def, indices, type, meta)
end
_ => error("$arg cannot be parsed")
end
end
function generate_var(a, varclass;
indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing,
type = Real)
var = indices === nothing ? Symbolics.variable(a; T = type) :
first(@variables $a[indices...]::type)
if varclass == :parameters
var = toparam(var)
end
var
end
singular(sym) = last(string(sym)) == 's' ? Symbol(string(sym)[1:(end - 1)]) : sym
function check_name_uniqueness(dict, a, newvarclass)
for varclass in [:variables, :parameters, :structural_parameters, :constants]
dvarclass = get(dict, varclass, nothing)
if dvarclass !== nothing && a in keys(dvarclass)
error("Cannot create a $(singular(newvarclass)) `$(a)` because there is already a $(singular(varclass)) with that name")
end
end
end
function generate_var!(dict, a, varclass;
indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing,
type = Real)
check_name_uniqueness(dict, a, varclass)
vd = get!(dict, varclass) do
Dict{Symbol, Dict{Symbol, Any}}()
end
vd isa Vector && (vd = first(vd))
vd[a] = Dict{Symbol, Any}()
indices !== nothing && (vd[a][:size] = Tuple(lastindex.(indices)))
generate_var(a, varclass; indices, type)
end
function generate_var!(dict, a, b, varclass, mod;
indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing,
type = Real)
iv = b == :t ? get_t(mod, b) : generate_var(b, :variables)
prev_iv = get!(dict, :independent_variable) do
iv
end
@assert isequal(iv, prev_iv) "Multiple independent variables are used in the model"
check_name_uniqueness(dict, a, varclass)
vd = get!(dict, varclass) do
Dict{Symbol, Dict{Symbol, Any}}()
end
vd isa Vector && (vd = first(vd))
vd[a] = Dict{Symbol, Any}()
var = if indices === nothing
Symbolics.variable(a, T = SymbolicUtils.FnType{Tuple{Any}, type})(iv)
else
vd[a][:size] = Tuple(lastindex.(indices))
first(@variables $a(iv)[indices...]::type)
end
if varclass == :parameters
var = toparam(var)
end
var
end
# Use the `t` defined in the `mod`. When it is unavailable, generate a new `t` with a warning.
function get_t(mod, t)
try
get_var(mod, t)
catch e
if e isa UndefVarError
@warn("Could not find a predefined `t` in `$mod`; generating a new one within this model.\nConsider defining it or importing `t` (or `t_nounits`, `t_unitful` as `t`) from ModelingToolkit.")
variable(:t)
else
throw(e)
end
end
end
function parse_default(mod, a)
a = Base.remove_linenums!(deepcopy(a))
MLStyle.@match a begin
Expr(:block, x) => parse_default(mod, x)
Expr(:tuple, x, y) => begin
def, _ = parse_default(mod, x)
meta = parse_metadata(mod, y)
(def, meta)
end
::Symbol || ::Number => (a, nothing)
Expr(:call, a...) => begin
def = parse_default.(Ref(mod), a)
expr = Expr(:call)
for (d, _) in def
push!(expr.args, d)
end
(expr, nothing)
end
Expr(:if, condition, x, y) => (a, nothing)
Expr(:vect, x...) => begin
(a, nothing)
end
_ => error("Cannot parse default $a $(typeof(a))")
end
end
function parse_metadata(mod, a)
MLStyle.@match a begin
Expr(:vect, eles...) => Dict(parse_metadata(mod, e) for e in eles)
Expr(:(=), a, b) => Symbolics.option_to_metadata_type(Val(a)) => get_var(mod, b)
_ => error("Cannot parse metadata $a")
end
end
function _set_var_metadata!(metadata_with_exprs, a, m, v::Expr)
push!(metadata_with_exprs, m => v)
a
end
function _set_var_metadata!(metadata_with_exprs, a, m, v)
wrap(set_scalar_metadata(unwrap(a), m, v))
end
function set_var_metadata(a, ms)
metadata_with_exprs = Dict{DataType, Expr}()
for (m, v) in ms
if m == VariableGuess && v isa Symbol
v = quote
$v
end
end
a = _set_var_metadata!(metadata_with_exprs, a, m, v)
end
a, metadata_with_exprs
end
function get_var(mod::Module, b)
if b isa Symbol
isdefined(mod, b) && return getproperty(mod, b)
isdefined(@__MODULE__, b) && return getproperty(@__MODULE__, b)
end
b
end
function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps, c_evts, d_evts,
dict, mod, arg, kwargs, where_types)
mname = arg.args[1]
body = arg.args[end]
if mname == Symbol("@components")
parse_components!(exprs, comps, dict, body, kwargs)
elseif mname == Symbol("@extend")
parse_extend!(exprs, ext, dict, mod, body, kwargs)
elseif mname == Symbol("@variables")
parse_variables!(exprs, vs, dict, mod, body, :variables, kwargs, where_types)
elseif mname == Symbol("@parameters")
parse_variables!(exprs, ps, dict, mod, body, :parameters, kwargs, where_types)
elseif mname == Symbol("@structural_parameters")
parse_structural_parameters!(exprs, sps, dict, mod, body, kwargs)
elseif mname == Symbol("@equations")
parse_equations!(exprs, eqs, dict, body)
elseif mname == Symbol("@constants")
parse_constants!(exprs, dict, body, mod)
elseif mname == Symbol("@continuous_events")
parse_continuous_events!(c_evts, dict, body)
elseif mname == Symbol("@discrete_events")
parse_discrete_events!(d_evts, dict, body)
elseif mname == Symbol("@icon")
isassigned(icon) && error("This model has more than one icon.")
parse_icon!(body, dict, icon, mod)
elseif mname == Symbol("@defaults")
parse_system_defaults!(exprs, arg, dict)
else
error("$mname is not handled.")
end
end
function parse_constants!(exprs, dict, body, mod)
Base.remove_linenums!(body)
for arg in body.args
MLStyle.@match arg begin
Expr(:(=), Expr(:(::), a, type), Expr(:tuple, b, metadata)) || Expr(:(=), Expr(:(::), a, type), b) => begin
type = getfield(mod, type)
b = _type_check!(get_var(mod, b), a, type, :constants)
push!(exprs,
:($(Symbolics._parse_vars(
:constants, type, [:($a = $b), metadata], toconstant))))
dict[:constants][a] = Dict(:value => b, :type => type)
if @isdefined metadata
for data in metadata.args
dict[:constants][a][data.args[1]] = data.args[2]
end
end
end
Expr(:(=), a, Expr(:tuple, b, metadata)) => begin
push!(exprs,
:($(Symbolics._parse_vars(
:constants, Real, [:($a = $b), metadata], toconstant))))
dict[:constants][a] = Dict{Symbol, Any}(:value => get_var(mod, b))
for data in metadata.args
dict[:constants][a][data.args[1]] = data.args[2]
end
end
Expr(:(=), a, b) => begin
push!(exprs,
:($(Symbolics._parse_vars(
:constants, Real, [:($a = $b)], toconstant))))
dict[:constants][a] = Dict(:value => get_var(mod, b))
end
_ => error("""Malformed constant definition `$arg`. Please use the following syntax:
```
@constants begin
var = value, [description = "This is an example constant."]
end
```
""")
end
end
end
push_additional_defaults!(dict, a, b::Number) = dict[:defaults][a] = b
push_additional_defaults!(dict, a, b::QuoteNode) = dict[:defaults][a] = b.value
function push_additional_defaults!(dict, a, b::Expr)
dict[:defaults][a] = readable_code(b)
end
function parse_system_defaults!(exprs, defaults_body, dict)
for default_arg in defaults_body.args[end].args
# for arg in default_arg.args
MLStyle.@match default_arg begin
# For cases like `p => 1` and `p => f()`. In both cases the definitions of
# `a`, here `p` and when `b` is a function, here `f` are available while
# defining the model
Expr(:call, :(=>), a, b) => begin
push!(exprs, :(defaults[$a] = $b))
push_additional_defaults!(dict, a, b)
end
_ => error("Invalid `defaults` entry $default_arg $(typeof(a)) $(typeof(b))")
end
end
end
function parse_structural_parameters!(exprs, sps, dict, mod, body, kwargs)
Base.remove_linenums!(body)
for arg in body.args
MLStyle.@match arg begin
Expr(:(=), Expr(:(::), a, type), b) => begin
type = getfield(mod, type)
b = _type_check!(get_var(mod, b), a, type, :structural_parameters)
push!(sps, a)
push!(kwargs, Expr(:kw, Expr(:(::), a, type), b))
dict[:structural_parameters][a] = dict[:kwargs][a] = Dict(
:value => b, :type => type)
end
Expr(:(=), a, b) => begin
push!(sps, a)
push!(kwargs, Expr(:kw, a, b))
dict[:structural_parameters][a] = dict[:kwargs][a] = Dict(:value => b)
end
a => begin
push!(sps, a)
push!(kwargs, a)
dict[:structural_parameters][a] = dict[:kwargs][a] = Dict(:value => nothing)
end
end
end
end
function extend_args!(a, b, dict, expr, kwargs, varexpr, has_param = false)
# Whenever `b` is a function call, skip the first arg aka the function name.
# Whenever it is a kwargs list, include it.
start = b.head == :call ? 2 : 1
for i in start:lastindex(b.args)
arg = b.args[i]
arg isa LineNumberNode && continue
MLStyle.@match arg begin
x::Symbol => begin
if b.head != :parameters
if has_param
popat!(b.args, i)
push!(b.args[2].args, x)
else
b.args[i] = Expr(:parameters, x)
end
end
push!(kwargs, Expr(:kw, x, nothing))
dict[:kwargs][x] = Dict(:value => nothing)
end
Expr(:kw, x) => begin
push!(kwargs, Expr(:kw, x, nothing))
dict[:kwargs][x] = Dict(:value => nothing)
end
Expr(:kw, x, y) => begin
b.args[i] = Expr(:kw, x, x)
push!(varexpr.args, :($x = $x === nothing ? $y : $x))
push!(kwargs, Expr(:kw, x, nothing))
dict[:kwargs][x] = Dict(:value => nothing)
end
Expr(:parameters, x...) => begin
has_param = true
extend_args!(a, arg, dict, expr, kwargs, varexpr, has_param)
end
_ => error("Could not parse $arg of component $a")
end
end
end
const EMPTY_DICT = Dict()
const EMPTY_VoVoSYMBOL = Vector{Symbol}[]
function Base.names(model::Model)
vars = keys(get(model.structure, :variables, EMPTY_DICT))
vars = union(vars, keys(get(model.structure, :parameters, EMPTY_DICT)))
vars = union(vars,
map(first, get(model.structure, :components, EMPTY_VoVoSYMBOL)))
collect(vars)
end
function _parse_extend!(ext, a, b, dict, expr, kwargs, varexpr, vars)
extend_args!(a, b, dict, expr, kwargs, varexpr)
ext[] = a
push!(b.args, Expr(:kw, :name, Meta.quot(a)))
push!(expr.args, :($a = $b))
dict[:extend] = [Symbol.(vars.args), a, b.args[1]]
push!(expr.args, :(@unpack $vars = $a))
end
function parse_extend!(exprs, ext, dict, mod, body, kwargs)
expr = Expr(:block)
varexpr = Expr(:block)
push!(exprs, varexpr)
push!(exprs, expr)
body = deepcopy(body)
MLStyle.@match body begin
Expr(:(=), a, b) => begin
if Meta.isexpr(b, :(=))
vars = a
if !Meta.isexpr(vars, :tuple)
error("`@extend` destructuring only takes an tuple as LHS. Got $body")
end
a, b = b.args
_parse_extend!(ext, a, b, dict, expr, kwargs, varexpr, vars)
else
error("When explicitly destructing in `@extend` please use the syntax: `@extend a, b = oneport = OnePort()`.")
end
end
Expr(:call, a′, _...) => begin
a = Symbol(Symbol("#mtkmodel"), :__anonymous__, a′)
b = body
if (model = getproperty(mod, b.args[1])) isa Model
vars = Expr(:tuple)
append!(vars.args, names(model))
_parse_extend!(ext, a, b, dict, expr, kwargs, varexpr, vars)
else
error("Cannot infer the exact `Model` that `@extend $(body)` refers." *
" Please specify the names that it brings into scope by:" *
" `@extend a, b = oneport = OnePort()`.")
end
end
_ => error("`@extend` only takes an assignment expression. Got $body")
end
return nothing
end
function parse_variable_arg!(exprs, vs, dict, mod, arg, varclass, kwargs, where_types)
name, ex = parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types)
push!(vs, name)
push!(exprs, ex)
end
function convert_units(varunits::DynamicQuantities.Quantity, value)
DynamicQuantities.ustrip(DynamicQuantities.uconvert(
DynamicQuantities.SymbolicUnits.as_quantity(varunits), value))
end
function convert_units(
varunits::DynamicQuantities.Quantity, value::AbstractArray{T}) where {T}
DynamicQuantities.ustrip.(DynamicQuantities.uconvert.(
DynamicQuantities.SymbolicUnits.as_quantity(varunits), value))
end
function convert_units(varunits::Unitful.FreeUnits, value)
Unitful.ustrip(varunits, value)
end
function convert_units(varunits::Unitful.FreeUnits, value::AbstractArray{T}) where {T}
Unitful.ustrip.(varunits, value)
end
function parse_variable_arg(dict, mod, arg, varclass, kwargs, where_types)
vv, def, metadata_with_exprs = parse_variable_def!(
dict, mod, arg, varclass, kwargs, where_types)
name = getname(vv)
varexpr = if haskey(metadata_with_exprs, VariableUnit)
unit = metadata_with_exprs[VariableUnit]
quote
$name = if $name === nothing
$setdefault($vv, $def)
else
try
$setdefault($vv, $convert_units($unit, $name))
catch e
if isa(e, $(DynamicQuantities.DimensionError)) ||
isa(e, $(Unitful.DimensionError))
error("Unable to convert units for \'" * string(:($$vv)) * "\'")
elseif isa(e, MethodError)
error("No or invalid units provided for \'" * string(:($$vv)) *
"\'")
else
rethrow(e)
end
end
end
end
else
quote
$name = if $name === nothing
$setdefault($vv, $def)
else
$setdefault($vv, $name)
end
end
end
metadata_expr = Expr(:block)
for (k, v) in metadata_with_exprs
push!(metadata_expr.args,
:($name = $wrap($set_scalar_metadata($unwrap($name), $k, $v))))
end
push!(varexpr.args, metadata_expr)
return vv isa Num ? name : :($name...), varexpr
end
function handle_conditional_vars!(
arg, conditional_branch, mod, varclass, kwargs, where_types)
conditional_dict = Dict(:kwargs => Dict(),
:parameters => Any[Dict{Symbol, Dict{Symbol, Any}}()],
:variables => Any[Dict{Symbol, Dict{Symbol, Any}}()])
for _arg in arg.args
name, ex = parse_variable_arg(
conditional_dict, mod, _arg, varclass, kwargs, where_types)
push!(conditional_branch.args, ex)
push!(conditional_branch.args, :(push!($varclass, $name)))
end
conditional_dict
end
function prune_conditional_dict!(conditional_tuple::Tuple)
prune_conditional_dict!.(collect(conditional_tuple))
end
function prune_conditional_dict!(conditional_dict::Dict)
for k in [:parameters, :variables]
length(conditional_dict[k]) == 1 && isempty(first(conditional_dict[k])) &&
delete!(conditional_dict, k)
end
isempty(conditional_dict[:kwargs]) && delete!(conditional_dict, :kwargs)
end
prune_conditional_dict!(_) = return nothing
function get_conditional_dict!(conditional_dict, conditional_y_tuple::Tuple)
k = get_conditional_dict!.(Ref(conditional_dict), collect(conditional_y_tuple))
push_something!(conditional_dict,
k...)
conditional_dict
end
function get_conditional_dict!(conditional_dict::Dict, conditional_y_tuple::Dict)
merge!(conditional_dict[:kwargs], conditional_y_tuple[:kwargs])
for key in [:parameters, :variables]
merge!(conditional_dict[key][1], conditional_y_tuple[key][1])
end
conditional_dict
end
get_conditional_dict!(a, b) = (return nothing)
function push_conditional_dict!(dict, condition, conditional_dict,
conditional_y_tuple, varclass)
vd = get!(dict, varclass) do
Dict{Symbol, Dict{Symbol, Any}}()
end
for k in keys(conditional_dict[varclass][1])
vd[k] = copy(conditional_dict[varclass][1][k])
vd[k][:condition] = (:if, condition, conditional_dict, conditional_y_tuple)
end
conditional_y_dict = Dict(:kwargs => Dict(),
:parameters => Any[Dict{Symbol, Dict{Symbol, Any}}()],
:variables => Any[Dict{Symbol, Dict{Symbol, Any}}()])
get_conditional_dict!(conditional_y_dict, conditional_y_tuple)
prune_conditional_dict!(conditional_y_dict)
prune_conditional_dict!(conditional_dict)
!isempty(conditional_y_dict) && for k in keys(conditional_y_dict[varclass][1])
vd[k] = copy(conditional_y_dict[varclass][1][k])
vd[k][:condition] = (:if, condition, conditional_dict, conditional_y_tuple)
end
end
function parse_variables!(exprs, vs, dict, mod, body, varclass, kwargs, where_types)
expr = Expr(:block)
push!(exprs, expr)
for arg in body.args
arg isa LineNumberNode && continue
MLStyle.@match arg begin
Expr(:if, condition, x) => begin
conditional_expr = Expr(:if, condition, Expr(:block))
conditional_dict = handle_conditional_vars!(x,
conditional_expr.args[2],
mod,
varclass,
kwargs,
where_types)
push!(expr.args, conditional_expr)
push_conditional_dict!(dict, condition, conditional_dict, nothing, varclass)
end
Expr(:if, condition, x, y) => begin
conditional_expr = Expr(:if, condition, Expr(:block))
conditional_dict = handle_conditional_vars!(x,
conditional_expr.args[2],
mod,
varclass,
kwargs,
where_types)
conditional_y_expr, conditional_y_tuple = handle_y_vars(y,
conditional_dict,
mod,
varclass,
kwargs, where_types)
push!(conditional_expr.args, conditional_y_expr)
push!(expr.args, conditional_expr)
push_conditional_dict!(dict,
condition,
conditional_dict,
conditional_y_tuple,
varclass)
end
_ => parse_variable_arg!(
exprs, vs, dict, mod, arg, varclass, kwargs, where_types)
end
end
end
function handle_y_vars(y, dict, mod, varclass, kwargs, where_types)
conditional_dict = if Meta.isexpr(y, :elseif)
conditional_y_expr = Expr(:elseif, y.args[1], Expr(:block))
conditional_dict = handle_conditional_vars!(y.args[2],
conditional_y_expr.args[2],
mod,
varclass,
kwargs,
where_types)
_y_expr, _conditional_dict = handle_y_vars(
y.args[end], dict, mod, varclass, kwargs, where_types)
push!(conditional_y_expr.args, _y_expr)
(:elseif, y.args[1], conditional_dict, _conditional_dict)
else
conditional_y_expr = Expr(:block)
handle_conditional_vars!(y, conditional_y_expr, mod, varclass, kwargs, where_types)
end
conditional_y_expr, conditional_dict
end
function handle_if_x_equations!(condition, dict, ifexpr, x)
if Meta.isexpr(x, :block)
push!(ifexpr.args, condition, :(push!(equations, $(x.args...))))
return readable_code.(x.args)
else
push!(ifexpr.args, condition, :(push!(equations, $x)))
return readable_code(x)
end
# push!(dict[:equations], [:if, readable_code(condition), readable_code.(x.args)])
end
function handle_if_y_equations!(ifexpr, y, dict)
if y.head == :elseif
elseifexpr = Expr(:elseif)
eq_entry = [:elseif, readable_code.(y.args[1].args)...]
push!(eq_entry, handle_if_x_equations!(y.args[1], dict, elseifexpr, y.args[2]))
get(y.args, 3, nothing) !== nothing &&
push!(eq_entry, handle_if_y_equations!(elseifexpr, y.args[3], dict))
push!(ifexpr.args, elseifexpr)
(eq_entry...,)
else
if Meta.isexpr(y, :block)
push!(ifexpr.args, :(push!(equations, $(y.args...))))
else
push!(ifexpr.args, :(push!(equations, $(y))))
end
readable_code.(y.args)
end
end
function parse_equations!(exprs, eqs, dict, body)
dict[:equations] = []
Base.remove_linenums!(body)
for arg in body.args
MLStyle.@match arg begin
Expr(:if, condition, x) => begin
ifexpr = Expr(:if)
eq_entry = handle_if_x_equations!(condition, dict, ifexpr, x)
push!(exprs, ifexpr)
push!(dict[:equations], (:if, condition, eq_entry))
end
Expr(:if, condition, x, y) => begin
ifexpr = Expr(:if)
xeq_entry = handle_if_x_equations!(condition, dict, ifexpr, x)
yeq_entry = handle_if_y_equations!(ifexpr, y, dict)
push!(exprs, ifexpr)
push!(dict[:equations], (:if, condition, xeq_entry, yeq_entry))
end
_ => begin
push!(eqs, arg)
push!(dict[:equations], readable_code.(eqs)...)
end
end
end
end
function parse_continuous_events!(c_evts, dict, body)
dict[:continuous_events] = []
Base.remove_linenums!(body)
for arg in body.args
push!(c_evts, arg)
push!(dict[:continuous_events], readable_code.(c_evts)...)
end
end
function parse_discrete_events!(d_evts, dict, body)
dict[:discrete_events] = []
Base.remove_linenums!(body)
for arg in body.args
push!(d_evts, arg)
push!(dict[:discrete_events], readable_code.(d_evts)...)
end
end
function parse_icon!(body::String, dict, icon, mod)
icon_dir = get(ENV, "MTK_ICONS_DIR", joinpath(DEPOT_PATH[1], "mtk_icons"))
dict[:icon] = icon[] = if isfile(body)
URI("file:///" * abspath(body))
elseif (iconpath = joinpath(icon_dir, body); isfile(iconpath))
URI("file:///" * abspath(iconpath))
elseif try
Base.isvalid(URI(body))
catch e
false
end
URI(body)
elseif (_body = lstrip(body); startswith(_body, r"<\?xml|<svg"))
String(_body) # With Julia-1.10 promoting `SubString{String}` to `String` can be dropped.
else
error("\n$body is not a valid icon")
end
end
function parse_icon!(body::Symbol, dict, icon, mod)
parse_icon!(getfield(mod, body), dict, icon, mod)
end
### Parsing Components:
function component_args!(a, b, varexpr, kwargs; index_name = nothing)
# Whenever `b` is a function call, skip the first arg aka the function name.
# Whenever it is a kwargs list, include it.
start = b.head == :call ? 2 : 1
for i in start:lastindex(b.args)
arg = b.args[i]
arg isa LineNumberNode && continue
MLStyle.@match arg begin
x::Symbol || Expr(:kw, x) => begin
varname, _varname = _rename(a, x)
b.args[i] = Expr(:kw, x, _varname)
push!(varexpr.args, :((if $varname !== nothing
$_varname = $varname
elseif @isdefined $x
# Allow users to define a var in `structural_parameters` and set
# that as positional arg of subcomponents; it is useful for cases
# where it needs to be passed to multiple subcomponents.
$_varname = $x
end)))
push!(kwargs, Expr(:kw, varname, nothing))
# dict[:kwargs][varname] = nothing
end
Expr(:parameters, x...) => begin
component_args!(a, arg, varexpr, kwargs)
end
Expr(:kw, x, y) => begin
varname, _varname = _rename(a, x)
b.args[i] = Expr(:kw, x, _varname)
if isnothing(index_name)
push!(varexpr.args, :($_varname = $varname === nothing ? $y : $varname))
else
push!(varexpr.args,
:($_varname = $varname === nothing ? $y : $varname[$index_name]))
end
push!(kwargs, Expr(:kw, varname, nothing))
# dict[:kwargs][varname] = nothing
end
_ => error("Could not parse $arg of component $a")
end
end
end
model_name(name, range) = Symbol.(name, :_, collect(range))
function _parse_components!(body, kwargs)
local expr
varexpr = Expr(:block)
comps = Vector{Union{Union{Expr, Symbol}, Expr}}[]
comp_names = []
Base.remove_linenums!(body)
arg = body.args[end]
MLStyle.@match arg begin
Expr(:(=), a, Expr(:comprehension, Expr(:generator, b, Expr(:(=), c, d)))) => begin
array_varexpr = Expr(:block)
push!(comp_names, :($a...))
push!(comps, [a, b.args[1], d])
b = deepcopy(b)
component_args!(a, b, array_varexpr, kwargs; index_name = c)