Skip to content

tree_view

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

ClassInfo dataclass 🔗

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

All information needed for a Class box.

collector 🔗

collector(diagram: context.ContextDiagram, params: dict[str, t.Any]) -> tuple[_elkjs.ELKInputData, _elkjs.ELKInputData]

Return the class tree data for ELK.

Source code in capellambse_context_diagrams/collectors/tree_view.py
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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].layoutOptions.update(
        makers.DEFAULT_LABEL_LAYOUT_OPTIONS
    )
    _set_layout_options(data, params)
    processor = ClassProcessor(data)
    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 🔗

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.

Source code in capellambse_context_diagrams/collectors/tree_view.py
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
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 (
            isinstance(root.super, information.Class)
            and not root.super.is_primitive
        ):
            for prop in root.super.owned_properties:
                process_property(
                    _PropertyInfo(
                        root.super,  # type: ignore[arg-type]
                        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,  # type: ignore[arg-type]
                    None,
                    partition,
                    generalizes=root,
                )
                classes.update(
                    get_all_classes(
                        root.super,  # type: ignore[arg-type]
                        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 🔗

process_property(property: _PropertyInfo) -> None

Process a single property for class information.

Source code in capellambse_context_diagrams/collectors/tree_view.py
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
244
245
246
247
248
249
250
251
252
253
def process_property(
    property: _PropertyInfo,
) -> None:
    """Process a single property for class information."""
    prop = property.prop
    if not prop.type:
        logger.debug("Ignoring property without type: %s", prop._short_repr_())
        return

    if not prop.type.xtype.endswith("Class") or prop.type.is_primitive:
        logger.debug("Ignoring non-class property: %s", 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,
            )
        )