Skip to content

Commit c99d207

Browse files
committed
[ModelicaSystem] rename _getconn => _session and add get_session()
1 parent 917540f commit c99d207

4 files changed

Lines changed: 25 additions & 22 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,9 @@ def __init__(
330330
self._linearized_states: list[str] = [] # linearization states list
331331

332332
if omc_process is not None:
333-
self._getconn = OMCSessionZMQ(omc_process=omc_process)
333+
self._session = OMCSessionZMQ(omc_process=omc_process)
334334
else:
335-
self._getconn = OMCSessionZMQ(omhome=omhome)
335+
self._session = OMCSessionZMQ(omhome=omhome)
336336

337337
# set commandLineOptions if provided by users
338338
self.setCommandLineOptions(commandLineOptions=commandLineOptions)
@@ -346,7 +346,7 @@ def __init__(
346346
self._lmodel = lmodel # may be needed if model is derived from other model
347347
self._model_name = modelName # Model class name
348348
if fileName is not None:
349-
file_name = self._getconn.omcpath(fileName).resolve()
349+
file_name = self._session.omcpath(fileName).resolve()
350350
else:
351351
file_name = None
352352
self._file_name: Optional[OMCPath] = file_name # Model file/package name
@@ -376,6 +376,9 @@ def __init__(
376376
if build:
377377
self.buildModel(variableFilter)
378378

379+
def get_session(self) -> OMCSessionZMQ:
380+
return self._session
381+
379382
def setCommandLineOptions(self, commandLineOptions: Optional[str] = None):
380383
# set commandLineOptions if provided by users
381384
if commandLineOptions is None:
@@ -417,11 +420,11 @@ def setWorkDirectory(self, customBuildDirectory: Optional[str | os.PathLike] = N
417420
directory. If no directory is defined a unique temporary directory is created.
418421
"""
419422
if customBuildDirectory is not None:
420-
workdir = self._getconn.omcpath(customBuildDirectory).absolute()
423+
workdir = self._session.omcpath(customBuildDirectory).absolute()
421424
if not workdir.is_dir():
422425
raise IOError(f"Provided work directory does not exists: {customBuildDirectory}!")
423426
else:
424-
workdir = self._getconn.omcpath_tempdir().absolute()
427+
workdir = self._session.omcpath_tempdir().absolute()
425428
if not workdir.is_dir():
426429
raise IOError(f"{workdir} could not be created")
427430

@@ -454,24 +457,24 @@ def buildModel(self, variableFilter: Optional[str] = None):
454457

455458
# check if the executable exists ...
456459
om_cmd = ModelicaSystemCmd(
457-
session=self._getconn,
460+
session=self._session,
458461
runpath=self.getWorkDirectory(),
459462
modelname=self._model_name,
460463
timeout=5.0,
461464
)
462465
# ... by running it - output help for command help
463466
om_cmd.arg_set(key="help", val="help")
464467
cmd_definition = om_cmd.definition()
465-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
468+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
466469
if returncode != 0:
467470
raise ModelicaSystemError("Model executable not working!")
468471

469-
xml_file = self._getconn.omcpath(buildModelResult[0]).parent / buildModelResult[1]
472+
xml_file = self._session.omcpath(buildModelResult[0]).parent / buildModelResult[1]
470473
self._xmlparse(xml_file=xml_file)
471474

472475
def sendExpression(self, expr: str, parsed: bool = True):
473476
try:
474-
retval = self._getconn.sendExpression(expr, parsed)
477+
retval = self._session.sendExpression(expr, parsed)
475478
except OMCSessionException as ex:
476479
raise ModelicaSystemError(f"Error executing {repr(expr)}") from ex
477480

@@ -941,7 +944,7 @@ def simulate_cmd(
941944
"""
942945

943946
om_cmd = ModelicaSystemCmd(
944-
session=self._getconn,
947+
session=self._session,
945948
runpath=self.getWorkDirectory(),
946949
modelname=self._model_name,
947950
timeout=timeout,
@@ -1021,7 +1024,7 @@ def simulate(
10211024
elif isinstance(resultfile, OMCPath):
10221025
self._result_file = resultfile
10231026
else:
1024-
self._result_file = self._getconn.omcpath(resultfile)
1027+
self._result_file = self._session.omcpath(resultfile)
10251028
if not self._result_file.is_absolute():
10261029
self._result_file = self.getWorkDirectory() / resultfile
10271030

@@ -1040,7 +1043,7 @@ def simulate(
10401043
self._result_file.unlink()
10411044
# ... run simulation ...
10421045
cmd_definition = om_cmd.definition()
1043-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
1046+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
10441047
# and check returncode *AND* resultfile
10451048
if returncode != 0 and self._result_file.is_file():
10461049
# check for an empty (=> 0B) result file which indicates a crash of the model executable
@@ -1095,7 +1098,7 @@ def getSolutions(
10951098
raise ModelicaSystemError("No result file found. Run simulate() first.")
10961099
result_file = self._result_file
10971100
else:
1098-
result_file = self._getconn.omcpath(resultfile)
1101+
result_file = self._session.omcpath(resultfile)
10991102

11001103
# check for result file exits
11011104
if not result_file.is_file():
@@ -1557,7 +1560,7 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
15571560
)
15581561

15591562
om_cmd = ModelicaSystemCmd(
1560-
session=self._getconn,
1563+
session=self._session,
15611564
runpath=self.getWorkDirectory(),
15621565
modelname=self._model_name,
15631566
timeout=timeout,
@@ -1597,7 +1600,7 @@ def linearize(self, lintime: Optional[float] = None, simflags: Optional[str] = N
15971600
linear_file.unlink(missing_ok=True)
15981601

15991602
cmd_definition = om_cmd.definition()
1600-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
1603+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
16011604
if returncode != 0:
16021605
raise ModelicaSystemError(f"Linearize failed with return code: {returncode}")
16031606
if not linear_file.is_file():
@@ -1765,9 +1768,9 @@ def __init__(
17651768
self._timeout = timeout
17661769

17671770
if resultpath is None:
1768-
self._resultpath = self._mod._getconn.omcpath_tempdir()
1771+
self._resultpath = self._mod.get_session().omcpath_tempdir()
17691772
else:
1770-
self._resultpath = self._mod._getconn.omcpath(resultpath)
1773+
self._resultpath = self._mod.get_session().omcpath(resultpath)
17711774
if not self._resultpath.is_dir():
17721775
raise ModelicaSystemError("Argument resultpath must be set to a valid path within the environment used "
17731776
f"for the OpenModelica session: {resultpath}!")
@@ -1938,12 +1941,12 @@ def worker(worker_id, task_queue):
19381941
raise ModelicaSystemError("Missing simulation definition!")
19391942

19401943
resultfile = cmd_definition.cmd_result_path
1941-
resultpath = self._mod._getconn.omcpath(resultfile)
1944+
resultpath = self._mod.get_session().omcpath(resultfile)
19421945

19431946
logger.info(f"[Worker {worker_id}] Performing task: {resultpath.name}")
19441947

19451948
try:
1946-
returncode = self._mod._getconn.run_model_executable(cmd_run_data=cmd_definition)
1949+
returncode = self._mod.get_session().run_model_executable(cmd_run_data=cmd_definition)
19471950
logger.info(f"[Worker {worker_id}] Simulation {resultpath.name} "
19481951
f"finished with return code: {returncode}")
19491952
except ModelicaSystemError as ex:

tests/test_ModelicaSystemCmd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def model_firstorder(tmp_path):
1919
def mscmd_firstorder(model_firstorder):
2020
mod = OMPython.ModelicaSystem(fileName=model_firstorder.as_posix(), modelName="M")
2121
mscmd = OMPython.ModelicaSystemCmd(
22-
session=mod._getconn,
22+
session=mod.get_session(),
2323
runpath=mod.getWorkDirectory(),
2424
modelname=mod._model_name,
2525
)

tests/test_OMSessionCmd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def test_isPackage():
1010
def test_isPackage2():
1111
mod = OMPython.ModelicaSystem(modelName="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog",
1212
lmodel=["Modelica"])
13-
omccmd = OMPython.OMCSessionCmd(session=mod._getconn)
13+
omccmd = OMPython.OMCSessionCmd(session=mod.get_session())
1414
assert omccmd.isPackage('Modelica')
1515

1616

tests/test_optimization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_optimization_example(tmp_path):
4848
r = mod.optimize()
4949
# it is necessary to specify resultfile, otherwise it wouldn't find it.
5050
resultfile_str = r["resultFile"]
51-
resultfile_omcpath = mod._getconn.omcpath(resultfile_str)
51+
resultfile_omcpath = mod.get_session().omcpath(resultfile_str)
5252
time, f, v = mod.getSolutions(["time", "f", "v"], resultfile=resultfile_omcpath.as_posix())
5353
assert np.isclose(f[0], 10)
5454
assert np.isclose(f[-1], -10)

0 commit comments

Comments
 (0)