@@ -976,17 +976,7 @@ def getSolutions(self, varList=None, resultfile=None): # 12
976976 raise ModelicaSystemError ("Unhandled input for getSolutions()" )
977977
978978 @staticmethod
979- def _strip_space (name ):
980- if isinstance (name , str ):
981- return name .replace (" " , "" )
982-
983- if isinstance (name , list ):
984- return [x .replace (" " , "" ) for x in name ]
985-
986- raise ModelicaSystemError ("Unhandled input for strip_space()" )
987-
988979 def _prepare_inputdata (
989- self ,
990980 rawinput : str | list [str ] | dict [str , str | int | float ],
991981 ) -> dict [str , str ]:
992982 """
@@ -1025,72 +1015,65 @@ def prepare_str(str_in: str) -> dict[str, str]:
10251015 return inputdata
10261016
10271017 if isinstance (rawinput , dict ):
1028- inputdata = {key : str (val ) for key , val in rawinput .items ()}
1018+ for key , val in rawinput .items ():
1019+ str_val = str (val )
1020+ if ' ' in key or ' ' in str_val :
1021+ raise ModelicaSystemError (f"Spaces not allowed in key/value pairs: { repr (key )} = { repr (val )} !" )
1022+ inputdata [key ] = str_val
10291023
10301024 return inputdata
10311025
1032- def setMethodHelper (
1026+ def _set_method_helper (
10331027 self ,
1034- inputdata : str | list [ str ] | dict [str , str | int | float ],
1028+ inputdata : dict [str , str ],
10351029 classdata : dict [str , Any ],
10361030 datatype : str ,
10371031 overwritedata : Optional [dict [str , str ]] = None ,
10381032 ) -> bool :
10391033 """
1040- Helper function for setParameter(),setContinuous(),setSimulationOptions(),setLinearizationOption(),setOptimizationOption()
1034+ Helper function for:
1035+ * setParameter()
1036+ * setContinuous()
1037+ * setSimulationOptions()
1038+ * setLinearizationOption()
1039+ * setOptimizationOption()
1040+ * setInputs()
10411041
1042- args1 - string or list of string given by user
1043- args2 - dict() containing the values of different variables(eg:, parameter,continuous,simulation parameters)
1044- args3 - function name (eg; continuous, parameter, simulation, linearization,optimization)
1045- args4 - dict() which stores the new override variables list,
1042+ Parameters
1043+ ----------
1044+ inputdata
1045+ string or list of string given by user
1046+ classdata
1047+ dict() containing the values of different variables (eg: parameter, continuous, simulation parameters)
1048+ datatype
1049+ type identifier (eg; continuous, parameter, simulation, linearization, optimization)
1050+ overwritedata
1051+ dict() which stores the new override variables list,
10461052 """
10471053
1048- # TODO: cleanup / data handling / ...
1049- # (1) args1: Optional[str | list[str]] -> convert to dict
1050- # (2) work on dict! inputs: dict[str, str | int | float | ?numbers.number?]
1051- # (3) handle function
1052- # (4) use it also for other functions with such an input, i.e. 'key=value' | ['key=value']
1053- # (5) include setInputs()
1054-
1055- def apply_single (key_val : str ):
1056- key_val_list = key_val .split ("=" )
1057- if len (key_val_list ) != 2 :
1058- raise ModelicaSystemError (f"Invalid key = value pair: { key_val } " )
1059-
1060- name = self ._strip_space (key_val_list [0 ])
1061- value = self ._strip_space (key_val_list [1 ])
1062-
1063- if name in classdata :
1064- if datatype == "parameter" and not self .isParameterChangeable (name ):
1065- logger .debug (f"It is not possible to set the parameter { repr (name )} . It seems to be "
1054+ inputdata_status : dict [str , bool ] = {}
1055+ for key , val in inputdata .items ():
1056+ status = False
1057+ if key in classdata :
1058+ if datatype == "parameter" and not self .isParameterChangeable (key ):
1059+ logger .debug (f"It is not possible to set the parameter { repr (key )} . It seems to be "
10661060 "structural, final, protected, evaluated or has a non-constant binding. "
10671061 "Use sendExpression(...) and rebuild the model using buildModel() API; example: "
10681062 "sendExpression(\" setParameterValue("
1069- f"{ self .modelName } , { name } , { value if value is not None else '<?value?>' } "
1063+ f"{ self .modelName } , { key } , { val if val is not None else '<?value?>' } "
10701064 ")\" ) " )
1071- return False
1072-
1073- classdata [name ] = value
1074- if overwritedata is not None :
1075- overwritedata [name ] = value
1076-
1077- return True
1078-
1065+ else :
1066+ classdata [key ] = val
1067+ if overwritedata is not None :
1068+ overwritedata [key ] = val
1069+ status = True
10791070 else :
10801071 raise ModelicaSystemError ("Unhandled case in setMethodHelper.apply_single() - "
1081- f"{ repr (name )} is not a { repr (datatype )} variable" )
1082-
1083- result = []
1084- if isinstance (inputdata , str ):
1085- result = [apply_single (inputdata )]
1072+ f"{ repr (key )} is not a { repr (datatype )} variable" )
10861073
1087- elif isinstance (inputdata , list ):
1088- result = []
1089- inputdata = self ._strip_space (inputdata )
1090- for var in inputdata :
1091- result .append (apply_single (var ))
1074+ inputdata_status [key ] = status
10921075
1093- return all (result )
1076+ return all (inputdata_status . values () )
10941077
10951078 def isParameterChangeable (
10961079 self ,
@@ -1106,11 +1089,14 @@ def setContinuous(self, cvals: str | list[str] | dict[str, str | int | float]) -
11061089 This method is used to set continuous values. It can be called:
11071090 with a sequence of continuous name and assigning corresponding values as arguments as show in the example below:
11081091 usage
1109- >>> setContinuous("Name=value")
1110- >>> setContinuous(["Name1=value1","Name2=value2"])
1092+ >>> setContinuous("Name=value") # depreciated
1093+ >>> setContinuous(["Name1=value1","Name2=value2"]) # depreciated
1094+ >>> setContinuous(cvals={"Name1": "value1", "Name2": "value2"})
11111095 """
1112- return self .setMethodHelper (
1113- inputdata = cvals ,
1096+ inputdata = self ._prepare_inputdata (rawinput = cvals )
1097+
1098+ return self ._set_method_helper (
1099+ inputdata = inputdata ,
11141100 classdata = self .continuouslist ,
11151101 datatype = "continuous" ,
11161102 overwritedata = self .overridevariables )
@@ -1120,11 +1106,14 @@ def setParameters(self, pvals: str | list[str] | dict[str, str | int | float]) -
11201106 This method is used to set parameter values. It can be called:
11211107 with a sequence of parameter name and assigning corresponding value as arguments as show in the example below:
11221108 usage
1123- >>> setParameters("Name=value")
1124- >>> setParameters(["Name1=value1","Name2=value2"])
1109+ >>> setParameters("Name=value") # depreciated
1110+ >>> setParameters(["Name1=value1","Name2=value2"]) # depreciated
1111+ >>> setParameters(pvals={"Name1": "value1", "Name2": "value2"})
11251112 """
1126- return self .setMethodHelper (
1127- inputdata = pvals ,
1113+ inputdata = self ._prepare_inputdata (rawinput = pvals )
1114+
1115+ return self ._set_method_helper (
1116+ inputdata = inputdata ,
11281117 classdata = self .paramlist ,
11291118 datatype = "parameter" ,
11301119 overwritedata = self .overridevariables )
@@ -1134,11 +1123,14 @@ def setSimulationOptions(self, simOptions: str | list[str] | dict[str, str | int
11341123 This method is used to set simulation options. It can be called:
11351124 with a sequence of simulation options name and assigning corresponding values as arguments as show in the example below:
11361125 usage
1137- >>> setSimulationOptions("Name=value")
1138- >>> setSimulationOptions(["Name1=value1","Name2=value2"])
1126+ >>> setSimulationOptions("Name=value") # depreciated
1127+ >>> setSimulationOptions(["Name1=value1","Name2=value2"]) # depreciated
1128+ >>> setSimulationOptions(simOptions={"Name1": "value1", "Name2": "value2"})
11391129 """
1140- return self .setMethodHelper (
1141- inputdata = simOptions ,
1130+ inputdata = self ._prepare_inputdata (rawinput = simOptions )
1131+
1132+ return self ._set_method_helper (
1133+ inputdata = inputdata ,
11421134 classdata = self .simulateOptions ,
11431135 datatype = "simulation-option" ,
11441136 overwritedata = self .simoptionsoverride )
@@ -1148,11 +1140,14 @@ def setLinearizationOptions(self, linearizationOptions: str | list[str] | dict[s
11481140 This method is used to set linearization options. It can be called:
11491141 with a sequence of linearization options name and assigning corresponding value as arguments as show in the example below
11501142 usage
1151- >>> setLinearizationOptions("Name=value")
1152- >>> setLinearizationOptions(["Name1=value1","Name2=value2"])
1143+ >>> setLinearizationOptions("Name=value") # depreciated
1144+ >>> setLinearizationOptions(["Name1=value1","Name2=value2"]) # depreciated
1145+ >>> setLinearizationOptions(linearizationOtions={"Name1": "value1", "Name2": "value2"})
11531146 """
1154- return self .setMethodHelper (
1155- inputdata = linearizationOptions ,
1147+ inputdata = self ._prepare_inputdata (rawinput = linearizationOptions )
1148+
1149+ return self ._set_method_helper (
1150+ inputdata = inputdata ,
11561151 classdata = self .linearOptions ,
11571152 datatype = "Linearization-option" ,
11581153 overwritedata = None )
@@ -1162,11 +1157,14 @@ def setOptimizationOptions(self, optimizationOptions: str | list[str] | dict[str
11621157 This method is used to set optimization options. It can be called:
11631158 with a sequence of optimization options name and assigning corresponding values as arguments as show in the example below:
11641159 usage
1165- >>> setOptimizationOptions("Name=value")
1166- >>> setOptimizationOptions(["Name1=value1","Name2=value2"])
1160+ >>> setOptimizationOptions("Name=value") # depreciated
1161+ >>> setOptimizationOptions(["Name1=value1","Name2=value2"]) # depreciated
1162+ >>> setOptimizationOptions(optimizationOptions={"Name1": "value1", "Name2": "value2"})
11671163 """
1168- return self .setMethodHelper (
1169- inputdata = optimizationOptions ,
1164+ inputdata = self ._prepare_inputdata (rawinput = optimizationOptions )
1165+
1166+ return self ._set_method_helper (
1167+ inputdata = inputdata ,
11701168 classdata = self .optimizeOptions ,
11711169 datatype = "optimization-option" ,
11721170 overwritedata = None )
@@ -1176,9 +1174,12 @@ def setInputs(self, name: str | list[str] | dict[str, str | int | float]) -> boo
11761174 This method is used to set input values. It can be called:
11771175 with a sequence of input name and assigning corresponding values as arguments as show in the example below:
11781176 usage
1179- >>> setInputs("Name=value")
1180- >>> setInputs(["Name1=value1","Name2=value2"])
1177+ >>> setInputs("Name=value") # depreciated
1178+ >>> setInputs(["Name1=value1","Name2=value2"]) # depreciated
1179+ >>> setInputs(name={"Name1": "value1", "Name2": "value2"})
11811180 """
1181+ # inputdata = self._prepare_inputdata(rawinput=name)
1182+
11821183 if isinstance (name , str ):
11831184 name1 : str = name
11841185 name1 = name1 .replace (" " , "" )
0 commit comments