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 3e9176c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 47 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
29 changes: 0 additions & 29 deletions capella2polarion/elements/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,42 +291,13 @@ def _condition(html: bool, value: str) -> CapellaWorkItem.Condition:
return {"type": _type, "value": value}


def component_or_actor(
obj: cs.Component, ctx: dict[str, t.Any]
) -> CapellaWorkItem:
"""Return attributes for a ``Component``."""
work_item = _generic_work_item(obj, ctx)
if obj.is_actor:
xtype = RE_CAMEL_CASE_2ND_WORD_PATTERN.sub(
r"\1Actor", type(obj).__name__
)
# pylint: disable-next=attribute-defined-outside-init
work_item.type = helpers.resolve_element_type(xtype)
return work_item


def physical_component(
obj: pa.PhysicalComponent, ctx: dict[str, t.Any]
) -> CapellaWorkItem:
"""Return attributes for a ``PhysicalComponent``."""
work_item = component_or_actor(obj, ctx)
xtype = work_item.type
if obj.nature is not None:
# pylint: disable-next=attribute-defined-outside-init
work_item.type = f"{xtype}{obj.nature.name.capitalize()}"
return work_item


Serializer = cabc.Callable[
[common.GenericElement, dict[str, t.Any]], CapellaWorkItem
]
SERIALIZERS: dict[str, Serializer] = {
"CapabilityRealization": include_pre_and_post_condition,
"LogicalComponent": component_or_actor,
"OperationalCapability": include_pre_and_post_condition,
"PhysicalComponent": physical_component,
"SystemCapability": include_pre_and_post_condition,
"SystemComponent": component_or_actor,
"Scenario": include_pre_and_post_condition,
"Constraint": constraint,
}

0 comments on commit 3e9176c

Please sign in to comment.