From 4d88d142fd80e8d70602e86815307987dd5834d4 Mon Sep 17 00:00:00 2001 From: ewuerger Date: Wed, 6 Dec 2023 15:05:39 +0100 Subject: [PATCH] refactor: Handle special `PhysicalComponent` types only once --- capella2polarion/elements/__init__.py | 35 +++++++++++++------------- capella2polarion/elements/serialize.py | 12 ++++----- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/capella2polarion/elements/__init__.py b/capella2polarion/elements/__init__.py index 9dc0696b..5c7c8b48 100644 --- a/capella2polarion/elements/__init__.py +++ b/capella2polarion/elements/__init__.py @@ -33,7 +33,9 @@ } PHYSICAL_COMPONENT_TYPES = { "PhysicalComponentNode": "PhysicalComponent", + "PhysicalActorNode": "PhysicalComponent", "PhysicalComponentBehavior": "PhysicalComponent", + "PhysicalActorBehavior": "PhysicalComponent", } POL2CAPELLA_TYPES: dict[str, str] = ( { @@ -232,25 +234,22 @@ def _fix_components( elements[typ] = actors elements[xtype] = components - nodes: list[common.GenericElement] = [] - behaviors: list[common.GenericElement] = [] - components = [] + nature_mapping: dict[str, tuple[list[common.GenericElement], str]] = { + "None": ([], "PhysicalComponentNode"), + "NODE": ([], "PhysicalComponentNode"), + "BEHAVIOR": ([], "PhysicalComponentBehavior"), + "NODE_actor": ([], "PhysicalActorNode"), + "BEHAVIOR_actor": ([], "PhysicalActorBehavior"), + } for obj in elements.get("PhysicalComponent", []): - if obj.nature is not None and obj.nature.name == "NODE": - nodes.append(obj) - type_map[obj.uuid] = "PhysicalComponentNode" - elif obj.nature is not None and obj.nature.name == "BEHAVIOR": - behaviors.append(obj) - type_map[obj.uuid] = "PhysicalComponentBehavior" - else: - components.append(obj) - - if nodes: - elements["PhysicalComponentNode"] = nodes - if behaviors: - elements["PhysicalComponentBehavior"] = behaviors - if components: - elements["PhysicalComponent"] = components + is_actor = "_actor" if obj.is_actor else "" + container, xtype = nature_mapping[f"{str(obj.nature)}{is_actor}"] + container.append(obj) + type_map[obj.uuid] = xtype + + for container, xtype in nature_mapping.values(): + if container: + elements[xtype] = container def make_model_elements_index(ctx: dict[str, t.Any]) -> None: diff --git a/capella2polarion/elements/serialize.py b/capella2polarion/elements/serialize.py index 856cf84a..dc2e6d95 100644 --- a/capella2polarion/elements/serialize.py +++ b/capella2polarion/elements/serialize.py @@ -291,7 +291,7 @@ def _condition(html: bool, value: str) -> CapellaWorkItem.Condition: return {"type": _type, "value": value} -def component_or_actor( +def _include_actor_in_type( obj: cs.Component, ctx: dict[str, t.Any] ) -> CapellaWorkItem: """Return attributes for a ``Component``.""" @@ -305,11 +305,11 @@ def component_or_actor( return work_item -def physical_component( +def _include_nature_in_type( obj: pa.PhysicalComponent, ctx: dict[str, t.Any] ) -> CapellaWorkItem: """Return attributes for a ``PhysicalComponent``.""" - work_item = component_or_actor(obj, ctx) + work_item = _include_actor_in_type(obj, ctx) xtype = work_item.type if obj.nature is not None: # pylint: disable-next=attribute-defined-outside-init @@ -322,11 +322,11 @@ def physical_component( ] SERIALIZERS: dict[str, Serializer] = { "CapabilityRealization": include_pre_and_post_condition, - "LogicalComponent": component_or_actor, + "LogicalComponent": _include_actor_in_type, "OperationalCapability": include_pre_and_post_condition, - "PhysicalComponent": physical_component, + "PhysicalComponent": _include_nature_in_type, "SystemCapability": include_pre_and_post_condition, - "SystemComponent": component_or_actor, + "SystemComponent": _include_actor_in_type, "Scenario": include_pre_and_post_condition, "Constraint": constraint, }