从平面 Modelica 代码中提取类型/类名 [英] Extract Types/Classnames from flat Modelica code

查看:44
本文介绍了从平面 Modelica 代码中提取类型/类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否已经存在从平面 Modelica 代码中提取所有变量及其相应类型(分别为类名)的可能性.

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

例如:
给定一个扁平 Modelica 模型的摘录:

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

理想情况下,对于 construction1.thickness 将有两行(=construction1.nLayers 的数量).

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

我知道,可以从翻译模型时生成的 dsin.txt 中获取已使用变量的列表.但是直到现在我还没有找到一种已经存在的方法来获取相应的类型.而且我真的很想避免编写自己的解析器:-).

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 :-).

推荐答案

您可以尝试生成FMI 标准.它包含大量信息,XML 应该更容易解析,例如python 有几个 xml 解析/读取包.如果您使用 Dymola,您只需设置标志 Advanced.FMI.GenerateModelDescriptionInterface2 = true 即可生成模型描述文件.

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.

第二个想法可能是让编译器/工具为您解析 Modelica 文件,因为他们无论如何都需要这样做,尝试搜索 AST(抽象语法树).在 Dymola 中,这可以通过 ModelManagement 库以及 Python 接口获得.

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.

第三个想法可能是使用可用的 Modelica 解析器之一,例如看看:

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

第四,如果所有这些都不起作用,您仍然不必编写完整的解析器,您可以使用ANTLR,然后使用现有的语法文件(例如查找modelica.g4).

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).

这篇关于从平面 Modelica 代码中提取类型/类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆