Skip to content

Commit fd22563

Browse files
authored
Merge pull request #1688 from SciML/fb/descriptions
add description metadata to variables
2 parents 2eec9bf + d503ab0 commit fd22563

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

docs/src/basics/Variable_metadata.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
# Symbolic metadata
2-
It is possible to add metadata to symbolic variables. The following
3-
information can be added (note, it's possible to extend this to user-defined metadata as well)
2+
It is possible to add metadata to symbolic variables, the metadata will be displayed when calling help on a variable.
3+
4+
The following information can be added (note, it's possible to extend this to user-defined metadata as well)
5+
6+
## Variable descriptions
7+
Descriptive strings can be attached to variables using the `[description = "descriptive string"]` syntax:
8+
```@example metadata
9+
using ModelingToolkit
10+
@variables u [description = "This is my input"]
11+
getdescription(u)
12+
```
13+
14+
When variables with descriptions are present in systems, they will be printed when the system is shown in the terminal:
15+
```@example metadata
16+
@parameters t
17+
@variables u(t) [description = "A short description of u"]
18+
@parameters p [description = "A description of p"]
19+
@named sys = ODESystem([u ~ p], t)
20+
show(stdout, "text/plain", sys) # hide
21+
```
22+
23+
Calling help on the variable `u` displays the description, alongside other metadata:
24+
```julia
25+
help?> u
26+
27+
A variable of type Symbolics.Num (Num wraps anything in a type that is a subtype of Real)
28+
29+
Metadata
30+
≡≡≡≡≡≡≡≡≡≡
31+
32+
ModelingToolkit.VariableDescription: This is my input
33+
34+
Symbolics.VariableSource: (:variables, :u)
35+
```
436

537
## Input or output
638
Designate a variable as either an input or an output using the following

src/ModelingToolkit.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export NonlinearSystem, OptimizationSystem
173173
export alias_elimination, flatten
174174
export connect, @connector, Connection, Flow, Stream, instream
175175
export isinput, isoutput, getbounds, hasbounds, isdisturbance, istunable, getdist, hasdist,
176-
tunable_parameters, isirreducible
176+
tunable_parameters, isirreducible, getdescription, hasdescription
177177
export ode_order_lowering, dae_order_lowering, liouville_transform
178178
export PDESystem
179179
export Differential, expand_derivatives, @derivatives

src/systems/abstractsystem.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,10 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem)
752752
:displaysize => (1, displaysize(io)[2])), val)
753753
print(io, "]")
754754
end
755+
description = getdescription(s)
756+
if description !== nothing && description != ""
757+
print(io, ": ", description)
758+
end
755759
end
756760
end
757761
limited && print(io, "\n")
@@ -774,6 +778,10 @@ function Base.show(io::IO, mime::MIME"text/plain", sys::AbstractSystem)
774778
:displaysize => (1, displaysize(io)[2])), val)
775779
print(io, "]")
776780
end
781+
description = getdescription(s)
782+
if description !== nothing && description != ""
783+
print(io, ": ", description)
784+
end
777785
end
778786
end
779787
limited && print(io, "\n")

src/variables.jl

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
struct VariableUnit end
22
struct VariableConnectType end
33
struct VariableNoiseType end
4-
struct VariableDescriptionType end
54
struct VariableInput end
65
struct VariableOutput end
76
struct VariableIrreducible end
87
Symbolics.option_to_metadata_type(::Val{:unit}) = VariableUnit
98
Symbolics.option_to_metadata_type(::Val{:connect}) = VariableConnectType
109
Symbolics.option_to_metadata_type(::Val{:noise}) = VariableNoiseType
11-
Symbolics.option_to_metadata_type(::Val{:description}) = VariableDescriptionType
1210
Symbolics.option_to_metadata_type(::Val{:input}) = VariableInput
1311
Symbolics.option_to_metadata_type(::Val{:output}) = VariableOutput
1412
Symbolics.option_to_metadata_type(::Val{:irreducible}) = VariableIrreducible
@@ -267,3 +265,24 @@ function getbounds(p::AbstractVector)
267265
ub = last.(bounds)
268266
(; lb, ub)
269267
end
268+
269+
## Description =================================================================
270+
struct VariableDescription end
271+
Symbolics.option_to_metadata_type(::Val{:description}) = VariableDescription
272+
273+
getdescription(x::Num) = getdescription(Symbolics.unwrap(x))
274+
275+
"""
276+
getdescription(x)
277+
278+
Return any description attached to variables `x`. If no description is attached, an empty string is returned.
279+
"""
280+
function getdescription(x)
281+
p = Symbolics.getparent(x, nothing)
282+
p === nothing || (x = p)
283+
Symbolics.getmetadata(x, VariableDescription, "")
284+
end
285+
286+
function hasdescription(x)
287+
getdescription(x) != ""
288+
end

test/test_variable_metadata.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,19 @@ sp = Set(p)
6666
@test T sp
6767
@test k2 sp
6868
@test length(p) == 3
69+
70+
## Descriptions
71+
@variables u [description = "This is my input"]
72+
@test getdescription(u) == "This is my input"
73+
@test hasdescription(u)
74+
75+
@variables u
76+
@test getdescription(u) == ""
77+
@test !hasdescription(u)
78+
79+
@parameters t
80+
@variables u(t) [description = "A short description of u"]
81+
@parameters p [description = "A description of p"]
82+
@named sys = ODESystem([u ~ p], t)
83+
84+
@test_nowarn show(stdout, "text/plain", sys)

0 commit comments

Comments
 (0)