Skip to content

Commit 7038805

Browse files
committed
[ModelicaSystem] rename _getconn => _session and add get_session()
1 parent 04b973f commit 7038805

4 files changed

Lines changed: 22 additions & 22 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 19 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 using default values or the user defined list
394394
if commandLineOptions is None:
@@ -410,7 +410,7 @@ def __init__(
410410
self._lmodel = lmodel # may be needed if model is derived from other model
411411
self._model_name = modelName # Model class name
412412
if fileName is not None:
413-
file_name = self._getconn.omcpath(fileName).resolve()
413+
file_name = self._session.omcpath(fileName).resolve()
414414
else:
415415
file_name = None
416416
self._file_name: Optional[OMCPath] = file_name # Model file/package name
@@ -444,7 +444,7 @@ def session(self) -> OMCSessionZMQ:
444444
"""
445445
Return the OMC session used for this class.
446446
"""
447-
return self._getconn
447+
return self._session
448448

449449
def setCommandLineOptions(self, commandLineOptions: str):
450450
"""
@@ -487,11 +487,11 @@ def setWorkDirectory(self, customBuildDirectory: Optional[str | os.PathLike] = N
487487
directory. If no directory is defined a unique temporary directory is created.
488488
"""
489489
if customBuildDirectory is not None:
490-
workdir = self._getconn.omcpath(customBuildDirectory).absolute()
490+
workdir = self._session.omcpath(customBuildDirectory).absolute()
491491
if not workdir.is_dir():
492492
raise IOError(f"Provided work directory does not exists: {customBuildDirectory}!")
493493
else:
494-
workdir = self._getconn.omcpath_tempdir().absolute()
494+
workdir = self._session.omcpath_tempdir().absolute()
495495
if not workdir.is_dir():
496496
raise IOError(f"{workdir} could not be created")
497497

@@ -527,24 +527,24 @@ def buildModel(self, variableFilter: Optional[str] = None):
527527

528528
# check if the executable exists ...
529529
om_cmd = ModelicaSystemCmd(
530-
session=self._getconn,
530+
session=self._session,
531531
runpath=self.getWorkDirectory(),
532532
modelname=self._model_name,
533533
timeout=5.0,
534534
)
535535
# ... by running it - output help for command help
536536
om_cmd.arg_set(key="help", val="help")
537537
cmd_definition = om_cmd.definition()
538-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
538+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
539539
if returncode != 0:
540540
raise ModelicaSystemError("Model executable not working!")
541541

542-
xml_file = self._getconn.omcpath(buildModelResult[0]).parent / buildModelResult[1]
542+
xml_file = self._session.omcpath(buildModelResult[0]).parent / buildModelResult[1]
543543
self._xmlparse(xml_file=xml_file)
544544

545545
def sendExpression(self, expr: str, parsed: bool = True) -> Any:
546546
try:
547-
retval = self._getconn.sendExpression(expr, parsed)
547+
retval = self._session.sendExpression(expr, parsed)
548548
except OMCSessionException as ex:
549549
raise ModelicaSystemError(f"Error executing {repr(expr)}") from ex
550550

@@ -1017,7 +1017,7 @@ def simulate_cmd(
10171017
"""
10181018

10191019
om_cmd = ModelicaSystemCmd(
1020-
session=self._getconn,
1020+
session=self._session,
10211021
runpath=self.getWorkDirectory(),
10221022
modelname=self._model_name,
10231023
timeout=timeout,
@@ -1098,7 +1098,7 @@ def simulate(
10981098
elif isinstance(resultfile, OMCPath):
10991099
self._result_file = resultfile
11001100
else:
1101-
self._result_file = self._getconn.omcpath(resultfile)
1101+
self._result_file = self._session.omcpath(resultfile)
11021102
if not self._result_file.is_absolute():
11031103
self._result_file = self.getWorkDirectory() / resultfile
11041104

@@ -1117,7 +1117,7 @@ def simulate(
11171117
self._result_file.unlink()
11181118
# ... run simulation ...
11191119
cmd_definition = om_cmd.definition()
1120-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
1120+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
11211121
# and check returncode *AND* resultfile
11221122
if returncode != 0 and self._result_file.is_file():
11231123
# check for an empty (=> 0B) result file which indicates a crash of the model executable
@@ -1141,12 +1141,12 @@ def plot(
11411141
plot is created by OMC which needs access to the local display. This is not the case for docker and WSL.
11421142
"""
11431143

1144-
if not isinstance(self._getconn.omc_process, OMCProcessLocal):
1144+
if not isinstance(self._session.omc_process, OMCProcessLocal):
11451145
raise ModelicaSystemError("Plot is using the OMC plot functionality; "
11461146
"thus, it is only working if OMC is running locally!")
11471147

11481148
if resultfile is not None:
1149-
plot_result_file = self._getconn.omcpath(resultfile)
1149+
plot_result_file = self._session.omcpath(resultfile)
11501150
elif self._result_file is not None:
11511151
plot_result_file = self._result_file
11521152
else:
@@ -1200,7 +1200,7 @@ def getSolutions(
12001200
raise ModelicaSystemError("No result file found. Run simulate() first.")
12011201
result_file = self._result_file
12021202
else:
1203-
result_file = self._getconn.omcpath(resultfile)
1203+
result_file = self._session.omcpath(resultfile)
12041204

12051205
# check if the result file exits
12061206
if not result_file.is_file():
@@ -1702,7 +1702,7 @@ def linearize(
17021702
)
17031703

17041704
om_cmd = ModelicaSystemCmd(
1705-
session=self._getconn,
1705+
session=self._session,
17061706
runpath=self.getWorkDirectory(),
17071707
modelname=self._model_name,
17081708
timeout=timeout,
@@ -1741,7 +1741,7 @@ def linearize(
17411741
linear_file.unlink(missing_ok=True)
17421742

17431743
cmd_definition = om_cmd.definition()
1744-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
1744+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
17451745
if returncode != 0:
17461746
raise ModelicaSystemError(f"Linearize failed with return code: {returncode}")
17471747
if not linear_file.is_file():
@@ -2097,7 +2097,7 @@ def worker(worker_id, task_queue):
20972097
logger.info(f"[Worker {worker_id}] Performing task: {resultpath.name}")
20982098

20992099
try:
2100-
returncode = self._mod._getconn.run_model_executable(cmd_run_data=cmd_definition)
2100+
returncode = self._mod.get_session().run_model_executable(cmd_run_data=cmd_definition)
21012101
logger.info(f"[Worker {worker_id}] Simulation {resultpath.name} "
21022102
f"finished with return code: {returncode}")
21032103
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.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.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
@@ -51,7 +51,7 @@ def test_optimization_example(tmp_path):
5151
r = mod.optimize()
5252
# it is necessary to specify resultfile, otherwise it wouldn't find it.
5353
resultfile_str = r["resultFile"]
54-
resultfile_omcpath = mod._getconn.omcpath(resultfile_str)
54+
resultfile_omcpath = mod.session().omcpath(resultfile_str)
5555
time, f, v = mod.getSolutions(["time", "f", "v"], resultfile=resultfile_omcpath.as_posix())
5656
assert np.isclose(f[0], 10)
5757
assert np.isclose(f[-1], -10)

0 commit comments

Comments
 (0)