Skip to content

Commit

Permalink
fix(class-tree): Fix duplicated edges and param test
Browse files Browse the repository at this point in the history
Also partitioning is now parametrized and can be controlled via render
parameters.
  • Loading branch information
ewuerger committed Oct 16, 2023
1 parent 54a6b90 commit 9e4a048
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
24 changes: 15 additions & 9 deletions capellambse_context_diagrams/collectors/class_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,29 @@ def collector(
diagram: context.ContextDiagram, params: dict[str, t.Any] | None = None
) -> _elkjs.ELKInputData:
"""Return the class tree data for ELK."""
params = params or {}
assert isinstance(diagram.target, information.Class)
data = generic.collector(diagram, no_symbol=True)
data["children"][0]["layoutOptions"] = {}
data["children"][0]["layoutOptions"]["elk.partitioning.partition"] = 0
if params.get("partitioning", False):
data["children"][0]["layoutOptions"] = {}
data["children"][0]["layoutOptions"]["elk.partitioning.partition"] = 0
data["layoutOptions"] = LAYOUT_OPTIONS
data["layoutOptions"]["algorithm"] = (params or {})["algorithm"]
data["layoutOptions"]["elk.direction"] = (params or {})["direction"]
data["layoutOptions"]["edgeRouting"] = (params or {})["edgeRouting"]

made_boxes: set[str] = set()
for uid, (source, prop, target) in get_all_classes(diagram.target):
partition = uid.split(" ")[-1]
for _, (source, prop, target, partition) in get_all_classes(
diagram.target
):
if target.uuid not in made_boxes:
made_boxes.add(target.uuid)
box = makers.make_box(target)
box["layoutOptions"] = {}
box["layoutOptions"]["elk.partitioning.partition"] = int(partition)
if params.get("partitioning", False):
box["layoutOptions"] = {}
box["layoutOptions"]["elk.partitioning.partition"] = int(
partition
)
data["children"].append(box)

width, height = helpers.extent_func(prop.name)
Expand All @@ -62,7 +68,7 @@ def collector(


ClassContext = tuple[
information.Class, information.Property, information.Class
information.Class, information.Property, information.Class, int
]


Expand All @@ -74,9 +80,9 @@ def get_all_classes(
classes: dict[str, ClassContext] = {}
for prop in root.properties:
if prop.type.xtype.endswith("Class"):
edge_id = f"{root.name} {prop.type.name} {partition}"
edge_id = f"{root.uuid} {prop.uuid} {prop.type.uuid}"
if edge_id not in classes:
classes[edge_id] = (root, prop, prop.type)
classes[edge_id] = (root, prop, prop.type, partition)
classes.update(dict(get_all_classes(prop.type, partition)))

yield from classes.items()
1 change: 1 addition & 0 deletions capellambse_context_diagrams/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,5 +350,6 @@ def _create_diagram(self, params: dict[str, t.Any]) -> cdiagram.Diagram:
params.setdefault("algorithm", "layered")
params.setdefault("direction", "DOWN")
params.setdefault("edgeRouting", "POLYLINE")
params.setdefault("partitioning", True)
params["elkdata"] = class_tree.collector(self, params)
return super()._create_diagram(params)
13 changes: 12 additions & 1 deletion tests/test_class_tree_diagrams.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@ def test_class_tree_diagram_gets_rendered_successfully(
assert diag.render(fmt)


@pytest.mark.parametrize("edgeRouting", ["SPLINE", "ORTHOGONAL", "POLYLINE"])
@pytest.mark.parametrize("direction", ["DOWN", "RIGHT"])
@pytest.mark.parametrize("partitioning", [True, False])
def test_class_tree_diagram_renders_with_additional_params(
model: capellambse.MelodyModel,
edgeRouting: str,
direction: str,
partitioning: bool,
) -> None:
obj = model.by_uuid(CLASS_UUID)

diag = obj.class_tree_diagram

assert diag.render("svgdiagram", edgeRouting="POLYLINE", direction="RIGHT")
assert diag.render(
"svgdiagram",
edgeRouting=edgeRouting,
direction=direction,
partitioning=partitioning,
)

0 comments on commit 9e4a048

Please sign in to comment.