|
4 | 4 | """ |
5 | 5 |
|
6 | 6 | import logging |
| 7 | +import numbers |
7 | 8 | import os |
8 | 9 | import pathlib |
9 | | -import platform |
10 | 10 | from typing import Any, Optional |
| 11 | +import warnings |
11 | 12 |
|
12 | 13 | import numpy as np |
13 | 14 |
|
14 | 15 | from OMPython.model_execution import ( |
15 | 16 | ModelExecutionCmd, |
16 | 17 | ModelExecutionException, |
17 | 18 | ) |
| 19 | +from OMPython.om_session_abc import ( |
| 20 | + OMPathABC, |
| 21 | +) |
18 | 22 | from OMPython.om_session_omc import ( |
19 | 23 | OMCSessionLocal, |
20 | 24 | ) |
21 | 25 | from OMPython.modelica_system_abc import ( |
| 26 | + LinearizationResult, |
22 | 27 | ModelicaSystemError, |
23 | 28 | ) |
24 | 29 | from OMPython.modelica_system_omc import ( |
@@ -72,8 +77,73 @@ def __init__( |
72 | 77 | def setCommandLineOptions(self, commandLineOptions: str): |
73 | 78 | super().set_command_line_options(command_line_option=commandLineOptions) |
74 | 79 |
|
75 | | - def _set_compatibility_helper( |
| 80 | + def simulate_cmd( # type: ignore[override] |
| 81 | + self, |
| 82 | + result_file: OMPathABC, |
| 83 | + simflags: Optional[str] = None, |
| 84 | + simargs: Optional[dict[str, Optional[str | dict[str, Any] | numbers.Number]]] = None, |
| 85 | + ) -> ModelExecutionCmd: |
| 86 | + """ |
| 87 | + Compatibility layer for OMPython v4.0.0 - keep simflags available and use ModelicaSystemCmd! |
| 88 | + """ |
| 89 | + |
| 90 | + if simargs is None: |
| 91 | + simargs = {} |
| 92 | + |
| 93 | + if simflags is not None: |
| 94 | + simargs_extra = parse_simflags(simflags=simflags) |
| 95 | + simargs = simargs | simargs_extra |
| 96 | + |
| 97 | + return super().simulate_cmd( |
| 98 | + result_file=result_file, |
| 99 | + simargs=simargs, |
| 100 | + ) |
| 101 | + |
| 102 | + def simulate( # type: ignore[override] |
| 103 | + self, |
| 104 | + resultfile: Optional[str | os.PathLike] = None, |
| 105 | + simflags: Optional[str] = None, |
| 106 | + simargs: Optional[dict[str, Optional[str | dict[str, Any] | numbers.Number]]] = None, |
| 107 | + ) -> None: |
| 108 | + """ |
| 109 | + Compatibility layer for OMPython v4.0.0 - keep simflags available and use ModelicaSystemCmd! |
| 110 | + """ |
| 111 | + |
| 112 | + if simargs is None: |
| 113 | + simargs = {} |
| 114 | + |
| 115 | + if simflags is not None: |
| 116 | + simargs_extra = parse_simflags(simflags=simflags) |
| 117 | + simargs = simargs | simargs_extra |
| 118 | + |
| 119 | + return super().simulate( |
| 120 | + resultfile=resultfile, |
| 121 | + simargs=simargs, |
| 122 | + ) |
| 123 | + |
| 124 | + def linearize( # type: ignore[override] |
76 | 125 | self, |
| 126 | + lintime: Optional[float] = None, |
| 127 | + simflags: Optional[str] = None, |
| 128 | + simargs: Optional[dict[str, Optional[str | dict[str, Any] | numbers.Number]]] = None, |
| 129 | + ) -> LinearizationResult: |
| 130 | + """ |
| 131 | + Compatibility layer for OMPython v4.0.0 - keep simflags available and use ModelicaSystemCmd! |
| 132 | + """ |
| 133 | + if simargs is None: |
| 134 | + simargs = {} |
| 135 | + |
| 136 | + if simflags is not None: |
| 137 | + simargs_extra = parse_simflags(simflags=simflags) |
| 138 | + simargs = simargs | simargs_extra |
| 139 | + |
| 140 | + return super().linearize( |
| 141 | + lintime=lintime, |
| 142 | + simargs=simargs, |
| 143 | + ) |
| 144 | + |
| 145 | + @staticmethod |
| 146 | + def _set_compatibility_helper( |
77 | 147 | pkey: str, |
78 | 148 | args: Any, |
79 | 149 | kwargs: dict[str, Any], |
@@ -329,51 +399,52 @@ class ModelicaSystemDoE(ModelicaDoEOMC): |
329 | 399 | @depreciated_class(msg="Please use class ModelExecutionCmd instead!") |
330 | 400 | class ModelicaSystemCmd(ModelExecutionCmd): |
331 | 401 | """ |
332 | | - Compatibility class; in the new version it is renamed as ModelExecutionCmd. |
333 | | - """ |
334 | | - |
335 | | - def __init__( |
336 | | - self, |
337 | | - runpath: pathlib.Path, |
338 | | - modelname: str, |
339 | | - timeout: float = 10.0, |
340 | | - ) -> None: |
341 | | - super().__init__( |
342 | | - runpath=runpath, |
343 | | - timeout=timeout, |
344 | | - cmd_prefix=[], |
345 | | - model_name=modelname, |
346 | | - ) |
| 402 | + Compatibility class; not much content. |
347 | 403 |
|
348 | | - def get_exe(self) -> pathlib.Path: |
349 | | - """Get the path to the compiled model executable.""" |
350 | | - |
351 | | - path_run = pathlib.Path(self._runpath) |
352 | | - if platform.system() == "Windows": |
353 | | - path_exe = path_run / f"{self._model_name}.exe" |
354 | | - else: |
355 | | - path_exe = path_run / self._model_name |
356 | | - |
357 | | - if not path_exe.exists(): |
358 | | - raise ModelicaSystemError(f"Application file path not found: {path_exe}") |
359 | | - |
360 | | - return path_exe |
361 | | - |
362 | | - def get_cmd(self) -> list: |
363 | | - """ |
364 | | - Get a list with the path to the executable and all command line args. |
365 | | -
|
366 | | - This can later be used as an argument for subprocess.run(). |
367 | | - """ |
| 404 | + Missing definitions: |
| 405 | + * get_exe() - see self.definition.cmd_model_executable |
| 406 | + * get_cmd() - use self.get_cmd_args() or self.definition().get_cmd() |
| 407 | + * run() - use self.definition().run() |
| 408 | + """ |
368 | 409 |
|
369 | | - cmdl = [self.get_exe().as_posix()] + self.get_cmd_args() |
370 | 410 |
|
371 | | - return cmdl |
| 411 | +def parse_simflags(simflags: str) -> dict[str, Optional[str | dict[str, Any] | numbers.Number]]: |
| 412 | + """ |
| 413 | + Parse a simflag definition; this is deprecated! |
372 | 414 |
|
373 | | - def run(self) -> int: |
374 | | - cmd_definition = self.definition() |
375 | | - try: |
376 | | - returncode = cmd_definition.run() |
377 | | - except ModelExecutionException as exc: |
378 | | - raise ModelicaSystemError(f"Cannot execute model: {exc}") from exc |
379 | | - return returncode |
| 415 | + The return data can be used as input for self.args_set(). |
| 416 | + """ |
| 417 | + warnings.warn( |
| 418 | + message="The argument 'simflags' is depreciated and will be removed in future versions; " |
| 419 | + "please use 'simargs' instead", |
| 420 | + category=DeprecationWarning, |
| 421 | + stacklevel=2, |
| 422 | + ) |
| 423 | + |
| 424 | + simargs: dict[str, Optional[str | dict[str, Any] | numbers.Number]] = {} |
| 425 | + |
| 426 | + args = [s for s in simflags.split(' ') if s] |
| 427 | + for arg in args: |
| 428 | + if arg[0] != '-': |
| 429 | + raise ModelExecutionException(f"Invalid simulation flag: {arg}") |
| 430 | + arg = arg[1:] |
| 431 | + parts = arg.split('=') |
| 432 | + if len(parts) == 1: |
| 433 | + simargs[parts[0]] = None |
| 434 | + elif parts[0] == 'override': |
| 435 | + override = '='.join(parts[1:]) |
| 436 | + |
| 437 | + override_dict = {} |
| 438 | + for item in override.split(','): |
| 439 | + kv = item.split('=') |
| 440 | + if not 0 < len(kv) < 3: |
| 441 | + raise ModelExecutionException(f"Invalid value for '-override': {override}") |
| 442 | + if kv[0]: |
| 443 | + try: |
| 444 | + override_dict[kv[0]] = kv[1] |
| 445 | + except (KeyError, IndexError) as ex: |
| 446 | + raise ModelExecutionException(f"Invalid value for '-override': {override}") from ex |
| 447 | + |
| 448 | + simargs[parts[0]] = override_dict |
| 449 | + |
| 450 | + return simargs |
0 commit comments