@@ -42,6 +42,8 @@ function _model_macro(mod, name, expr, isconnector)
42
42
eqs = Expr[]
43
43
icon = Ref {Union{String, URI}} ()
44
44
ps, sps, vs, = [], [], []
45
+ c_evts = []
46
+ d_evts = []
45
47
kwargs = Set ()
46
48
47
49
push! (exprs. args, :(variables = []))
@@ -53,7 +55,7 @@ function _model_macro(mod, name, expr, isconnector)
53
55
for arg in expr. args
54
56
if arg. head == :macrocall
55
57
parse_model! (exprs. args, comps, ext, eqs, icon, vs, ps,
56
- sps, dict, mod, arg, kwargs)
58
+ sps, c_evts, d_evts, dict, mod, arg, kwargs)
57
59
elseif arg. head == :block
58
60
push! (exprs. args, arg)
59
61
elseif arg. head == :if
@@ -90,6 +92,7 @@ function _model_macro(mod, name, expr, isconnector)
90
92
gui_metadata = isassigned (icon) > 0 ? GUIMetadata (GlobalRef (mod, name), icon[]) :
91
93
GUIMetadata (GlobalRef (mod, name))
92
94
95
+
93
96
sys = :($ ODESystem ($ Equation[equations... ], $ iv, variables, parameters;
94
97
name, systems, gui_metadata = $ gui_metadata))
95
98
@@ -102,6 +105,13 @@ function _model_macro(mod, name, expr, isconnector)
102
105
isconnector && push! (exprs. args,
103
106
:($ Setfield. @set! (var"#___sys___" . connector_type= $ connector_type (var"#___sys___" ))))
104
107
108
+ ! (c_evts== []) && push! (exprs. args,
109
+ :($ Setfield. @set! (var"#___sys___" . continuous_events= $ SymbolicContinuousCallback .([$ (c_evts... )]))))
110
+
111
+ ! (d_evts== []) && push! (exprs. args,
112
+ :($ Setfield. @set! (var"#___sys___" . discrete_events= $ SymbolicDiscreteCallback .([$ (d_evts... )]))))
113
+
114
+
105
115
f = :($ (Symbol (:__ , name, :__ ))(; name, $ (kwargs... )) = $ exprs)
106
116
:($ name = $ Model ($ f, $ dict, $ isconnector))
107
117
end
@@ -278,7 +288,7 @@ function get_var(mod::Module, b)
278
288
end
279
289
end
280
290
281
- function parse_model! (exprs, comps, ext, eqs, icon, vs, ps, sps,
291
+ function parse_model! (exprs, comps, ext, eqs, icon, vs, ps, sps, c_evts, d_evts,
282
292
dict, mod, arg, kwargs)
283
293
mname = arg. args[1 ]
284
294
body = arg. args[end ]
@@ -294,6 +304,10 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps,
294
304
parse_structural_parameters! (exprs, sps, dict, mod, body, kwargs)
295
305
elseif mname == Symbol (" @equations" )
296
306
parse_equations! (exprs, eqs, dict, body)
307
+ elseif mname == Symbol (" @continuous_events" )
308
+ parse_continuous_events! (c_evts, dict, body)
309
+ elseif mname == Symbol (" @discrete_events" )
310
+ parse_discrete_events! (d_evts, dict, body)
297
311
elseif mname == Symbol (" @icon" )
298
312
isassigned (icon) && error (" This model has more than one icon." )
299
313
parse_icon! (body, dict, icon, mod)
@@ -609,6 +623,23 @@ function parse_equations!(exprs, eqs, dict, body)
609
623
end
610
624
end
611
625
626
+ function parse_continuous_events! (c_evts, dict, body)
627
+ dict[:continuous_events ] = []
628
+ Base. remove_linenums! (body)
629
+ for arg in body. args
630
+ push! (c_evts, arg)
631
+ push! (dict[:continuous_events ], readable_code .(c_evts)... )
632
+ end
633
+ end
634
+
635
+ function parse_discrete_events! (d_evts, dict, body)
636
+ dict[:discrete_events ] = []
637
+ Base. remove_linenums! (body)
638
+ for arg in body. args
639
+ push! (dict[:discrete_events ], readable_code .(d_evts)... )
640
+ end
641
+ end
642
+
612
643
function parse_icon! (body:: String , dict, icon, mod)
613
644
icon_dir = get (ENV , " MTK_ICONS_DIR" , joinpath (DEPOT_PATH [1 ], " mtk_icons" ))
614
645
dict[:icon ] = icon[] = if isfile (body)
0 commit comments