From 2114f99ad45f54f9f8b0c29b543a52a395d617af Mon Sep 17 00:00:00 2001 From: Venkateshprasad <32921645+ven-k@users.noreply.github.com> Date: Tue, 5 Dec 2023 12:56:12 +0530 Subject: [PATCH] feat: add `label` metadata to symbolic variables --- docs/src/basics/Variable_metadata.md | 8 ++++++++ src/ModelingToolkit.jl | 2 +- src/variables.jl | 19 +++++++++++++++++++ test/test_variable_metadata.jl | 9 +++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/docs/src/basics/Variable_metadata.md b/docs/src/basics/Variable_metadata.md index d186cdf959..6968556e49 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 99bf0b015b..61fb83b602 100644 --- a/src/ModelingToolkit.jl +++ b/src/ModelingToolkit.jl @@ -204,7 +204,7 @@ export connect, domain_connect, @connector, Connection, Flow, Stream, instream export @component, @mtkmodel, @mtkbuild export isinput, isoutput, getbounds, hasbounds, 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/variables.jl b/src/variables.jl index e0addefe6b..4c48f09a89 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -368,6 +368,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/test_variable_metadata.jl b/test/test_variable_metadata.jl index 578d5b5232..454085e864 100644 --- a/test/test_variable_metadata.jl +++ b/test/test_variable_metadata.jl @@ -76,6 +76,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"]