Skip to content

Commit 70c3252

Browse files
authored
Merge pull request #2346 from ven-k/vkb/icons-refactor
Improves `@icon` and adds related docs
2 parents 7926a69 + 645de82 commit 70c3252

File tree

4 files changed

+56
-29
lines changed

4 files changed

+56
-29
lines changed

docs/src/basics/MTKModel_Connector.md

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ equations.
2626
- `@components`: for listing sub-components of the system
2727
- `@equations`: for the list of equations
2828
- `@extend`: for extending a base system and unpacking its states
29+
- `@icon` : for embedding the model icon
2930
- `@parameters`: for specifying the symbolic parameters
3031
- `@structural_parameters`: for specifying non-symbolic parameters
3132
- `@variables`: for specifing the states
@@ -50,6 +51,7 @@ end
5051
end
5152
5253
@mtkmodel ModelC begin
54+
@icon "https://github.com/SciML/SciMLDocs/blob/main/docs/src/assets/logo.png"
5355
@structural_parameters begin
5456
f = sin
5557
end
@@ -58,6 +60,7 @@ end
5860
end
5961
@variables begin
6062
v(t) = v_var
63+
v_array(t)[1:2, 1:3]
6164
end
6265
@extend ModelB(; p1)
6366
@components begin
@@ -69,6 +72,41 @@ end
6972
end
7073
```
7174

75+
#### `@icon`
76+
77+
An icon can be embedded in 3 ways:
78+
79+
- URI
80+
- Path to a valid image-file.<br>
81+
It can be an absolute path. Or, a path relative to an icon directory; which is
82+
`DEPOT_PATH[1]/mtk_icons` by default and can be changed by setting
83+
`ENV["MTK_ICONS_DIR"]`.<br>
84+
Internally, it is saved in the _File URI_ scheme.
85+
86+
```julia
87+
@mtkmodel WithPathtoIcon begin
88+
@icon "/home/user/.julia/dev/mtk_icons/icon.png"
89+
# Rest of the model definition
90+
end
91+
```
92+
93+
- Inlined SVG.
94+
95+
```julia
96+
@mtkmodel WithInlinedSVGIcon begin
97+
@icon """<svg height="100" width="100">
98+
<circle cx="50" cy="50" r="40" stroke="green" fill="none" stroke-width="3"/>
99+
</svg>
100+
"""
101+
# Rest of the model definition
102+
end
103+
```
104+
105+
#### `@structural_parameters` begin block
106+
107+
- This block is for non symbolic input arguements. These are for inputs that usually are not meant to be part of components; but influence how they are defined. One can list inputs like boolean flags, functions etc... here.
108+
- Whenever default values are specified, unlike parameters/variables, they are reflected in the keyword argument list.
109+
72110
#### `@parameters` and `@variables` begin block
73111

74112
- Parameters and variables are declared with respective begin blocks.
@@ -80,15 +118,10 @@ end
80118
```julia
81119
julia> @mtkbuild model_c1 = ModelC(; v = 2.0);
82120

83-
julia> ModelingToolkit.getdefault(model_c.v)
121+
julia> ModelingToolkit.getdefault(model_c1.v)
84122
2.0
85123
```
86124

87-
#### `@structural_parameters` begin block
88-
89-
- This block is for non symbolic input arguements. These are for inputs that usually are not meant to be part of components; but influence how they are defined. One can list inputs like boolean flags, functions etc... here.
90-
- Whenever default values are specified, unlike parameters/variables, they are reflected in the keyword argument list.
91-
92125
#### `@extend` begin block
93126

94127
- Partial systems can be extended in a higher system as `@extend PartialSystem(; kwargs)`.
@@ -209,11 +242,12 @@ For example, the structure of `ModelC` is:
209242

210243
```julia
211244
julia> ModelC.structure
212-
Dict{Symbol, Any} with 6 entries:
245+
Dict{Symbol, Any} with 7 entries:
213246
:components => [[:model_a, :ModelA]]
214-
:variables => Dict{Symbol, Dict{Symbol, Any}}(:v=>Dict(:default=>:v_var), :v_array=>Dict(:size=>(4,)))
247+
:variables => Dict{Symbol, Dict{Symbol, Any}}(:v=>Dict(:default=>:v_var), :v_array=>Dict(:size=>(2, 3)))
248+
:icon => URI("https://github.com/SciML/SciMLDocs/blob/main/docs/src/assets/logo.png")
215249
:kwargs => Dict{Symbol, Any}(:f=>:sin, :v=>:v_var, :v_array=>nothing, :model_a__k_array=>nothing, :p1=>nothing)
216250
:independent_variable => t
217-
:extend => Any[[:p1, :p2], :model_b, :ModelB]
218-
:equations => ["model_a.k1 ~ f(v)"]
251+
:extend => Any[[:p2, :p1], Symbol("#mtkmodel__anonymous__ModelB"), :ModelB]
252+
:equations => ["model_a.k ~ f(v)"]
219253
```

src/systems/model_parsing.jl

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps,
259259
elseif mname == Symbol("@equations")
260260
parse_equations!(exprs, eqs, dict, body)
261261
elseif mname == Symbol("@icon")
262-
parse_icon!(icon, dict, mod, body)
262+
isassigned(icon) && error("This model has more than one icon.")
263+
parse_icon!(icon, dict, body)
263264
else
264265
error("$mname is not handled.")
265266
end
@@ -471,7 +472,7 @@ function parse_equations!(exprs, eqs, dict, body)
471472
dict[:equations] = readable_code.(eqs)
472473
end
473474

474-
function parse_icon!(icon, dict, mod, body::String)
475+
function parse_icon!(icon, dict, body::String)
475476
icon_dir = get(ENV, "MTK_ICONS_DIR", joinpath(DEPOT_PATH[1], "mtk_icons"))
476477
dict[:icon] = icon[] = if isfile(body)
477478
URI("file:///" * abspath(body))
@@ -483,17 +484,13 @@ function parse_icon!(icon, dict, mod, body::String)
483484
false
484485
end
485486
URI(body)
487+
elseif (_body = lstrip(body); startswith(_body, r"<\?xml|<svg"))
488+
String(_body) # With Julia-1.10 promoting `SubString{String}` to `String` can be dropped.
486489
else
487-
error("$body is not a valid icon")
490+
error("\n$body is not a valid icon")
488491
end
489492
end
490493

491-
function parse_icon!(icon, dict, mod, body::Expr)
492-
_icon = body.args[end]
493-
dict[:icon] = icon[] = MLStyle.@match _icon begin
494-
::Symbol => get_var(mod, _icon)
495-
::String => _icon
496-
Expr(:call, read, a...) => eval(_icon)
497-
_ => error("$_icon isn't a valid icon")
498-
end
494+
function parse_icon!(icon, dict, body::Expr)
495+
parse_icon!(icon, dict, eval(body))
499496
end

test/icons/resistor.svg

Lines changed: 1 addition & 1 deletion
Loading

test/model_parsing.jl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ end
6262
@components begin
6363
g = Pin()
6464
end
65-
@icon begin
66-
read(abspath(ENV["MTK_ICONS_DIR"], "ground.svg"), String)
67-
end
65+
@icon read(abspath(ENV["MTK_ICONS_DIR"], "ground.svg"), String)
6866
@equations begin
6967
g.v ~ 0
7068
end
@@ -76,10 +74,9 @@ resistor_log = "$(@__DIR__)/logo/resistor.svg"
7674
@parameters begin
7775
R, [unit = u""]
7876
end
79-
@icon begin
80-
"""<?xml version="1.0" encoding="UTF-8"?>
77+
@icon """<?xml version="1.0" encoding="UTF-8"?>
8178
<svg xmlns="http://www.w3.org/2000/svg" width="80" height="30">
82-
<path d="M10 15
79+
<path d="M10 15
8380
l15 0
8481
l2.5 -5
8582
l5 10
@@ -91,7 +88,6 @@ l2.5 -5
9188
l15 0" stroke="black" stroke-width="1" stroke-linejoin="bevel" fill="none"></path>
9289
</svg>
9390
"""
94-
end
9591
@equations begin
9692
v ~ i * R
9793
end

0 commit comments

Comments
 (0)