Skip to content

Commit

Permalink
model.base: fix Referable.__repr__() for SubmodelElementList-chil…
Browse files Browse the repository at this point in the history
…dren

Since AASd-120 prohibits specifying id_shorts of direct children of
`SubmodelElementLists`, this commit adjusts `Referable.__repr__()` such
that the index of the element in the corresponding list is returned
instead.
Furthermore, the tests are adjusted accordingly.
  • Loading branch information
jkhsjdhjs authored and s-heppner committed Oct 3, 2023
1 parent e8e664d commit 598f05b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
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
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
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

0 comments on commit 598f05b

Please sign in to comment.