diff --git a/docs/src/basics/Variable_metadata.md b/docs/src/basics/Variable_metadata.md index aa0d579383..b6cb99c851 100644 --- a/docs/src/basics/Variable_metadata.md +++ b/docs/src/basics/Variable_metadata.md @@ -39,6 +39,14 @@ help?> u Symbolics.VariableSource: (:variables, :u) ``` +## Label + +Label is descriptive name of a symbolic variable. It can be defined as: + +```@example metadata +@parameters R [label = "Resistance"] +``` + ## Connect Variables in connectors can have `connect` metadata which describes the type of connections. diff --git a/src/ModelingToolkit.jl b/src/ModelingToolkit.jl index b0197245f9..b77835b129 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -206,7 +206,7 @@ export @component, @mtkmodel, @mtkbuild export isinput, isoutput, getbounds, hasbounds, getguess, hasguess, isdisturbance, istunable, getdist, hasdist, tunable_parameters, isirreducible, getdescription, hasdescription, isbinaryvar, - isintegervar + isintegervar, getlabel, haslabel export ode_order_lowering, dae_order_lowering, liouville_transform export PDESystem export Differential, expand_derivatives, @derivatives diff --git a/src/systems/model_parsing.jl b/src/systems/model_parsing.jl index 4a8d73652c..4944dfe695 100644 --- a/src/systems/model_parsing.jl +++ b/src/systems/model_parsing.jl @@ -110,6 +110,7 @@ function parse_variable_def!(dict, mod, arg, varclass, kwargs; def = nothing, indices::Union{Vector{UnitRange{Int}}, Nothing} = nothing) metatypes = [(:connection_type, VariableConnectType), (:description, VariableDescription), + (:label, VariableLabel), (:unit, VariableUnit), (:bounds, VariableBounds), (:noise, VariableNoiseType), diff --git a/src/variables.jl b/src/variables.jl index 964eaa7859..1311da24ef 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -372,6 +372,25 @@ function hasdescription(x) getdescription(x) != "" end +## Label ================================================================= +struct VariableLabel end +Symbolics.option_to_metadata_type(::Val{:label}) = VariableLabel + +getlabel(x::Num) = getlabel(Symbolics.unwrap(x)) + +""" + getlabel(x) + +Return labels attached to variables `x`. If no label is attached, an empty string is returned. +""" +function getlabel(x) + p = Symbolics.getparent(x, nothing) + p === nothing || (x = p) + Symbolics.getmetadata(x, VariableLabel, "") +end + +haslabel(x) = (getlabel(x) != "") + ## binary variables ================================================================= struct VariableBinary end Symbolics.option_to_metadata_type(::Val{:binary}) = VariableBinary diff --git a/test/model_parsing.jl b/test/model_parsing.jl index f94d7483fd..6a422c5eb0 100644 --- a/test/model_parsing.jl +++ b/test/model_parsing.jl @@ -274,13 +274,15 @@ end @testset "Metadata in variables" begin 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)) + :binary => false, :tunable => false, :disturbance => true, :dist => Normal(1, 1), + :label => "MockVariable") @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)] + label = "MockVariable", 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 diff --git a/test/test_variable_metadata.jl b/test/test_variable_metadata.jl index 4e75f1f56c..28b2c4e248 100644 --- a/test/test_variable_metadata.jl +++ b/test/test_variable_metadata.jl @@ -84,6 +84,15 @@ sp = Set(p) @test getdescription(u) == "" @test !hasdescription(u) +## Labels +@variables l [label = "Label"] +@test getlabel(l) == "Label" +@test haslabel(l) + +@variables l +@test getlabel(l) == "" +@test !haslabel(l) + @parameters t @variables u(t) [description = "A short description of u"] @parameters p [description = "A description of p"]