@@ -65,6 +65,8 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector)
6565 ps, sps, vs, = [], [], []
6666 c_evts = []
6767 d_evts = []
68+ cons = []
69+ costs = []
6870 kwargs = OrderedCollections. OrderedSet ()
6971 where_types = Union{Symbol, Expr}[]
7072
@@ -80,7 +82,7 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector)
8082 for arg in expr. args
8183 if arg. head == :macrocall
8284 parse_model! (exprs. args, comps, ext, eqs, icon, vs, ps,
83- sps, c_evts, d_evts, dict, mod, arg, kwargs, where_types)
85+ sps, c_evts, d_evts, cons, costs, dict, mod, arg, kwargs, where_types)
8486 elseif arg. head == :block
8587 push! (exprs. args, arg)
8688 elseif arg. head == :if
@@ -120,13 +122,15 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector)
120122 gui_metadata = isassigned (icon) > 0 ? GUIMetadata (GlobalRef (mod, name), icon[]) :
121123 GUIMetadata (GlobalRef (mod, name))
122124
125+ consolidate = get (dict, :consolidate , nothing )
123126 description = get (dict, :description , " " )
124127
125128 @inline pop_structure_dict! .(
126129 Ref (dict), [:constants , :defaults , :kwargs , :structural_parameters ])
127130
128131 sys = :($ type ($ (flatten_equations)(equations), $ iv, variables, parameters;
129- name, description = $ description, systems, gui_metadata = $ gui_metadata, defaults))
132+ name, description = $ description, systems, gui_metadata = $ gui_metadata, defaults,
133+ costs = [$ (costs... )], constraints = [$ (cons... )], consolidate = $ consolidate))
130134
131135 if length (ext) == 0
132136 push! (exprs. args, :(var"#___sys___" = $ sys))
@@ -610,7 +614,7 @@ function get_var(mod::Module, b)
610614end
611615
612616function parse_model! (exprs, comps, ext, eqs, icon, vs, ps, sps, c_evts, d_evts,
613- dict, mod, arg, kwargs, where_types)
617+ cons, costs, dict, mod, arg, kwargs, where_types)
614618 mname = arg. args[1 ]
615619 body = arg. args[end ]
616620 if mname == Symbol (" @description" )
@@ -638,6 +642,12 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps, c_evts, d_evts,
638642 parse_icon! (body, dict, icon, mod)
639643 elseif mname == Symbol (" @defaults" )
640644 parse_system_defaults! (exprs, arg, dict)
645+ elseif mname == Symbol (" @constraints" )
646+ parse_costs! (cons, dict, body)
647+ elseif mname == Symbol (" @costs" )
648+ parse_constraints! (costs, dict, body)
649+ elseif mname == Symbol (" @consolidate" )
650+ parse_consolidate! (body, dict)
641651 else
642652 error (" $mname is not handled." )
643653 end
@@ -1149,6 +1159,32 @@ function parse_discrete_events!(d_evts, dict, body)
11491159 end
11501160end
11511161
1162+ function parse_constraints! (cons, dict, body)
1163+ dict[:constraints ] = []
1164+ Base. remove_linenums! (body)
1165+ for arg in body. args
1166+ push! (cons, arg)
1167+ push! (dict[:constraints ], readable_code .(cons)... )
1168+ end
1169+ end
1170+
1171+ function parse_costs! (costs, dict, body)
1172+ dict[:costs ] = []
1173+ Base. remove_linenums! (body)
1174+ for arg in body. args
1175+ push! (costs, arg)
1176+ push! (dict[:costs ], readable_code .(costs)... )
1177+ end
1178+ end
1179+
1180+ function parse_consolidate! (body, dict)
1181+ if ! (occursin (" ->" , string (body)) || occursin (" =" , string (body)))
1182+ error (" Consolidate must be a function definition." )
1183+ else
1184+ dict[:consolidate ] = body
1185+ end
1186+ end
1187+
11521188function parse_icon! (body:: String , dict, icon, mod)
11531189 icon_dir = get (ENV , " MTK_ICONS_DIR" , joinpath (DEPOT_PATH [1 ], " mtk_icons" ))
11541190 dict[:icon ] = icon[] = if isfile (body)
0 commit comments