From 981650e5412c5f96d20baf7baaa506174a34318d Mon Sep 17 00:00:00 2001 From: huyenngn Date: Wed, 15 May 2024 11:38:28 +0200 Subject: [PATCH] refactor: Refactored default collector --- .../collectors/default.py | 50 +++++++------------ 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/capellambse_context_diagrams/collectors/default.py b/capellambse_context_diagrams/collectors/default.py index ade84e02..39609ffe 100644 --- a/capellambse_context_diagrams/collectors/default.py +++ b/capellambse_context_diagrams/collectors/default.py @@ -34,14 +34,9 @@ def collector( ports = port_collector(diagram.target, diagram.type) centerbox = data["children"][0] connections = port_exchange_collector(ports) - centerbox["ports"] = [ - makers.make_port(uuid) for uuid, edges in connections.items() if edges - ] + centerbox["ports"] = [makers.make_port(uuid) for uuid in connections] ex_datas: list[generic.ExchangeData] = [] - edges: common.ElementList[fa.AbstractExchange] = list( - chain.from_iterable(connections.values()) - ) - for ex in edges: + for ex in (edges := list(chain.from_iterable(connections.values()))): if is_hierarchical := exchanges.is_hierarchical(ex, centerbox): if not diagram.display_parent_relation: continue @@ -75,21 +70,21 @@ def _make_box_and_update_globals( made_boxes[obj.uuid] = box return box - def _make_owner_box(current: t.Any) -> t.Any: - if not (parent_box := global_boxes.get(current.owner.uuid)): + def _make_owner_box(obj: t.Any) -> t.Any: + if not (parent_box := global_boxes.get(obj.owner.uuid)): parent_box = _make_box_and_update_globals( - current.owner, + obj.owner, no_symbol=diagram.display_symbols_as_boxes, layout_options=makers.DEFAULT_LABEL_LAYOUT_OPTIONS, ) for box in (children := parent_box.setdefault("children", [])): - if box["id"] == current.uuid: - box = global_boxes.get(current.uuid, current) + if box["id"] == obj.uuid: + box = global_boxes.get(obj.uuid, obj) break else: - children.append(global_boxes.get(current.uuid, current)) - boxes_to_delete.add(current.uuid) - return current.owner + children.append(global_boxes.get(obj.uuid, obj)) + boxes_to_delete.add(obj.uuid) + return obj.owner if diagram.display_parent_relation: try: @@ -173,8 +168,9 @@ def _make_owner_box(current: t.Any) -> t.Any: generic.move_edges(owner_boxes, edges, data) centerbox["height"] = max(centerbox["height"], *stack_heights.values()) - if diagram.display_derived_interfaces: - add_derived_components_and_interfaces(diagram, data) + derivator = DERIVATORS.get(type(diagram.target)) + if diagram.display_derived_interfaces and derivator is not None: + derivator(diagram, data, made_boxes[diagram.target.uuid]) return data @@ -219,7 +215,8 @@ def port_exchange_collector( edges: dict[str, common.ElementList[fa.AbstractExchange]] = {} for i in ports: try: - edges[i.uuid] = filter(getattr(i, "exchanges")) + if e := filter(getattr(i, "exchanges")): + edges[i.uuid] = e except AttributeError: pass return edges @@ -292,19 +289,10 @@ def port_context_collector( return iter(ctx.values()) -def add_derived_components_and_interfaces( - diagram: context.ContextDiagram, data: _elkjs.ELKInputData -) -> None: - """Add hidden Boxes and Exchanges to ``obj``'s context. - - The derived exchanges are displayed with a dashed line. - """ - if derivator := DERIVATORS.get(type(diagram.target)): - derivator(diagram, data) - - def derive_from_functions( - diagram: context.ContextDiagram, data: _elkjs.ELKInputData + diagram: context.ContextDiagram, + data: _elkjs.ELKInputData, + centerbox: _elkjs.ELKInputChild, ) -> None: """Derive Components from allocated functions of the context target. @@ -342,8 +330,6 @@ def derive_from_functions( # Idea: Include flow direction of derived interfaces from all functional # exchanges. Mixed means bidirectional. Just even out bidirectional # interfaces and keep flow direction of others. - - centerbox = data["children"][0] for i, (uuid, derived_component) in enumerate(components.items(), 1): box = makers.make_box( derived_component,