Skip to content

Commit a4628ce

Browse files
authored
[ModelicaSystem] rename getconn => session (#334)
* [ModelicaSystem] rename _getconn => _session and add get_session() * [ModelicaSystemDoE] fix missing usage of _getconn
1 parent 5095494 commit a4628ce

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

OMPython/ModelicaSystem.py

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

395395
if omc_process is not None:
396-
self._getconn = OMCSessionZMQ(omc_process=omc_process)
396+
self._session = OMCSessionZMQ(omc_process=omc_process)
397397
else:
398-
self._getconn = OMCSessionZMQ(omhome=omhome)
398+
self._session = OMCSessionZMQ(omhome=omhome)
399399

400400
# set commandLineOptions using default values or the user defined list
401401
if commandLineOptions is None:
@@ -417,7 +417,7 @@ def __init__(
417417
self._lmodel = lmodel # may be needed if model is derived from other model
418418
self._model_name = modelName # Model class name
419419
if fileName is not None:
420-
file_name = self._getconn.omcpath(fileName).resolve()
420+
file_name = self._session.omcpath(fileName).resolve()
421421
else:
422422
file_name = None
423423
self._file_name: Optional[OMCPath] = file_name # Model file/package name
@@ -451,7 +451,7 @@ def session(self) -> OMCSessionZMQ:
451451
"""
452452
Return the OMC session used for this class.
453453
"""
454-
return self._getconn
454+
return self._session
455455

456456
def setCommandLineOptions(self, commandLineOptions: str):
457457
"""
@@ -494,11 +494,11 @@ def setWorkDirectory(self, customBuildDirectory: Optional[str | os.PathLike] = N
494494
directory. If no directory is defined a unique temporary directory is created.
495495
"""
496496
if customBuildDirectory is not None:
497-
workdir = self._getconn.omcpath(customBuildDirectory).absolute()
497+
workdir = self._session.omcpath(customBuildDirectory).absolute()
498498
if not workdir.is_dir():
499499
raise IOError(f"Provided work directory does not exists: {customBuildDirectory}!")
500500
else:
501-
workdir = self._getconn.omcpath_tempdir().absolute()
501+
workdir = self._session.omcpath_tempdir().absolute()
502502
if not workdir.is_dir():
503503
raise IOError(f"{workdir} could not be created")
504504

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

535535
# check if the executable exists ...
536536
om_cmd = ModelicaSystemCmd(
537-
session=self._getconn,
537+
session=self._session,
538538
runpath=self.getWorkDirectory(),
539539
modelname=self._model_name,
540540
timeout=5.0,
541541
)
542542
# ... by running it - output help for command help
543543
om_cmd.arg_set(key="help", val="help")
544544
cmd_definition = om_cmd.definition()
545-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
545+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
546546
if returncode != 0:
547547
raise ModelicaSystemError("Model executable not working!")
548548

549-
xml_file = self._getconn.omcpath(buildModelResult[0]).parent / buildModelResult[1]
549+
xml_file = self._session.omcpath(buildModelResult[0]).parent / buildModelResult[1]
550550
self._xmlparse(xml_file=xml_file)
551551

552552
def sendExpression(self, expr: str, parsed: bool = True) -> Any:
553553
try:
554-
retval = self._getconn.sendExpression(expr, parsed)
554+
retval = self._session.sendExpression(expr, parsed)
555555
except OMCSessionException as ex:
556556
raise ModelicaSystemError(f"Error executing {repr(expr)}") from ex
557557

@@ -1024,7 +1024,7 @@ def simulate_cmd(
10241024
"""
10251025

10261026
om_cmd = ModelicaSystemCmd(
1027-
session=self._getconn,
1027+
session=self._session,
10281028
runpath=self.getWorkDirectory(),
10291029
modelname=self._model_name,
10301030
timeout=timeout,
@@ -1105,7 +1105,7 @@ def simulate(
11051105
elif isinstance(resultfile, OMCPath):
11061106
self._result_file = resultfile
11071107
else:
1108-
self._result_file = self._getconn.omcpath(resultfile)
1108+
self._result_file = self._session.omcpath(resultfile)
11091109
if not self._result_file.is_absolute():
11101110
self._result_file = self.getWorkDirectory() / resultfile
11111111

@@ -1124,7 +1124,7 @@ def simulate(
11241124
self._result_file.unlink()
11251125
# ... run simulation ...
11261126
cmd_definition = om_cmd.definition()
1127-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
1127+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
11281128
# and check returncode *AND* resultfile
11291129
if returncode != 0 and self._result_file.is_file():
11301130
# check for an empty (=> 0B) result file which indicates a crash of the model executable
@@ -1148,12 +1148,12 @@ def plot(
11481148
plot is created by OMC which needs access to the local display. This is not the case for docker and WSL.
11491149
"""
11501150

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

11551155
if resultfile is not None:
1156-
plot_result_file = self._getconn.omcpath(resultfile)
1156+
plot_result_file = self._session.omcpath(resultfile)
11571157
elif self._result_file is not None:
11581158
plot_result_file = self._result_file
11591159
else:
@@ -1207,7 +1207,7 @@ def getSolutions(
12071207
raise ModelicaSystemError("No result file found. Run simulate() first.")
12081208
result_file = self._result_file
12091209
else:
1210-
result_file = self._getconn.omcpath(resultfile)
1210+
result_file = self._session.omcpath(resultfile)
12111211

12121212
# check if the result file exits
12131213
if not result_file.is_file():
@@ -1709,7 +1709,7 @@ def linearize(
17091709
)
17101710

17111711
om_cmd = ModelicaSystemCmd(
1712-
session=self._getconn,
1712+
session=self._session,
17131713
runpath=self.getWorkDirectory(),
17141714
modelname=self._model_name,
17151715
timeout=timeout,
@@ -1748,7 +1748,7 @@ def linearize(
17481748
linear_file.unlink(missing_ok=True)
17491749

17501750
cmd_definition = om_cmd.definition()
1751-
returncode = self._getconn.run_model_executable(cmd_run_data=cmd_definition)
1751+
returncode = self._session.run_model_executable(cmd_run_data=cmd_definition)
17521752
if returncode != 0:
17531753
raise ModelicaSystemError(f"Linearize failed with return code: {returncode}")
17541754
if not linear_file.is_file():
@@ -2104,7 +2104,7 @@ def worker(worker_id, task_queue):
21042104
logger.info(f"[Worker {worker_id}] Performing task: {resultpath.name}")
21052105

21062106
try:
2107-
returncode = self._mod._getconn.run_model_executable(cmd_run_data=cmd_definition)
2107+
returncode = self.session().run_model_executable(cmd_run_data=cmd_definition)
21082108
logger.info(f"[Worker {worker_id}] Simulation {resultpath.name} "
21092109
f"finished with return code: {returncode}")
21102110
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)