Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/pyfmi/fmi2.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1770,7 +1770,7 @@ cdef class FMUModelBase2(FMI_BASE.ModelBase):
cdef int selected_type = 0 #If a type has been selected
cdef int selected_variability = 0 #If a variability has been selected
cdef int selected_causality = 0 #If a causality has been selected
cdef int has_start, is_fixed
cdef int has_start
cdef int i, j
cdef int selected_filter = 1 if filter else 0
cdef int length_filter = 0
Expand Down
49 changes: 26 additions & 23 deletions src/pyfmi/fmi3.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1902,7 +1902,6 @@ cdef class FMUModelBase3(FMI_BASE.ModelBase):
Default: None (i.e all).

include_alias --
Currently not supported (does nothing).
If alias should be included or not.
Default: True

Expand Down Expand Up @@ -1942,6 +1941,15 @@ cdef class FMUModelBase3(FMI_BASE.ModelBase):
filter = filter
)

def _variable_name_matches_filter(self, variable_name: str, filter_list: Union[list, None]) -> bool:
"""Checks if a given variable_name matches any pattern in the filter_list."""
if filter_list is None:
return True
for pattern in filter_list:
if pattern.match(variable_name):
return True
return False

@enable_caching
def _get_model_variables(self,
type,
Expand All @@ -1965,18 +1973,16 @@ cdef class FMUModelBase3(FMI_BASE.ModelBase):
cdef FMIL3.fmi3_import_alias_variable_t* alias_var

cdef int has_start = 0
# TODO: Can we rename the keyword arg 'filter' to variable_filter?
variable_filter = filter
# TODO: Can we rename the keyword arg 'type' to variable_type?
variable_type = type
cdef list filter_list, variable_return_list = []
cdef list filter_list = None
variable_dict = {}

variable_list = FMIL3.fmi3_import_get_variable_list(self._fmu, 0)
variable_list_size = FMIL3.fmi3_import_get_variable_list_size(variable_list)

if variable_filter:
filter_list = self._convert_filter(variable_filter)
if filter:
filter_list = self._convert_filter(filter)

user_specified_type = isinstance(variable_type, int)
user_specified_variability = isinstance(variability, int)
Expand Down Expand Up @@ -2013,30 +2019,27 @@ cdef class FMUModelBase3(FMI_BASE.ModelBase):
if user_specified_causality and (data_causality != causality):
continue

if variable_filter:
for pattern in filter_list:
if pattern.match(name):
break
else:
continue

variable_dict[name] = FMI3ModelVariable(
name,
value_ref,
data_type,
description,
data_variability,
data_causality,
False, # alias
data_initial
)
if self._variable_name_matches_filter(name, filter_list):
variable_dict[name] = FMI3ModelVariable(
name,
value_ref,
data_type,
description,
data_variability,
data_causality,
False, # alias
data_initial
)

if include_alias and FMIL3.fmi3_import_get_variable_has_alias(variable):
alias_list = FMIL3.fmi3_import_get_variable_alias_list(variable)
alias_list_size = FMIL3.fmi3_import_get_alias_variable_list_size(alias_list)
for idx in range(alias_list_size):
alias_var = FMIL3.fmi3_import_get_alias(alias_list, idx)
alias_name = pyfmi_util.decode(FMIL3.fmi3_import_get_alias_variable_name(alias_var))

if not self._variable_name_matches_filter(alias_name, filter_list):
continue
alias_descr = self._get_alias_description(alias_var)

variable_dict[alias_name] = FMI3ModelVariable(
Expand Down
9 changes: 9 additions & 0 deletions tests/test_fmi3.py
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,15 @@ def test_get_description_of_alias(self):
assert self.fmu.get_variable_description("v5_a1") == ""
assert self.fmu.get_variable_description("v5_a2") == "v5_a2_desc"

def test_get_model_variables_filter_including_alias(self):
variables = self.fmu.get_model_variables(filter="v5_*", include_alias = True)
assert set(variables) == set(["v5_a1", "v5_a2"])

def test_get_model_variables_filter_excluding_alias(self):
variables = self.fmu.get_model_variables(filter="v5_*", include_alias = False)
assert set(variables) == set()


class Test_FMUModelBase3:
def test_declared_enumeration_type(self):
fmu = _get_fmu(FMI3_REF_FMU_PATH / "Feedthrough.fmu", _connect_dll = False)
Expand Down