Skip to content

Commit

Permalink
Ignore emit_protected flag for the encrypted models (#12043)
Browse files Browse the repository at this point in the history
* Ignore emit_protected flag for the encrypted models

* Fix build

* Fix

* Add try
  • Loading branch information
adeas31 committed Mar 4, 2024
1 parent 23752f8 commit d9aa256
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 36 deletions.
15 changes: 11 additions & 4 deletions OMCompiler/Compiler/NSimCode/NSimCode.mo
Expand Up @@ -270,7 +270,7 @@ public
// old SimCode strcutures
Absyn.Program program;
list<String> libs, includes, includeDirs, libPaths;
String directory;
String fileName, directory;
OldSimCodeFunction.MakefileParams makefileParams;
list<OldSimCodeFunction.Function> functions;
list<OldSimCodeFunction.RecordDeclaration> recordDecls;
Expand Down Expand Up @@ -396,8 +396,12 @@ public
(jac_blocks, simCodeIndices) := SimStrongComponent.Block.fixIndices(jac_blocks, {}, simCodeIndices);

generic_loop_calls := list(SimGenericCall.fromIdentifier(tpl) for tpl in UnorderedMap.toList(simCodeIndices.generic_call_map));

(modelInfo, simCodeIndices) := ModelInfo.create(vars, name, directory, functions, linearLoops, nonlinearLoops, bdae.eventInfo, simCodeIndices);
try
Absyn.CLASS(info = SOURCEINFO(fileName = fileName)) := InteractiveUtil.getPathedClassInProgram(name, program);
else
fileName := "";
end try;
(modelInfo, simCodeIndices) := ModelInfo.create(vars, name, fileName, directory, functions, linearLoops, nonlinearLoops, bdae.eventInfo, simCodeIndices);

simCode := SIM_CODE(
modelInfo = modelInfo,
Expand Down Expand Up @@ -567,6 +571,7 @@ public
uniontype ModelInfo
record MODEL_INFO
Absyn.Path name;
String fileName;
String description;
String directory;
SimVars vars;
Expand Down Expand Up @@ -595,6 +600,7 @@ public
function create
input SimVars vars;
input Absyn.Path name;
input String fileName;
input String directory;
input list<OldSimCodeFunction.Function> functions;
input list<SimStrongComponent.Block> linearLoops;
Expand All @@ -606,7 +612,7 @@ public
VarInfo info;
algorithm
info := VarInfo.create(vars, eventInfo, simCodeIndices);
modelInfo := MODEL_INFO(name, "", directory, vars, info, functions, {}, {}, {}, 0, 0, 0, true, linearLoops, nonlinearLoops);
modelInfo := MODEL_INFO(name, fileName, "", directory, vars, info, functions, {}, {}, {}, 0, 0, 0, true, linearLoops, nonlinearLoops);
end create;

function setSeedVars
Expand Down Expand Up @@ -635,6 +641,7 @@ public
varInfo := VarInfo.convert(modelInfo.varInfo);
oldModelInfo := OldSimCode.MODELINFO(
name = modelInfo.name,
fileName = modelInfo.fileName,
description = modelInfo.description,
directory = modelInfo.directory,
varInfo = VarInfo.convert(modelInfo.varInfo),
Expand Down
5 changes: 1 addition & 4 deletions OMCompiler/Compiler/Script/CevalScriptBackend.mo
Expand Up @@ -3468,7 +3468,6 @@ protected function callTranslateModel
output String outFileDir;
output list<tuple<String,Values.Value>> resultValues;
algorithm
(success, outCache, outStringLst, outFileDir, resultValues) :=
SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.NORMAL(), inCache, inEnv,
className, inFileNamePrefix, runBackend, Flags.getConfigBool(Flags.DAE_MODE), runSilent, inSimSettingsOpt, Absyn.FUNCTIONARGS({},{}));
Expand Down Expand Up @@ -4187,12 +4186,10 @@ protected function translateModelXML " author: Alachew
protected
Boolean success;
algorithm
(success,cache) := SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.XML(), cache, env, className,
fileNamePrefix, true, false, true, inSimSettingsOpt);
(success,cache) := SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.XML(), cache, env, className, fileNamePrefix, true, false, true, inSimSettingsOpt);
outValue := Values.STRING(if success then ((if not Testsuite.isRunning() then System.pwd() + Autoconf.pathDelimiter else "") + fileNamePrefix+".xml") else "");
end translateModelXML;


public function translateGraphics "function: translates the graphical annotations from old to new version"
input Absyn.Path className;
input Absyn.Msg inMsg;
Expand Down
1 change: 1 addition & 0 deletions OMCompiler/Compiler/SimCode/SimCode.mo
Expand Up @@ -262,6 +262,7 @@ end BaseUnit;
uniontype ModelInfo "Container for metadata about a Modelica model."
record MODELINFO
Absyn.Path name;
String fileName;
String description;
String directory;
VarInfo varInfo;
Expand Down
17 changes: 15 additions & 2 deletions OMCompiler/Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -121,6 +121,7 @@ import ZeroCrossings;
import ReduceDAE;
import Settings;
import UnorderedSet;
import InteractiveUtil;

protected constant String UNDERLINE = "========================================";

Expand Down Expand Up @@ -7676,7 +7677,7 @@ public function createModelInfo
input list<SimCodeVar.SimVar> tempVars;
output SimCode.ModelInfo modelInfo;
protected
String description, directory;
String fileName, description, directory;
SimCode.VarInfo varInfo;
SimCodeVar.SimVars vars;
Integer nx, ny, ndy, np, na, next, numOutVars, numInVars, ny_int, np_int, na_int, ny_bool, np_bool, dim_1, dim_2, numOptimizeConstraints, numOptimizeFinalConstraints, numRealInputVars;
Expand Down Expand Up @@ -7721,7 +7722,8 @@ algorithm
if debug then execStat("simCode: createVarInfo"); end if;
hasLargeEqSystems := hasLargeEquationSystems(dlow, inInitDAE);
if debug then execStat("simCode: hasLargeEquationSystems"); end if;
modelInfo := SimCode.MODELINFO(class_, dlow.shared.info.description, directory, varInfo, vars, functions,
Absyn.CLASS(info = SOURCEINFO(fileName = fileName)) := InteractiveUtil.getPathedClassInProgram(class_, program);
modelInfo := SimCode.MODELINFO(class_, fileName, dlow.shared.info.description, directory, varInfo, vars, functions,
labels,
if Flags.getConfigBool(Flags.BUILDING_FMU) then getResources(program.classes, dlow, inInitDAE) else {},
List.sort(program.classes, AbsynUtil.classNameGreater),
Expand Down Expand Up @@ -16045,5 +16047,16 @@ algorithm
end match;
end getExpNominal;

public function isMocFile
input String fileName;
output Integer result;
algorithm
if (StringUtil.endsWith(fileName, ".moc")) then
result := 1;
else
result := 0;
end if;
end isMocFile;

annotation(__OpenModelica_Interface="backend");
end SimCodeUtil;
1 change: 1 addition & 0 deletions OMCompiler/Compiler/Template/CodegenC.tpl
Expand Up @@ -1470,6 +1470,7 @@ template populateModelInfo(ModelInfo modelInfo, String fileNamePrefix, String gu
data->modelData->resultFileName = NULL;
data->modelData->modelDir = "<%directory%>";
data->modelData->modelGUID = "{<%guid%>}";
data->modelData->encrypted = <%SimCodeUtil.isMocFile(fileName)%>;
<% match isModelExchangeFMU
case "1.0" then
<<
Expand Down
6 changes: 6 additions & 0 deletions OMCompiler/Compiler/Template/SimCodeTV.mo
Expand Up @@ -766,6 +766,7 @@ package SimCode
uniontype ModelInfo
record MODELINFO
Absyn.Path name;
String fileName;
String description;
String directory;
VarInfo varInfo;
Expand Down Expand Up @@ -1432,6 +1433,11 @@ package SimCodeUtil
input DAE.Exp exp;
output DAE.Exp nom;
end getExpNominal;

function isMocFile
input String fileName;
output Integer result;
end isMocFile;
end SimCodeUtil;

package SimCodeFunctionUtil
Expand Down
40 changes: 20 additions & 20 deletions OMCompiler/SimulationRuntime/c/simulation/simulation_input_xml.c
Expand Up @@ -617,10 +617,10 @@ void read_input_xml(MODEL_DATA* modelData,
* the check is like this:
* - we filter if isProtected (protected variables)
* - we filter if annotation(HideResult=true)
* - we emit (remove filtering) if emitProtected && isProtected
* - we emit (remove filtering) if !encrypted && emitProtected && isProtected
* - we emit (remove filtering) if ignoreHideResult && annotation(HideResult=true)
*/
#define setFilterOuput(v, s, n) \
#define setFilterOuput(v, s, n, e) \
{ \
int ep = omc_flag[FLAG_EMIT_PROTECTED]; \
int ihr = omc_flag[FLAG_IGNORE_HIDERESULT]; \
Expand All @@ -638,7 +638,7 @@ void read_input_xml(MODEL_DATA* modelData,
infoStreamPrint(LOG_DEBUG, 0, "filtering variable %s due to HideResult annotation", (n)); \
(s).filterOutput = 1; \
} \
if (ep && ipcmptrue) \
if (!e && ep && ipcmptrue) \
{ \
infoStreamPrint(LOG_DEBUG, 0, "emitting protected variable %s due to flag %s", (n), omc_flagValue[FLAG_EMIT_PROTECTED]); \
(s).filterOutput = 0; \
Expand All @@ -651,7 +651,7 @@ void read_input_xml(MODEL_DATA* modelData,
}

/* read all static data from File for every variable */
#define READ_VARIABLES(out, in, attributeKind, read_var_attribute, debugName, start, nStates, mapAlias) \
#define READ_VARIABLES(out, in, attributeKind, read_var_attribute, debugName, start, nStates, mapAlias, e) \
infoStreamPrint(LOG_DEBUG, 1, "read xml file for %s", debugName); \
for(i = 0; i < nStates; i++) \
{ \
Expand All @@ -661,7 +661,7 @@ void read_input_xml(MODEL_DATA* modelData,
omc_ScalarVariable *v = *findHashLongVar(in, i); \
read_var_info(v, info); \
read_var_attribute(v, attribute); \
setFilterOuput(v, out[j], info->name); \
setFilterOuput(v, out[j], info->name, e); \
addHashStringLong(&mapAlias, info->name, j); /* create a mapping for Alias variable to get the correct index */ \
debugStreamPrint(LOG_DEBUG, 0, "real %s: mapAlias[%s] = %ld", debugName, info->name, (long) j); \
if (omc_flag[FLAG_IDAS] && 0 == strcmp(debugName, "real sensitivities")) \
Expand All @@ -677,22 +677,22 @@ void read_input_xml(MODEL_DATA* modelData,
} \
messageClose(LOG_DEBUG);

READ_VARIABLES(modelData->realVarsData,mi.rSta,REAL_ATTRIBUTE,read_var_attribute_real,"real states",0,modelData->nStates,mapAlias);
READ_VARIABLES(modelData->realVarsData,mi.rDer,REAL_ATTRIBUTE,read_var_attribute_real,"real state derivatives",modelData->nStates,modelData->nStates,mapAlias);
READ_VARIABLES(modelData->realVarsData,mi.rAlg,REAL_ATTRIBUTE,read_var_attribute_real,"real algebraics",2*modelData->nStates,modelData->nVariablesReal - 2*modelData->nStates,mapAlias);
READ_VARIABLES(modelData->realVarsData,mi.rSta,REAL_ATTRIBUTE,read_var_attribute_real,"real states",0,modelData->nStates,mapAlias,modelData->encrypted);
READ_VARIABLES(modelData->realVarsData,mi.rDer,REAL_ATTRIBUTE,read_var_attribute_real,"real state derivatives",modelData->nStates,modelData->nStates,mapAlias,modelData->encrypted);
READ_VARIABLES(modelData->realVarsData,mi.rAlg,REAL_ATTRIBUTE,read_var_attribute_real,"real algebraics",2*modelData->nStates,modelData->nVariablesReal - 2*modelData->nStates,mapAlias,modelData->encrypted);

READ_VARIABLES(modelData->integerVarsData,mi.iAlg,INTEGER_ATTRIBUTE,read_var_attribute_int,"integer variables",0,modelData->nVariablesInteger,mapAlias);
READ_VARIABLES(modelData->booleanVarsData,mi.bAlg,BOOLEAN_ATTRIBUTE,read_var_attribute_bool,"boolean variables",0,modelData->nVariablesBoolean,mapAlias);
READ_VARIABLES(modelData->stringVarsData,mi.sAlg,STRING_ATTRIBUTE,read_var_attribute_string,"string variables",0,modelData->nVariablesString,mapAlias);
READ_VARIABLES(modelData->integerVarsData,mi.iAlg,INTEGER_ATTRIBUTE,read_var_attribute_int,"integer variables",0,modelData->nVariablesInteger,mapAlias,modelData->encrypted);
READ_VARIABLES(modelData->booleanVarsData,mi.bAlg,BOOLEAN_ATTRIBUTE,read_var_attribute_bool,"boolean variables",0,modelData->nVariablesBoolean,mapAlias,modelData->encrypted);
READ_VARIABLES(modelData->stringVarsData,mi.sAlg,STRING_ATTRIBUTE,read_var_attribute_string,"string variables",0,modelData->nVariablesString,mapAlias,modelData->encrypted);

READ_VARIABLES(modelData->realParameterData,mi.rPar,REAL_ATTRIBUTE,read_var_attribute_real,"real parameters",0,modelData->nParametersReal,mapAliasParam);
READ_VARIABLES(modelData->integerParameterData,mi.iPar,INTEGER_ATTRIBUTE,read_var_attribute_int,"integer parameters",0,modelData->nParametersInteger,mapAliasParam);
READ_VARIABLES(modelData->booleanParameterData,mi.bPar,BOOLEAN_ATTRIBUTE,read_var_attribute_bool,"boolean parameters",0,modelData->nParametersBoolean,mapAliasParam);
READ_VARIABLES(modelData->stringParameterData,mi.sPar,STRING_ATTRIBUTE,read_var_attribute_string,"string parameters",0,modelData->nParametersString,mapAliasParam);
READ_VARIABLES(modelData->realParameterData,mi.rPar,REAL_ATTRIBUTE,read_var_attribute_real,"real parameters",0,modelData->nParametersReal,mapAliasParam,modelData->encrypted);
READ_VARIABLES(modelData->integerParameterData,mi.iPar,INTEGER_ATTRIBUTE,read_var_attribute_int,"integer parameters",0,modelData->nParametersInteger,mapAliasParam,modelData->encrypted);
READ_VARIABLES(modelData->booleanParameterData,mi.bPar,BOOLEAN_ATTRIBUTE,read_var_attribute_bool,"boolean parameters",0,modelData->nParametersBoolean,mapAliasParam,modelData->encrypted);
READ_VARIABLES(modelData->stringParameterData,mi.sPar,STRING_ATTRIBUTE,read_var_attribute_string,"string parameters",0,modelData->nParametersString,mapAliasParam,modelData->encrypted);

if (omc_flag[FLAG_IDAS])
{
READ_VARIABLES(modelData->realSensitivityData,mi.rSen,REAL_ATTRIBUTE,read_var_attribute_real,"real sensitivities",0, modelData->nSensitivityVars,mapAliasSen);
READ_VARIABLES(modelData->realSensitivityData,mi.rSen,REAL_ATTRIBUTE,read_var_attribute_real,"real sensitivities",0, modelData->nSensitivityVars,mapAliasSen,modelData->encrypted);
}

/*
Expand All @@ -712,7 +712,7 @@ void read_input_xml(MODEL_DATA* modelData,
}
infoStreamPrint(LOG_DEBUG, 0, "read for %s negated %d from setup file", modelData->realAlias[i].info.name, modelData->realAlias[i].negate);

setFilterOuput(*findHashLongVar(mi.rAli,i), modelData->realAlias[i], modelData->realAlias[i].info.name);
setFilterOuput(*findHashLongVar(mi.rAli,i), modelData->realAlias[i], modelData->realAlias[i].info.name, modelData->encrypted);

free((char*)aliasTmp);
aliasTmp = NULL;
Expand Down Expand Up @@ -758,7 +758,7 @@ void read_input_xml(MODEL_DATA* modelData,

infoStreamPrint(LOG_DEBUG, 0, "read for %s negated %d from setup file",modelData->integerAlias[i].info.name,modelData->integerAlias[i].negate);

setFilterOuput(*findHashLongVar(mi.iAli,i), modelData->integerAlias[i], modelData->integerAlias[i].info.name);
setFilterOuput(*findHashLongVar(mi.iAli,i), modelData->integerAlias[i], modelData->integerAlias[i].info.name, modelData->encrypted);

free((char*)aliasTmp);
aliasTmp = NULL;
Expand Down Expand Up @@ -802,7 +802,7 @@ void read_input_xml(MODEL_DATA* modelData,

infoStreamPrint(LOG_DEBUG, 0, "read for %s negated %d from setup file", modelData->booleanAlias[i].info.name, modelData->booleanAlias[i].negate);

setFilterOuput(*findHashLongVar(mi.bAli,i), modelData->booleanAlias[i], modelData->booleanAlias[i].info.name);
setFilterOuput(*findHashLongVar(mi.bAli,i), modelData->booleanAlias[i], modelData->booleanAlias[i].info.name, modelData->encrypted);

free((char*)aliasTmp);
aliasTmp = NULL;
Expand Down Expand Up @@ -845,7 +845,7 @@ void read_input_xml(MODEL_DATA* modelData,
}
infoStreamPrint(LOG_DEBUG, 0, "read for %s negated %d from setup file", modelData->stringAlias[i].info.name, modelData->stringAlias[i].negate);

setFilterOuput(*findHashLongVar(mi.sAli,i), modelData->stringAlias[i], modelData->stringAlias[i].info.name);
setFilterOuput(*findHashLongVar(mi.sAli,i), modelData->stringAlias[i], modelData->stringAlias[i].info.name, modelData->encrypted);

free((char*)aliasTmp);
aliasTmp = NULL;
Expand Down
1 change: 1 addition & 0 deletions OMCompiler/SimulationRuntime/c/simulation_data.h
Expand Up @@ -563,6 +563,7 @@ typedef struct MODEL_DATA
char* resultFileName; /* default is <modelFilePrefix>_res.mat, but it can be overriden using -r=<resultFilename> */
const char* modelDir;
const char* modelGUID;
modelica_boolean encrypted;
const char* initXMLData;
char* resourcesDir; /* Resources directory, only set for FMUs */
modelica_boolean runTestsuite; /* true if this model was generated during testing */
Expand Down
8 changes: 5 additions & 3 deletions OMEdit/OMEditLIB/Plotting/VariablesWidget.cpp
Expand Up @@ -609,11 +609,13 @@ void VariablesTreeModel::parseInitXml(QXmlStreamReader &xmlReader, SimulationOpt
bool hideResultIsFalse = scalarVariable.value("hideResult").compare(QStringLiteral("false")) == 0;
bool isProtected = scalarVariable.value("isProtected").compare(QStringLiteral("true")) == 0;
/* Skip variables,
* 1. If ignoreHideResult is not set and hideResult is true.
* 2. If emit protected flag is false and variable is protected.
* 1. If encrytped model.
* 2. If ignoreHideResult is not set and hideResult is true.
* 3. If emit protected flag is false and variable is protected.
* If hideResult is false for protected variable then we show it.
*/
if ((ignoreHideResult || !hideResultIsTrue)
if (!simulationOptions.getFileName().endsWith(".moc")
&& (ignoreHideResult || !hideResultIsTrue)
&& (protectedVariables || (!isProtected || (!ignoreHideResult && hideResultIsFalse)))) {
mScalarVariablesHash.insert(scalarVariable.value("name"),scalarVariable);
if (addVariablesToList) {
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/Simulation/SimulationDialog.cpp
Expand Up @@ -503,7 +503,7 @@ void SimulationDialog::setUpForm()
mpVariableFilterHelpButton->setToolTip(tr("Variable Filter help"));
connect(mpVariableFilterHelpButton, SIGNAL(clicked()), SLOT(showVariableFilterHelp()));
// Protected Variabels
mpProtectedVariablesCheckBox = new QCheckBox(tr("Protected Variables"));
mpProtectedVariablesCheckBox = new QCheckBox(tr("Protected Variables if not encrypted"));
// ignore hide result
mpIgnoreHideResultCheckBox = new QCheckBox(tr("Ignore HideResult"));
// Equidistant time grid
Expand Down
4 changes: 2 additions & 2 deletions doc/UsersGuide/source/omedit.rst
Expand Up @@ -855,7 +855,7 @@ Output
- *Variable Filter (Optional)* - only output variables with names fully matching the
regular expression.

- *Protected Variables* - adds the protected variables in result file.
- *Protected Variables if not encrypted* - adds the protected variables in result file.

- *Equidistant Time Grid -* output the internal steps given by dassl instead of
interpolating results into an equidistant time grid as given by stepSize or
Expand Down Expand Up @@ -1446,7 +1446,7 @@ General Options

- *Show Protected Classes* - If enabled then Libraries Browser will also list the protected classes.

- *Show Hidden Classes* - If enabled then Libraries Browser will also list the hidden classes.
- *Show Hidden Classes if not encrypted* - If enabled then Libraries Browser will also list the hidden classes.
Ignores the annotation(Protection(access = Access.hide))

- *Synchronize with Model Widget* - If enabled then Libraries Browser will scroll automatically
Expand Down

0 comments on commit d9aa256

Please sign in to comment.