@@ -342,6 +342,10 @@ algorithm
342342 end matchcontinue;
343343end loadFile;
344344
345+
346+ protected type LoadModelFoldArg =
347+ tuple< String /* modelicaPath*/ , Boolean /* forceLoad*/ , Boolean /* notifyLoad*/ , Boolean /* checkUses*/ , Boolean /* requireExactVersion*/ >;
348+
345349public function loadModel
346350 input list< tuple< Absyn . Path ,list< String >>> imodelsToLoad;
347351 input String modelicaPath;
@@ -352,44 +356,63 @@ public function loadModel
352356 input Boolean requireExactVersion;
353357 output Absyn . Program pnew;
354358 output Boolean success;
359+ protected
360+ LoadModelFoldArg arg = (modelicaPath, forceLoad, notifyLoad, checkUses, requireExactVersion);
355361algorithm
356- (pnew,success) := matchcontinue (imodelsToLoad,modelicaPath,ip,forceLoad,notifyLoad,checkUses)
357- local
358- Absyn . Path path;
359- String pathStr,versions,className,version;
360- list< String > strings;
361- Boolean b,b1,b2;
362- Absyn . Program p;
363- list< tuple< Absyn . Path ,list< String >>> modelsToLoad;
364-
365- case ({},_,p,_,_,_) then (p,true );
366- case ((path,strings)::modelsToLoad,_,p,_,_,_)
367- equation
368- b = checkModelLoaded((path,strings),p,forceLoad,NONE ());
369- pnew = if not b then ClassLoader . loadClass(path, strings, modelicaPath, NONE (), requireExactVersion) else Absyn . PROGRAM ({},Absyn . TOP ());
370- className = Absyn . pathString(path);
371- version = if not b then getPackageVersion(path, pnew) else "" ;
372- Error . assertionOrAddSourceMessage(b or not notifyLoad or forceLoad,Error . NOTIFY_NOT_LOADED ,{className,version},Absyn . dummyInfo);
373- p = Interactive . updateProgram(pnew, p);
374- (p,b1) = loadModel(if checkUses then Interactive . getUsesAnnotationOrDefault(pnew, requireExactVersion) else {}, modelicaPath, p, false , notifyLoad, checkUses, requireExactVersion);
375- (p,b2) = loadModel(modelsToLoad, modelicaPath, p, forceLoad, notifyLoad, checkUses, requireExactVersion);
376- then (p,b1 and b2);
377- case ((path,strings)::_,_,p,true ,_,_)
378- equation
379- pathStr = Absyn . pathString(path);
380- versions = stringDelimitList(strings,"," );
381- Error . addMessage(Error . LOAD_MODEL ,{pathStr,versions,modelicaPath});
382- then (p,false );
383- case ((path,strings)::modelsToLoad,_,p,false ,_,_)
384- equation
385- pathStr = Absyn . pathString(path);
386- versions = stringDelimitList(strings,"," );
387- Error . addMessage(Error . NOTIFY_LOAD_MODEL_FAILED ,{pathStr,versions,modelicaPath});
388- (p,b) = loadModel(modelsToLoad, modelicaPath, p, forceLoad, notifyLoad, checkUses, requireExactVersion);
389- then (p,b);
390- end matchcontinue;
362+ (pnew, success) := List . fold1(imodelsToLoad, loadModel1, arg, (ip, true ));
391363end loadModel;
392364
365+ protected function loadModel1
366+ input tuple< Absyn . Path ,list< String >> modelToLoad;
367+ input LoadModelFoldArg inArg;
368+ input tuple< Absyn . Program , Boolean > inTpl;
369+ output tuple< Absyn . Program , Boolean > outTpl;
370+ protected
371+ list< tuple< Absyn . Path ,list< String >>> modelsToLoad;
372+ Boolean b, b1, success, forceLoad, notifyLoad, checkUses, requireExactVersion;
373+ Absyn . Path path;
374+ list< String > versionsLst;
375+ String pathStr, versions, className, version, modelicaPath;
376+ Absyn . Program p, pnew;
377+ Error . MessageTokens msgTokens;
378+ algorithm
379+ (path, versionsLst) := modelToLoad;
380+ (modelicaPath, forceLoad, notifyLoad, checkUses, requireExactVersion) := inArg;
381+ try
382+ (p, success) := inTpl;
383+ if checkModelLoaded(modelToLoad, p, forceLoad, NONE ()) then
384+ pnew := Absyn . PROGRAM ({}, Absyn . TOP ());
385+ version := "" ;
386+ else
387+ pnew := ClassLoader . loadClass(path, versionsLst, modelicaPath, NONE (), requireExactVersion);
388+ version := getPackageVersion(path, pnew);
389+ b := not notifyLoad or forceLoad;
390+ msgTokens := {Absyn . pathString(path), version};
391+ Error . assertionOrAddSourceMessage(b, Error . NOTIFY_NOT_LOADED , msgTokens, Absyn . dummyInfo);
392+ end if ;
393+ p := Interactive . updateProgram(pnew, p);
394+
395+ b := true ;
396+ if checkUses then
397+ modelsToLoad := Interactive . getUsesAnnotationOrDefault(pnew, requireExactVersion);
398+ (p, b) := loadModel(modelsToLoad, modelicaPath, p, false , notifyLoad, checkUses, requireExactVersion);
399+ end if ;
400+ outTpl := (p, success and b);
401+ else
402+ (p, _) := inTpl;
403+ pathStr := Absyn . pathString(path);
404+ versions := stringDelimitList(versionsLst, "," );
405+ msgTokens := {pathStr, versions, modelicaPath};
406+ if forceLoad then
407+ Error . addMessage(Error . LOAD_MODEL , msgTokens);
408+ outTpl := (p, false );
409+ else
410+ Error . addMessage(Error . NOTIFY_LOAD_MODEL_FAILED , msgTokens);
411+ outTpl := inTpl;
412+ end if ;
413+ end try ;
414+ end loadModel1;
415+
393416protected function checkModelLoaded
394417 input tuple< Absyn . Path ,list< String >> tpl;
395418 input Absyn . Program p;
0 commit comments