Skip to content

Commit fbcb985

Browse files
committed
add event support for mtkmodel components
1 parent 3fbbdb0 commit fbcb985

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

src/systems/model_parsing.jl

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ function _model_macro(mod, name, expr, isconnector)
4242
eqs = Expr[]
4343
icon = Ref{Union{String, URI}}()
4444
ps, sps, vs, = [], [], []
45+
c_evts = []
46+
d_evts = []
4547
kwargs = Set()
4648

4749
push!(exprs.args, :(variables = []))
@@ -53,7 +55,7 @@ function _model_macro(mod, name, expr, isconnector)
5355
for arg in expr.args
5456
if arg.head == :macrocall
5557
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)
5759
elseif arg.head == :block
5860
push!(exprs.args, arg)
5961
elseif arg.head == :if
@@ -90,6 +92,7 @@ function _model_macro(mod, name, expr, isconnector)
9092
gui_metadata = isassigned(icon) > 0 ? GUIMetadata(GlobalRef(mod, name), icon[]) :
9193
GUIMetadata(GlobalRef(mod, name))
9294

95+
9396
sys = :($ODESystem($Equation[equations...], $iv, variables, parameters;
9497
name, systems, gui_metadata = $gui_metadata))
9598

@@ -102,6 +105,13 @@ function _model_macro(mod, name, expr, isconnector)
102105
isconnector && push!(exprs.args,
103106
:($Setfield.@set!(var"#___sys___".connector_type=$connector_type(var"#___sys___"))))
104107

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+
105115
f = :($(Symbol(:__, name, :__))(; name, $(kwargs...)) = $exprs)
106116
:($name = $Model($f, $dict, $isconnector))
107117
end
@@ -278,7 +288,7 @@ function get_var(mod::Module, b)
278288
end
279289
end
280290

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,
282292
dict, mod, arg, kwargs)
283293
mname = arg.args[1]
284294
body = arg.args[end]
@@ -294,6 +304,10 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps,
294304
parse_structural_parameters!(exprs, sps, dict, mod, body, kwargs)
295305
elseif mname == Symbol("@equations")
296306
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)
297311
elseif mname == Symbol("@icon")
298312
isassigned(icon) && error("This model has more than one icon.")
299313
parse_icon!(body, dict, icon, mod)
@@ -609,6 +623,23 @@ function parse_equations!(exprs, eqs, dict, body)
609623
end
610624
end
611625

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+
612643
function parse_icon!(body::String, dict, icon, mod)
613644
icon_dir = get(ENV, "MTK_ICONS_DIR", joinpath(DEPOT_PATH[1], "mtk_icons"))
614645
dict[:icon] = icon[] = if isfile(body)

0 commit comments

Comments
 (0)