From cabc19344dcc44b53bd1e8ffd5c2b9732e097cdd Mon Sep 17 00:00:00 2001 From: zrgt Date: Tue, 7 Nov 2023 18:56:02 +0100 Subject: [PATCH] Refactor `Entity` and `AssetInformation` - Bundle check methods together at the end of classes - Extract validation of `global_asset_id` to `_validate_asset_ids()` - Rename `Entity._validate_asset_ids()` to `_validate_aasd_014` and `AssetInformation._validate_asset_ids()` to `_validate_aasd_131`, as the methods only validate these constraints and not all asset ids --- basyx/aas/model/aas.py | 33 ++++++++++++++---------- basyx/aas/model/submodel.py | 50 ++++++++++++++++++++----------------- 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/basyx/aas/model/aas.py b/basyx/aas/model/aas.py index aa7b63f05..9edf891ea 100644 --- a/basyx/aas/model/aas.py +++ b/basyx/aas/model/aas.py @@ -69,17 +69,8 @@ def __init__(self, item_del_hook=self._check_constraint_del_spec_asset_id ) self._global_asset_id: Optional[base.Identifier] = global_asset_id - self._validate_asset_ids(global_asset_id, bool(specific_asset_id)) - - def _check_constraint_set_spec_asset_id(self, items_to_replace: List[base.SpecificAssetId], - new_items: List[base.SpecificAssetId], - old_list: List[base.SpecificAssetId]) -> None: - self._validate_asset_ids(self.global_asset_id, - len(old_list) - len(items_to_replace) + len(new_items) > 0) - - def _check_constraint_del_spec_asset_id(self, _item_to_del: base.SpecificAssetId, - old_list: List[base.SpecificAssetId]) -> None: - self._validate_asset_ids(self.global_asset_id, len(old_list) > 1) + self._validate_global_asset_id(global_asset_id) + self._validate_aasd_131(global_asset_id, bool(specific_asset_id)) @property def global_asset_id(self) -> Optional[base.Identifier]: @@ -87,7 +78,8 @@ def global_asset_id(self) -> Optional[base.Identifier]: @global_asset_id.setter def global_asset_id(self, global_asset_id: Optional[base.Identifier]) -> None: - self._validate_asset_ids(global_asset_id, bool(self.specific_asset_id)) + self._validate_global_asset_id(global_asset_id) + self._validate_aasd_131(global_asset_id, bool(self.specific_asset_id)) self._global_asset_id = global_asset_id @property @@ -99,8 +91,23 @@ def specific_asset_id(self, specific_asset_id: Iterable[base.SpecificAssetId]) - # constraints are checked via _check_constraint_set_spec_asset_id() in this case self._specific_asset_id[:] = specific_asset_id + def _check_constraint_set_spec_asset_id(self, items_to_replace: List[base.SpecificAssetId], + new_items: List[base.SpecificAssetId], + old_list: List[base.SpecificAssetId]) -> None: + self._validate_aasd_131(self.global_asset_id, + len(old_list) - len(items_to_replace) + len(new_items) > 0) + + def _check_constraint_del_spec_asset_id(self, _item_to_del: base.SpecificAssetId, + old_list: List[base.SpecificAssetId]) -> None: + self._validate_aasd_131(self.global_asset_id, len(old_list) > 1) + + @staticmethod + def _validate_global_asset_id(global_asset_id: Optional[base.Identifier]) -> None: + if global_asset_id is not None: + _string_constraints.check_identifier(global_asset_id) + @staticmethod - def _validate_asset_ids(global_asset_id: Optional[base.Identifier], specific_asset_id_nonempty: bool) -> None: + def _validate_aasd_131(global_asset_id: Optional[base.Identifier], specific_asset_id_nonempty: bool) -> None: if global_asset_id is None and not specific_asset_id_nonempty: raise base.AASConstraintViolation(131, "An AssetInformation has to have a globalAssetId or a specificAssetId") diff --git a/basyx/aas/model/submodel.py b/basyx/aas/model/submodel.py index 36fdce48f..af9c074fe 100644 --- a/basyx/aas/model/submodel.py +++ b/basyx/aas/model/submodel.py @@ -1115,21 +1115,8 @@ def __init__(self, item_set_hook=self._check_constraint_set_spec_asset_id, item_del_hook=self._check_constraint_del_spec_asset_id ) - self._validate_asset_ids(entity_type, global_asset_id, bool(specific_asset_id)) - - def _check_constraint_add_spec_asset_id(self, _new_item: base.SpecificAssetId, - _old_list: List[base.SpecificAssetId]) -> None: - self._validate_asset_ids(self.entity_type, self.global_asset_id, True) - - def _check_constraint_set_spec_asset_id(self, items_to_replace: List[base.SpecificAssetId], - new_items: List[base.SpecificAssetId], - old_list: List[base.SpecificAssetId]) -> None: - self._validate_asset_ids(self.entity_type, self.global_asset_id, - len(old_list) - len(items_to_replace) + len(new_items) > 0) - - def _check_constraint_del_spec_asset_id(self, _item_to_del: base.SpecificAssetId, - old_list: List[base.SpecificAssetId]) -> None: - self._validate_asset_ids(self.entity_type, self.global_asset_id, len(old_list) > 1) + self._validate_global_asset_id(global_asset_id) + self._validate_aasd_014(entity_type, global_asset_id, bool(specific_asset_id)) @property def entity_type(self) -> base.EntityType: @@ -1137,7 +1124,7 @@ def entity_type(self) -> base.EntityType: @entity_type.setter def entity_type(self, entity_type: base.EntityType) -> None: - self._validate_asset_ids(entity_type, self.global_asset_id, bool(self.specific_asset_id)) + self._validate_aasd_014(entity_type, self.global_asset_id, bool(self.specific_asset_id)) self._entity_type = entity_type @property @@ -1146,7 +1133,8 @@ def global_asset_id(self) -> Optional[base.Identifier]: @global_asset_id.setter def global_asset_id(self, global_asset_id: Optional[base.Identifier]) -> None: - self._validate_asset_ids(self.entity_type, global_asset_id, bool(self.specific_asset_id)) + self._validate_global_asset_id(global_asset_id) + self._validate_aasd_014(self.entity_type, global_asset_id, bool(self.specific_asset_id)) self._global_asset_id = global_asset_id @property @@ -1158,10 +1146,29 @@ def specific_asset_id(self, specific_asset_id: Iterable[base.SpecificAssetId]) - # constraints are checked via _check_constraint_set_spec_asset_id() in this case self._specific_asset_id[:] = specific_asset_id + def _check_constraint_add_spec_asset_id(self, _new_item: base.SpecificAssetId, + _old_list: List[base.SpecificAssetId]) -> None: + self._validate_aasd_014(self.entity_type, self.global_asset_id, True) + + def _check_constraint_set_spec_asset_id(self, items_to_replace: List[base.SpecificAssetId], + new_items: List[base.SpecificAssetId], + old_list: List[base.SpecificAssetId]) -> None: + self._validate_aasd_014(self.entity_type, self.global_asset_id, + len(old_list) - len(items_to_replace) + len(new_items) > 0) + + def _check_constraint_del_spec_asset_id(self, _item_to_del: base.SpecificAssetId, + old_list: List[base.SpecificAssetId]) -> None: + self._validate_aasd_014(self.entity_type, self.global_asset_id, len(old_list) > 1) + @staticmethod - def _validate_asset_ids(entity_type: base.EntityType, - global_asset_id: Optional[base.Identifier], - specific_asset_id_nonempty: bool) -> None: + def _validate_global_asset_id(global_asset_id: Optional[base.Identifier]) -> None: + if global_asset_id is not None: + _string_constraints.check_identifier(global_asset_id) + + @staticmethod + def _validate_aasd_014(entity_type: base.EntityType, + global_asset_id: Optional[base.Identifier], + specific_asset_id_nonempty: bool) -> None: if entity_type == base.EntityType.SELF_MANAGED_ENTITY and global_asset_id is None \ and not specific_asset_id_nonempty: raise base.AASConstraintViolation( @@ -1171,9 +1178,6 @@ def _validate_asset_ids(entity_type: base.EntityType, raise base.AASConstraintViolation( 14, "A co-managed entity has to have neither a globalAssetId nor a specificAssetId") - if global_asset_id is not None: - _string_constraints.check_identifier(global_asset_id) - class EventElement(SubmodelElement, metaclass=abc.ABCMeta): """