Skip to content

Commit 883840c

Browse files
committed
[ModelicaSystem] rename _getconn => _session and add get_session()
1 parent 858b873 commit 883840c

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 using default values or the user defined list
392392
if commandLineOptions is None:
@@ -408,7 +408,7 @@ def __init__(
408408
self._lmodel = lmodel # may be needed if model is derived from other model
409409
self._model_name = modelName # Model class name
410410
if fileName is not None:
411-
file_name = self._getconn.omcpath(fileName).resolve()
411+
file_name = self._session.omcpath(fileName).resolve()
412412
else:
413413
file_name = None
414414
self._file_name: Optional[OMCPath] = file_name # Model file/package name
@@ -438,6 +438,9 @@ def __init__(
438438
if build:
439439
self.buildModel(variableFilter)
440440

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

@@ -519,24 +522,24 @@ def buildModel(self, variableFilter: Optional[str] = None):
519522

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

534-
xml_file = self._getconn.omcpath(buildModelResult[0]).parent / buildModelResult[1]
537+
xml_file = self._session.omcpath(buildModelResult[0]).parent / buildModelResult[1]
535538
self._xmlparse(xml_file=xml_file)
536539

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

@@ -1011,7 +1014,7 @@ def simulate_cmd(
10111014
"""
10121015

10131016
om_cmd = ModelicaSystemCmd(
1014-
session=self._getconn,
1017+
session=self._session,
10151018
runpath=self.getWorkDirectory(),
10161019
modelname=self._model_name,
10171020
timeout=timeout,
@@ -1091,7 +1094,7 @@ def simulate(
10911094
elif isinstance(resultfile, OMCPath):
10921095
self._result_file = resultfile
10931096
else:
1094-
self._result_file = self._getconn.omcpath(resultfile)
1097+
self._result_file = self._session.omcpath(resultfile)
10951098
if not self._result_file.is_absolute():
10961099
self._result_file = self.getWorkDirectory() / resultfile
10971100

@@ -1110,7 +1113,7 @@ def simulate(
11101113
self._result_file.unlink()
11111114
# ... run simulation ...
11121115
cmd_definition = om_cmd.definition()
1113-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
1116+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
11141117
# and check returncode *AND* resultfile
11151118
if returncode != 0 and self._result_file.is_file():
11161119
# check for an empty (=> 0B) result file which indicates a crash of the model executable
@@ -1165,7 +1168,7 @@ def getSolutions(
11651168
raise ModelicaSystemError("No result file found. Run simulate() first.")
11661169
result_file = self._result_file
11671170
else:
1168-
result_file = self._getconn.omcpath(resultfile)
1171+
result_file = self._session.omcpath(resultfile)
11691172

11701173
# check if the result file exits
11711174
if not result_file.is_file():
@@ -1632,7 +1635,7 @@ def linearize(
16321635
)
16331636

16341637
om_cmd = ModelicaSystemCmd(
1635-
session=self._getconn,
1638+
session=self._session,
16361639
runpath=self.getWorkDirectory(),
16371640
modelname=self._model_name,
16381641
timeout=timeout,
@@ -1672,7 +1675,7 @@ def linearize(
16721675
linear_file.unlink(missing_ok=True)
16731676

16741677
cmd_definition = om_cmd.definition()
1675-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
1678+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
16761679
if returncode != 0:
16771680
raise ModelicaSystemError(f"Linearize failed with return code: {returncode}")
16781681
if not linear_file.is_file():
@@ -1840,9 +1843,9 @@ def __init__(
18401843
self._timeout = timeout
18411844

18421845
if resultpath is None:
1843-
self._resultpath = self._mod._getconn.omcpath_tempdir()
1846+
self._resultpath = self._mod.get_session().omcpath_tempdir()
18441847
else:
1845-
self._resultpath = self._mod._getconn.omcpath(resultpath)
1848+
self._resultpath = self._mod.get_session().omcpath(resultpath)
18461849
if not self._resultpath.is_dir():
18471850
raise ModelicaSystemError("Argument resultpath must be set to a valid path within the environment used "
18481851
f"for the OpenModelica session: {resultpath}!")
@@ -2013,12 +2016,12 @@ def worker(worker_id, task_queue):
20132016
raise ModelicaSystemError("Missing simulation definition!")
20142017

20152018
resultfile = cmd_definition.cmd_result_path
2016-
resultpath = self._mod._getconn.omcpath(resultfile)
2019+
resultpath = self._mod.get_session().omcpath(resultfile)
20172020

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

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