Skip to content

Commit

Permalink
Refactor Entity and AssetInformation
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
zrgt authored and s-heppner committed Nov 14, 2023
1 parent c25e5f0 commit cabc193
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 36 deletions.
33 changes: 20 additions & 13 deletions basyx/aas/model/aas.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,17 @@ 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]:
return self._global_asset_id

@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
Expand All @@ -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")
Expand Down
50 changes: 27 additions & 23 deletions basyx/aas/model/submodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1115,29 +1115,16 @@ 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:
return self._entity_type

@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
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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):
"""
Expand Down

0 comments on commit cabc193

Please sign in to comment.