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(tree-view): Add handling of properties w/o Associations #140

Merged
merged 2 commits into from
Sep 19, 2024
Merged
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
43 changes: 26 additions & 17 deletions capellambse_context_diagrams/collectors/tree_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import annotations

import collections
import collections.abc as cabc
import copy
import dataclasses
Expand All @@ -12,7 +13,7 @@
import typing as t

import capellambse.model as m
from capellambse.metamodel import information
from capellambse.metamodel import information, modeltypes

from .. import _elkjs, context
from . import generic, makers
Expand All @@ -31,17 +32,14 @@


class ClassProcessor:
def __init__(
self,
data: _elkjs.ELKInputData,
all_associations: cabc.Iterable[information.Association],
) -> None:
def __init__(self, data: _elkjs.ELKInputData) -> None:
self.data = data
self.made_boxes: set[str] = {data.children[0].id}
self.made_edges: set[str] = set()
self.data_types: set[str] = set()
self.legend_boxes: list[_elkjs.ELKInputChild] = []
self.all_associations = all_associations

self._edge_count: dict[str, int] = collections.defaultdict(int)

def __contains__(self, uuid: str) -> bool:
objects = self.data.children + self.data.edges # type: ignore[operator]
Expand All @@ -51,13 +49,26 @@ def process_class(self, cls: ClassInfo, params: dict[str, t.Any]):
self._process_box(cls.source, cls.partition, params)

if not cls.primitive and isinstance(cls.target, information.Class):
assert cls.prop is not None
self._process_box(cls.target, cls.partition, params)
edges = [
assoc
for assoc in self.all_associations
if cls.prop in assoc.navigable_members # type: ignore[attr-defined]
]
edge_id = edges[0].uuid

if cls.prop.association is not None:
edge_id = cls.prop.association.uuid
else:
logger.warning(
"No Association found for %s set on 'navigable_members'",
cls.prop._short_repr_(),
)
if cls.prop.kind != modeltypes.AggregationKind.UNSET:
styleclass = cls.prop.kind.name.capitalize()
else:
styleclass = "Association"

self._edge_count.setdefault(cls.prop.uuid, 0)
self._edge_count[cls.prop.uuid] += 1
Comment on lines +67 to +68
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The beauty of a defaultdict is that you don't need the explicit setdefault call anymore. 🙃 But having it this way doesn't exactly hurt either.

i = self._edge_count[cls.prop.uuid]
edge_id = f"__{styleclass}:{cls.prop.uuid}-{i}"

if edge_id not in self.made_edges:
self.made_edges.add(edge_id)
text = cls.prop.name if cls.prop else ""
Expand All @@ -68,6 +79,7 @@ def process_class(self, cls: ClassInfo, params: dict[str, t.Any]):

if start != "1" or end != "1":
text = f"[{start}..{end}] {text}"

self.data.edges.append(
_elkjs.ELKInputEdge(
id=edge_id,
Expand Down Expand Up @@ -141,11 +153,8 @@ def collector(
data.children[0].labels[0].layoutOptions.update(
makers.DEFAULT_LABEL_LAYOUT_OPTIONS
)
all_associations: cabc.Iterable[information.Association] = (
diagram._model.search("Association")
)
_set_layout_options(data, params)
processor = ClassProcessor(data, all_associations)
processor = ClassProcessor(data)
processor._set_data_types_and_labels(data.children[0], diagram.target)
for _, cls in get_all_classes(
diagram.target,
Expand Down
Loading