Skip to content

Commit ce7eed0

Browse files
Merge pull request #2330 from thazhemadam/at/fix-model-parsing-mtkmodel-from-libs
feat: support components with fully-qualified names with `@mtkmodel`
2 parents 493e695 + d0b2e33 commit ce7eed0

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

src/systems/model_parsing.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ function _parse_components!(exprs, body, kwargs)
673673
expr = Expr(:block)
674674
varexpr = Expr(:block)
675675
# push!(exprs, varexpr)
676-
comps = Vector{Symbol}[]
676+
comps = Vector{Union{Symbol, Expr}}[]
677677
comp_names = []
678678

679679
for arg in body.args
@@ -692,7 +692,9 @@ function _parse_components!(exprs, body, kwargs)
692692
arg.args[2] = b
693693
push!(expr.args, arg)
694694
push!(comp_names, a)
695-
push!(comps, [a, b.args[1]])
695+
if (isa(b.args[1], Symbol) || Meta.isexpr(b.args[1], :.))
696+
push!(comps, [a, b.args[1]])
697+
end
696698
end
697699
_ => error("Couldn't parse the component body: $arg")
698700
end

test/model_parsing.jl

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,30 @@ using Unitful
77

88
ENV["MTK_ICONS_DIR"] = "$(@__DIR__)/icons"
99

10+
# Mock module used to test if the `@mtkmodel` macro works with fully-qualified names as well.
11+
module MyMockModule
12+
using ..ModelingToolkit, ..Unitful
13+
14+
export Pin
15+
@connector Pin begin
16+
v(t), [unit = u"V"] # Potential at the pin [V]
17+
i(t), [connect = Flow, unit = u"A"] # Current flowing into the pin [A]
18+
@icon "pin.png"
19+
end
20+
21+
@mtkmodel Ground begin
22+
@components begin
23+
g = Pin()
24+
end
25+
@icon read(abspath(ENV["MTK_ICONS_DIR"], "ground.svg"), String)
26+
@equations begin
27+
g.v ~ 0
28+
end
29+
end
30+
end
31+
32+
using .MyMockModule
33+
1034
@connector RealInput begin
1135
u(t), [input = true, unit = u"V"]
1236
end
@@ -28,12 +52,6 @@ end
2852
@variables t [unit = u"s"]
2953
D = Differential(t)
3054

31-
@connector Pin begin
32-
v(t), [unit = u"V"] # Potential at the pin [V]
33-
i(t), [connect = Flow, unit = u"A"] # Current flowing into the pin [A]
34-
@icon "pin.png"
35-
end
36-
3755
@named p = Pin(; v = π)
3856
@test getdefault(p.v) == π
3957
@test Pin.isconnector == true
@@ -57,16 +75,6 @@ end
5775

5876
@test OnePort.isconnector == false
5977

60-
@mtkmodel Ground begin
61-
@components begin
62-
g = Pin()
63-
end
64-
@icon read(abspath(ENV["MTK_ICONS_DIR"], "ground.svg"), String)
65-
@equations begin
66-
g.v ~ 0
67-
end
68-
end
69-
7078
resistor_log = "$(@__DIR__)/logo/resistor.svg"
7179
@mtkmodel Resistor begin
7280
@extend v, i = oneport = OnePort()
@@ -127,7 +135,7 @@ end
127135
capacitor = Capacitor(; C = C_val)
128136
source = Voltage()
129137
constant = Constant(; k = k_val)
130-
ground = Ground()
138+
ground = MyMockModule.Ground()
131139
end
132140

133141
@equations begin

0 commit comments

Comments
 (0)