Skip to content

Commit fe98d2b

Browse files
committed
[ModelicaSystem] rename _getconn => _session and add get_session()
1 parent 52f1172 commit fe98d2b

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

OMPython/ModelicaSystem.py

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

387387
if omc_process is not None:
388-
self._getconn = OMCSessionZMQ(omc_process=omc_process)
388+
self._session = OMCSessionZMQ(omc_process=omc_process)
389389
else:
390-
self._getconn = OMCSessionZMQ(omhome=omhome)
390+
self._session = OMCSessionZMQ(omhome=omhome)
391391

392392
# set commandLineOptions using default values or the user defined list
393393
if commandLineOptions is None:
@@ -409,7 +409,7 @@ def __init__(
409409
self._lmodel = lmodel # may be needed if model is derived from other model
410410
self._model_name = modelName # Model class name
411411
if fileName is not None:
412-
file_name = self._getconn.omcpath(fileName).resolve()
412+
file_name = self._session.omcpath(fileName).resolve()
413413
else:
414414
file_name = None
415415
self._file_name: Optional[OMCPath] = file_name # Model file/package name
@@ -439,6 +439,9 @@ def __init__(
439439
if build:
440440
self.buildModel(variableFilter)
441441

442+
def get_session(self) -> OMCSessionZMQ:
443+
return self._session
444+
442445
def setCommandLineOptions(self, commandLineOptions: str):
443446
"""
444447
Set the provided command line option via OMC setCommandLineOptions().
@@ -480,11 +483,11 @@ def setWorkDirectory(self, customBuildDirectory: Optional[str | os.PathLike] = N
480483
directory. If no directory is defined a unique temporary directory is created.
481484
"""
482485
if customBuildDirectory is not None:
483-
workdir = self._getconn.omcpath(customBuildDirectory).absolute()
486+
workdir = self._session.omcpath(customBuildDirectory).absolute()
484487
if not workdir.is_dir():
485488
raise IOError(f"Provided work directory does not exists: {customBuildDirectory}!")
486489
else:
487-
workdir = self._getconn.omcpath_tempdir().absolute()
490+
workdir = self._session.omcpath_tempdir().absolute()
488491
if not workdir.is_dir():
489492
raise IOError(f"{workdir} could not be created")
490493

@@ -520,24 +523,24 @@ def buildModel(self, variableFilter: Optional[str] = None):
520523

521524
# check if the executable exists ...
522525
om_cmd = ModelicaSystemCmd(
523-
session=self._getconn,
526+
session=self._session,
524527
runpath=self.getWorkDirectory(),
525528
modelname=self._model_name,
526529
timeout=5.0,
527530
)
528531
# ... by running it - output help for command help
529532
om_cmd.arg_set(key="help", val="help")
530533
cmd_definition = om_cmd.definition()
531-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
534+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
532535
if returncode != 0:
533536
raise ModelicaSystemError("Model executable not working!")
534537

535-
xml_file = self._getconn.omcpath(buildModelResult[0]).parent / buildModelResult[1]
538+
xml_file = self._session.omcpath(buildModelResult[0]).parent / buildModelResult[1]
536539
self._xmlparse(xml_file=xml_file)
537540

538541
def sendExpression(self, expr: str, parsed: bool = True) -> Any:
539542
try:
540-
retval = self._getconn.sendExpression(expr, parsed)
543+
retval = self._session.sendExpression(expr, parsed)
541544
except OMCSessionException as ex:
542545
raise ModelicaSystemError(f"Error executing {repr(expr)}") from ex
543546

@@ -1012,7 +1015,7 @@ def simulate_cmd(
10121015
"""
10131016

10141017
om_cmd = ModelicaSystemCmd(
1015-
session=self._getconn,
1018+
session=self._session,
10161019
runpath=self.getWorkDirectory(),
10171020
modelname=self._model_name,
10181021
timeout=timeout,
@@ -1092,7 +1095,7 @@ def simulate(
10921095
elif isinstance(resultfile, OMCPath):
10931096
self._result_file = resultfile
10941097
else:
1095-
self._result_file = self._getconn.omcpath(resultfile)
1098+
self._result_file = self._session.omcpath(resultfile)
10961099
if not self._result_file.is_absolute():
10971100
self._result_file = self.getWorkDirectory() / resultfile
10981101

@@ -1111,7 +1114,7 @@ def simulate(
11111114
self._result_file.unlink()
11121115
# ... run simulation ...
11131116
cmd_definition = om_cmd.definition()
1114-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
1117+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
11151118
# and check returncode *AND* resultfile
11161119
if returncode != 0 and self._result_file.is_file():
11171120
# check for an empty (=> 0B) result file which indicates a crash of the model executable
@@ -1166,7 +1169,7 @@ def getSolutions(
11661169
raise ModelicaSystemError("No result file found. Run simulate() first.")
11671170
result_file = self._result_file
11681171
else:
1169-
result_file = self._getconn.omcpath(resultfile)
1172+
result_file = self._session.omcpath(resultfile)
11701173

11711174
# check if the result file exits
11721175
if not result_file.is_file():
@@ -1633,7 +1636,7 @@ def linearize(
16331636
)
16341637

16351638
om_cmd = ModelicaSystemCmd(
1636-
session=self._getconn,
1639+
session=self._session,
16371640
runpath=self.getWorkDirectory(),
16381641
modelname=self._model_name,
16391642
timeout=timeout,
@@ -1673,7 +1676,7 @@ def linearize(
16731676
linear_file.unlink(missing_ok=True)
16741677

16751678
cmd_definition = om_cmd.definition()
1676-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
1679+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
16771680
if returncode != 0:
16781681
raise ModelicaSystemError(f"Linearize failed with return code: {returncode}")
16791682
if not linear_file.is_file():
@@ -1841,9 +1844,9 @@ def __init__(
18411844
self._timeout = timeout
18421845

18431846
if resultpath is None:
1844-
self._resultpath = self._mod._getconn.omcpath_tempdir()
1847+
self._resultpath = self._mod.get_session().omcpath_tempdir()
18451848
else:
1846-
self._resultpath = self._mod._getconn.omcpath(resultpath)
1849+
self._resultpath = self._mod.get_session().omcpath(resultpath)
18471850
if not self._resultpath.is_dir():
18481851
raise ModelicaSystemError("Argument resultpath must be set to a valid path within the environment used "
18491852
f"for the OpenModelica session: {resultpath}!")
@@ -2014,12 +2017,12 @@ def worker(worker_id, task_queue):
20142017
raise ModelicaSystemError("Missing simulation definition!")
20152018

20162019
resultfile = cmd_definition.cmd_result_path
2017-
resultpath = self._mod._getconn.omcpath(resultfile)
2020+
resultpath = self._mod.get_session().omcpath(resultfile)
20182021

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

20212024
try:
2022-
returncode = self._mod._getconn.run_model_executable(cmd_run_data=cmd_definition)
2025+
returncode = self._mod.get_session().run_model_executable(cmd_run_data=cmd_definition)
20232026
logger.info(f"[Worker {worker_id}] Simulation {resultpath.name} "
20242027
f"finished with return code: {returncode}")
20252028
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)