我想知道是否已经存在从平面 Modelica 代码中提取所有变量及其相应类型(分别为类名)的可能性。
例如:
给定扁平 Modelica 模型的摘录:
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";
在这里,想要的输出将类似于:
nSurfaces, Integer, constant;
construction1.nLayers, Integer, constant;
construction1.thickness[construction1.nLayers], Modelica.SIunits.Length, parameter
理想情况下,对于construction1.thickness
将会有两行(=construction1.nLayers
).
我知道,可以从以下位置获取已使用变量的列表dsin.txt
,这是在翻译模型时生成的。但到目前为止我还没有找到一种已经存在的方法来获取相应的类型。我真的很想避免编写自己的解析器:-)。
您可以尝试生成文件modelDescription.xml
定义为FMI标准。它包含大量信息,并且 XML 应该更容易解析,例如python 有几个 xml 解析/读取包。
如果您使用 Dymola,您只需设置标志Advanced.FMI.GenerateModelDescriptionInterface2 = true
生成模型描述文件。
第二个想法可能是让编译器/工具为您解析 Modelica 文件,因为无论如何他们都需要这样做,尝试搜索 AST(抽象语法树)。在 Dymola 中,这可以通过 ModelManagement 库以及 Python 界面获得。
第三个想法可能是使用可用的 Modelica 解析器之一,例如看一下:
- https://github.com/lbl-srg/modelica-json
- https://hackage.haskell.org/package/modelicaparser
- https://github.com/xie-dongping/modparc
- https://github.com/pymoca/pymoca
- https://github.com/pymola/pymola/tree/master/src/pymola
第四,如果所有这些都不起作用,您仍然不必编写完整的解析器,您可以使用 ANTLR,然后使用现有的语法文件(例如查找modelica.g4
).
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)