Skip to content

Commit

Permalink
Added importFMUModelDescription API
Browse files Browse the repository at this point in the history
  • Loading branch information
alash325 authored and OpenModelica-Hudson committed Sep 30, 2016
1 parent 64c43c2 commit f9cfd80
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
15 changes: 15 additions & 0 deletions Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -2348,6 +2348,21 @@ external "builtin";
annotation(preferredView="text");
end importFMU;

function importFMUModelDescription "Imports modelDescription.xml
Example command:
importFMUModelDescription(\"A.xml\");"
input String filename "the fmu file name";
input String workdir = "<default>" "The output directory for imported FMU files. <default> will put the files to current working directory.";
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";
input Boolean fullPath = false "When true the full output path is returned otherwise only the file name.";
input Boolean debugLogging = false "When true the FMU's debug output is printed.";
input Boolean generateInputConnectors = true "When true creates the input connector pins.";
input Boolean generateOutputConnectors = true "When true creates the output connector pins.";
output String generatedFileName "Returns the full path of the generated file.";
external "builtin";
annotation(preferredView="text");
end importFMUModelDescription;

function translateModelFMU
"translates a modelica model into a Functional Mockup Unit.
The only required argument is the className, while all others have some default values.
Expand Down
28 changes: 27 additions & 1 deletion Compiler/Script/CevalScriptBackend.mo
Expand Up @@ -674,7 +674,7 @@ algorithm
title,xLabel,yLabel,filename2,varNameStr,xml_filename,xml_contents,visvar_str,pwd,omhome,omlib,omcpath,os,
platform,usercflags,senddata,res,workdir,gcc,confcmd,touch_file,uname,filenameprefix,compileDir,libDir,exeDir,configDir,from,to,
gridStr, logXStr, logYStr, x1Str, x2Str, y1Str, y2Str, curveWidthStr, curveStyleStr, legendPosition, footer, autoScaleStr,scriptFile,logFile, simflags2, outputFile,
systemPath, gccVersion, gd, strlinearizeTime, suffix,cname;
systemPath, gccVersion, gd, strlinearizeTime, suffix,cname, modeldescriptionfilename;
list<DAE.Exp> simOptions;
list<Values.Value> vals;
Absyn.Path path,classpath,className,baseClassPath;
Expand Down Expand Up @@ -1549,6 +1549,32 @@ algorithm
then
(cache,Values.STRING(""),st);

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,_)
equation
Error.clearMessages() "Clear messages";
true = System.regularFileExists(filename);
workdir = if System.directoryExists(workdir) then workdir else System.pwd();
modeldescriptionfilename="modelDescription.fmu";
System.systemCall("zip " + modeldescriptionfilename + " " + filename);
true = System.regularFileExists(modeldescriptionfilename);
/* Initialize FMI objects */
(b, fmiContext, fmiInstance, fmiInfo, fmiTypeDefinitionsList, fmiExperimentAnnotation, fmiModelVariablesInstance, fmiModelVariablesList) = FMIExt.initializeFMIImport(modeldescriptionfilename, workdir, fmiLogLevel, inputConnectors, outputConnectors);
true = b; /* if something goes wrong while initializing */
fmiTypeDefinitionsList = listReverse(fmiTypeDefinitionsList);
fmiModelVariablesList = listReverse(fmiModelVariablesList);
s1 = System.tolower(System.platform());
str = Tpl.tplString(CodegenFMU.importFMUModelDescription, FMI.FMIIMPORT(s1, modeldescriptionfilename, workdir, fmiLogLevel, b2, fmiContext, fmiInstance, fmiInfo, fmiTypeDefinitionsList, fmiExperimentAnnotation, fmiModelVariablesInstance, fmiModelVariablesList, inputConnectors, outputConnectors));
pd = System.pathDelimiter();
str1 = FMI.getFMIModelIdentifier(fmiInfo);
str3 = FMI.getFMIVersion(fmiInfo);
outputFile = stringAppendList({workdir,pd,str1,"_Input_Output_FMU.mo"});
filename_1 = if b1 then stringAppendList({workdir,pd,str1,"_Input_Output_FMU.mo"}) else stringAppendList({str1,"_Input_Output_FMU.mo"});
System.writeFile(outputFile, str);
/* Release FMI objects */
FMIExt.releaseFMIImport(fmiModelVariablesInstance, fmiInstance, fmiContext, str3);
then
(cache,Values.STRING(filename_1),st);

case (cache,_,"getIndexReductionMethod",_,st,_)
equation
str = Config.getIndexReductionMethod();
Expand Down
70 changes: 69 additions & 1 deletion Compiler/Template/CodegenFMU.tpl
Expand Up @@ -1344,6 +1344,75 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simula
>>
end fmudeffile;

template importFMUModelDescription(FmiImport fmi)
"Generates Modelica code for FMU model description"
::=
match fmi
case FMIIMPORT(fmiInfo=INFO(__),fmiExperimentAnnotation=EXPERIMENTANNOTATION(__)) then
<<
model <%fmiInfo.fmiModelIdentifier%>_<%getFMIType(fmiInfo)%>_FMU<%if stringEq(fmiInfo.fmiDescription, "") then "" else " \""+fmiInfo.fmiDescription+"\""%>
<%dumpFMITypeDefinitions(fmiTypeDefinitionsList)%>
<%dumpFMUModelDescriptionVariablesList("1.0", fmiModelVariablesList, fmiTypeDefinitionsList, generateInputConnectors, generateOutputConnectors)%>
end <%fmiInfo.fmiModelIdentifier%>_<%getFMIType(fmiInfo)%>_FMU;
>>
end importFMUModelDescription;

template dumpFMUModelDescriptionVariablesList(String FMUVersion, list<ModelVariables> fmiModelVariablesList, list<TypeDefinitions> fmiTypeDefinitionsList, Boolean generateInputConnectors, Boolean generateOutputConnectors)
"Generates the Model Variables code."
::=
<<
<%fmiModelVariablesList |> fmiModelVariable => dumpFMUModelDescriptionVariable(FMUVersion, fmiModelVariable, fmiTypeDefinitionsList, generateInputConnectors, generateOutputConnectors) ;separator="\n"%>
>>
end dumpFMUModelDescriptionVariablesList;

template dumpFMUModelDescriptionVariable(String FMUVersion, ModelVariables fmiModelVariable, list<TypeDefinitions> fmiTypeDefinitionsList, Boolean generateInputConnectors, Boolean generateOutputConnectors)
::=
match FMUVersion
case "1.0" then
match fmiModelVariable
case REALVARIABLE(__) then
let isInputOrOutput = if boolOr(stringEq(causality, "input"), stringEq(causality, "output")) then true
if isInputOrOutput then
<<
<%dumpFMUModelDescriptionInputOutputVariable(name, causality, baseType, generateInputConnectors, generateOutputConnectors)%> <%dumpFMIModelVariableDescription(description)%><%dumpFMIModelVariablePlacementAnnotation(x1Placement, x2Placement, y1Placement, y2Placement, generateInputConnectors, generateOutputConnectors, causality)%>;
>>
case INTEGERVARIABLE(__) then
let isInputOrOutput = if boolOr(stringEq(causality, "input"), stringEq(causality, "output")) then true
if isInputOrOutput then
<<
<%dumpFMUModelDescriptionInputOutputVariable(name, causality, baseType, generateInputConnectors, generateOutputConnectors)%> <%dumpFMIModelVariableDescription(description)%><%dumpFMIModelVariablePlacementAnnotation(x1Placement, x2Placement, y1Placement, y2Placement, generateInputConnectors, generateOutputConnectors, causality)%>;
>>
case BOOLEANVARIABLE(__) then
let isInputOrOutput = if boolOr(stringEq(causality, "input"), stringEq(causality, "output")) then true
if isInputOrOutput then
<<
<%dumpFMUModelDescriptionInputOutputVariable(name, causality, baseType, generateInputConnectors, generateOutputConnectors)%> <%dumpFMIModelVariableDescription(description)%><%dumpFMIModelVariablePlacementAnnotation(x1Placement, x2Placement, y1Placement, y2Placement, generateInputConnectors, generateOutputConnectors, causality)%>;
>>
case STRINGVARIABLE(__) then
let isInputOrOutput = if boolOr(stringEq(causality, "input"), stringEq(causality, "output")) then true
if isInputOrOutput then
<<
<%dumpFMUModelDescriptionInputOutputVariable(name, causality, baseType, generateInputConnectors, generateOutputConnectors)%> <%dumpFMIModelVariableDescription(description)%><%dumpFMIModelVariablePlacementAnnotation(x1Placement, x2Placement, y1Placement, y2Placement, generateInputConnectors, generateOutputConnectors, causality)%>;
>>
case ENUMERATIONVARIABLE(__) then
let isInputOrOutput = if boolOr(stringEq(causality, "input"), stringEq(causality, "output")) then true
if isInputOrOutput then
<<
<%dumpFMUModelDescriptionInputOutputVariable(name, causality, baseType, generateInputConnectors, generateOutputConnectors)%> <%dumpFMIModelVariableDescription(description)%><%dumpFMIModelVariablePlacementAnnotation(x1Placement, x2Placement, y1Placement, y2Placement, generateInputConnectors, generateOutputConnectors, causality)%>;
>>
end match
end dumpFMUModelDescriptionVariable;

template dumpFMUModelDescriptionInputOutputVariable(String name, String causality, String baseType, Boolean generateInputConnectors, Boolean generateOutputConnectors)
::=
if boolAnd(generateInputConnectors, boolAnd(stringEq(causality, "input"),stringEq(baseType, "Real"))) then "Modelica.Blocks.Interfaces.RealInput "+name+""
else if boolAnd(generateInputConnectors, boolAnd(stringEq(causality, "input"),stringEq(baseType, "Integer"))) then "Modelica.Blocks.Interfaces.IntegerInput "+name+""
else if boolAnd(generateInputConnectors, boolAnd(stringEq(causality, "input"),stringEq(baseType, "Boolean"))) then "Modelica.Blocks.Interfaces.BooleanInput "+name+""
else if boolAnd(generateOutputConnectors, boolAnd(stringEq(causality, "output"),stringEq(baseType, "Real"))) then "Modelica.Blocks.Interfaces.RealOutput "+name+""
else if boolAnd(generateOutputConnectors, boolAnd(stringEq(causality, "output"),stringEq(baseType, "Integer"))) then "Modelica.Blocks.Interfaces.IntegerOutput "+name+""
else if boolAnd(generateOutputConnectors, boolAnd(stringEq(causality, "output"),stringEq(baseType, "Boolean"))) then "Modelica.Blocks.Interfaces.BooleanOutput "+name+""
end dumpFMUModelDescriptionInputOutputVariable;

template importFMUModelica(FmiImport fmi)
"Generates the Modelica code depending on the FMU type."
::=
Expand Down Expand Up @@ -2478,7 +2547,6 @@ template dumpFMIModelVariableCausalityAndBaseType(String causality, String baseT
else if boolAnd(generateOutputConnectors, boolAnd(stringEq(causality, "output"),stringEq(baseType, "Real"))) then "Modelica.Blocks.Interfaces.RealOutput"
else if boolAnd(generateOutputConnectors, boolAnd(stringEq(causality, "output"),stringEq(baseType, "Integer"))) then "Modelica.Blocks.Interfaces.IntegerOutput"
else if boolAnd(generateOutputConnectors, boolAnd(stringEq(causality, "output"),stringEq(baseType, "Boolean"))) then "Modelica.Blocks.Interfaces.BooleanOutput"
else if stringEq(causality, "") then baseType else causality+" "+baseType
end dumpFMIModelVariableCausalityAndBaseType;

template dumpFMIModelVariableCausality(String causality)
Expand Down

0 comments on commit f9cfd80

Please sign in to comment.