Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model.base: fix Referable.__repr__() for SubmodelElementList-children #129

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
9 changes: 7 additions & 2 deletions basyx/aas/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,12 +610,17 @@ def __init__(self):
def __repr__(self) -> str:
reversed_path = []
item = self # type: Any
from .submodel import SubmodelElementList
while item is not None:
if isinstance(item, Identifiable):
reversed_path.append(str(item.id))
reversed_path.append(item.id)
break
elif isinstance(item, Referable):
reversed_path.append(item.id_short)
if isinstance(item.parent, SubmodelElementList):
reversed_path.append(f"{item.parent.id_short}[{item.parent.value.index(item)}]")
item = item.parent
s-heppner marked this conversation as resolved.
Show resolved Hide resolved
else:
reversed_path.append(item.id_short)
item = item.parent
else:
raise AttributeError('Referable must have an identifiable as root object and only parents that are '
Expand Down
13 changes: 12 additions & 1 deletion basyx/aas/model/submodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,8 +715,19 @@ def __init__(self,

# Items must be added after the above contraint has been checked. Otherwise, it can lead to errors, since the
# constraints in _check_constraints() assume that this constraint has been checked.
self._value: base.OrderedNamespaceSet[_SE] = base.OrderedNamespaceSet(self, [("id_short", True)], value,
self._value: base.OrderedNamespaceSet[_SE] = base.OrderedNamespaceSet(self, [("id_short", True)], (),
self._check_constraints)
# SubmodelElements need to be added after the assignment of the ordered NamespaceSet, otherwise, if a constraint
# check fails, Referable.__repr__ may be called for an already-contained item during the AASd-114 check, which
# in turn tries to access the SubmodelElementLists value / _value attribute, which wouldn't be set yet if all
# elements are passed to the OrderedNamespaceSet initializer.
try:
for i in value:
self._value.add(i)
except Exception:
# Remove all SubmodelElements if an exception occurs during initialization of the SubmodelElementList
self._value.clear()
raise

def _check_constraints(self, new: _SE, existing: Iterable[_SE]) -> None:
# We can't use isinstance(new, self.type_value_list_element) here, because each subclass of
Expand Down
8 changes: 4 additions & 4 deletions test/examples/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ def test_submodel_element_list_checker(self):
checker.check_submodel_element_list_equal(list_, list_expected)
self.assertEqual(4, sum(1 for _ in checker.failed_checks))
checker_iterator = checker.failed_checks
self.assertEqual("FAIL: Attribute id_short of Range[test_list / range1] must be == range2 (value='range1')",
self.assertEqual("FAIL: Attribute id_short of Range[test_list[0]] must be == range2 (value='range1')",
repr(next(checker_iterator)))
self.assertEqual("FAIL: Attribute max of Range[test_list / range1] must be == 1337 (value=142857)",
self.assertEqual("FAIL: Attribute max of Range[test_list[0]] must be == 1337 (value=142857)",
repr(next(checker_iterator)))
self.assertEqual("FAIL: Attribute id_short of Range[test_list / range2] must be == range1 (value='range2')",
self.assertEqual("FAIL: Attribute id_short of Range[test_list[1]] must be == range1 (value='range2')",
repr(next(checker_iterator)))
self.assertEqual("FAIL: Attribute max of Range[test_list / range2] must be == 142857 (value=1337)",
self.assertEqual("FAIL: Attribute max of Range[test_list[1]] must be == 142857 (value=1337)",
repr(next(checker_iterator)))

# order_relevant
Expand Down
2 changes: 1 addition & 1 deletion test/model/test_submodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_constraints(self):
model.SubmodelElementList("test_list", model.MultiLanguageProperty, [mlp1, mlp2])
self.assertEqual("Element to be added MultiLanguageProperty[mlp2] has semantic_id "
"ExternalReference(key=(Key(type=GLOBAL_REFERENCE, value=urn:x-test:different),)), "
"while already contained element MultiLanguageProperty[test_list / mlp1] has semantic_id "
"while already contained element MultiLanguageProperty[test_list[0]] has semantic_id "
"ExternalReference(key=(Key(type=GLOBAL_REFERENCE, value=urn:x-test:test),)), "
"which aren't equal. (Constraint AASd-114)", str(cm.exception))
mlp2.semantic_id = semantic_id1
Expand Down
Loading