modelica / ModelicaSpecification

Specification of the Modelica Language
https://specification.modelica.org
Creative Commons Attribution Share Alike 4.0 International
98 stars 42 forks source link

Can %par textString expressions be used as literal expressions in DynamicSelect? #3573

Open casella opened 2 days ago

casella commented 2 days ago

Section 18.6.5.5 defines the meaning of Text annotations including "%" in textString, in particular

%par and %{par} replaced by the value of the parameter par

while Section 18.6.6, which defines DynamicSelect, reads:

DynamicSelect has the syntax of a function call with two arguments, where the first argument specifies the value of the editing state and the second argument the value of the non-editing state. The first argument must be a literal expression.

Consider now the following annotation:

annotation(Icon(graphics = {Text(textString=DynamicSelect("%x_start", String(x))}));

where x_start is a parameter. On the one hand, "x_start" is a literal String expression. On the other hand, it clearly refers to a parameter value, so it's not really literal.

How should this case be interpreted?

maltelenz commented 2 days ago

Test model:

model M
  parameter Real x_start = 2;
  annotation(
    Icon(
      graphics = {
        Text(
          origin = {18.445, 40.975},
          extent = {{-38.445, -30.975}, {38.445, 30.975}},
          textString = DynamicSelect("%x_start", String(x))
        )
      }
    )
  );
end M;

I think it's allowed. (System Modeler supports this by showing the value of x_start in the icon).

HansOlsson commented 2 days ago

I also think this make sense.

bilderbuchi commented 1 day ago

I think there is actually no contradiction here. "%x_start" is a literal expression, so this works as a first argument of DynamicSelect.

At the same time, https://specification.modelica.org/maint/3.6/annotations.html#text explains the text macros (emphasis mine):

There are a number of common macros that can be used in the text, and they should be replaced when displaying the text

So, in my mind

  1. DynamicSelect is evaluated to produce (e.g.) the string "%x_start"
  2. This string put into a Text annotation
  3. When displaying the (literal) string it is processed for macros, at which point, %x_start is interpolated to 2.