Skip to content

Commit 47d8f05

Browse files
authored
Add isconnector to Model type + add all metadata to Model.structure (#2204)
1 parent c458b85 commit 47d8f05

File tree

2 files changed

+58
-13
lines changed

2 files changed

+58
-13
lines changed

src/systems/model_parsing.jl

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
struct Model{F, S}
22
f::F
33
structure::S
4+
isconnector::Bool
45
end
56
(m::Model)(args...; kw...) = m.f(args...; kw...)
67

@@ -54,11 +55,27 @@ function connector_macro(mod, name, body)
5455
var"#___sys___" = $ODESystem($(Equation[]), $iv, [$(vs...)], $([]);
5556
name, gui_metadata = $gui_metadata)
5657
$Setfield.@set!(var"#___sys___".connector_type=$connector_type(var"#___sys___"))
57-
end, $dict)
58+
end, $dict, true)
5859
end
5960
end
6061

6162
function parse_variable_def!(dict, mod, arg, varclass, kwargs, def = nothing)
63+
metatypes = [(:connection_type, VariableConnectType),
64+
(:description, VariableDescription),
65+
(:unit, VariableUnit),
66+
(:bounds, VariableBounds),
67+
(:noise, VariableNoiseType),
68+
(:input, VariableInput),
69+
(:output, VariableOutput),
70+
(:irreducible, VariableIrreducible),
71+
(:state_priority, VariableStatePriority),
72+
(:misc, VariableMisc),
73+
(:disturbance, VariableDisturbance),
74+
(:tunable, VariableTunable),
75+
(:dist, VariableDistribution),
76+
(:binary, VariableBinary),
77+
(:integer, VariableInteger)]
78+
6279
arg isa LineNumberNode && return
6380
MLStyle.@match arg begin
6481
a::Symbol => begin
@@ -78,9 +95,12 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, def = nothing)
7895
def, meta = parse_default(mod, b)
7996
var, _ = parse_variable_def!(dict, mod, a, varclass, kwargs, def)
8097
dict[varclass][getname(var)][:default] = def
81-
if !isnothing(meta)
82-
if (ct = get(meta, VariableConnectType, nothing)) !== nothing
83-
dict[varclass][getname(var)][:connection_type] = nameof(ct)
98+
if meta !== nothing
99+
for (type, key) in metatypes
100+
if (mt = get(meta, key, nothing)) !== nothing
101+
key == VariableConnectType && (mt = nameof(mt))
102+
dict[varclass][getname(var)][type] = mt
103+
end
84104
end
85105
var = set_var_metadata(var, meta)
86106
end
@@ -89,8 +109,14 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs, def = nothing)
89109
Expr(:tuple, a, b) => begin
90110
var, def = parse_variable_def!(dict, mod, a, varclass, kwargs)
91111
meta = parse_metadata(mod, b)
92-
if (ct = get(meta, VariableConnectType, nothing)) !== nothing
93-
dict[varclass][getname(var)][:connection_type] = nameof(ct)
112+
if meta !== nothing
113+
for (type, key) in metatypes
114+
if (mt = get(meta, key, nothing)) !== nothing
115+
key == VariableConnectType && (mt = nameof(mt))
116+
dict[varclass][getname(var)][type] = mt
117+
end
118+
end
119+
var = set_var_metadata(var, meta)
94120
end
95121
(set_var_metadata(var, meta), def)
96122
end
@@ -213,7 +239,7 @@ function mtkmodel_macro(mod, name, expr)
213239
push!(exprs.args, :($extend($sys, $(ext[]))))
214240
end
215241

216-
:($name = $Model((; name, $(kwargs...)) -> $exprs, $dict))
242+
:($name = $Model((; name, $(kwargs...)) -> $exprs, $dict, false))
217243
end
218244

219245
function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, dict,
@@ -240,13 +266,13 @@ end
240266
function parse_components!(exprs, cs, dict, body, kwargs)
241267
expr = Expr(:block)
242268
push!(exprs, expr)
243-
comps = Vector{String}[]
269+
comps = Vector{Symbol}[]
244270
for arg in body.args
245271
arg isa LineNumberNode && continue
246272
MLStyle.@match arg begin
247273
Expr(:(=), a, b) => begin
248274
push!(cs, a)
249-
push!(comps, [String(a), String(b.args[1])])
275+
push!(comps, [a, b.args[1]])
250276
arg = deepcopy(arg)
251277
b = deepcopy(arg.args[2])
252278

test/model_parsing.jl

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ end
3333

3434
@named p = Pin(; v = π)
3535
@test getdefault(p.v) == π
36+
@test Pin.isconnector == true
3637

3738
@mtkmodel OnePort begin
3839
@components begin
@@ -51,6 +52,8 @@ end
5152
end
5253
end
5354

55+
@test OnePort.isconnector == false
56+
5457
@mtkmodel Ground begin
5558
@components begin
5659
g = Pin()
@@ -94,16 +97,16 @@ end
9497
@parameters begin
9598
C
9699
end
97-
@variables begin
98-
v = 0.0
99-
end
100-
@extend v, i = oneport = OnePort(; v = v)
100+
@extend v, i = oneport = OnePort(; v = 0.0)
101101
@icon "https://upload.wikimedia.org/wikipedia/commons/7/78/Capacitor_symbol.svg"
102102
@equations begin
103103
D(v) ~ i / C
104104
end
105105
end
106106

107+
@named capacitor = Capacitor(C = 10, oneport.v = 10.0)
108+
@test getdefault(capacitor.v) == 10.0
109+
107110
@mtkmodel Voltage begin
108111
@extend v, i = oneport = OnePort()
109112
@components begin
@@ -133,6 +136,7 @@ end
133136
@named rc = RC(; resistor.R = 20)
134137
@test getdefault(rc.resistor.R) == 20
135138
@test getdefault(rc.capacitor.C) == 10
139+
@test getdefault(rc.capacitor.v) == 0.0
136140
@test getdefault(rc.constant.k) == 1
137141

138142
@test get_gui_metadata(rc.resistor).layout == Resistor.structure[:icon] ==
@@ -212,3 +216,18 @@ getdefault(a.b.k) == 1
212216
getdefault(a.b.i) == 20
213217
getdefault(a.b.j) == 30
214218
getdefault(a.b.k) == 40
219+
220+
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+
224+
@connector MockMeta begin
225+
m(t),
226+
[description = "Variable to test metadata in the Model.structure",
227+
input = true, bounds = (-1, 1), connect = Flow, integer = true,
228+
binary = false, tunable = false, disturbance = true, dist = Normal(1, 1)]
229+
end
230+
231+
for (k, v) in metadata
232+
@test MockMeta.structure[:variables][:m][k] == v
233+
end

0 commit comments

Comments
 (0)