Skip to content

Commit

Permalink
test: conditionally define component array
Browse files Browse the repository at this point in the history
  • Loading branch information
ven-k committed Mar 4, 2024
1 parent a4a79ce commit e324633
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/src/basics/MTKModel_Connector.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ end
```

!!! note

For more examples of usage, checkout [ModelingToolkitStandardLibrary.jl](https://github.com/SciML/ModelingToolkitStandardLibrary.jl/)

## More on `Model.structure`
Expand Down
4 changes: 2 additions & 2 deletions src/systems/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -844,9 +844,9 @@ function _parse_components!(body, kwargs)
arg.args[2] = b
expr = :(@named $arg)
push!(comp_names, a)
if (isa(b.args[1], Symbol) || Meta.isexpr(b.args[1], :.))
if (isa(b.args[1], Symbol) || Meta.isexpr(b.args[1], :.))
push!(comps, [a, b.args[1]])
end
end
end
_ => error("Couldn't parse the component body: $arg")
end
Expand Down
29 changes: 28 additions & 1 deletion test/model_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -680,13 +680,40 @@ end
:comprehension_2,
:written_out_for_1,
:written_out_for_2,
:single_sub_component,
:single_sub_component
]

@test getdefault(component.comprehension_1.sc) == 1
@test getdefault(component.comprehension_2.sc) == 2
@test getdefault(component.written_out_for_1.sc) == 2
@test getdefault(component.written_out_for_2.sc) == 3

@mtkmodel ConditionalComponent begin
@structural_parameters begin
N = 2
end
@components begin
if N == 2
if_comprehension = [SubComponent(sc = i) for i in 1:N]
elseif N == 3
elseif_comprehension = [SubComponent(sc = i) for i in 1:N]
else
else_comprehension = [SubComponent(sc = i) for i in 1:N]
end
end
end

@named if_component = ConditionalComponent()
@test nameof.(get_systems(if_component)) == [:if_comprehension_1, :if_comprehension_2]

@named elseif_component = ConditionalComponent(; N = 3)
@test nameof.(get_systems(elseif_component)) ==
[:elseif_comprehension_1, :elseif_comprehension_2, :elseif_comprehension_3]

@named else_component = ConditionalComponent(; N = 4)
@test nameof.(get_systems(else_component)) ==
[:else_comprehension_1, :else_comprehension_2,
:else_comprehension_3, :else_comprehension_4]
end

@testset "Parent module of Models" begin
Expand Down

0 comments on commit e324633

Please sign in to comment.