Skip to content

Commit 26432f3

Browse files
committed
- Automatically load dependencies of a model (uses annotation) upon instantiation git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@11736 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent fed810e commit 26432f3

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
lines changed

Compiler/FrontEnd/ModelicaBuiltin.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,7 @@ end generateCode;
15711571
function loadModel "Loads the Modelica Standard Library."
15721572
input TypeName className;
15731573
input String[:] priorityVersion := {"default"};
1574+
input Boolean notify := false "Give a notification of the libraries and versions that were loaded";
15741575
output Boolean success;
15751576
external "builtin";
15761577
annotation(Documentation(info="<html>

Compiler/Script/CevalScript.mo

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -565,28 +565,32 @@ protected function loadModel
565565
input String modelicaPath;
566566
input Absyn.Program ip;
567567
input Boolean forceLoad;
568+
input Boolean notifyLoad;
568569
output Absyn.Program pnew;
569570
output Boolean success;
570571
algorithm
571-
(pnew,success) := matchcontinue (imodelsToLoad,modelicaPath,ip,forceLoad)
572+
(pnew,success) := matchcontinue (imodelsToLoad,modelicaPath,ip,forceLoad,notifyLoad)
572573
local
573574
Absyn.Path path;
574-
String pathStr,versions;
575+
String pathStr,versions,className,version;
575576
list<String> strings;
576577
Boolean b,b1,b2;
577578
Absyn.Program p;
578579
list<tuple<Absyn.Path,list<String>>> modelsToLoad;
579580

580-
case ({},_,p,_) then (p,true);
581-
case ((path,strings)::modelsToLoad,modelicaPath,p,forceLoad)
581+
case ({},_,p,_,_) then (p,true);
582+
case ((path,strings)::modelsToLoad,modelicaPath,p,forceLoad,notifyLoad)
582583
equation
583584
b = checkModelLoaded((path,strings),p,forceLoad,NONE());
584585
pnew = Debug.bcallret3(not b, ClassLoader.loadClass, path, strings, modelicaPath, Absyn.PROGRAM({},Absyn.TOP(),Absyn.dummyTimeStamp));
586+
className = Absyn.pathString(path);
587+
version = Debug.bcallret2(not b, getPackageVersion, path, pnew, "");
588+
Error.assertionOrAddSourceMessage(b or not notifyLoad,Error.NOTIFY_NOT_LOADED,{className,version},Absyn.dummyInfo);
585589
p = Interactive.updateProgram(pnew, p);
586-
(p,b1) = loadModel(Interactive.getUsesAnnotationOrDefault(pnew),modelicaPath,p,false);
587-
(p,b2) = loadModel(modelsToLoad,modelicaPath,p,forceLoad);
590+
(p,b1) = loadModel(Interactive.getUsesAnnotationOrDefault(pnew),modelicaPath,p,false,notifyLoad);
591+
(p,b2) = loadModel(modelsToLoad,modelicaPath,p,forceLoad,notifyLoad);
588592
then (p,b1 and b2);
589-
case ((path,strings)::_,modelicaPath,p,_)
593+
case ((path,strings)::_,modelicaPath,p,_,_)
590594
equation
591595
pathStr = Absyn.pathString(path);
592596
versions = stringDelimitList(strings,",");
@@ -1429,9 +1433,7 @@ algorithm
14291433

14301434
case (cache,env,"getVersion",{Values.CODE(Absyn.C_TYPENAME(path))},st as Interactive.SYMBOLTABLE(ast=p),msg)
14311435
equation
1432-
Config.setEvaluateParametersInAnnotations(true);
1433-
Absyn.STRING(str_1) = Interactive.getNamedAnnotation(path, p, "version", SOME(Absyn.STRING("")), Interactive.getAnnotationExp);
1434-
Config.setEvaluateParametersInAnnotations(false);
1436+
str_1 = getPackageVersion(path,p);
14351437
then
14361438
(cache,Values.STRING(str_1),st);
14371439

@@ -1572,7 +1574,7 @@ algorithm
15721574
case (cache,env,"generateSeparateCode",{},st,msg)
15731575
then (cache,Values.BOOL(false),st);
15741576

1575-
case (cache,env,"loadModel",{Values.CODE(Absyn.C_TYPENAME(path)),Values.ARRAY(valueLst=cvars)},
1577+
case (cache,env,"loadModel",{Values.CODE(Absyn.C_TYPENAME(path)),Values.ARRAY(valueLst=cvars),Values.BOOL(b)},
15761578
(st as Interactive.SYMBOLTABLE(
15771579
ast = p,depends=aDep,instClsLst = ic,
15781580
lstVarVal = iv,compiledFunctions = cf,
@@ -1582,7 +1584,7 @@ algorithm
15821584
equation
15831585
mp = Settings.getModelicaPath();
15841586
strings = List.map(cvars, ValuesUtil.extractValueString);
1585-
(p,b) = loadModel({(path,strings)},mp,p,true);
1587+
(p,b) = loadModel({(path,strings)},mp,p,true,b);
15861588
str = Print.getString();
15871589
newst = Interactive.SYMBOLTABLE(p,aDep,NONE(),{},iv,cf,lf);
15881590
then
@@ -2589,8 +2591,7 @@ algorithm
25892591
re = Absyn.restrString(restriction);
25902592
Error.assertionOrAddSourceMessage(relaxedFrontEnd or not (Absyn.isFunctionRestriction(restriction) or Absyn.isPackageRestriction(restriction)),
25912593
Error.INST_INVALID_RESTRICTION,{str,re},Absyn.dummyInfo);
2592-
usedModels = Interactive.getUsesAnnotation(Absyn.PROGRAM({absynClass},Absyn.TOP(),Absyn.dummyTimeStamp));
2593-
_ = List.map3(usedModels, checkModelLoaded, p, false, SOME(str));
2594+
(p,true) = loadModel(Interactive.getUsesAnnotationOrDefault(Absyn.PROGRAM({absynClass},Absyn.TOP(),Absyn.dummyTimeStamp)),Settings.getModelicaPath(),p,false,true);
25942595

25952596
ptot = Dependency.getTotalProgram(className,p);
25962597

@@ -2672,14 +2673,6 @@ algorithm
26722673

26732674
case (cache,env,className,st as Interactive.SYMBOLTABLE(ast=p),fileNamePrefix,addDummy,inSimSettingsOpt)
26742675
equation
2675-
(absynClass as Absyn.CLASS(restriction = restriction)) = Interactive.getPathedClassInProgram(className, p);
2676-
str = Absyn.pathString(className);
2677-
re = Absyn.restrString(restriction);
2678-
Error.assertionOrAddSourceMessage(not (Absyn.isFunctionRestriction(restriction) or Absyn.isPackageRestriction(restriction)),
2679-
Error.INST_INVALID_RESTRICTION,{str,re},Absyn.dummyInfo);
2680-
usedModels = Interactive.getUsesAnnotation(Absyn.PROGRAM({absynClass},Absyn.TOP(),Absyn.dummyTimeStamp));
2681-
_ = List.map3(usedModels, checkModelLoaded, p, false, SOME(str));
2682-
26832676
(cache, outValMsg, st, indexed_dlow, libs, file_dir, resultValues) =
26842677
SimCode.translateModel(cache,env,className,st,fileNamePrefix,addDummy,inSimSettingsOpt,Absyn.FUNCTIONARGS({},{}));
26852678
then
@@ -6338,4 +6331,20 @@ algorithm
63386331
end matchcontinue;
63396332
end isShortDefinition;
63406333

6334+
protected function getPackageVersion
6335+
input Absyn.Path path;
6336+
input Absyn.Program p;
6337+
output String version;
6338+
algorithm
6339+
version := matchcontinue (path,p)
6340+
case (path,p)
6341+
equation
6342+
Config.setEvaluateParametersInAnnotations(true);
6343+
Absyn.STRING(version) = Interactive.getNamedAnnotation(path, p, "version", SOME(Absyn.STRING("")), Interactive.getAnnotationExp);
6344+
Config.setEvaluateParametersInAnnotations(false);
6345+
then version;
6346+
else "(version unknown)";
6347+
end matchcontinue;
6348+
end getPackageVersion;
6349+
63416350
end CevalScript;

Compiler/Util/Error.mo

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,8 @@ public constant Message CONNECT_IN_INITIAL_EQUATION = MESSAGE(221, TRANSLATION()
589589
"Connect equations are not allowed in initial equation sections.");
590590
public constant Message FINAL_COMPONENT_OVERRIDE = MESSAGE(222, TRANSLATION(), ERROR(),
591591
"Trying to override final component %s with modifier %s\n");
592+
public constant Message NOTIFY_NOT_LOADED = MESSAGE(223, SCRIPTING(), NOTIFICATION(),
593+
"Automatically loaded package %s %s due to uses annotation.");
592594

593595
public constant Message UNBOUND_PARAMETER_WARNING = MESSAGE(500, TRANSLATION(), WARNING(),
594596
"Parameter %s has neither value nor start value, and is fixed during initialization (fixed=true)");

0 commit comments

Comments
 (0)