|
32 | 32 | CONDITIONS OF OSMC-PL. |
33 | 33 | """ |
34 | 34 |
|
| 35 | +import ast |
35 | 36 | import csv |
36 | 37 | from dataclasses import dataclass |
37 | 38 | import importlib |
@@ -1292,57 +1293,37 @@ def setInputs( |
1292 | 1293 | >>> setInputs(["Name1=value1","Name2=value2"]) # depreciated |
1293 | 1294 | >>> setInputs(name={"Name1": "value1", "Name2": "value2"}) |
1294 | 1295 | """ |
1295 | | - # inputdata = self._prepare_input_data(raw_input=name) |
1296 | | - |
1297 | | - if isinstance(name, str): |
1298 | | - name1: str = name |
1299 | | - name1 = name1.replace(" ", "") |
1300 | | - value1 = name1.split("=") |
1301 | | - if value1[0] in self.inputlist: |
1302 | | - tmpvalue = eval(value1[1]) |
1303 | | - if isinstance(tmpvalue, (int, float)): |
1304 | | - self.inputlist[value1[0]] = [(float(self._simulateOptions["startTime"]), float(value1[1])), |
1305 | | - (float(self._simulateOptions["stopTime"]), float(value1[1]))] |
1306 | | - elif isinstance(tmpvalue, list): |
1307 | | - self._checkValidInputs(tmpvalue) |
1308 | | - self._inputs[value1[0]] = tmpvalue |
| 1296 | + inputdata = self._prepare_input_data(raw_input=name) |
| 1297 | + |
| 1298 | + for key, val in inputdata.items(): |
| 1299 | + if key in self._inputs: |
| 1300 | + val_evaluated = ast.literal_eval(val) |
| 1301 | + if isinstance(val_evaluated, (int, float)): |
| 1302 | + self._inputs[key] = [(float(self._simulate_options["startTime"]), float(val)), |
| 1303 | + (float(self._simulate_options["stopTime"]), float(val))] |
| 1304 | + elif isinstance(val_evaluated, list): |
| 1305 | + if not all([isinstance(item, tuple) for item in val_evaluated]): |
| 1306 | + raise ModelicaSystemError("Value for setInput() must be in tuple format; " |
| 1307 | + f"got {repr(val_evaluated)}") |
| 1308 | + if val_evaluated != sorted(val_evaluated, key=lambda x: x[0]): |
| 1309 | + raise ModelicaSystemError("Time value should be in increasing order; " |
| 1310 | + f"got {repr(val_evaluated)}") |
| 1311 | + |
| 1312 | + for item in val_evaluated: |
| 1313 | + if item[0] < float(self._simulate_options["startTime"]): |
| 1314 | + raise ModelicaSystemError(f"Time value in {repr(item)} of {repr(val_evaluated)} is less " |
| 1315 | + "than the simulation start time") |
| 1316 | + if len(item) != 2: |
| 1317 | + raise ModelicaSystemError(f"Value {repr(item)} of {repr(val_evaluated)} " |
| 1318 | + "is in incorrect format!") |
| 1319 | + |
| 1320 | + self._inputs[key] = val_evaluated |
1309 | 1321 | self._inputFlag = True |
1310 | 1322 | else: |
1311 | | - raise ModelicaSystemError(f"{value1[0]} is not an input") |
1312 | | - elif isinstance(name, list): |
1313 | | - name_list: list[str] = name |
1314 | | - for name2 in name_list: |
1315 | | - name2 = name2.replace(" ", "") |
1316 | | - value2 = name2.split("=") |
1317 | | - if value2[0] in self.inputlist: |
1318 | | - tmpvalue = eval(value2[1]) |
1319 | | - if isinstance(tmpvalue, (int, float)): |
1320 | | - self.inputlist[value2[0]] = [(float(self._simulateOptions["startTime"]), float(value2[1])), |
1321 | | - (float(self.:simulateOptions["stopTime"]), float(value2[1]))] |
1322 | | - elif isinstance(tmpvalue, list): |
1323 | | - self._checkValidInputs(tmpvalue) |
1324 | | - self._inputs[value2[0]] = tmpvalue |
1325 | | - self._inputFlag = True |
1326 | | - else: |
1327 | | - raise ModelicaSystemError(f"{value2[0]} is not an input!") |
1328 | | - elif isinstance(name, dict): |
1329 | | - raise NotImplementedError("Must be defined!") |
| 1323 | + raise ModelicaSystemError(f"{key} is not an input") |
1330 | 1324 |
|
1331 | 1325 | return True |
1332 | 1326 |
|
1333 | | - def _checkValidInputs(self, name): |
1334 | | - if name != sorted(name, key=lambda x: x[0]): |
1335 | | - raise ModelicaSystemError('Time value should be in increasing order') |
1336 | | - for l in name: |
1337 | | - if isinstance(l, tuple): |
1338 | | - # if l[0] < float(self.simValuesList[0]): |
1339 | | - if l[0] < float(self._simulate_options["startTime"]): |
1340 | | - raise ModelicaSystemError('Input time value is less than simulation startTime') |
1341 | | - if len(l) != 2: |
1342 | | - raise ModelicaSystemError(f'Value for {l} is in incorrect format!') |
1343 | | - else: |
1344 | | - raise ModelicaSystemError('Error!!! Value must be in tuple format') |
1345 | | - |
1346 | 1327 | def _createCSVData(self) -> pathlib.Path: |
1347 | 1328 | start_time: float = float(self._simulate_options["startTime"]) |
1348 | 1329 | stop_time: float = float(self._simulate_options["stopTime"]) |
|
0 commit comments