44"""
55
66import logging
7+ import numbers
78import os
89import pathlib
9- import platform
1010from typing import Any , Optional
1111import warnings
1212
1616 ModelExecutionConfig ,
1717 ModelExecutionException ,
1818)
19+ from OMPython .om_session_abc import (
20+ OMPathABC ,
21+ )
1922from OMPython .om_session_omc import (
2023 OMCSessionLocal ,
2124)
2225from OMPython .modelica_system_abc import (
26+ LinearizationResult ,
2327 ModelicaSystemError ,
2428)
2529from OMPython .modelica_system_omc import (
@@ -73,8 +77,73 @@ def __init__(
7377 def setCommandLineOptions (self , commandLineOptions : str ):
7478 super ().set_command_line_options (command_line_option = commandLineOptions )
7579
76- 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]
77125 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 (
78147 pkey : str ,
79148 args : Any ,
80149 kwargs : dict [str , Any ],
@@ -330,7 +399,12 @@ class ModelicaSystemDoE(ModelicaDoEOMC):
330399@depreciated_class (msg = "Please use class ModelExecutionConfig instead!" )
331400class ModelicaSystemCmd (ModelExecutionConfig ):
332401 """
333- Compatibility class; in the new version it is renamed as ModelExecutionConfig.
402+ Compatibility class; not much content.
403+
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()
334408 """
335409
336410 def __init__ (
@@ -346,35 +420,44 @@ def __init__(
346420 model_name = modelname ,
347421 )
348422
349- def get_exe (self ) -> pathlib .Path :
350- """Get the path to the compiled model executable."""
351-
352- path_run = pathlib .Path (self ._runpath )
353- if platform .system () == "Windows" :
354- path_exe = path_run / f"{ self ._model_name } .exe"
355- else :
356- path_exe = path_run / self ._model_name
357423
358- if not path_exe .exists ():
359- raise ModelicaSystemError (f"Application file path not found: { path_exe } " )
360-
361- return path_exe
362-
363- def get_cmd (self ) -> list :
364- """
365- Get a list with the path to the executable and all command line args.
366-
367- This can later be used as an argument for subprocess.run().
368- """
369-
370- cmdl = [self .get_exe ().as_posix ()] + self .get_cmd_args ()
371-
372- return cmdl
424+ def parse_simflags (simflags : str ) -> dict [str , Optional [str | dict [str , Any ] | numbers .Number ]]:
425+ """
426+ Parse a simflag definition; this is deprecated!
373427
374- def run (self ) -> int :
375- cmd_definition = self .definition ()
376- try :
377- returncode = cmd_definition .run ()
378- except ModelExecutionException as exc :
379- raise ModelicaSystemError (f"Cannot execute model: { exc } " ) from exc
380- return returncode
428+ The return data can be used as input for self.args_set().
429+ """
430+ warnings .warn (
431+ message = "The argument 'simflags' is depreciated and will be removed in future versions; "
432+ "please use 'simargs' instead" ,
433+ category = DeprecationWarning ,
434+ stacklevel = 2 ,
435+ )
436+
437+ simargs : dict [str , Optional [str | dict [str , Any ] | numbers .Number ]] = {}
438+
439+ args = [s for s in simflags .split (' ' ) if s ]
440+ for arg in args :
441+ if arg [0 ] != '-' :
442+ raise ModelExecutionException (f"Invalid simulation flag: { arg } " )
443+ arg = arg [1 :]
444+ parts = arg .split ('=' )
445+ if len (parts ) == 1 :
446+ simargs [parts [0 ]] = None
447+ elif parts [0 ] == 'override' :
448+ override = '=' .join (parts [1 :])
449+
450+ override_dict = {}
451+ for item in override .split (',' ):
452+ kv = item .split ('=' )
453+ if not 0 < len (kv ) < 3 :
454+ raise ModelExecutionException (f"Invalid value for '-override': { override } " )
455+ if kv [0 ]:
456+ try :
457+ override_dict [kv [0 ]] = kv [1 ]
458+ except (KeyError , IndexError ) as ex :
459+ raise ModelExecutionException (f"Invalid value for '-override': { override } " ) from ex
460+
461+ simargs [parts [0 ]] = override_dict
462+
463+ return simargs
0 commit comments