Skip to content

Commit eeebcf1

Browse files
committedDec 21, 2023
fix: remove evals from model parsing
- Ensure that modules consisting MTKModels with component arrays and icons of `Expr` type and `unit` metadata can be precompiled.
1 parent 14679d3 commit eeebcf1

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed
 

‎format/Project.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[deps]
2+
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"

‎src/systems/model_parsing.jl

+6-11
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,10 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs;
178178
(var, def)
179179
end
180180
Expr(:ref, a, b...) => begin
181+
indices = map(i -> UnitRange(i.args[2], i.args[end]), b)
181182
parse_variable_def!(dict, mod, a, varclass, kwargs;
182-
def, indices = [eval.(b)...])
183+
def, indices)
183184
end
184-
#= Expr(:if, condition, a) => begin
185-
var, def = [], []
186-
for var_def in a.args
187-
parse_variable_def!(dict, mod, var_def, varclass, kwargs)
188-
end
189-
end =#
190185
_ => error("$arg cannot be parsed")
191186
end
192187
end
@@ -301,7 +296,7 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps,
301296
parse_equations!(exprs, eqs, dict, body)
302297
elseif mname == Symbol("@icon")
303298
isassigned(icon) && error("This model has more than one icon.")
304-
parse_icon!(icon, dict, body)
299+
parse_icon!(body, dict, icon, mod)
305300
else
306301
error("$mname is not handled.")
307302
end
@@ -614,7 +609,7 @@ function parse_equations!(exprs, eqs, dict, body)
614609
end
615610
end
616611

617-
function parse_icon!(icon, dict, body::String)
612+
function parse_icon!(body::String, dict, icon, mod)
618613
icon_dir = get(ENV, "MTK_ICONS_DIR", joinpath(DEPOT_PATH[1], "mtk_icons"))
619614
dict[:icon] = icon[] = if isfile(body)
620615
URI("file:///" * abspath(body))
@@ -633,8 +628,8 @@ function parse_icon!(icon, dict, body::String)
633628
end
634629
end
635630

636-
function parse_icon!(icon, dict, body::Expr)
637-
parse_icon!(icon, dict, eval(body))
631+
function parse_icon!(body::Symbol, dict, icon, mod)
632+
parse_icon!(getfield(mod, body), dict, icon, mod)
638633
end
639634

640635
### Parsing Components:

‎test/model_parsing.jl

+18-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ export Pin
1818
@icon "pin.png"
1919
end
2020

21+
ground_logo = read(abspath(ENV["MTK_ICONS_DIR"], "ground.svg"), String)
2122
@mtkmodel Ground begin
2223
@components begin
2324
g = Pin()
2425
end
25-
@icon read(abspath(ENV["MTK_ICONS_DIR"], "ground.svg"), String)
26+
@icon ground_logo
2627
@equations begin
2728
g.v ~ 0
2829
end
@@ -164,8 +165,7 @@ resistor = getproperty(rc, :resistor; namespace = false)
164165

165166
@test get_gui_metadata(rc.resistor).layout == Resistor.structure[:icon] ==
166167
read(joinpath(ENV["MTK_ICONS_DIR"], "resistor.svg"), String)
167-
@test get_gui_metadata(rc.ground).layout ==
168-
read(abspath(ENV["MTK_ICONS_DIR"], "ground.svg"), String)
168+
@test get_gui_metadata(rc.ground).layout == read(abspath(ENV["MTK_ICONS_DIR"], "ground.svg"), String)
169169
@test get_gui_metadata(rc.capacitor).layout ==
170170
URI("https://upload.wikimedia.org/wikipedia/commons/7/78/Capacitor_symbol.svg")
171171
@test OnePort.structure[:icon] ==
@@ -321,6 +321,21 @@ end
321321
@test A.structure[:components] == [[:cc, :C]]
322322
end
323323

324+
# Ensure that modules consisting MTKModels with component arrays and icons of
325+
# `Expr` type and `unit` metadata can be precompiled.
326+
@testset "Precompile packages with MTKModels" begin
327+
push!(LOAD_PATH, joinpath(@__DIR__, "precompile_test"))
328+
329+
using ModelParsingPrecompile: ModelWithComponentArray
330+
331+
@named model_with_component_array = ModelWithComponentArray()
332+
333+
@test ModelWithComponentArray.structure[:parameters][:R][:unit] == u""
334+
@test lastindex(parameters(model_with_component_array)) == 3
335+
336+
pop!(LOAD_PATH)
337+
end
338+
324339
@testset "Conditional statements inside the blocks" begin
325340
@mtkmodel C begin end
326341

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module ModelParsingPrecompile
2+
3+
using ModelingToolkit
4+
using Unitful
5+
6+
@mtkmodel ModelWithComponentArray begin
7+
@parameters begin
8+
R(t)[1:3] = 1, [description = "Parameter array", unit = u""]
9+
end
10+
end
11+
12+
end

0 commit comments

Comments
 (0)
Please sign in to comment.