Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isconnector to Model type + add all metadata to Model.structure #2204

Merged
merged 4 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions src/systems/model_parsing.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
struct Model{F, S}
f::F
structure::S
isconnector::Bool
end
(m::Model)(args...; kw...) = m.f(args...; kw...)

Expand Down Expand Up @@ -54,11 +55,27 @@
var"#___sys___" = $ODESystem($(Equation[]), $iv, [$(vs...)], $([]);
name, gui_metadata = $gui_metadata)
$Setfield.@set!(var"#___sys___".connector_type=$connector_type(var"#___sys___"))
end, $dict)
end, $dict, true)
end
end

function parse_variable_def!(dict, mod, arg, varclass, kwargs, def = nothing)
metatypes = [(:connection_type, VariableConnectType),

Check warning on line 63 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L63

Added line #L63 was not covered by tests
(:description, VariableDescription),
(:unit, VariableUnit),
(:bounds, VariableBounds),
(:noise, VariableNoiseType),
(:input, VariableInput),
(:output, VariableOutput),
(:irreducible, VariableIrreducible),
(:state_priority, VariableStatePriority),
(:misc, VariableMisc),
(:disturbance, VariableDisturbance),
(:tunable, VariableTunable),
(:dist, VariableDistribution),
(:binary, VariableBinary),
(:integer, VariableInteger)]

arg isa LineNumberNode && return
MLStyle.@match arg begin
a::Symbol => begin
Expand All @@ -78,9 +95,12 @@
def, meta = parse_default(mod, b)
var, _ = parse_variable_def!(dict, mod, a, varclass, kwargs, def)
dict[varclass][getname(var)][:default] = def
if !isnothing(meta)
if (ct = get(meta, VariableConnectType, nothing)) !== nothing
dict[varclass][getname(var)][:connection_type] = nameof(ct)
if meta !== nothing
for (type, key) in metatypes
if (mt = get(meta, key, nothing)) !== nothing
key == VariableConnectType && (mt = nameof(mt))
dict[varclass][getname(var)][type] = mt

Check warning on line 102 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L98-L102

Added lines #L98 - L102 were not covered by tests
end
end
var = set_var_metadata(var, meta)
end
Expand All @@ -89,8 +109,14 @@
Expr(:tuple, a, b) => begin
var, def = parse_variable_def!(dict, mod, a, varclass, kwargs)
meta = parse_metadata(mod, b)
if (ct = get(meta, VariableConnectType, nothing)) !== nothing
dict[varclass][getname(var)][:connection_type] = nameof(ct)
if meta !== nothing
for (type, key) in metatypes
if (mt = get(meta, key, nothing)) !== nothing
key == VariableConnectType && (mt = nameof(mt))
dict[varclass][getname(var)][type] = mt

Check warning on line 116 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L112-L116

Added lines #L112 - L116 were not covered by tests
end
end
var = set_var_metadata(var, meta)

Check warning on line 119 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L118-L119

Added lines #L118 - L119 were not covered by tests
end
(set_var_metadata(var, meta), def)
end
Expand Down Expand Up @@ -213,7 +239,7 @@
push!(exprs.args, :($extend($sys, $(ext[]))))
end

:($name = $Model((; name, $(kwargs...)) -> $exprs, $dict))
:($name = $Model((; name, $(kwargs...)) -> $exprs, $dict, false))

Check warning on line 242 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L242

Added line #L242 was not covered by tests
end

function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, dict,
Expand All @@ -240,13 +266,13 @@
function parse_components!(exprs, cs, dict, body, kwargs)
expr = Expr(:block)
push!(exprs, expr)
comps = Vector{String}[]
comps = Vector{Symbol}[]

Check warning on line 269 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L269

Added line #L269 was not covered by tests
for arg in body.args
arg isa LineNumberNode && continue
MLStyle.@match arg begin
Expr(:(=), a, b) => begin
push!(cs, a)
push!(comps, [String(a), String(b.args[1])])
push!(comps, [a, b.args[1]])

Check warning on line 275 in src/systems/model_parsing.jl

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L275

Added line #L275 was not covered by tests
arg = deepcopy(arg)
b = deepcopy(arg.args[2])

Expand Down
27 changes: 23 additions & 4 deletions test/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ end

@named p = Pin(; v = π)
@test getdefault(p.v) == π
@test Pin.isconnector == true

@mtkmodel OnePort begin
@components begin
Expand All @@ -51,6 +52,8 @@ end
end
end

@test OnePort.isconnector == false

@mtkmodel Ground begin
@components begin
g = Pin()
Expand Down Expand Up @@ -94,16 +97,16 @@ end
@parameters begin
C
end
@variables begin
v = 0.0
end
@extend v, i = oneport = OnePort(; v = v)
@extend v, i = oneport = OnePort(; v = 0.0)
@icon "https://upload.wikimedia.org/wikipedia/commons/7/78/Capacitor_symbol.svg"
@equations begin
D(v) ~ i / C
end
end

@named capacitor = Capacitor(C = 10, oneport.v = 10.0)
@test getdefault(capacitor.v) == 10.0

@mtkmodel Voltage begin
@extend v, i = oneport = OnePort()
@components begin
Expand Down Expand Up @@ -133,6 +136,7 @@ end
@named rc = RC(; resistor.R = 20)
@test getdefault(rc.resistor.R) == 20
@test getdefault(rc.capacitor.C) == 10
@test getdefault(rc.capacitor.v) == 0.0
@test getdefault(rc.constant.k) == 1

@test get_gui_metadata(rc.resistor).layout == Resistor.structure[:icon] ==
Expand Down Expand Up @@ -212,3 +216,18 @@ getdefault(a.b.k) == 1
getdefault(a.b.i) == 20
getdefault(a.b.j) == 30
getdefault(a.b.k) == 40

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

@connector MockMeta begin
m(t),
[description = "Variable to test metadata in the Model.structure",
input = true, bounds = (-1, 1), connect = Flow, integer = true,
binary = false, tunable = false, disturbance = true, dist = Normal(1, 1)]
end

for (k, v) in metadata
@test MockMeta.structure[:variables][:m][k] == v
end
Loading