Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
sjoelund committed Apr 12, 2012
1 parent fed810e commit 26432f3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
1 change: 1 addition & 0 deletions Compiler/FrontEnd/ModelicaBuiltin.mo
Expand Up @@ -1571,6 +1571,7 @@ end generateCode;
function loadModel "Loads the Modelica Standard Library."
input TypeName className;
input String[:] priorityVersion := {"default"};
input Boolean notify := false "Give a notification of the libraries and versions that were loaded";
output Boolean success;
external "builtin";
annotation(Documentation(info="<html>
Expand Down
53 changes: 31 additions & 22 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -565,28 +565,32 @@ protected function loadModel
input String modelicaPath;
input Absyn.Program ip;
input Boolean forceLoad;
input Boolean notifyLoad;
output Absyn.Program pnew;
output Boolean success;
algorithm
(pnew,success) := matchcontinue (imodelsToLoad,modelicaPath,ip,forceLoad)
(pnew,success) := matchcontinue (imodelsToLoad,modelicaPath,ip,forceLoad,notifyLoad)
local
Absyn.Path path;
String pathStr,versions;
String pathStr,versions,className,version;
list<String> strings;
Boolean b,b1,b2;
Absyn.Program p;
list<tuple<Absyn.Path,list<String>>> modelsToLoad;

case ({},_,p,_) then (p,true);
case ((path,strings)::modelsToLoad,modelicaPath,p,forceLoad)
case ({},_,p,_,_) then (p,true);
case ((path,strings)::modelsToLoad,modelicaPath,p,forceLoad,notifyLoad)
equation
b = checkModelLoaded((path,strings),p,forceLoad,NONE());
pnew = Debug.bcallret3(not b, ClassLoader.loadClass, path, strings, modelicaPath, Absyn.PROGRAM({},Absyn.TOP(),Absyn.dummyTimeStamp));
className = Absyn.pathString(path);
version = Debug.bcallret2(not b, getPackageVersion, path, pnew, "");
Error.assertionOrAddSourceMessage(b or not notifyLoad,Error.NOTIFY_NOT_LOADED,{className,version},Absyn.dummyInfo);
p = Interactive.updateProgram(pnew, p);
(p,b1) = loadModel(Interactive.getUsesAnnotationOrDefault(pnew),modelicaPath,p,false);
(p,b2) = loadModel(modelsToLoad,modelicaPath,p,forceLoad);
(p,b1) = loadModel(Interactive.getUsesAnnotationOrDefault(pnew),modelicaPath,p,false,notifyLoad);
(p,b2) = loadModel(modelsToLoad,modelicaPath,p,forceLoad,notifyLoad);
then (p,b1 and b2);
case ((path,strings)::_,modelicaPath,p,_)
case ((path,strings)::_,modelicaPath,p,_,_)
equation
pathStr = Absyn.pathString(path);
versions = stringDelimitList(strings,",");
Expand Down Expand Up @@ -1429,9 +1433,7 @@ algorithm

case (cache,env,"getVersion",{Values.CODE(Absyn.C_TYPENAME(path))},st as Interactive.SYMBOLTABLE(ast=p),msg)
equation
Config.setEvaluateParametersInAnnotations(true);
Absyn.STRING(str_1) = Interactive.getNamedAnnotation(path, p, "version", SOME(Absyn.STRING("")), Interactive.getAnnotationExp);
Config.setEvaluateParametersInAnnotations(false);
str_1 = getPackageVersion(path,p);
then
(cache,Values.STRING(str_1),st);

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

case (cache,env,"loadModel",{Values.CODE(Absyn.C_TYPENAME(path)),Values.ARRAY(valueLst=cvars)},
case (cache,env,"loadModel",{Values.CODE(Absyn.C_TYPENAME(path)),Values.ARRAY(valueLst=cvars),Values.BOOL(b)},
(st as Interactive.SYMBOLTABLE(
ast = p,depends=aDep,instClsLst = ic,
lstVarVal = iv,compiledFunctions = cf,
Expand All @@ -1582,7 +1584,7 @@ algorithm
equation
mp = Settings.getModelicaPath();
strings = List.map(cvars, ValuesUtil.extractValueString);
(p,b) = loadModel({(path,strings)},mp,p,true);
(p,b) = loadModel({(path,strings)},mp,p,true,b);
str = Print.getString();
newst = Interactive.SYMBOLTABLE(p,aDep,NONE(),{},iv,cf,lf);
then
Expand Down Expand Up @@ -2589,8 +2591,7 @@ algorithm
re = Absyn.restrString(restriction);
Error.assertionOrAddSourceMessage(relaxedFrontEnd or not (Absyn.isFunctionRestriction(restriction) or Absyn.isPackageRestriction(restriction)),
Error.INST_INVALID_RESTRICTION,{str,re},Absyn.dummyInfo);
usedModels = Interactive.getUsesAnnotation(Absyn.PROGRAM({absynClass},Absyn.TOP(),Absyn.dummyTimeStamp));
_ = List.map3(usedModels, checkModelLoaded, p, false, SOME(str));
(p,true) = loadModel(Interactive.getUsesAnnotationOrDefault(Absyn.PROGRAM({absynClass},Absyn.TOP(),Absyn.dummyTimeStamp)),Settings.getModelicaPath(),p,false,true);

ptot = Dependency.getTotalProgram(className,p);

Expand Down Expand Up @@ -2672,14 +2673,6 @@ algorithm

case (cache,env,className,st as Interactive.SYMBOLTABLE(ast=p),fileNamePrefix,addDummy,inSimSettingsOpt)
equation
(absynClass as Absyn.CLASS(restriction = restriction)) = Interactive.getPathedClassInProgram(className, p);
str = Absyn.pathString(className);
re = Absyn.restrString(restriction);
Error.assertionOrAddSourceMessage(not (Absyn.isFunctionRestriction(restriction) or Absyn.isPackageRestriction(restriction)),
Error.INST_INVALID_RESTRICTION,{str,re},Absyn.dummyInfo);
usedModels = Interactive.getUsesAnnotation(Absyn.PROGRAM({absynClass},Absyn.TOP(),Absyn.dummyTimeStamp));
_ = List.map3(usedModels, checkModelLoaded, p, false, SOME(str));

(cache, outValMsg, st, indexed_dlow, libs, file_dir, resultValues) =
SimCode.translateModel(cache,env,className,st,fileNamePrefix,addDummy,inSimSettingsOpt,Absyn.FUNCTIONARGS({},{}));
then
Expand Down Expand Up @@ -6338,4 +6331,20 @@ algorithm
end matchcontinue;
end isShortDefinition;

protected function getPackageVersion
input Absyn.Path path;
input Absyn.Program p;
output String version;
algorithm
version := matchcontinue (path,p)
case (path,p)
equation
Config.setEvaluateParametersInAnnotations(true);
Absyn.STRING(version) = Interactive.getNamedAnnotation(path, p, "version", SOME(Absyn.STRING("")), Interactive.getAnnotationExp);
Config.setEvaluateParametersInAnnotations(false);
then version;
else "(version unknown)";
end matchcontinue;
end getPackageVersion;

end CevalScript;
2 changes: 2 additions & 0 deletions Compiler/Util/Error.mo
Expand Up @@ -589,6 +589,8 @@ public constant Message CONNECT_IN_INITIAL_EQUATION = MESSAGE(221, TRANSLATION()
"Connect equations are not allowed in initial equation sections.");
public constant Message FINAL_COMPONENT_OVERRIDE = MESSAGE(222, TRANSLATION(), ERROR(),
"Trying to override final component %s with modifier %s\n");
public constant Message NOTIFY_NOT_LOADED = MESSAGE(223, SCRIPTING(), NOTIFICATION(),
"Automatically loaded package %s %s due to uses annotation.");

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

0 comments on commit 26432f3

Please sign in to comment.