Skip to content

Commit

Permalink
refactor: Handle special PhysicalComponent types only once
Browse files Browse the repository at this point in the history
  • Loading branch information
ewuerger committed Dec 6, 2023
1 parent ee3816e commit 4d88d14
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
35 changes: 17 additions & 18 deletions capella2polarion/elements/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
}
PHYSICAL_COMPONENT_TYPES = {
"PhysicalComponentNode": "PhysicalComponent",
"PhysicalActorNode": "PhysicalComponent",
"PhysicalComponentBehavior": "PhysicalComponent",
"PhysicalActorBehavior": "PhysicalComponent",
}
POL2CAPELLA_TYPES: dict[str, str] = (
{
Expand Down Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions capella2polarion/elements/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``."""
Expand All @@ -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
Expand All @@ -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,
}

0 comments on commit 4d88d14

Please sign in to comment.