Skip to content

Commit 2ac81e4

Browse files
committed
[ModelicaSystem] rename _getconn => _session and add get_session()
1 parent a65f8dc commit 2ac81e4

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
@@ -384,9 +384,9 @@ def __init__(
384384
self._linearized_states: list[str] = [] # linearization states list
385385

386386
if omc_process is not None:
387-
self._getconn = OMCSessionZMQ(omc_process=omc_process)
387+
self._session = OMCSessionZMQ(omc_process=omc_process)
388388
else:
389-
self._getconn = OMCSessionZMQ(omhome=omhome)
389+
self._session = OMCSessionZMQ(omhome=omhome)
390390

391391
# set commandLineOptions if provided by users
392392
self.setCommandLineOptions(commandLineOptions=commandLineOptions)
@@ -400,7 +400,7 @@ def __init__(
400400
self._lmodel = lmodel # may be needed if model is derived from other model
401401
self._model_name = modelName # Model class name
402402
if fileName is not None:
403-
file_name = self._getconn.omcpath(fileName).resolve()
403+
file_name = self._session.omcpath(fileName).resolve()
404404
else:
405405
file_name = None
406406
self._file_name: Optional[OMCPath] = file_name # Model file/package name
@@ -430,6 +430,9 @@ def __init__(
430430
if build:
431431
self.buildModel(variableFilter)
432432

433+
def get_session(self) -> OMCSessionZMQ:
434+
return self._session
435+
433436
def setCommandLineOptions(self, commandLineOptions: Optional[str] = None):
434437
# set commandLineOptions if provided by users
435438
if commandLineOptions is None:
@@ -471,11 +474,11 @@ def setWorkDirectory(self, customBuildDirectory: Optional[str | os.PathLike] = N
471474
directory. If no directory is defined a unique temporary directory is created.
472475
"""
473476
if customBuildDirectory is not None:
474-
workdir = self._getconn.omcpath(customBuildDirectory).absolute()
477+
workdir = self._session.omcpath(customBuildDirectory).absolute()
475478
if not workdir.is_dir():
476479
raise IOError(f"Provided work directory does not exists: {customBuildDirectory}!")
477480
else:
478-
workdir = self._getconn.omcpath_tempdir().absolute()
481+
workdir = self._session.omcpath_tempdir().absolute()
479482
if not workdir.is_dir():
480483
raise IOError(f"{workdir} could not be created")
481484

@@ -511,24 +514,24 @@ def buildModel(self, variableFilter: Optional[str] = None):
511514

512515
# check if the executable exists ...
513516
om_cmd = ModelicaSystemCmd(
514-
session=self._getconn,
517+
session=self._session,
515518
runpath=self.getWorkDirectory(),
516519
modelname=self._model_name,
517520
timeout=5.0,
518521
)
519522
# ... by running it - output help for command help
520523
om_cmd.arg_set(key="help", val="help")
521524
cmd_definition = om_cmd.definition()
522-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
525+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
523526
if returncode != 0:
524527
raise ModelicaSystemError("Model executable not working!")
525528

526-
xml_file = self._getconn.omcpath(buildModelResult[0]).parent / buildModelResult[1]
529+
xml_file = self._session.omcpath(buildModelResult[0]).parent / buildModelResult[1]
527530
self._xmlparse(xml_file=xml_file)
528531

529532
def sendExpression(self, expr: str, parsed: bool = True) -> Any:
530533
try:
531-
retval = self._getconn.sendExpression(expr, parsed)
534+
retval = self._session.sendExpression(expr, parsed)
532535
except OMCSessionException as ex:
533536
raise ModelicaSystemError(f"Error executing {repr(expr)}") from ex
534537

@@ -1003,7 +1006,7 @@ def simulate_cmd(
10031006
"""
10041007

10051008
om_cmd = ModelicaSystemCmd(
1006-
session=self._getconn,
1009+
session=self._session,
10071010
runpath=self.getWorkDirectory(),
10081011
modelname=self._model_name,
10091012
timeout=timeout,
@@ -1083,7 +1086,7 @@ def simulate(
10831086
elif isinstance(resultfile, OMCPath):
10841087
self._result_file = resultfile
10851088
else:
1086-
self._result_file = self._getconn.omcpath(resultfile)
1089+
self._result_file = self._session.omcpath(resultfile)
10871090
if not self._result_file.is_absolute():
10881091
self._result_file = self.getWorkDirectory() / resultfile
10891092

@@ -1102,7 +1105,7 @@ def simulate(
11021105
self._result_file.unlink()
11031106
# ... run simulation ...
11041107
cmd_definition = om_cmd.definition()
1105-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
1108+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
11061109
# and check returncode *AND* resultfile
11071110
if returncode != 0 and self._result_file.is_file():
11081111
# check for an empty (=> 0B) result file which indicates a crash of the model executable
@@ -1157,7 +1160,7 @@ def getSolutions(
11571160
raise ModelicaSystemError("No result file found. Run simulate() first.")
11581161
result_file = self._result_file
11591162
else:
1160-
result_file = self._getconn.omcpath(resultfile)
1163+
result_file = self._session.omcpath(resultfile)
11611164

11621165
# check if the result file exits
11631166
if not result_file.is_file():
@@ -1624,7 +1627,7 @@ def linearize(
16241627
)
16251628

16261629
om_cmd = ModelicaSystemCmd(
1627-
session=self._getconn,
1630+
session=self._session,
16281631
runpath=self.getWorkDirectory(),
16291632
modelname=self._model_name,
16301633
timeout=timeout,
@@ -1664,7 +1667,7 @@ def linearize(
16641667
linear_file.unlink(missing_ok=True)
16651668

16661669
cmd_definition = om_cmd.definition()
1667-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
1670+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
16681671
if returncode != 0:
16691672
raise ModelicaSystemError(f"Linearize failed with return code: {returncode}")
16701673
if not linear_file.is_file():
@@ -1832,9 +1835,9 @@ def __init__(
18321835
self._timeout = timeout
18331836

18341837
if resultpath is None:
1835-
self._resultpath = self._mod._getconn.omcpath_tempdir()
1838+
self._resultpath = self._mod.get_session().omcpath_tempdir()
18361839
else:
1837-
self._resultpath = self._mod._getconn.omcpath(resultpath)
1840+
self._resultpath = self._mod.get_session().omcpath(resultpath)
18381841
if not self._resultpath.is_dir():
18391842
raise ModelicaSystemError("Argument resultpath must be set to a valid path within the environment used "
18401843
f"for the OpenModelica session: {resultpath}!")
@@ -2005,12 +2008,12 @@ def worker(worker_id, task_queue):
20052008
raise ModelicaSystemError("Missing simulation definition!")
20062009

20072010
resultfile = cmd_definition.cmd_result_path
2008-
resultpath = self._mod._getconn.omcpath(resultfile)
2011+
resultpath = self._mod.get_session().omcpath(resultfile)
20092012

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

20122015
try:
2013-
returncode = self._mod._getconn.run_model_executable(cmd_run_data=cmd_definition)
2016+
returncode = self._mod.get_session().run_model_executable(cmd_run_data=cmd_definition)
20142017
logger.info(f"[Worker {worker_id}] Simulation {resultpath.name} "
20152018
f"finished with return code: {returncode}")
20162019
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)