From 339512106ea8dd869b867cd8628dd5adb1e8818c Mon Sep 17 00:00:00 2001 From: Adrian Pop Date: Thu, 1 Feb 2018 18:16:36 +0100 Subject: [PATCH] ticket:4674: add new api to retrieve instantiated parameters Belonging to [master]: - OpenModelica/OMCompiler#2155 - OpenModelica/OpenModelica-testsuite#837 --- Compiler/FrontEnd/DAEDump.mo | 2 +- Compiler/FrontEnd/DAEUtil.mo | 30 +++++++++++++++++++++++++++ Compiler/FrontEnd/ModelicaBuiltin.mo | 11 ++++++++++ Compiler/Script/CevalScriptBackend.mo | 16 ++++++++++++++ Compiler/Script/Interactive.mo | 25 ++++++++++++++++++++++ 5 files changed, 83 insertions(+), 1 deletion(-) diff --git a/Compiler/FrontEnd/DAEDump.mo b/Compiler/FrontEnd/DAEDump.mo index 006eac4eef1..10ecbd51af9 100644 --- a/Compiler/FrontEnd/DAEDump.mo +++ b/Compiler/FrontEnd/DAEDump.mo @@ -3318,7 +3318,7 @@ algorithm print("tuple_: "+boolString(tpl)+" builtin: "+boolString(bi)+" impure: "+boolString(impure_)+"\n\n"); end dumpCallAttr; -protected function dumpVarBindingStr +public function dumpVarBindingStr input Option inBinding; output String outString; algorithm diff --git a/Compiler/FrontEnd/DAEUtil.mo b/Compiler/FrontEnd/DAEUtil.mo index 3bc205a23e2..cfbc2e05941 100644 --- a/Compiler/FrontEnd/DAEUtil.mo +++ b/Compiler/FrontEnd/DAEUtil.mo @@ -6525,6 +6525,36 @@ algorithm end match; end moveElementToInitialSection; +public function getParameters + input list elts; + input list acc; + output list params; +algorithm + (params) := match (elts,acc) + local + DAE.Element e; + list rest, celts, a; + + case ({},_) then acc; + + case ((e as DAE.COMP(dAElist = celts))::rest,_) + algorithm + a := getParameters(celts, acc); + a := getParameters(rest, a); + then + a; + + case ((e as DAE.VAR())::rest,_) + then if isParameterOrConstant(e) + then e::getParameters(rest, acc) + else getParameters(rest, acc); + + case (_::rest,_) + then getParameters(rest, acc); + + end match; +end getParameters; + annotation(__OpenModelica_Interface="frontend"); end DAEUtil; diff --git a/Compiler/FrontEnd/ModelicaBuiltin.mo b/Compiler/FrontEnd/ModelicaBuiltin.mo index fbfae01ecef..08a808ea020 100644 --- a/Compiler/FrontEnd/ModelicaBuiltin.mo +++ b/Compiler/FrontEnd/ModelicaBuiltin.mo @@ -3037,6 +3037,17 @@ annotation( preferredView="text"); end getComponentModifierValues; +function getInstantiatedParametersAndValues + input TypeName cls; + output String[:] values; +external "builtin"; +annotation( + Documentation(info=" +

Returns the parameter names and values from the DAE.

+"), + preferredView="text"); +end getInstantiatedParametersAndValues; + function removeComponentModifiers input TypeName class_; input String componentName; diff --git a/Compiler/Script/CevalScriptBackend.mo b/Compiler/Script/CevalScriptBackend.mo index 40a10f70db2..813c811a8c2 100644 --- a/Compiler/Script/CevalScriptBackend.mo +++ b/Compiler/Script/CevalScriptBackend.mo @@ -2532,6 +2532,22 @@ algorithm then (cache,Values.BOOL(b)); + case (cache,env,"getInstantiatedParametersAndValues",{Values.CODE(Absyn.C_TYPENAME(className))},_) + equation + (cache,env,odae) = runFrontEnd(cache,env,className,true); + strings = Interactive.getInstantiatedParametersAndValues(odae); + vals = List.map(strings, ValuesUtil.makeString); + v = ValuesUtil.makeArray(vals); + then + (cache,v); + + case (cache,_,"getInstantiatedParametersAndValues",_,_) + equation + Error.addCompilerWarning("getInstantiatedParametersAndValues failed to instantiate the model."); + v = ValuesUtil.makeArray({}); + then + (cache,v); + case (cache,_,"getConnectionCount",{Values.CODE(Absyn.C_TYPENAME(path))},_) equation absynClass = Interactive.getPathedClassInProgram(path, SymbolTable.getAbsyn()); diff --git a/Compiler/Script/Interactive.mo b/Compiler/Script/Interactive.mo index 2acbc89df46..5e3ab0ac7f6 100644 --- a/Compiler/Script/Interactive.mo +++ b/Compiler/Script/Interactive.mo @@ -68,6 +68,7 @@ import ClockIndexes; import Config; import Connect; import Constants; +import DAEDump; import DAEUtil; import Debug; import DoubleEndedList; @@ -18061,5 +18062,29 @@ algorithm end matchcontinue; end excludeElementsFromFile; +public function getInstantiatedParametersAndValues + input Option odae; + output list parametersAndValues = {}; +protected + list els, params; + list strs = {}; + String s; + Option oe; +algorithm + parametersAndValues := match(odae) + case SOME(DAE.DAE(els)) + algorithm + params := DAEUtil.getParameters(els, {}); + for p in params loop + DAE.VAR(binding=oe) := p; + s := DAEUtil.varName(p) + DAEDump.dumpVarBindingStr(oe); + strs := s::strs; + end for; + then + listReverse(strs); + else strs; + end match; +end getInstantiatedParametersAndValues; + annotation(__OpenModelica_Interface="backend"); end Interactive;