Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit f9cfd80

Browse files
alash325OpenModelica-Hudson
authored andcommitted
Added importFMUModelDescription API
1 parent 64c43c2 commit f9cfd80

3 files changed

Lines changed: 111 additions & 2 deletions

File tree

Compiler/FrontEnd/ModelicaBuiltin.mo

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,6 +2348,21 @@ external "builtin";
23482348
annotation(preferredView="text");
23492349
end importFMU;
23502350

2351+
function importFMUModelDescription "Imports modelDescription.xml
2352+
Example command:
2353+
importFMUModelDescription(\"A.xml\");"
2354+
input String filename "the fmu file name";
2355+
input String workdir = "<default>" "The output directory for imported FMU files. <default> will put the files to current working directory.";
2356+
input Integer loglevel = 3 "loglevel_nothing=0;loglevel_fatal=1;loglevel_error=2;loglevel_warning=3;loglevel_info=4;loglevel_verbose=5;loglevel_debug=6";
2357+
input Boolean fullPath = false "When true the full output path is returned otherwise only the file name.";
2358+
input Boolean debugLogging = false "When true the FMU's debug output is printed.";
2359+
input Boolean generateInputConnectors = true "When true creates the input connector pins.";
2360+
input Boolean generateOutputConnectors = true "When true creates the output connector pins.";
2361+
output String generatedFileName "Returns the full path of the generated file.";
2362+
external "builtin";
2363+
annotation(preferredView="text");
2364+
end importFMUModelDescription;
2365+
23512366
function translateModelFMU
23522367
"translates a modelica model into a Functional Mockup Unit.
23532368
The only required argument is the className, while all others have some default values.

Compiler/Script/CevalScriptBackend.mo

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ algorithm
674674
title,xLabel,yLabel,filename2,varNameStr,xml_filename,xml_contents,visvar_str,pwd,omhome,omlib,omcpath,os,
675675
platform,usercflags,senddata,res,workdir,gcc,confcmd,touch_file,uname,filenameprefix,compileDir,libDir,exeDir,configDir,from,to,
676676
gridStr, logXStr, logYStr, x1Str, x2Str, y1Str, y2Str, curveWidthStr, curveStyleStr, legendPosition, footer, autoScaleStr,scriptFile,logFile, simflags2, outputFile,
677-
systemPath, gccVersion, gd, strlinearizeTime, suffix,cname;
677+
systemPath, gccVersion, gd, strlinearizeTime, suffix,cname, modeldescriptionfilename;
678678
list<DAE.Exp> simOptions;
679679
list<Values.Value> vals;
680680
Absyn.Path path,classpath,className,baseClassPath;
@@ -1549,6 +1549,32 @@ algorithm
15491549
then
15501550
(cache,Values.STRING(""),st);
15511551

1552+
case (cache,_,"importFMUModelDescription",{Values.STRING(filename), Values.STRING(workdir),Values.INTEGER(fmiLogLevel),Values.BOOL(b1), Values.BOOL(b2), Values.BOOL(inputConnectors), Values.BOOL(outputConnectors)},st,_)
1553+
equation
1554+
Error.clearMessages() "Clear messages";
1555+
true = System.regularFileExists(filename);
1556+
workdir = if System.directoryExists(workdir) then workdir else System.pwd();
1557+
modeldescriptionfilename="modelDescription.fmu";
1558+
System.systemCall("zip " + modeldescriptionfilename + " " + filename);
1559+
true = System.regularFileExists(modeldescriptionfilename);
1560+
/* Initialize FMI objects */
1561+
(b, fmiContext, fmiInstance, fmiInfo, fmiTypeDefinitionsList, fmiExperimentAnnotation, fmiModelVariablesInstance, fmiModelVariablesList) = FMIExt.initializeFMIImport(modeldescriptionfilename, workdir, fmiLogLevel, inputConnectors, outputConnectors);
1562+
true = b; /* if something goes wrong while initializing */
1563+
fmiTypeDefinitionsList = listReverse(fmiTypeDefinitionsList);
1564+
fmiModelVariablesList = listReverse(fmiModelVariablesList);
1565+
s1 = System.tolower(System.platform());
1566+
str = Tpl.tplString(CodegenFMU.importFMUModelDescription, FMI.FMIIMPORT(s1, modeldescriptionfilename, workdir, fmiLogLevel, b2, fmiContext, fmiInstance, fmiInfo, fmiTypeDefinitionsList, fmiExperimentAnnotation, fmiModelVariablesInstance, fmiModelVariablesList, inputConnectors, outputConnectors));
1567+
pd = System.pathDelimiter();
1568+
str1 = FMI.getFMIModelIdentifier(fmiInfo);
1569+
str3 = FMI.getFMIVersion(fmiInfo);
1570+
outputFile = stringAppendList({workdir,pd,str1,"_Input_Output_FMU.mo"});
1571+
filename_1 = if b1 then stringAppendList({workdir,pd,str1,"_Input_Output_FMU.mo"}) else stringAppendList({str1,"_Input_Output_FMU.mo"});
1572+
System.writeFile(outputFile, str);
1573+
/* Release FMI objects */
1574+
FMIExt.releaseFMIImport(fmiModelVariablesInstance, fmiInstance, fmiContext, str3);
1575+
then
1576+
(cache,Values.STRING(filename_1),st);
1577+
15521578
case (cache,_,"getIndexReductionMethod",_,st,_)
15531579
equation
15541580
str = Config.getIndexReductionMethod();

Compiler/Template/CodegenFMU.tpl

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,75 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simula
13441344
>>
13451345
end fmudeffile;
13461346
1347+
template importFMUModelDescription(FmiImport fmi)
1348+
"Generates Modelica code for FMU model description"
1349+
::=
1350+
match fmi
1351+
case FMIIMPORT(fmiInfo=INFO(__),fmiExperimentAnnotation=EXPERIMENTANNOTATION(__)) then
1352+
<<
1353+
model <%fmiInfo.fmiModelIdentifier%>_<%getFMIType(fmiInfo)%>_FMU<%if stringEq(fmiInfo.fmiDescription, "") then "" else " \""+fmiInfo.fmiDescription+"\""%>
1354+
<%dumpFMITypeDefinitions(fmiTypeDefinitionsList)%>
1355+
<%dumpFMUModelDescriptionVariablesList("1.0", fmiModelVariablesList, fmiTypeDefinitionsList, generateInputConnectors, generateOutputConnectors)%>
1356+
end <%fmiInfo.fmiModelIdentifier%>_<%getFMIType(fmiInfo)%>_FMU;
1357+
>>
1358+
end importFMUModelDescription;
1359+
1360+
template dumpFMUModelDescriptionVariablesList(String FMUVersion, list<ModelVariables> fmiModelVariablesList, list<TypeDefinitions> fmiTypeDefinitionsList, Boolean generateInputConnectors, Boolean generateOutputConnectors)
1361+
"Generates the Model Variables code."
1362+
::=
1363+
<<
1364+
<%fmiModelVariablesList |> fmiModelVariable => dumpFMUModelDescriptionVariable(FMUVersion, fmiModelVariable, fmiTypeDefinitionsList, generateInputConnectors, generateOutputConnectors) ;separator="\n"%>
1365+
>>
1366+
end dumpFMUModelDescriptionVariablesList;
1367+
1368+
template dumpFMUModelDescriptionVariable(String FMUVersion, ModelVariables fmiModelVariable, list<TypeDefinitions> fmiTypeDefinitionsList, Boolean generateInputConnectors, Boolean generateOutputConnectors)
1369+
::=
1370+
match FMUVersion
1371+
case "1.0" then
1372+
match fmiModelVariable
1373+
case REALVARIABLE(__) then
1374+
let isInputOrOutput = if boolOr(stringEq(causality, "input"), stringEq(causality, "output")) then true
1375+
if isInputOrOutput then
1376+
<<
1377+
<%dumpFMUModelDescriptionInputOutputVariable(name, causality, baseType, generateInputConnectors, generateOutputConnectors)%> <%dumpFMIModelVariableDescription(description)%><%dumpFMIModelVariablePlacementAnnotation(x1Placement, x2Placement, y1Placement, y2Placement, generateInputConnectors, generateOutputConnectors, causality)%>;
1378+
>>
1379+
case INTEGERVARIABLE(__) then
1380+
let isInputOrOutput = if boolOr(stringEq(causality, "input"), stringEq(causality, "output")) then true
1381+
if isInputOrOutput then
1382+
<<
1383+
<%dumpFMUModelDescriptionInputOutputVariable(name, causality, baseType, generateInputConnectors, generateOutputConnectors)%> <%dumpFMIModelVariableDescription(description)%><%dumpFMIModelVariablePlacementAnnotation(x1Placement, x2Placement, y1Placement, y2Placement, generateInputConnectors, generateOutputConnectors, causality)%>;
1384+
>>
1385+
case BOOLEANVARIABLE(__) then
1386+
let isInputOrOutput = if boolOr(stringEq(causality, "input"), stringEq(causality, "output")) then true
1387+
if isInputOrOutput then
1388+
<<
1389+
<%dumpFMUModelDescriptionInputOutputVariable(name, causality, baseType, generateInputConnectors, generateOutputConnectors)%> <%dumpFMIModelVariableDescription(description)%><%dumpFMIModelVariablePlacementAnnotation(x1Placement, x2Placement, y1Placement, y2Placement, generateInputConnectors, generateOutputConnectors, causality)%>;
1390+
>>
1391+
case STRINGVARIABLE(__) then
1392+
let isInputOrOutput = if boolOr(stringEq(causality, "input"), stringEq(causality, "output")) then true
1393+
if isInputOrOutput then
1394+
<<
1395+
<%dumpFMUModelDescriptionInputOutputVariable(name, causality, baseType, generateInputConnectors, generateOutputConnectors)%> <%dumpFMIModelVariableDescription(description)%><%dumpFMIModelVariablePlacementAnnotation(x1Placement, x2Placement, y1Placement, y2Placement, generateInputConnectors, generateOutputConnectors, causality)%>;
1396+
>>
1397+
case ENUMERATIONVARIABLE(__) then
1398+
let isInputOrOutput = if boolOr(stringEq(causality, "input"), stringEq(causality, "output")) then true
1399+
if isInputOrOutput then
1400+
<<
1401+
<%dumpFMUModelDescriptionInputOutputVariable(name, causality, baseType, generateInputConnectors, generateOutputConnectors)%> <%dumpFMIModelVariableDescription(description)%><%dumpFMIModelVariablePlacementAnnotation(x1Placement, x2Placement, y1Placement, y2Placement, generateInputConnectors, generateOutputConnectors, causality)%>;
1402+
>>
1403+
end match
1404+
end dumpFMUModelDescriptionVariable;
1405+
1406+
template dumpFMUModelDescriptionInputOutputVariable(String name, String causality, String baseType, Boolean generateInputConnectors, Boolean generateOutputConnectors)
1407+
::=
1408+
if boolAnd(generateInputConnectors, boolAnd(stringEq(causality, "input"),stringEq(baseType, "Real"))) then "Modelica.Blocks.Interfaces.RealInput "+name+""
1409+
else if boolAnd(generateInputConnectors, boolAnd(stringEq(causality, "input"),stringEq(baseType, "Integer"))) then "Modelica.Blocks.Interfaces.IntegerInput "+name+""
1410+
else if boolAnd(generateInputConnectors, boolAnd(stringEq(causality, "input"),stringEq(baseType, "Boolean"))) then "Modelica.Blocks.Interfaces.BooleanInput "+name+""
1411+
else if boolAnd(generateOutputConnectors, boolAnd(stringEq(causality, "output"),stringEq(baseType, "Real"))) then "Modelica.Blocks.Interfaces.RealOutput "+name+""
1412+
else if boolAnd(generateOutputConnectors, boolAnd(stringEq(causality, "output"),stringEq(baseType, "Integer"))) then "Modelica.Blocks.Interfaces.IntegerOutput "+name+""
1413+
else if boolAnd(generateOutputConnectors, boolAnd(stringEq(causality, "output"),stringEq(baseType, "Boolean"))) then "Modelica.Blocks.Interfaces.BooleanOutput "+name+""
1414+
end dumpFMUModelDescriptionInputOutputVariable;
1415+
13471416
template importFMUModelica(FmiImport fmi)
13481417
"Generates the Modelica code depending on the FMU type."
13491418
::=
@@ -2478,7 +2547,6 @@ template dumpFMIModelVariableCausalityAndBaseType(String causality, String baseT
24782547
else if boolAnd(generateOutputConnectors, boolAnd(stringEq(causality, "output"),stringEq(baseType, "Real"))) then "Modelica.Blocks.Interfaces.RealOutput"
24792548
else if boolAnd(generateOutputConnectors, boolAnd(stringEq(causality, "output"),stringEq(baseType, "Integer"))) then "Modelica.Blocks.Interfaces.IntegerOutput"
24802549
else if boolAnd(generateOutputConnectors, boolAnd(stringEq(causality, "output"),stringEq(baseType, "Boolean"))) then "Modelica.Blocks.Interfaces.BooleanOutput"
2481-
else if stringEq(causality, "") then baseType else causality+" "+baseType
24822550
end dumpFMIModelVariableCausalityAndBaseType;
24832551

24842552
template dumpFMIModelVariableCausality(String causality)

0 commit comments

Comments
 (0)