Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(context-diagram): Add cycle detection #155

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions capellambse_context_diagrams/collectors/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def __init__(
self.global_boxes = {self.centerbox.id: self.centerbox}
self.made_boxes = {self.centerbox.id: self.centerbox}
self.boxes_to_delete = {self.centerbox.id}
self.exchanges: list[fa.AbstractExchange] = []
self.exchanges: dict[str, fa.AbstractExchange] = {}
if self.diagram._display_parent_relation:
self.diagram_target_owners = list(
generic.get_all_owners(self.diagram.target)
Expand Down Expand Up @@ -95,14 +95,12 @@ def process_context(self):
self.data.children.extend(self.global_boxes.values())
if self.diagram._display_parent_relation:
owner_boxes: dict[str, _elkjs.ELKInputChild] = {
uuid: box
for uuid, box in self.made_boxes.items()
if box.children
uuid: box for uuid, box in self.made_boxes.items()
}
generic.move_parent_boxes_to_owner(
owner_boxes, self.diagram.target, self.data
)
generic.move_edges(owner_boxes, self.exchanges, self.data)
generic.move_edges(owner_boxes, self.exchanges.values(), self.data)

self.centerbox.height = max(
self.centerbox.height, *stack_heights.values()
Expand All @@ -112,7 +110,7 @@ def process_context(self):
hidden = set(edge.id for edge in self.centerbox.edges)
centerbox_ports = set(port.id for port in self.centerbox.ports)
port_uuids = set()
for ex in self.exchanges:
for ex in self.exchanges.values():
if ex.uuid not in hidden:
if ex.source.uuid in centerbox_ports:
port_uuids.add(ex.source.uuid)
Expand Down Expand Up @@ -169,10 +167,10 @@ def _process_exchanges(
self._process_port_spread(
out_exchanges, "target", -1, port_spread, owners
)
self.exchanges = inc_exchanges + out_exchanges
self.exchanges = {ex.uuid: ex for ex in inc_exchanges + out_exchanges}
ex_datas: list[generic.ExchangeData] = []
seen_exchanges: set[str] = set()
for ex in self.exchanges:
for ex in self.exchanges.values():
if ex.uuid in seen_exchanges:
continue

Expand Down Expand Up @@ -216,6 +214,7 @@ def _process_exchanges(
port = makers.make_port(p.uuid)
if self.diagram._display_port_labels:
port.labels = makers.make_label(p.name)

self.centerbox.ports.append(port)
self.centerbox.layoutOptions["portLabels.placement"] = "OUTSIDE"

Expand Down
13 changes: 11 additions & 2 deletions capellambse_context_diagrams/collectors/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def move_parent_boxes_to_owner(

def move_edges(
boxes: cabc.Mapping[str, _elkjs.ELKInputChild],
connections: cabc.Sequence[m.ModelElement],
connections: cabc.Iterable[m.ModelElement],
data: _elkjs.ELKInputData,
) -> None:
"""Move edges to boxes."""
Expand All @@ -234,11 +234,19 @@ def move_edges(
source_owner_uuids.remove(c.source.uuid)
target_owner_uuids.remove(c.source.uuid)

if c.source.owner is not None and c.target.owner is not None:
cycle_detected = c.source.owner.uuid == c.target.owner.uuid
else:
cycle_detected = False

common_owner_uuid = None
for owner in source_owner_uuids:
if owner in target_owner_uuids:
common_owner_uuid = owner
break
if cycle_detected:
cycle_detected = False
else:
break

if not common_owner_uuid or not (
owner_box := boxes.get(common_owner_uuid)
Expand All @@ -249,6 +257,7 @@ def move_edges(
if edge.id == c.uuid:
owner_box.edges.append(edge)
edges_to_remove.append(edge.id)

data.edges = [e for e in data.edges if e.id not in edges_to_remove]


Expand Down
Loading
Loading