From 21b14b6013e1a33d99fe7b54e04d7fca2c952804 Mon Sep 17 00:00:00 2001 From: huyenngn Date: Tue, 12 Nov 2024 07:35:36 +0100 Subject: [PATCH] feat(custom_diagram): Add nested recursion and recursion depth --- .../collectors/custom.py | 23 ++++++++++++------- capellambse_context_diagrams/context.py | 4 ---- docs/custom_diagram.md | 1 + 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/capellambse_context_diagrams/collectors/custom.py b/capellambse_context_diagrams/collectors/custom.py index 3ac0aa5..e11a45f 100644 --- a/capellambse_context_diagrams/collectors/custom.py +++ b/capellambse_context_diagrams/collectors/custom.py @@ -35,7 +35,8 @@ def __init__( self.data = makers.make_diagram(diagram) self.params = params self.instructions = self.diagram._collect - self.repeat_instructions: dict[str, t.Any] | None = None + self.repeat_instructions: list[dict[str, t.Any]] = [] + self.repeat_depth: list[int] = [] self.visited: set[str] = set() self.boxes: dict[str, _elkjs.ELKInputChild] = {} self.edges: dict[str, _elkjs.ELKInputEdge] = {} @@ -105,15 +106,21 @@ def _matches_filters( def _perform_get( self, obj: m.ModelElement, instructions: dict[str, t.Any] ) -> None: - if instructions.pop("repeat", False): - self.repeat_instructions = instructions + if max_depth := instructions.get("repeat", None): + if self.repeat_instructions: + self.repeat_depth[-1] -= 1 + if self.repeat_depth[-1] == 0: + self.repeat_instructions.pop() + else: + self.repeat_instructions.append(instructions) + self.repeat_depth.append(max_depth) if insts := instructions.get("get"): create = False elif insts := instructions.get("include"): create = True if not insts: if self.repeat_instructions: - self._perform_get(obj, self.repeat_instructions) + self._perform_get(obj, self.repeat_instructions[-1]) return if isinstance(insts, dict): insts = [insts] @@ -206,14 +213,14 @@ def _make_edge_and_ports( tgt_obj = edge_obj.target src_owner = src_obj.owner tgt_owner = tgt_obj.owner + src_owners = list(generic.get_all_owners(src_obj)) + tgt_owners = list(generic.get_all_owners(tgt_obj)) if self.diagram._hide_direct_children: if ( - getattr(src_owner, "owner", None) == self.boxable_target - or getattr(tgt_owner, "owner", None) == self.boxable_target + self.boxable_target.uuid in src_owners + or self.boxable_target.uuid in tgt_owners ): return None - src_owners = list(generic.get_all_owners(src_obj)) - tgt_owners = list(generic.get_all_owners(tgt_obj)) if self.diagram._display_parent_relation: common_owner = None for owner in src_owners: diff --git a/capellambse_context_diagrams/context.py b/capellambse_context_diagrams/context.py index 29d5753..d7dc3e4 100644 --- a/capellambse_context_diagrams/context.py +++ b/capellambse_context_diagrams/context.py @@ -271,7 +271,6 @@ class ContextDiagram(m.AbstractDiagram): * display_unused_ports - Display ports that are not connected to an edge. """ - _collect: dict[str, t.Any] _display_symbols_as_boxes: bool _display_parent_relation: bool _hide_direct_children: bool @@ -281,7 +280,6 @@ class ContextDiagram(m.AbstractDiagram): _port_label_position: str _transparent_background: bool _display_unused_ports: bool - _unify_edge_direction: str def __init__( self, @@ -301,7 +299,6 @@ def __init__( self._elk_input_data: CollectorOutputData | None = None self.__filters: cabc.MutableSet[str] = self.FilterSet(self) self._default_render_parameters = { - "collect": {}, "display_symbols_as_boxes": False, "display_parent_relation": False, "hide_direct_children": False, @@ -311,7 +308,6 @@ def __init__( "port_label_position": _elkjs.PORT_LABEL_POSITION.OUTSIDE.name, "display_unused_ports": False, "transparent_background": False, - "unify_edge_direction": "NONE", } | default_render_parameters if standard_filter := STANDARD_FILTERS.get(class_): diff --git a/docs/custom_diagram.md b/docs/custom_diagram.md index c235b89..3c40ea3 100644 --- a/docs/custom_diagram.md +++ b/docs/custom_diagram.md @@ -33,6 +33,7 @@ get: - `get` element at attribute defined in `name` - `include` element at attribute defined in `name` - `filter` elements if element is a list +- `repeat` elements if for n times ## API Usage