Skip to content

Commit 591931f

Browse files
committed
Fix: Types for nested attributes
1 parent 3eeb50c commit 591931f

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

flamapy/metamodels/fm_metamodel/models/feature_model.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class AttributeType(Enum):
104104
INTEGER = 'Integer'
105105
REAL = 'Real'
106106
STRING = 'String'
107+
VECTOR = 'Vector' # Vector (list of values)
107108
NESTED = 'Nested' # Nested attribute (i.e., list of attributes)
108109

109110

@@ -410,10 +411,15 @@ def get_features(self) -> list["Feature"]:
410411
return features
411412

412413
def get_attributes(self) -> list["Attribute"]:
413-
attributes: set["Attribute"] = set()
414+
attributes_dict: dict[str, "Attribute"] = {}
414415
for feature in self.get_features():
415-
attributes.update(feature.get_attributes())
416-
return list(attributes)
416+
attributes_dict.update({attr.name: attr for attr in feature.get_attributes()})
417+
attributes = []
418+
for attr in attributes_dict.values():
419+
attribute = Attribute(attr.name, attr.domain, None, attr.null_value)
420+
attribute.attribute_type = attr.attribute_type
421+
attributes.append(attribute)
422+
return attributes
417423

418424
def get_attribute_by_name(self, attribute_name: str) -> Optional["Attribute"]:
419425
return next((a for a in self.get_attributes() if a.name == attribute_name), None)
@@ -598,9 +604,9 @@ def _infer_attribute_type(self, value: Any) -> Optional["AttributeType"]:
598604
elif isinstance(value, str):
599605
attr_type = AttributeType.STRING
600606
elif isinstance(value, list):
601-
attr_type = AttributeType.NESTED
607+
attr_type = AttributeType.VECTOR
602608
else:
603-
attr_type = None
609+
attr_type = AttributeType.NESTED
604610
return attr_type
605611

606612
def get_name(self) -> str:

flamapy/metamodels/fm_metamodel/transformations/uvl_reader.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,27 +183,34 @@ def _check_attributes(
183183
feature.is_abstract = True
184184
else:
185185
# Handle attributes
186-
if isinstance(value, dict): # it represents nested attributes
187-
attributes_list = self._process_nested_attribute(feature, value)
186+
if value is None: # for boolean values the value may be not provided
187+
default_value = True
188+
elif isinstance(value, dict): # it represents nested attributes
189+
attributes_list = self._process_nested_attribute(feature, key, value)
188190
for attr in attributes_list:
189191
feature.add_attribute(attr)
192+
default_value = None
190193
else:
191194
default_value = value
192195
feature.add_attribute(Attribute(name=str(key), default_value=default_value))
193196
feature.constraints_attributes = self.constraints_attributes[feature]
194197

195198
def _process_nested_attribute(self,
196199
parent: Feature,
200+
parent_attribute_name: str,
197201
nested_values: dict[Any, Any]) -> list[Attribute]:
198202
attributes = []
199203
for key, value in nested_values.items():
200-
if isinstance(value, dict):
201-
attributes_list = self._process_nested_attribute(parent, value)
204+
if value is None: # for boolean values the value may be not provided
205+
default_value = True
206+
elif isinstance(value, dict):
207+
attributes_list = self._process_nested_attribute(parent, f'{parent_attribute_name}.{key}', value)
202208
for attr in attributes_list:
203209
attributes.append(attr)
210+
default_value = None
204211
else:
205212
default_value = value
206-
attribute = Attribute(name=str(key), default_value=default_value)
213+
attribute = Attribute(name=f'{parent_attribute_name}.{key}', default_value=default_value)
207214
attribute.parent = parent
208215
attributes.append(attribute)
209216
return attributes

0 commit comments

Comments
 (0)