Skip to content

Commit e40cd68

Browse files
committed
add event support for mtkmodel components rebase
1 parent 1b858f6 commit e40cd68

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/systems/model_parsing.jl

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ function _model_macro(mod, name, expr, isconnector)
4848
eqs = Expr[]
4949
icon = Ref{Union{String, URI}}()
5050
ps, sps, vs, = [], [], []
51+
c_evts = []
52+
d_evts = []
5153
kwargs = Set()
5254
where_types = Expr[]
5355

@@ -61,7 +63,7 @@ function _model_macro(mod, name, expr, isconnector)
6163
for arg in expr.args
6264
if arg.head == :macrocall
6365
parse_model!(exprs.args, comps, ext, eqs, icon, vs, ps,
64-
sps, dict, mod, arg, kwargs, where_types)
66+
sps, c_evts, d_evts, dict, mod, arg, kwargs, where_types)
6567
elseif arg.head == :block
6668
push!(exprs.args, arg)
6769
elseif arg.head == :if
@@ -116,6 +118,12 @@ function _model_macro(mod, name, expr, isconnector)
116118
isconnector && push!(exprs.args,
117119
:($Setfield.@set!(var"#___sys___".connector_type=$connector_type(var"#___sys___"))))
118120

121+
!(c_evts==[]) && push!(exprs.args,
122+
:($Setfield.@set!(var"#___sys___".continuous_events=$SymbolicContinuousCallback.([$(c_evts...)]))))
123+
124+
!(d_evts==[]) && push!(exprs.args,
125+
:($Setfield.@set!(var"#___sys___".discrete_events=$SymbolicDiscreteCallback.([$(d_evts...)]))))
126+
119127
f = if length(where_types) == 0
120128
:($(Symbol(:__, name, :__))(; name, $(kwargs...)) = $exprs)
121129
else
@@ -124,6 +132,7 @@ function _model_macro(mod, name, expr, isconnector)
124132
:($(Symbol(:__, name, :__))(; name, $(kwargs...))), where_types...)
125133
:($f_with_where = $exprs)
126134
end
135+
127136
:($name = $Model($f, $dict, $isconnector))
128137
end
129138

@@ -341,7 +350,7 @@ function get_var(mod::Module, b)
341350
end
342351
end
343352

344-
function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps,
353+
function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps, c_evts, d_evts,
345354
dict, mod, arg, kwargs, where_types)
346355
mname = arg.args[1]
347356
body = arg.args[end]
@@ -359,6 +368,10 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps,
359368
parse_equations!(exprs, eqs, dict, body)
360369
elseif mname == Symbol("@constants")
361370
parse_constants!(exprs, dict, body, mod)
371+
elseif mname == Symbol("@continuous_events")
372+
parse_continuous_events!(c_evts, dict, body)
373+
elseif mname == Symbol("@discrete_events")
374+
parse_discrete_events!(d_evts, dict, body)
362375
elseif mname == Symbol("@icon")
363376
isassigned(icon) && error("This model has more than one icon.")
364377
parse_icon!(body, dict, icon, mod)
@@ -753,6 +766,23 @@ function parse_equations!(exprs, eqs, dict, body)
753766
end
754767
end
755768

769+
function parse_continuous_events!(c_evts, dict, body)
770+
dict[:continuous_events] = []
771+
Base.remove_linenums!(body)
772+
for arg in body.args
773+
push!(c_evts, arg)
774+
push!(dict[:continuous_events], readable_code.(c_evts)...)
775+
end
776+
end
777+
778+
function parse_discrete_events!(d_evts, dict, body)
779+
dict[:discrete_events] = []
780+
Base.remove_linenums!(body)
781+
for arg in body.args
782+
push!(dict[:discrete_events], readable_code.(d_evts)...)
783+
end
784+
end
785+
756786
function parse_icon!(body::String, dict, icon, mod)
757787
icon_dir = get(ENV, "MTK_ICONS_DIR", joinpath(DEPOT_PATH[1], "mtk_icons"))
758788
dict[:icon] = icon[] = if isfile(body)

0 commit comments

Comments
 (0)