Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
8d19d26
[ModelicaSystem] add type hints for set*() functions and rename argum…
syntron Jun 23, 2025
9ac6a0e
[ModelicaSystem] add _prepare_inputdata()
syntron Jun 23, 2025
980ac33
[ModelicaSystem] update _set_method_helper()
syntron Jun 23, 2025
7c24f10
[ModelicaSystem] improve definition of _prepare_inputdata()
syntron Jun 23, 2025
b864a6c
[ModelicaSystem] rename _prepare_inputdata() => _prepare_input_data()
syntron Jun 23, 2025
3f180a6
[ModelicaSystem] update setInput()
syntron Jun 23, 2025
55051d9
update tests - use new dict based input for set*() methods
syntron Jun 23, 2025
3d8b83a
[ModelicaSystem] add type hint for return value of isParameterChangea…
syntron Jun 23, 2025
1b5917c
[ModelicaSystem] fix type hint for _prepare_input_data() - use dict[s…
syntron Jun 23, 2025
b5766f4
[ModelicaSystem] setInput() - handly input data as list of tuples
syntron Jun 23, 2025
9ef9d69
update tests - use new dict based input for setInput() method
syntron Jun 23, 2025
2fc67b9
[test_linearization] fix setInput() call
syntron Jun 24, 2025
bbd8740
[ModelicaSystem] simplify _set_method_helper()
syntron Jun 26, 2025
675a648
[ModelicaSystem] improve setInputs() - reduce spaces / cleanup
syntron Jun 26, 2025
9fcd39a
[ModelicaSystem] fix rebase fallout
syntron Jul 7, 2025
92651c5
[ModelicaSystem] fix rebase fallout 2
syntron Jul 7, 2025
d695899
[ModelicaSystem] remove _has_inputs - is defined by _inputs empty or not
syntron Jul 7, 2025
4fa05f1
[test_ModelicaSystem] cleanup
syntron Jul 7, 2025
30cb70e
[ModelicaSystem] simplify handling of inputs
syntron Jul 11, 2025
0a89151
[ModelicaSystem._set_method_helper] fail if parameter is *NOT* change…
syntron Jul 15, 2025
9a92c19
[ModelicaSystem] rename overwritedata => overridedata
syntron Aug 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[ModelicaSystem] update _set_method_helper()
* rename from setMethodHelper()
* use _prepare_inputdata()
* cleanup code to align with new input as dict[str, str]

* setInput() is a special case
  • Loading branch information
syntron committed Jul 11, 2025
commit 980ac33021dfdce48426ed5c203ea6b0c639d12a
157 changes: 79 additions & 78 deletions OMPython/ModelicaSystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1117,17 +1117,7 @@ def getSolutions(self, varList: Optional[str | list[str]] = None, resultfile: Op
return np_res

@staticmethod
def _strip_space(name):
if isinstance(name, str):
return name.replace(" ", "")

if isinstance(name, list):
return [x.replace(" ", "") for x in name]

raise ModelicaSystemError("Unhandled input for strip_space()")

def _prepare_inputdata(
self,
rawinput: str | list[str] | dict[str, str | int | float],
) -> dict[str, str]:
"""
Expand Down Expand Up @@ -1166,72 +1156,65 @@ def prepare_str(str_in: str) -> dict[str, str]:
return inputdata

if isinstance(rawinput, dict):
inputdata = {key: str(val) for key, val in rawinput.items()}
for key, val in rawinput.items():
str_val = str(val)
if ' ' in key or ' ' in str_val:
raise ModelicaSystemError(f"Spaces not allowed in key/value pairs: {repr(key)} = {repr(val)}!")
inputdata[key] = str_val

return inputdata

def setMethodHelper(
def _set_method_helper(
self,
inputdata: str | list[str] | dict[str, str | int | float],
inputdata: dict[str, str],
classdata: dict[str, Any],
datatype: str,
overwritedata: Optional[dict[str, str]] = None,
) -> bool:
"""
Helper function for setters.
Helper function for:
* setParameter()
* setContinuous()
* setSimulationOptions()
* setLinearizationOption()
* setOptimizationOption()
* setInputs()

args1 - string or list of string given by user
args2 - dict() containing the values of different variables(eg:, parameter,continuous,simulation parameters)
args3 - function name (eg; continuous, parameter, simulation, linearization,optimization)
args4 - dict() which stores the new override variables list,
Parameters
----------
inputdata
string or list of string given by user
classdata
dict() containing the values of different variables (eg: parameter, continuous, simulation parameters)
datatype
type identifier (eg; continuous, parameter, simulation, linearization, optimization)
overwritedata
dict() which stores the new override variables list,
"""

# TODO: cleanup / data handling / ...
# (1) args1: Optional[str | list[str]] -> convert to dict
# (2) work on dict! inputs: dict[str, str | int | float | ?numbers.number?]
# (3) handle function
# (4) use it also for other functions with such an input, i.e. 'key=value' | ['key=value']
# (5) include setInputs()

def apply_single(key_val: str):
key_val_list = key_val.split("=")
if len(key_val_list) != 2:
raise ModelicaSystemError(f"Invalid key = value pair: {key_val}")

name = self._strip_space(key_val_list[0])
value = self._strip_space(key_val_list[1])

if name in classdata:
if datatype == "parameter" and not self.isParameterChangeable(name):
logger.debug(f"It is not possible to set the parameter {repr(name)}. It seems to be "
inputdata_status: dict[str, bool] = {}
for key, val in inputdata.items():
status = False
if key in classdata:
if datatype == "parameter" and not self.isParameterChangeable(key):
logger.debug(f"It is not possible to set the parameter {repr(key)}. It seems to be "
"structural, final, protected, evaluated or has a non-constant binding. "
"Use sendExpression(...) and rebuild the model using buildModel() API; example: "
"sendExpression(\"setParameterValue("
f"{self.modelName}, {name}, {value if value is not None else '<?value?>'}"
f"{self.modelName}, {key}, {val if val is not None else '<?value?>'}"
")\") ")
return False

classdata[name] = value
if overwritedata is not None:
overwritedata[name] = value

return True

else:
classdata[key] = val
if overwritedata is not None:
overwritedata[key] = val
status = True
else:
raise ModelicaSystemError("Unhandled case in setMethodHelper.apply_single() - "
f"{repr(name)} is not a {repr(datatype)} variable")

result = []
if isinstance(inputdata, str):
result = [apply_single(inputdata)]
f"{repr(key)} is not a {repr(datatype)} variable")

elif isinstance(inputdata, list):
result = []
inputdata = self._strip_space(inputdata)
for var in inputdata:
result.append(apply_single(var))
inputdata_status[key] = status

return all(result)
return all(inputdata_status.values())

def isParameterChangeable(
self,
Expand All @@ -1250,11 +1233,14 @@ def setContinuous(
This method is used to set continuous values. It can be called:
with a sequence of continuous name and assigning corresponding values as arguments as show in the example below:
usage
>>> setContinuous("Name=value")
>>> setContinuous(["Name1=value1","Name2=value2"])
>>> setContinuous("Name=value") # depreciated
>>> setContinuous(["Name1=value1","Name2=value2"]) # depreciated
>>> setContinuous(cvals={"Name1": "value1", "Name2": "value2"})
"""
return self._setMethodHelper(
inputdata=cvals,
inputdata = self._prepare_inputdata(rawinput=cvals)

return self._set_method_helper(
inputdata=inputdata,
classdata=self.continuouslist,
datatype="continuous",
overwritedata=self.overridevariables)
Expand All @@ -1267,11 +1253,14 @@ def setParameters(
This method is used to set parameter values. It can be called:
with a sequence of parameter name and assigning corresponding value as arguments as show in the example below:
usage
>>> setParameters("Name=value")
>>> setParameters(["Name1=value1","Name2=value2"])
>>> setParameters("Name=value") # depreciated
>>> setParameters(["Name1=value1","Name2=value2"]) # depreciated
>>> setParameters(pvals={"Name1": "value1", "Name2": "value2"})
"""
return self._setMethodHelper(
inputdata=pvals,
inputdata = self._prepare_inputdata(rawinput=pvals)

return self._set_method_helper(
inputdata=inputdata,
classdata=self.paramlist,
datatype="parameter",
overwritedata=self.overridevariables)
Expand All @@ -1284,11 +1273,14 @@ def setSimulationOptions(
This method is used to set simulation options. It can be called:
with a sequence of simulation options name and assigning corresponding values as arguments as show in the example below:
usage
>>> setSimulationOptions("Name=value")
>>> setSimulationOptions(["Name1=value1","Name2=value2"])
>>> setSimulationOptions("Name=value") # depreciated
>>> setSimulationOptions(["Name1=value1","Name2=value2"]) # depreciated
>>> setSimulationOptions(simOptions={"Name1": "value1", "Name2": "value2"})
"""
return _self.setMethodHelper(
inputdata=simOptions,
inputdata = self._prepare_inputdata(rawinput=simOptions)

return self._set_method_helper(
inputdata=inputdata,
classdata=self.simulateOptions,
datatype="simulation-option",
overwritedata=self.simoptionsoverride)
Expand All @@ -1301,11 +1293,14 @@ def setLinearizationOptions(
This method is used to set linearization options. It can be called:
with a sequence of linearization options name and assigning corresponding value as arguments as show in the example below
usage
>>> setLinearizationOptions("Name=value")
>>> setLinearizationOptions(["Name1=value1","Name2=value2"])
>>> setLinearizationOptions("Name=value") # depreciated
>>> setLinearizationOptions(["Name1=value1","Name2=value2"]) # depreciated
>>> setLinearizationOptions(linearizationOtions={"Name1": "value1", "Name2": "value2"})
"""
return self._setMethodHelper(
inputdata=linearizationOptions,
inputdata = self._prepare_inputdata(rawinput=linearizationOptions)

return self._set_method_helper(
inputdata=inputdata,
classdata=self.linearOptions,
datatype="Linearization-option",
overwritedata=None)
Expand All @@ -1315,11 +1310,14 @@ def setOptimizationOptions(self, optimizationOptions: str | list[str] | dict[str
This method is used to set optimization options. It can be called:
with a sequence of optimization options name and assigning corresponding values as arguments as show in the example below:
usage
>>> setOptimizationOptions("Name=value")
>>> setOptimizationOptions(["Name1=value1","Name2=value2"])
>>> setOptimizationOptions("Name=value") # depreciated
>>> setOptimizationOptions(["Name1=value1","Name2=value2"]) # depreciated
>>> setOptimizationOptions(optimizationOptions={"Name1": "value1", "Name2": "value2"})
"""
return self._setMethodHelper(
inputdata=optimizationOptions,
inputdata = self._prepare_inputdata(rawinput=optimizationOptions)

return self._set_method_helper(
inputdata=inputdata,
classdata=self.optimizeOptions,
datatype="optimization-option",
overwritedata=None)
Expand All @@ -1332,9 +1330,12 @@ def setInputs(
This method is used to set input values. It can be called:
with a sequence of input name and assigning corresponding values as arguments as show in the example below:
usage
>>> setInputs("Name=value")
>>> setInputs(["Name1=value1","Name2=value2"])
>>> setInputs("Name=value") # depreciated
>>> setInputs(["Name1=value1","Name2=value2"]) # depreciated
>>> setInputs(name={"Name1": "value1", "Name2": "value2"})
"""
# inputdata = self._prepare_inputdata(rawinput=name)

if isinstance(name, str):
name1: str = name
name1 = name1.replace(" ", "")
Expand Down