Skip to content

Commit

Permalink
refactor: simplify the component arg parsing by setting the expr and …
Browse files Browse the repository at this point in the history
…symbols as defuaults

- this avoids having to getdefault vals of the constituent pars/vars
- while here, fixes the case where more than one hierarchal args
- adds tests that cover both
  • Loading branch information
ven-k committed Jul 1, 2023
1 parent fa0a4bb commit 070340c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/systems/abstractsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ function varname_fix!(expr::Expr)
for arg in expr.args
MLStyle.@match arg begin
::Symbol => continue
Expr(:kw, a) => varname_sanitization!(arg)
Expr(:kw, a...) || Expr(:kw, a) => varname_sanitization!(arg)
Expr(:parameters, a...) => begin
for _arg in arg.args
varname_sanitization!(_arg)
Expand Down
34 changes: 11 additions & 23 deletions src/systems/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ end
@inline is_kwarg(::Symbol) = false
@inline is_kwarg(e::Expr) = (e.head == :parameters)

function connector_macro(mod, name, body; arglist = Set([]), kwargs = Set([]))
function connector_macro(mod, name, body)

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

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L18

Added line #L18 was not covered by tests
if !Meta.isexpr(body, :block)
err = """
connector body must be a block! It should be in the form of
Expand All @@ -29,6 +29,7 @@ function connector_macro(mod, name, body; arglist = Set([]), kwargs = Set([]))
error(err)
end
vs = []
kwargs = []

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

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L32

Added line #L32 was not covered by tests
icon = Ref{Union{String, URI}}()
dict = Dict{Symbol, Any}()
dict[:kwargs] = Dict{Symbol, Any}()
Expand All @@ -48,7 +49,7 @@ function connector_macro(mod, name, body; arglist = Set([]), kwargs = Set([]))
gui_metadata = isassigned(icon) ? GUIMetadata(GlobalRef(mod, name), icon[]) :
nothing
quote
$name = $Model(($(arglist...); name, $(kwargs...)) -> begin
$name = $Model((; name, $(kwargs...)) -> begin

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

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L52

Added line #L52 was not covered by tests
$expr
var"#___sys___" = $ODESystem($(Equation[]), $iv, [$(vs...)], $([]);
name, gui_metadata = $gui_metadata)
Expand Down Expand Up @@ -173,7 +174,7 @@ function get_var(mod::Module, b)
b isa Symbol ? getproperty(mod, b) : b
end

function mtkmodel_macro(mod, name, expr; arglist = Set([]), kwargs = Set([]))
function mtkmodel_macro(mod, name, expr)

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

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L177

Added line #L177 was not covered by tests
exprs = Expr(:block)
dict = Dict{Symbol, Any}()
dict[:kwargs] = Dict{Symbol, Any}()
Expand All @@ -183,6 +184,7 @@ function mtkmodel_macro(mod, name, expr; arglist = Set([]), kwargs = Set([]))
icon = Ref{Union{String, URI}}()
vs = []
ps = []
kwargs = []

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

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L187

Added line #L187 was not covered by tests

for arg in expr.args
arg isa LineNumberNode && continue
Expand Down Expand Up @@ -211,7 +213,7 @@ function mtkmodel_macro(mod, name, expr; arglist = Set([]), kwargs = Set([]))
push!(exprs.args, :($extend($sys, $(ext[]))))
end

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

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

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L216

Added line #L216 was not covered by tests
end

function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, dict,
Expand Down Expand Up @@ -272,33 +274,19 @@ function component_args!(a, b, expr, kwargs)
arg = b.args[i]
arg isa LineNumberNode && continue
MLStyle.@match arg begin
::Symbol => begin
_v = _rename(a, arg)
push!(kwargs, _v)
b.args[i] = Expr(:kw, arg, _v)
x::Symbol || Expr(:kw, x) => begin

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

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L277

Added line #L277 was not covered by tests
_v = _rename(a, x)
b.args[i] = Expr(:kw, x, _v)
push!(kwargs, Expr(:kw, _v, nothing))

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

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L280

Added line #L280 was not covered by tests
end
Expr(:parameters, x...) => begin
component_args!(a, arg, expr, kwargs)

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

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L282-L283

Added lines #L282 - L283 were not covered by tests
end
Expr(:kw, x) => begin
_v = _rename(a, x)
b.args[i] = Expr(:kw, x, _v)
push!(kwargs, _v, nothing)
end
Expr(:kw, x, y::Number) => begin
Expr(:kw, x, y) => begin
_v = _rename(a, x)
b.args[i] = Expr(:kw, x, _v)

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

View check run for this annotation

Codecov / codecov/patch

src/systems/model_parsing.jl#L287

Added line #L287 was not covered by tests
push!(kwargs, Expr(:kw, _v, y))
end
Expr(:kw, x, y) => begin
_v = _rename(a, x)
push!(expr.args, :($_v = $y))
def = Expr(:kw)
push!(def.args, x)
push!(def.args, :($getdefault($_v)))
b.args[i] = def
push!(kwargs, Expr(:kw, _v, nothing))
end
_ => error("Could not parse $arg of component $a")
end
end
Expand Down
28 changes: 28 additions & 0 deletions test/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,31 @@ model = complete(model)
@test getdefault(model.i) == 4
@test isequal(getdefault(model.j), model.jval)
@test isequal(getdefault(model.k), model.kval)


@mtkmodel A begin
@parameters begin
p
end
@components begin
b = B(i = p, j = 1 / p, k = 1)
end
end

@mtkmodel B begin
@parameters begin
i
j
k
end
end

@named a = A(p = 10)
getdefault(a.b.i) == 10
getdefault(a.b.j) == 0.1
getdefault(a.b.k) == 1

@named a = A(p = 10, b.i = 20, b.j = 30, b.k = 40)
getdefault(a.b.i) == 20
getdefault(a.b.j) == 30
getdefault(a.b.k) == 40

0 comments on commit 070340c

Please sign in to comment.