Skip to content

tree_view

This submodule defines the collector for the Class-Tree diagram.

ClassInfo dataclass 🔗

All information needed for a Class box.

Source code in capellambse_context_diagrams/collectors/tree_view.py
178
179
180
181
182
183
184
185
186
187
188
@dataclasses.dataclass
class ClassInfo:
    """All information needed for a ``Class`` box."""

    source: information.Class
    target: information.Class | None
    prop: information.Property
    partition: int
    multiplicity: tuple[str, str] | None
    generalizes: information.Class | None = None
    primitive: bool = False

collector(diagram, params) 🔗

Return the class tree data for ELK.

Source code in capellambse_context_diagrams/collectors/tree_view.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
def collector(
    diagram: context.ContextDiagram, params: dict[str, t.Any]
) -> tuple[_elkjs.ELKInputData, _elkjs.ELKInputData]:
    """Return the class tree data for ELK."""
    assert isinstance(diagram.target, information.Class)
    data = generic.collector(diagram, no_symbol=True)
    data["children"][0]["labels"][0].setdefault("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._set_data_types_and_labels(data["children"][0], diagram.target)
    for _, cls in get_all_classes(
        diagram.target,
        max_partition=params.get("depth"),
        super=params.get("super", "ROOT"),
        sub=params.get("sub", "ROOT"),
    ):
        processor.process_class(cls, params)

    legend = makers.make_diagram(diagram)
    legend["layoutOptions"] = copy.deepcopy(_elkjs.RECT_PACKING_LAYOUT_OPTIONS)  # type: ignore[arg-type]
    legend["children"] = processor.legend_boxes
    return data, legend

get_all_classes(root, partition=0, classes=None, max_partition=None, super='ALL', sub='ALL') 🔗

Yield all classes of the class tree.

Source code in capellambse_context_diagrams/collectors/tree_view.py
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
def get_all_classes(
    root: information.Class,
    partition: int = 0,
    classes: dict[str, ClassInfo] | None = None,
    max_partition: int | None = None,
    super: t.Literal["ROOT"] | t.Literal["ALL"] = "ALL",
    sub: t.Literal["ROOT"] | t.Literal["ALL"] = "ALL",
) -> cabc.Iterator[tuple[str, ClassInfo]]:
    """Yield all classes of the class tree."""
    partition += 1
    classes = classes or {}
    if max_partition is not None and partition > max_partition:
        return

    for prop in root.owned_properties:
        property = _PropertyInfo(
            root, prop, partition, classes, None, max_partition, super, sub
        )
        process_property(property)

    if super == "ALL" or (super == "ROOT" and partition == 1):
        if root.super and not root.super.is_primitive:
            for prop in root.super.owned_properties:
                process_property(
                    _PropertyInfo(
                        root.super,
                        prop,
                        partition + 1,
                        classes,
                        root,
                        max_partition,
                        super,
                        sub,
                    )
                )

            edge_id = f"{root.uuid} {root.super.uuid}"
            if edge_id not in classes:
                classes[edge_id] = _make_class_info(
                    root.super, None, partition, generalizes=root
                )
                classes.update(
                    get_all_classes(
                        root.super,
                        partition,
                        classes,
                        max_partition,
                        super,
                        sub,
                    )
                )

    if sub == "ALL" or (sub == "ROOT" and partition == 1):
        for cls in root.sub:
            if cls.is_primitive:
                continue

            for prop in cls.owned_properties:
                process_property(
                    _PropertyInfo(
                        root,
                        prop,
                        partition,
                        classes,
                        cls,
                        max_partition,
                        super,
                        sub,
                    )
                )

            if (edge_id := f"{root.uuid} {cls.uuid}") not in classes:
                classes[edge_id] = _make_class_info(
                    root, None, partition, generalizes=cls
                )
                classes.update(
                    get_all_classes(
                        cls, partition, classes, max_partition, super, sub
                    )
                )

    yield from classes.items()

process_property(property) 🔗

Process a single property for class information.

Source code in capellambse_context_diagrams/collectors/tree_view.py
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
def process_property(
    property: _PropertyInfo,
) -> None:
    """Process a single property for class information."""
    prop = property.prop
    if not prop.type:
        logger.warning(
            "Property without abstract type found: %r", prop._short_repr_()
        )
        return

    if not prop.type.xtype.endswith("Class") or prop.type.is_primitive:
        logger.debug("Ignoring non-class property: %r", prop._short_repr_())
        return

    if (
        property.max_partition is not None
        and property.partition > property.max_partition
    ):
        return

    edge_id = f"{property.source.uuid} {prop.uuid} {prop.type.uuid}"
    if edge_id not in property.classes:
        property.classes[edge_id] = _make_class_info(
            property.source,
            prop,
            property.partition,
            generalizes=property.generalizes,
        )
        property.classes.update(
            get_all_classes(
                prop.type,
                property.partition,
                property.classes,
                property.max_partition,
                property.super,
                property.sub,
            )
        )