@@ -992,17 +992,7 @@ def getSolutions(self, varList=None, resultfile=None): # 12
992992 return np_res
993993
994994 @staticmethod
995- def _strip_space (name ):
996- if isinstance (name , str ):
997- return name .replace (" " , "" )
998-
999- if isinstance (name , list ):
1000- return [x .replace (" " , "" ) for x in name ]
1001-
1002- raise ModelicaSystemError ("Unhandled input for strip_space()" )
1003-
1004995 def _prepare_inputdata (
1005- self ,
1006996 rawinput : str | list [str ] | dict [str , str | int | float ],
1007997 ) -> dict [str , str ]:
1008998 """
@@ -1041,72 +1031,65 @@ def prepare_str(str_in: str) -> dict[str, str]:
10411031 return inputdata
10421032
10431033 if isinstance (rawinput , dict ):
1044- inputdata = {key : str (val ) for key , val in rawinput .items ()}
1034+ for key , val in rawinput .items ():
1035+ str_val = str (val )
1036+ if ' ' in key or ' ' in str_val :
1037+ raise ModelicaSystemError (f"Spaces not allowed in key/value pairs: { repr (key )} = { repr (val )} !" )
1038+ inputdata [key ] = str_val
10451039
10461040 return inputdata
10471041
1048- def setMethodHelper (
1042+ def _set_method_helper (
10491043 self ,
1050- inputdata : str | list [ str ] | dict [str , str | int | float ],
1044+ inputdata : dict [str , str ],
10511045 classdata : dict [str , Any ],
10521046 datatype : str ,
10531047 overwritedata : Optional [dict [str , str ]] = None ,
10541048 ) -> bool :
10551049 """
1056- Helper function for setParameter(),setContinuous(),setSimulationOptions(),setLinearizationOption(),setOptimizationOption()
1057-
1058- args1 - string or list of string given by user
1059- args2 - dict() containing the values of different variables(eg:, parameter,continuous,simulation parameters)
1060- args3 - function name (eg; continuous, parameter, simulation, linearization,optimization)
1061- args4 - dict() which stores the new override variables list,
1062- """
1063-
1064- # TODO: cleanup / data handling / ...
1065- # (1) args1: Optional[str | list[str]] -> convert to dict
1066- # (2) work on dict! inputs: dict[str, str | int | float | ?numbers.number?]
1067- # (3) handle function
1068- # (4) use it also for other functions with such an input, i.e. 'key=value' | ['key=value']
1069- # (5) include setInputs()
1070-
1071- def apply_single (key_val : str ):
1072- key_val_list = key_val .split ("=" )
1073- if len (key_val_list ) != 2 :
1074- raise ModelicaSystemError (f"Invalid key = value pair: { key_val } " )
1075-
1076- name = self ._strip_space (key_val_list [0 ])
1077- value = self ._strip_space (key_val_list [1 ])
1050+ Helper function for:
1051+ * setParameter()
1052+ * setContinuous()
1053+ * setSimulationOptions()
1054+ * setLinearizationOption()
1055+ * setOptimizationOption()
1056+ * setInputs()
10781057
1079- if name in classdata :
1080- if datatype == "parameter" and not self .isParameterChangeable (name ):
1081- logger .debug (f"It is not possible to set the parameter { repr (name )} . It seems to be "
1058+ Parameters
1059+ ----------
1060+ inputdata
1061+ string or list of string given by user
1062+ classdata
1063+ dict() containing the values of different variables (eg: parameter, continuous, simulation parameters)
1064+ datatype
1065+ type identifier (eg; continuous, parameter, simulation, linearization, optimization)
1066+ overwritedata
1067+ dict() which stores the new override variables list,
1068+ """
1069+
1070+ inputdata_status : dict [str , bool ] = {}
1071+ for key , val in inputdata .items ():
1072+ status = False
1073+ if key in classdata :
1074+ if datatype == "parameter" and not self .isParameterChangeable (key ):
1075+ logger .debug (f"It is not possible to set the parameter { repr (key )} . It seems to be "
10821076 "structural, final, protected, evaluated or has a non-constant binding. "
10831077 "Use sendExpression(...) and rebuild the model using buildModel() API; example: "
10841078 "sendExpression(\" setParameterValue("
1085- f"{ self .modelName } , { name } , { value if value is not None else '<?value?>' } "
1079+ f"{ self .modelName } , { key } , { val if val is not None else '<?value?>' } "
10861080 ")\" ) " )
1087- return False
1088-
1089- classdata [name ] = value
1090- if overwritedata is not None :
1091- overwritedata [name ] = value
1092-
1093- return True
1094-
1081+ else :
1082+ classdata [key ] = val
1083+ if overwritedata is not None :
1084+ overwritedata [key ] = val
1085+ status = True
10951086 else :
10961087 raise ModelicaSystemError ("Unhandled case in setMethodHelper.apply_single() - "
1097- f"{ repr (name )} is not a { repr (datatype )} variable" )
1098-
1099- result = []
1100- if isinstance (inputdata , str ):
1101- result = [apply_single (inputdata )]
1088+ f"{ repr (key )} is not a { repr (datatype )} variable" )
11021089
1103- elif isinstance (inputdata , list ):
1104- result = []
1105- inputdata = self ._strip_space (inputdata )
1106- for var in inputdata :
1107- result .append (apply_single (var ))
1090+ inputdata_status [key ] = status
11081091
1109- return all (result )
1092+ return all (inputdata_status . values () )
11101093
11111094 def isParameterChangeable (
11121095 self ,
@@ -1122,11 +1105,14 @@ def setContinuous(self, cvals: str | list[str] | dict[str, str | int | float]) -
11221105 This method is used to set continuous values. It can be called:
11231106 with a sequence of continuous name and assigning corresponding values as arguments as show in the example below:
11241107 usage
1125- >>> setContinuous("Name=value")
1126- >>> setContinuous(["Name1=value1","Name2=value2"])
1108+ >>> setContinuous("Name=value") # depreciated
1109+ >>> setContinuous(["Name1=value1","Name2=value2"]) # depreciated
1110+ >>> setContinuous(cvals={"Name1": "value1", "Name2": "value2"})
11271111 """
1128- return self .setMethodHelper (
1129- inputdata = cvals ,
1112+ inputdata = self ._prepare_inputdata (rawinput = cvals )
1113+
1114+ return self ._set_method_helper (
1115+ inputdata = inputdata ,
11301116 classdata = self .continuouslist ,
11311117 datatype = "continuous" ,
11321118 overwritedata = self .overridevariables )
@@ -1136,11 +1122,14 @@ def setParameters(self, pvals: str | list[str] | dict[str, str | int | float]) -
11361122 This method is used to set parameter values. It can be called:
11371123 with a sequence of parameter name and assigning corresponding value as arguments as show in the example below:
11381124 usage
1139- >>> setParameters("Name=value")
1140- >>> setParameters(["Name1=value1","Name2=value2"])
1125+ >>> setParameters("Name=value") # depreciated
1126+ >>> setParameters(["Name1=value1","Name2=value2"]) # depreciated
1127+ >>> setParameters(pvals={"Name1": "value1", "Name2": "value2"})
11411128 """
1142- return self .setMethodHelper (
1143- inputdata = pvals ,
1129+ inputdata = self ._prepare_inputdata (rawinput = pvals )
1130+
1131+ return self ._set_method_helper (
1132+ inputdata = inputdata ,
11441133 classdata = self .paramlist ,
11451134 datatype = "parameter" ,
11461135 overwritedata = self .overridevariables )
@@ -1150,11 +1139,14 @@ def setSimulationOptions(self, simOptions: str | list[str] | dict[str, str | int
11501139 This method is used to set simulation options. It can be called:
11511140 with a sequence of simulation options name and assigning corresponding values as arguments as show in the example below:
11521141 usage
1153- >>> setSimulationOptions("Name=value")
1154- >>> setSimulationOptions(["Name1=value1","Name2=value2"])
1142+ >>> setSimulationOptions("Name=value") # depreciated
1143+ >>> setSimulationOptions(["Name1=value1","Name2=value2"]) # depreciated
1144+ >>> setSimulationOptions(simOptions={"Name1": "value1", "Name2": "value2"})
11551145 """
1156- return self .setMethodHelper (
1157- inputdata = simOptions ,
1146+ inputdata = self ._prepare_inputdata (rawinput = simOptions )
1147+
1148+ return self ._set_method_helper (
1149+ inputdata = inputdata ,
11581150 classdata = self .simulateOptions ,
11591151 datatype = "simulation-option" ,
11601152 overwritedata = self .simoptionsoverride )
@@ -1164,11 +1156,14 @@ def setLinearizationOptions(self, linearizationOptions: str | list[str] | dict[s
11641156 This method is used to set linearization options. It can be called:
11651157 with a sequence of linearization options name and assigning corresponding value as arguments as show in the example below
11661158 usage
1167- >>> setLinearizationOptions("Name=value")
1168- >>> setLinearizationOptions(["Name1=value1","Name2=value2"])
1159+ >>> setLinearizationOptions("Name=value") # depreciated
1160+ >>> setLinearizationOptions(["Name1=value1","Name2=value2"]) # depreciated
1161+ >>> setLinearizationOptions(linearizationOtions={"Name1": "value1", "Name2": "value2"})
11691162 """
1170- return self .setMethodHelper (
1171- inputdata = linearizationOptions ,
1163+ inputdata = self ._prepare_inputdata (rawinput = linearizationOptions )
1164+
1165+ return self ._set_method_helper (
1166+ inputdata = inputdata ,
11721167 classdata = self .linearOptions ,
11731168 datatype = "Linearization-option" ,
11741169 overwritedata = None )
@@ -1178,11 +1173,14 @@ def setOptimizationOptions(self, optimizationOptions: str | list[str] | dict[str
11781173 This method is used to set optimization options. It can be called:
11791174 with a sequence of optimization options name and assigning corresponding values as arguments as show in the example below:
11801175 usage
1181- >>> setOptimizationOptions("Name=value")
1182- >>> setOptimizationOptions(["Name1=value1","Name2=value2"])
1176+ >>> setOptimizationOptions("Name=value") # depreciated
1177+ >>> setOptimizationOptions(["Name1=value1","Name2=value2"]) # depreciated
1178+ >>> setOptimizationOptions(optimizationOptions={"Name1": "value1", "Name2": "value2"})
11831179 """
1184- return self .setMethodHelper (
1185- inputdata = optimizationOptions ,
1180+ inputdata = self ._prepare_inputdata (rawinput = optimizationOptions )
1181+
1182+ return self ._set_method_helper (
1183+ inputdata = inputdata ,
11861184 classdata = self .optimizeOptions ,
11871185 datatype = "optimization-option" ,
11881186 overwritedata = None )
@@ -1192,9 +1190,12 @@ def setInputs(self, name: str | list[str] | dict[str, str | int | float]) -> boo
11921190 This method is used to set input values. It can be called:
11931191 with a sequence of input name and assigning corresponding values as arguments as show in the example below:
11941192 usage
1195- >>> setInputs("Name=value")
1196- >>> setInputs(["Name1=value1","Name2=value2"])
1193+ >>> setInputs("Name=value") # depreciated
1194+ >>> setInputs(["Name1=value1","Name2=value2"]) # depreciated
1195+ >>> setInputs(name={"Name1": "value1", "Name2": "value2"})
11971196 """
1197+ # inputdata = self._prepare_inputdata(rawinput=name)
1198+
11981199 if isinstance (name , str ):
11991200 name1 : str = name
12001201 name1 = name1 .replace (" " , "" )
0 commit comments