@@ -1072,17 +1072,7 @@ def getSolutions(self, varList: Optional[str | list[str]] = None, resultfile: Op
10721072 return np_res
10731073
10741074 @staticmethod
1075- def _strip_space (name ):
1076- if isinstance (name , str ):
1077- return name .replace (" " , "" )
1078-
1079- if isinstance (name , list ):
1080- return [x .replace (" " , "" ) for x in name ]
1081-
1082- raise ModelicaSystemError ("Unhandled input for strip_space()" )
1083-
10841075 def _prepare_inputdata (
1085- self ,
10861076 rawinput : str | list [str ] | dict [str , str | int | float ],
10871077 ) -> dict [str , str ]:
10881078 """
@@ -1121,72 +1111,65 @@ def prepare_str(str_in: str) -> dict[str, str]:
11211111 return inputdata
11221112
11231113 if isinstance (rawinput , dict ):
1124- inputdata = {key : str (val ) for key , val in rawinput .items ()}
1114+ for key , val in rawinput .items ():
1115+ str_val = str (val )
1116+ if ' ' in key or ' ' in str_val :
1117+ raise ModelicaSystemError (f"Spaces not allowed in key/value pairs: { repr (key )} = { repr (val )} !" )
1118+ inputdata [key ] = str_val
11251119
11261120 return inputdata
11271121
1128- def setMethodHelper (
1122+ def _set_method_helper (
11291123 self ,
1130- inputdata : str | list [ str ] | dict [str , str | int | float ],
1124+ inputdata : dict [str , str ],
11311125 classdata : dict [str , Any ],
11321126 datatype : str ,
11331127 overwritedata : Optional [dict [str , str ]] = None ,
11341128 ) -> bool :
11351129 """
1136- Helper function for setters.
1130+ Helper function for:
1131+ * setParameter()
1132+ * setContinuous()
1133+ * setSimulationOptions()
1134+ * setLinearizationOption()
1135+ * setOptimizationOption()
1136+ * setInputs()
11371137
1138- args1 - string or list of string given by user
1139- args2 - dict() containing the values of different variables(eg:, parameter,continuous,simulation parameters)
1140- args3 - function name (eg; continuous, parameter, simulation, linearization,optimization)
1141- args4 - dict() which stores the new override variables list,
1138+ Parameters
1139+ ----------
1140+ inputdata
1141+ string or list of string given by user
1142+ classdata
1143+ dict() containing the values of different variables (eg: parameter, continuous, simulation parameters)
1144+ datatype
1145+ type identifier (eg; continuous, parameter, simulation, linearization, optimization)
1146+ overwritedata
1147+ dict() which stores the new override variables list,
11421148 """
11431149
1144- # TODO: cleanup / data handling / ...
1145- # (1) args1: Optional[str | list[str]] -> convert to dict
1146- # (2) work on dict! inputs: dict[str, str | int | float | ?numbers.number?]
1147- # (3) handle function
1148- # (4) use it also for other functions with such an input, i.e. 'key=value' | ['key=value']
1149- # (5) include setInputs()
1150-
1151- def apply_single (key_val : str ):
1152- key_val_list = key_val .split ("=" )
1153- if len (key_val_list ) != 2 :
1154- raise ModelicaSystemError (f"Invalid key = value pair: { key_val } " )
1155-
1156- name = self ._strip_space (key_val_list [0 ])
1157- value = self ._strip_space (key_val_list [1 ])
1158-
1159- if name in classdata :
1160- if datatype == "parameter" and not self .isParameterChangeable (name ):
1161- logger .debug (f"It is not possible to set the parameter { repr (name )} . It seems to be "
1150+ inputdata_status : dict [str , bool ] = {}
1151+ for key , val in inputdata .items ():
1152+ status = False
1153+ if key in classdata :
1154+ if datatype == "parameter" and not self .isParameterChangeable (key ):
1155+ logger .debug (f"It is not possible to set the parameter { repr (key )} . It seems to be "
11621156 "structural, final, protected, evaluated or has a non-constant binding. "
11631157 "Use sendExpression(...) and rebuild the model using buildModel() API; example: "
11641158 "sendExpression(\" setParameterValue("
1165- f"{ self .modelName } , { name } , { value if value is not None else '<?value?>' } "
1159+ f"{ self .modelName } , { key } , { val if val is not None else '<?value?>' } "
11661160 ")\" ) " )
1167- return False
1168-
1169- classdata [name ] = value
1170- if overwritedata is not None :
1171- overwritedata [name ] = value
1172-
1173- return True
1174-
1161+ else :
1162+ classdata [key ] = val
1163+ if overwritedata is not None :
1164+ overwritedata [key ] = val
1165+ status = True
11751166 else :
11761167 raise ModelicaSystemError ("Unhandled case in setMethodHelper.apply_single() - "
1177- f"{ repr (name )} is not a { repr (datatype )} variable" )
1178-
1179- result = []
1180- if isinstance (inputdata , str ):
1181- result = [apply_single (inputdata )]
1168+ f"{ repr (key )} is not a { repr (datatype )} variable" )
11821169
1183- elif isinstance (inputdata , list ):
1184- result = []
1185- inputdata = self ._strip_space (inputdata )
1186- for var in inputdata :
1187- result .append (apply_single (var ))
1170+ inputdata_status [key ] = status
11881171
1189- return all (result )
1172+ return all (inputdata_status . values () )
11901173
11911174 def isParameterChangeable (
11921175 self ,
@@ -1205,11 +1188,14 @@ def setContinuous(
12051188 This method is used to set continuous values. It can be called:
12061189 with a sequence of continuous name and assigning corresponding values as arguments as show in the example below:
12071190 usage
1208- >>> setContinuous("Name=value")
1209- >>> setContinuous(["Name1=value1","Name2=value2"])
1191+ >>> setContinuous("Name=value") # depreciated
1192+ >>> setContinuous(["Name1=value1","Name2=value2"]) # depreciated
1193+ >>> setContinuous(cvals={"Name1": "value1", "Name2": "value2"})
12101194 """
1211- return self ._setMethodHelper (
1212- inputdata = cvals ,
1195+ inputdata = self ._prepare_inputdata (rawinput = cvals )
1196+
1197+ return self ._set_method_helper (
1198+ inputdata = inputdata ,
12131199 classdata = self .continuouslist ,
12141200 datatype = "continuous" ,
12151201 overwritedata = self .overridevariables )
@@ -1222,11 +1208,14 @@ def setParameters(
12221208 This method is used to set parameter values. It can be called:
12231209 with a sequence of parameter name and assigning corresponding value as arguments as show in the example below:
12241210 usage
1225- >>> setParameters("Name=value")
1226- >>> setParameters(["Name1=value1","Name2=value2"])
1211+ >>> setParameters("Name=value") # depreciated
1212+ >>> setParameters(["Name1=value1","Name2=value2"]) # depreciated
1213+ >>> setParameters(pvals={"Name1": "value1", "Name2": "value2"})
12271214 """
1228- return self ._setMethodHelper (
1229- inputdata = pvals ,
1215+ inputdata = self ._prepare_inputdata (rawinput = pvals )
1216+
1217+ return self ._set_method_helper (
1218+ inputdata = inputdata ,
12301219 classdata = self .paramlist ,
12311220 datatype = "parameter" ,
12321221 overwritedata = self .overridevariables )
@@ -1239,11 +1228,14 @@ def setSimulationOptions(
12391228 This method is used to set simulation options. It can be called:
12401229 with a sequence of simulation options name and assigning corresponding values as arguments as show in the example below:
12411230 usage
1242- >>> setSimulationOptions("Name=value")
1243- >>> setSimulationOptions(["Name1=value1","Name2=value2"])
1231+ >>> setSimulationOptions("Name=value") # depreciated
1232+ >>> setSimulationOptions(["Name1=value1","Name2=value2"]) # depreciated
1233+ >>> setSimulationOptions(simOptions={"Name1": "value1", "Name2": "value2"})
12441234 """
1245- return _self .setMethodHelper (
1246- inputdata = simOptions ,
1235+ inputdata = self ._prepare_inputdata (rawinput = simOptions )
1236+
1237+ return self ._set_method_helper (
1238+ inputdata = inputdata ,
12471239 classdata = self .simulateOptions ,
12481240 datatype = "simulation-option" ,
12491241 overwritedata = self .simoptionsoverride )
@@ -1256,11 +1248,14 @@ def setLinearizationOptions(
12561248 This method is used to set linearization options. It can be called:
12571249 with a sequence of linearization options name and assigning corresponding value as arguments as show in the example below
12581250 usage
1259- >>> setLinearizationOptions("Name=value")
1260- >>> setLinearizationOptions(["Name1=value1","Name2=value2"])
1251+ >>> setLinearizationOptions("Name=value") # depreciated
1252+ >>> setLinearizationOptions(["Name1=value1","Name2=value2"]) # depreciated
1253+ >>> setLinearizationOptions(linearizationOtions={"Name1": "value1", "Name2": "value2"})
12611254 """
1262- return self ._setMethodHelper (
1263- inputdata = linearizationOptions ,
1255+ inputdata = self ._prepare_inputdata (rawinput = linearizationOptions )
1256+
1257+ return self ._set_method_helper (
1258+ inputdata = inputdata ,
12641259 classdata = self .linearOptions ,
12651260 datatype = "Linearization-option" ,
12661261 overwritedata = None )
@@ -1270,11 +1265,14 @@ def setOptimizationOptions(self, optimizationOptions: str | list[str] | dict[str
12701265 This method is used to set optimization options. It can be called:
12711266 with a sequence of optimization options name and assigning corresponding values as arguments as show in the example below:
12721267 usage
1273- >>> setOptimizationOptions("Name=value")
1274- >>> setOptimizationOptions(["Name1=value1","Name2=value2"])
1268+ >>> setOptimizationOptions("Name=value") # depreciated
1269+ >>> setOptimizationOptions(["Name1=value1","Name2=value2"]) # depreciated
1270+ >>> setOptimizationOptions(optimizationOptions={"Name1": "value1", "Name2": "value2"})
12751271 """
1276- return self ._setMethodHelper (
1277- inputdata = optimizationOptions ,
1272+ inputdata = self ._prepare_inputdata (rawinput = optimizationOptions )
1273+
1274+ return self ._set_method_helper (
1275+ inputdata = inputdata ,
12781276 classdata = self .optimizeOptions ,
12791277 datatype = "optimization-option" ,
12801278 overwritedata = None )
@@ -1287,9 +1285,12 @@ def setInputs(
12871285 This method is used to set input values. It can be called:
12881286 with a sequence of input name and assigning corresponding values as arguments as show in the example below:
12891287 usage
1290- >>> setInputs("Name=value")
1291- >>> setInputs(["Name1=value1","Name2=value2"])
1288+ >>> setInputs("Name=value") # depreciated
1289+ >>> setInputs(["Name1=value1","Name2=value2"]) # depreciated
1290+ >>> setInputs(name={"Name1": "value1", "Name2": "value2"})
12921291 """
1292+ # inputdata = self._prepare_inputdata(rawinput=name)
1293+
12931294 if isinstance (name , str ):
12941295 name1 : str = name
12951296 name1 = name1 .replace (" " , "" )
0 commit comments