From b95f27d0618fd6960bba4c0994c1287d0f284cb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ernst=20W=C3=BCrger?= Date: Tue, 2 Jul 2024 09:54:50 +0200 Subject: [PATCH] feat: Enable `display_derived_interfaces` on single child (#114) --- capellambse_context_diagrams/context.py | 16 + tests/data/ContextDiagram.aird | 380 ++++++++++++++++++++++-- tests/data/ContextDiagram.capella | 42 +++ tests/test_context_diagrams.py | 11 + 4 files changed, 426 insertions(+), 23 deletions(-) diff --git a/capellambse_context_diagrams/context.py b/capellambse_context_diagrams/context.py index a0ae4a55..8661a37f 100644 --- a/capellambse_context_diagrams/context.py +++ b/capellambse_context_diagrams/context.py @@ -353,6 +353,10 @@ def _create_diagram(self, params: dict[str, t.Any]) -> cdiagram.Diagram: setattr(self, param_name, override) data = params.get("elkdata") or get_elkdata(self, params) + if has_single_child(data): + self.display_derived_interfaces = True + data = get_elkdata(self, params) + layout = try_to_layout(data) add_context(layout, params.get("is_legend", False)) return self.serializer.make_diagram( @@ -687,3 +691,15 @@ def calculate_label_position( center_y = y + height / 2 tspan_y = center_y - width / 2 + padding return (x + width / 2, center_y, tspan_y) + + +def has_single_child(data: _elkjs.ELKInputData | _elkjs.ELKInputChild) -> bool: + """Checks if ``data`` has a single or no child.""" + if not data.children: + return True + + for child in data.children: + if not has_single_child(child): + return False + + return len(data.children) == 1 diff --git a/tests/data/ContextDiagram.aird b/tests/data/ContextDiagram.aird index 262c9dc7..772afcb7 100644 --- a/tests/data/ContextDiagram.aird +++ b/tests/data/ContextDiagram.aird @@ -86,7 +86,7 @@ - +
@@ -142,6 +142,14 @@ + + +
+
+ + + + @@ -2960,17 +2968,6 @@ - - - - - - - - - - - @@ -3008,6 +3005,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -3465,15 +3495,6 @@ - - - - - - - - - @@ -3501,6 +3522,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + KEEP_LOCATION KEEP_SIZE KEEP_RATIO @@ -3548,8 +3596,8 @@ KEEP_LOCATION KEEP_SIZE KEEP_RATIO - - + + @@ -14427,4 +14475,290 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + KEEP_LOCATION + KEEP_SIZE + KEEP_RATIO + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/data/ContextDiagram.capella b/tests/data/ContextDiagram.capella index 0d4c574a..35ebbc06 100644 --- a/tests/data/ContextDiagram.capella +++ b/tests/data/ContextDiagram.capella @@ -2909,6 +2909,8 @@ The predator is far away id="24d3e25a-5399-4374-aa85-8a4fd2b2a815" name="teach Care of Magical Creatures"/> + @@ -3210,6 +3212,16 @@ The predator is far away + + + + + + @@ -3342,6 +3354,12 @@ The predator is far away + + name="Left 2" abstractType="#57635b74-b251-4b9b-8cc3-706dcc7b65db"/> + name="LC 1" abstractType="#44d051f2-875d-4a51-93f0-6c7b20a45842"/> + + @@ -4258,6 +4282,18 @@ The predator is far away + + + + + + sourceElement="#5d562aa6-738a-48ce-aeb9-eb9d656edc7b"/> + + + None: assert diag.nodes +def test_context_is_collected_again_with_derivated( + model: capellambse.MelodyModel, +) -> None: + obj = model.by_uuid(TEST_DERIVATION_UUID) + + diagram = obj.context_diagram.render(None) + + assert len(diagram) > 1 + + @pytest.mark.parametrize( "parameter", [