Skip to content

Commit 3161da3

Browse files
committed
[ModelicaSystem] rename _getconn => _session and add get_session()
1 parent 55c1395 commit 3161da3

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

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

393393
# set commandLineOptions if provided by users
394394
self.setCommandLineOptions(commandLineOptions=commandLineOptions)
@@ -402,7 +402,7 @@ def __init__(
402402
self._lmodel = lmodel # may be needed if model is derived from other model
403403
self._model_name = modelName # Model class name
404404
if fileName is not None:
405-
file_name = self._getconn.omcpath(fileName).resolve()
405+
file_name = self._session.omcpath(fileName).resolve()
406406
else:
407407
file_name = None
408408
self._file_name: Optional[OMCPath] = file_name # Model file/package name
@@ -432,6 +432,9 @@ def __init__(
432432
if build:
433433
self.buildModel(variableFilter)
434434

435+
def get_session(self) -> OMCSessionZMQ:
436+
return self._session
437+
435438
def setCommandLineOptions(self, commandLineOptions: Optional[str] = None):
436439
# set commandLineOptions if provided by users
437440
if commandLineOptions is None:
@@ -473,11 +476,11 @@ def setWorkDirectory(self, customBuildDirectory: Optional[str | os.PathLike] = N
473476
directory. If no directory is defined a unique temporary directory is created.
474477
"""
475478
if customBuildDirectory is not None:
476-
workdir = self._getconn.omcpath(customBuildDirectory).absolute()
479+
workdir = self._session.omcpath(customBuildDirectory).absolute()
477480
if not workdir.is_dir():
478481
raise IOError(f"Provided work directory does not exists: {customBuildDirectory}!")
479482
else:
480-
workdir = self._getconn.omcpath_tempdir().absolute()
483+
workdir = self._session.omcpath_tempdir().absolute()
481484
if not workdir.is_dir():
482485
raise IOError(f"{workdir} could not be created")
483486

@@ -513,24 +516,24 @@ def buildModel(self, variableFilter: Optional[str] = None):
513516

514517
# check if the executable exists ...
515518
om_cmd = ModelicaSystemCmd(
516-
session=self._getconn,
519+
session=self._session,
517520
runpath=self.getWorkDirectory(),
518521
modelname=self._model_name,
519522
timeout=5.0,
520523
)
521524
# ... by running it - output help for command help
522525
om_cmd.arg_set(key="help", val="help")
523526
cmd_definition = om_cmd.definition()
524-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
527+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
525528
if returncode != 0:
526529
raise ModelicaSystemError("Model executable not working!")
527530

528-
xml_file = self._getconn.omcpath(buildModelResult[0]).parent / buildModelResult[1]
531+
xml_file = self._session.omcpath(buildModelResult[0]).parent / buildModelResult[1]
529532
self._xmlparse(xml_file=xml_file)
530533

531534
def sendExpression(self, expr: str, parsed: bool = True) -> Any:
532535
try:
533-
retval = self._getconn.sendExpression(expr, parsed)
536+
retval = self._session.sendExpression(expr, parsed)
534537
except OMCSessionException as ex:
535538
raise ModelicaSystemError(f"Error executing {repr(expr)}") from ex
536539

@@ -1005,7 +1008,7 @@ def simulate_cmd(
10051008
"""
10061009

10071010
om_cmd = ModelicaSystemCmd(
1008-
session=self._getconn,
1011+
session=self._session,
10091012
runpath=self.getWorkDirectory(),
10101013
modelname=self._model_name,
10111014
timeout=timeout,
@@ -1085,7 +1088,7 @@ def simulate(
10851088
elif isinstance(resultfile, OMCPath):
10861089
self._result_file = resultfile
10871090
else:
1088-
self._result_file = self._getconn.omcpath(resultfile)
1091+
self._result_file = self._session.omcpath(resultfile)
10891092
if not self._result_file.is_absolute():
10901093
self._result_file = self.getWorkDirectory() / resultfile
10911094

@@ -1104,7 +1107,7 @@ def simulate(
11041107
self._result_file.unlink()
11051108
# ... run simulation ...
11061109
cmd_definition = om_cmd.definition()
1107-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
1110+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
11081111
# and check returncode *AND* resultfile
11091112
if returncode != 0 and self._result_file.is_file():
11101113
# check for an empty (=> 0B) result file which indicates a crash of the model executable
@@ -1159,7 +1162,7 @@ def getSolutions(
11591162
raise ModelicaSystemError("No result file found. Run simulate() first.")
11601163
result_file = self._result_file
11611164
else:
1162-
result_file = self._getconn.omcpath(resultfile)
1165+
result_file = self._session.omcpath(resultfile)
11631166

11641167
# check if the result file exits
11651168
if not result_file.is_file():
@@ -1626,7 +1629,7 @@ def linearize(
16261629
)
16271630

16281631
om_cmd = ModelicaSystemCmd(
1629-
session=self._getconn,
1632+
session=self._session,
16301633
runpath=self.getWorkDirectory(),
16311634
modelname=self._model_name,
16321635
timeout=timeout,
@@ -1667,7 +1670,7 @@ def linearize(
16671670
linear_file.unlink(missing_ok=True)
16681671

16691672
cmd_definition = om_cmd.definition()
1670-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
1673+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
16711674
if returncode != 0:
16721675
raise ModelicaSystemError(f"Linearize failed with return code: {returncode}")
16731676

@@ -1836,9 +1839,9 @@ def __init__(
18361839
self._timeout = timeout
18371840

18381841
if resultpath is None:
1839-
self._resultpath = self._mod._getconn.omcpath_tempdir()
1842+
self._resultpath = self._mod.get_session().omcpath_tempdir()
18401843
else:
1841-
self._resultpath = self._mod._getconn.omcpath(resultpath)
1844+
self._resultpath = self._mod.get_session().omcpath(resultpath)
18421845
if not self._resultpath.is_dir():
18431846
raise ModelicaSystemError("Argument resultpath must be set to a valid path within the environment used "
18441847
f"for the OpenModelica session: {resultpath}!")
@@ -2009,12 +2012,12 @@ def worker(worker_id, task_queue):
20092012
raise ModelicaSystemError("Missing simulation definition!")
20102013

20112014
resultfile = cmd_definition.cmd_result_path
2012-
resultpath = self._mod._getconn.omcpath(resultfile)
2015+
resultpath = self._mod.get_session().omcpath(resultfile)
20132016

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

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