Skip to content

Commit c7b54c8

Browse files
committed
G002-bugfix
[ModelExecutionException] catch exception if ModelExecutionCmd.run() is used [bugfix] [ModelicaSystem] fix exception; use ModelicaSystemError (instead of wrong ModelExecutionException) [bugfix] [ModelicaSystemABC] fix _prepare_input_data() - ensure returned data is dict[str, str]
1 parent 27a49e5 commit c7b54c8

3 files changed

Lines changed: 35 additions & 12 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def getContinuous(
140140
retval3.append(str(val))
141141
return retval3
142142

143-
raise ModelExecutionException("Invalid data!")
143+
raise ModelicaSystemError("Invalid data!")
144144

145145
def getOutputs(
146146
self,
@@ -167,7 +167,7 @@ def getOutputs(
167167
retval3.append(str(val))
168168
return retval3
169169

170-
raise ModelExecutionException("Invalid data!")
170+
raise ModelicaSystemError("Invalid data!")
171171

172172

173173
class ModelicaSystemDoE(ModelicaDoEOMC):
@@ -209,7 +209,8 @@ def get_exe(self) -> pathlib.Path:
209209
return path_exe
210210

211211
def get_cmd(self) -> list:
212-
"""Get a list with the path to the executable and all command line args.
212+
"""
213+
Get a list with the path to the executable and all command line args.
213214
214215
This can later be used as an argument for subprocess.run().
215216
"""
@@ -218,6 +219,10 @@ def get_cmd(self) -> list:
218219

219220
return cmdl
220221

221-
def run(self):
222+
def run(self) -> int:
222223
cmd_definition = self.definition()
223-
return cmd_definition.run()
224+
try:
225+
returncode = cmd_definition.run()
226+
except ModelExecutionException as exc:
227+
raise ModelicaSystemError(f"Cannot execute model: {exc}") from exc
228+
return returncode

OMPython/modelica_doe_abc.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from OMPython.model_execution import (
1616
ModelExecutionData,
17+
ModelExecutionException,
1718
)
1819
from OMPython.om_session_abc import (
1920
OMPathABC,
@@ -310,8 +311,8 @@ def worker(worker_id, task_queue):
310311
returncode = cmd_definition.run()
311312
logger.info(f"[Worker {worker_id}] Simulation {resultpath.name} "
312313
f"finished with return code: {returncode}")
313-
except ModelicaSystemError as ex:
314-
logger.warning(f"Simulation error for {resultpath.name}: {ex}")
314+
except ModelExecutionException as exc:
315+
logger.warning(f"Simulation error for {resultpath.name}: {exc}")
315316

316317
# Mark the task as done
317318
task_queue.task_done()

OMPython/modelica_system_abc.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from OMPython.model_execution import (
2020
ModelExecutionCmd,
21+
ModelExecutionException,
2122
)
2223
from OMPython.om_session_abc import (
2324
OMPathABC,
@@ -199,7 +200,10 @@ def check_model_executable(self):
199200
# ... by running it - output help for command help
200201
om_cmd.arg_set(key="help", val="help")
201202
cmd_definition = om_cmd.definition()
202-
returncode = cmd_definition.run()
203+
try:
204+
returncode = cmd_definition.run()
205+
except ModelExecutionException as exc:
206+
raise ModelicaSystemError(f"Cannot execute model: {exc}") from exc
203207
if returncode != 0:
204208
raise ModelicaSystemError("Model executable not working!")
205209

@@ -730,7 +734,10 @@ def simulate(
730734
self._result_file.unlink()
731735
# ... run simulation ...
732736
cmd_definition = om_cmd.definition()
733-
returncode = cmd_definition.run()
737+
try:
738+
returncode = cmd_definition.run()
739+
except ModelExecutionException as exc:
740+
raise ModelicaSystemError(f"Cannot execute model: {exc}") from exc
734741
# and check returncode *AND* resultfile
735742
if returncode != 0 and self._result_file.is_file():
736743
# check for an empty (=> 0B) result file which indicates a crash of the model executable
@@ -758,8 +765,10 @@ def prepare_str(str_in: str) -> dict[str, str]:
758765
key_val_list: list[str] = str_in.split("=")
759766
if len(key_val_list) != 2:
760767
raise ModelicaSystemError(f"Invalid 'key=value' pair: {str_in}")
768+
if len(key_val_list[0]) == 0:
769+
raise ModelicaSystemError(f"Empty key: {str_in}")
761770

762-
input_data_from_str: dict[str, str] = {key_val_list[0]: key_val_list[1]}
771+
input_data_from_str: dict[str, str] = {str(key_val_list[0]): str(key_val_list[1])}
763772

764773
return input_data_from_str
765774

@@ -785,7 +794,12 @@ def prepare_str(str_in: str) -> dict[str, str]:
785794
raise ModelicaSystemError(f"Invalid input data type for set*() function: {type(item)}!")
786795
input_data = input_data | prepare_str(item)
787796
elif isinstance(input_arg, dict):
788-
input_data = input_data | input_arg
797+
input_arg_str: dict[str, str] = {}
798+
for key, val in input_arg.items():
799+
if not isinstance(key, str) or len(key) == 0:
800+
raise ModelicaSystemError(f"Invalid key for set*() functions: {repr(key)}")
801+
input_arg_str[key] = str(val)
802+
input_data = input_data | input_arg_str
789803
else:
790804
raise ModelicaSystemError(f"Invalid input data type for set*() function: {type(input_arg)}!")
791805

@@ -1173,7 +1187,10 @@ def linearize(
11731187
linear_file.unlink(missing_ok=True)
11741188

11751189
cmd_definition = om_cmd.definition()
1176-
returncode = cmd_definition.run()
1190+
try:
1191+
returncode = cmd_definition.run()
1192+
except ModelExecutionException as exc:
1193+
raise ModelicaSystemError(f"Cannot execute model: {exc}") from exc
11771194
if returncode != 0:
11781195
raise ModelicaSystemError(f"Linearize failed with return code: {returncode}")
11791196
if not linear_file.is_file():

0 commit comments

Comments
 (0)