如何使用URI在Openmodelica中的combiTable1Ds中加载文件?

如何使用URI在Openmodelica中的combiTable1Ds中加载文件?,modelica,openmodelica,Modelica,Openmodelica,在OpenModelica 1.14.0中,我想将一些数据从文件加载到combiTable1Ds中。我想使用Modelica URI以可移植的方式指定路径(即不使用完全绝对路径) 但是,我指定了fileName参数,但是,我总是得到一个错误,即找不到该文件 最简单的例子: 我有以下包布局: ├── TestURI2 │   ├── loadURI2.mo │   ├── package.mo │   ├── package.order │   └── somefile.txt 在文件loadU

在OpenModelica 1.14.0中,我想将一些数据从文件加载到combiTable1Ds中。我想使用Modelica URI以可移植的方式指定路径(即不使用完全绝对路径)

但是,我指定了
fileName
参数,但是,我总是得到一个错误,即找不到该文件

最简单的例子:

我有以下包布局:

├── TestURI2
│   ├── loadURI2.mo
│   ├── package.mo
│   ├── package.order
│   └── somefile.txt
在文件
loadURI2.mo

within TestURI2;

model loadURI2
  Integer x;
  String filepath = Modelica.Utilities.Files.loadResource("modelica://TestURI2/somefile.txt");
  Modelica.Blocks.Tables.CombiTable1Ds combiTable1Ds(fileName = "modelica://TestURI2/somefile.txt", tableName = "bla", tableOnFile = true)  annotation(Placement(visible = true, transformation(origin = {-60, 68}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
combiTable1Ds.u = time;
x  = Modelica.Utilities.Strings.length(filepath);
end loadURI2;
如果我注释掉与
combiTable1Ds
相关的行,则模型将编译,x将显示文件路径的正确长度,表明
loadResource
正确标识URI中的路径
modelica://TestURI2/somefile.txt

如果我让战斗1DS保持活动状态,我会

/tmp/OpenModelica_bilderbuchi/OMEdit/TestURI2.loadURI2/TestURI2.loadURI2 -port=34607 -logFormat=xmltcp -override=startTime=0,stopTime=1,stepSize=0.002,tolerance=1e-6,solver=dassl,outputFormat=mat,variableFilter=.* -r=/tmp/OpenModelica_bilderbuchi/OMEdit/TestURI2.loadURI2/TestURI2.loadURI2_res.mat -w -lv=LOG_STATS -inputPath=/tmp/OpenModelica_bilderbuchi/OMEdit/TestURI2.loadURI2 -outputPath=/tmp/OpenModelica_bilderbuchi/OMEdit/TestURI2.loadURI2
... loading "bla" from "modelica://TestURI2/somefile.txt"

Not possible to open file "modelica://TestURI2/somefile.txt": No such file or directory

simulation terminated by an assertion at initialization
Simulation process failed. Exited with code 255.
考虑到
loadResource
工作正常,我一点也不理解这一点


这里出了什么问题?

您需要在
combiTable1Ds
中的
文件名的修改器中使用
Modelica.Utilities.Files.loadResource
。请注意,
Modelica.Utilities.Files.loadResource
是一个坏名称,该函数不加载任何内容,它只是将Modelica URI转换为资源的绝对文件路径

within TestURI2;
model loadURI2
  Integer x;
  parameter String filepath = Modelica.Utilities.Files.loadResource("modelica://TestURI2/somefile.txt");
  Modelica.Blocks.Tables.CombiTable1Ds combiTable1Ds(fileName = filepath, tableName = "bla", tableOnFile = true)  annotation(Placement(visible = true, transformation(origin = {-60, 68}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
equation
combiTable1Ds.u = time;
x  = Modelica.Utilities.Strings.length(filepath);
end loadURI2;

谢谢,我昨晚也意识到了这个解决办法!我可以发誓我在某个地方读到过(OM tracker?MSL tracker?)Combi*表在内部使用
loadResource
解析路径,这就是为什么我希望它能工作的原因。不幸的是,我再也找不到相关的段落了。一个相关的问题是-起初我在
filename
参数中只有文件名。由于该文件与Modelica文件位于同一位置,因此我希望它能够正确解析。但是,OMEdit工作目录(似乎是解析的相关根目录)位于
/tmp/
中的某个位置,而不是文件所在的位置,因此它没有找到文件。那时我开始研究URI方法。虽然这有点难看,因为需要我定义一个包——我认为它不能与普通模型一起工作,至少现在它可以工作了。可执行文件将从当前目录中读取文件,如果是OMEdit,该目录位于%TMP%的某个位置。如果您通过omcscript.mos使用命令行,并在文件所在的位置运行命令,那么它就可以工作了。但最好只制作一个适当的Modelica库并使用URI。