@@ -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
@@ -117,16 +119,19 @@ function _model_macro(mod, fullname::Union{Expr, Symbol}, expr, isconnector)
117119 push! (exprs. args, :(push! (systems, $ (comps... ))))
118120 push! (exprs. args, :(push! (variables, $ (vs... ))))
119121
122+
120123 gui_metadata = isassigned (icon) > 0 ? GUIMetadata (GlobalRef (mod, name), icon[]) :
121124 GUIMetadata (GlobalRef (mod, name))
122125
126+ consolidate = get (dict, :consolidate , nothing )
123127 description = get (dict, :description , " " )
124128
125129 @inline pop_structure_dict! .(
126130 Ref (dict), [:constants , :defaults , :kwargs , :structural_parameters ])
127131
128132 sys = :($ type ($ (flatten_equations)(equations), $ iv, variables, parameters;
129- name, description = $ description, systems, gui_metadata = $ gui_metadata, defaults))
133+ name, description = $ description, systems, gui_metadata = $ gui_metadata, defaults,
134+ costs = [$ (costs... )], constraints = [$ (cons... )], consolidate = $ consolidate))
130135
131136 if length (ext) == 0
132137 push! (exprs. args, :(var"#___sys___" = $ sys))
@@ -610,9 +615,10 @@ function get_var(mod::Module, b)
610615end
611616
612617function parse_model! (exprs, comps, ext, eqs, icon, vs, ps, sps, c_evts, d_evts,
613- dict, mod, arg, kwargs, where_types)
618+ cons, costs, dict, mod, arg, kwargs, where_types)
614619 mname = arg. args[1 ]
615620 body = arg. args[end ]
621+ @show dict
616622 if mname == Symbol (" @description" )
617623 parse_description! (body, dict)
618624 elseif mname == Symbol (" @components" )
@@ -637,7 +643,13 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps, c_evts, d_evts,
637643 isassigned (icon) && error (" This model has more than one icon." )
638644 parse_icon! (body, dict, icon, mod)
639645 elseif mname == Symbol (" @defaults" )
640- parse_system_defaults! (exprs, arg, dict)
646+ parse_system_defaults! (exprs, dict, body)
647+ elseif mname == Symbol (" @constraints" )
648+ parse_costs! (cons, dict, body)
649+ elseif mname == Symbol (" @costs" )
650+ parse_constraints! (costs, dict, body)
651+ elseif mname == Symbol (" @consolidate" )
652+ parse_consolidate! (body, dict)
641653 else
642654 error (" $mname is not handled." )
643655 end
@@ -1149,6 +1161,33 @@ function parse_discrete_events!(d_evts, dict, body)
11491161 end
11501162end
11511163
1164+ function parse_constraints! (cons, dict, body)
1165+ dict[:constraints ] = []
1166+ Base. remove_linenums! (body)
1167+ for arg in body. args
1168+ push! (cons, arg)
1169+ push! (dict[:constraints ], readable_code .(cons)... )
1170+ end
1171+ end
1172+
1173+ function parse_costs! (costs, dict, body)
1174+ @show dict
1175+ dict[:costs ] = []
1176+ Base. remove_linenums! (body)
1177+ for arg in body. args
1178+ push! (costs, arg)
1179+ push! (dict[:costs ], readable_code .(costs)... )
1180+ end
1181+ end
1182+
1183+ function parse_consolidate! (body, dict)
1184+ if ! (occursin (" ->" , string (body)) || occursin (" =" , string (body)))
1185+ error (" Consolidate must be a function definition." )
1186+ else
1187+ dict[:consolidate ] = body
1188+ end
1189+ end
1190+
11521191function parse_icon! (body:: String , dict, icon, mod)
11531192 icon_dir = get (ENV , " MTK_ICONS_DIR" , joinpath (DEPOT_PATH [1 ], " mtk_icons" ))
11541193 dict[:icon ] = icon[] = if isfile (body)
0 commit comments