3

I was wondering if there already exists a possibility to extract from flat Modelica code all variables AND their corresponding types (classnames respectively).

For example:
Given an extract from a flattened Modelica model:

constant Integer nSurfaces = 8;
constant Integer construction1.nLayers(min = 1.0) = 2 "Number of layers of the construction";
parameter Modelica.SIunits.Length construction1.thickness[construction1.nLayers]= {0.2, 0.1} "Thickness of each construction layer";

Here, the wanted output would be something like:

nSurfaces, Integer, constant;
construction1.nLayers, Integer, constant; 
construction1.thickness[construction1.nLayers], Modelica.SIunits.Length, parameter

Ideally, for construction1.thickness there would be two lines (=number of construction1.nLayers).

I know, that it is possible to get a list of used variables from the dsin.txt, which is produced while translating a model. But until now I did not find an already existing way to get the corresponding types. And I really would like to avoid writing an own parser :-).

jemo
  • 63
  • 5
  • related: https://stackoverflow.com/questions/26023434/how-are-the-data-types-in-dsin-txt-defined – matth Mar 08 '18 at 08:11

1 Answers1

3

You could try to generate the file modelDescription.xml as defined by the FMI standard. It contains a ton of information and XML should be easier to parse, e.g. python has a couple of xml parsing/reading packages. If you are using Dymola you just set the flag Advanced.FMI.GenerateModelDescriptionInterface2 = true to generate the model description file.

The second idea could be to let the compiler/tool parse the Modelica file for you as they need to do that anyway, try searching for AST (abstract syntax tree). In Dymola, this is available through the ModelManagement library, and also through the Python interface.

Third idea could be to use one of the Modelica parsers available, e.g. have a look at:

Fourth, if all that did not work, you still do not have to write a full parser, you could use ANTLR, then use an existing grammar file (look for e.g. modelica.g4).

matth
  • 2,568
  • 2
  • 21
  • 41
  • Thanks for all the hints. I should have googled for "Modelica" and "parser" - I was too fixed on the term "classes". The mentioned Dymola flag worked quite well for me. – jemo Mar 08 '18 at 19:22
  • It is strange that most of the parsers are not in active development. – Jack May 07 '20 at 18:44