Skip to content

Commit 01aa166

Browse files
authored
Merge pull request #2242 from ven-k/vkb/eval-metadata-expr
Evaluate metadata expr in `@mtkmodel`
2 parents 12c83d4 + c76c9ae commit 01aa166

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/systems/model_parsing.jl

+7-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,13 @@ function set_var_metadata(a, ms)
198198
end
199199

200200
function get_var(mod::Module, b)
201-
b isa Symbol ? getproperty(mod, b) : b
201+
if b isa Symbol
202+
getproperty(mod, b)
203+
elseif b isa Expr
204+
Core.eval(mod, b)
205+
else
206+
b
207+
end
202208
end
203209

204210
function mtkmodel_macro(mod, name, expr)

test/model_parsing.jl

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
using ModelingToolkit, Test
22
using ModelingToolkit: get_gui_metadata, VariableDescription, getdefault
33
using URIs: URI
4+
using Distributions
5+
using Unitful
46

57
ENV["MTK_ICONS_DIR"] = "$(@__DIR__)/icons"
68

79
@connector RealInput begin
8-
u(t), [input = true]
10+
u(t), [input = true, unit = u"V"]
911
end
1012
@connector RealOutput begin
11-
u(t), [output = true]
13+
u(t), [output = true, unit = u"V"]
1214
end
1315
@mtkmodel Constant begin
1416
@components begin
@@ -22,12 +24,12 @@ end
2224
end
2325
end
2426

25-
@variables t
27+
@variables t [unit = u"s"]
2628
D = Differential(t)
2729

2830
@connector Pin begin
29-
v(t) # Potential at the pin [V]
30-
i(t), [connect = Flow] # Current flowing into the pin [A]
31+
v(t), [unit = u"V"] # Potential at the pin [V]
32+
i(t), [connect = Flow, unit = u"A"] # Current flowing into the pin [A]
3133
@icon "pin.png"
3234
end
3335

@@ -41,8 +43,8 @@ end
4143
n = Pin()
4244
end
4345
@variables begin
44-
v(t)
45-
i(t)
46+
v(t), [unit = u"V"]
47+
i(t), [unit = u"A"]
4648
end
4749
@icon "oneport.png"
4850
@equations begin
@@ -70,7 +72,7 @@ resistor_log = "$(@__DIR__)/logo/resistor.svg"
7072
@mtkmodel Resistor begin
7173
@extend v, i = oneport = OnePort()
7274
@parameters begin
73-
R
75+
R, [unit = u""]
7476
end
7577
@icon begin
7678
"""<?xml version="1.0" encoding="UTF-8"?>
@@ -95,7 +97,7 @@ end
9597

9698
@mtkmodel Capacitor begin
9799
@parameters begin
98-
C
100+
C, [unit = u"F"]
99101
end
100102
@extend v, i = oneport = OnePort(; v = 0.0)
101103
@icon "https://upload.wikimedia.org/wikipedia/commons/7/78/Capacitor_symbol.svg"
@@ -218,8 +220,8 @@ getdefault(a.b.j) == 30
218220
getdefault(a.b.k) == 40
219221

220222
metadata = Dict(:description => "Variable to test metadata in the Model.structure",
221-
:input => true, :bounds => :((-1, 1)), :connection_type => :Flow, :integer => true,
222-
:binary => false, :tunable => false, :disturbance => true, :dist => :(Normal(1, 1)))
223+
:input => true, :bounds => (-1, 1), :connection_type => :Flow, :integer => true,
224+
:binary => false, :tunable => false, :disturbance => true, :dist => Normal(1, 1))
223225

224226
@connector MockMeta begin
225227
m(t),

0 commit comments

Comments
 (0)