Skip to content

context

Definitions of Custom Accessor- and Diagram-Classtypes based on Accessor and AbstractDiagram.

ClassTreeAccessor 🔗

Bases: ContextAccessor

Provides access to the tree view diagrams.

Source code in capellambse_context_diagrams/context.py
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
class ClassTreeAccessor(ContextAccessor):
    """Provides access to the tree view diagrams."""

    # pylint: disable=super-init-not-called
    def __init__(
        self, diagclass: str, render_params: dict[str, t.Any] | None = None
    ) -> None:
        self._dgcls = diagclass
        self._default_render_params = render_params or {}

    def __get__(  # type: ignore
        self,
        obj: common.T | None,
        objtype: type | None = None,
    ) -> common.Accessor | ContextDiagram:
        """Make a ClassTreeDiagram for the given model object."""
        del objtype
        if obj is None:  # pragma: no cover
            return self
        assert isinstance(obj, common.GenericElement)
        return self._get(obj, ClassTreeDiagram, "{}_class_tree")

__get__(obj, objtype=None) 🔗

Make a ClassTreeDiagram for the given model object.

Source code in capellambse_context_diagrams/context.py
155
156
157
158
159
160
161
162
163
164
165
def __get__(  # type: ignore
    self,
    obj: common.T | None,
    objtype: type | None = None,
) -> common.Accessor | ContextDiagram:
    """Make a ClassTreeDiagram for the given model object."""
    del objtype
    if obj is None:  # pragma: no cover
        return self
    assert isinstance(obj, common.GenericElement)
    return self._get(obj, ClassTreeDiagram, "{}_class_tree")

ClassTreeDiagram 🔗

Bases: ContextDiagram

An automatically generated ClassTree Diagram.

This diagram is exclusively for Classes.

Source code in capellambse_context_diagrams/context.py
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
class ClassTreeDiagram(ContextDiagram):
    """An automatically generated ClassTree Diagram.

    This diagram is exclusively for ``Class``es.
    """

    def __init__(self, class_: str, obj: common.GenericElement, **kw) -> None:
        super().__init__(class_, obj, **kw, display_symbols_as_boxes=True)

    @property
    def uuid(self) -> str:  # type: ignore
        """Returns the UUID of the diagram."""
        return f"{self.target.uuid}_tree_view"

    @property
    def name(self) -> str:  # type: ignore
        """Returns the name of the diagram."""
        return f"Tree view of {self.target.name}"

    def _create_diagram(self, params: dict[str, t.Any]) -> cdiagram.Diagram:
        params.setdefault("algorithm", params.get("algorithm", "layered"))
        params.setdefault("elk.direction", params.pop("direction", "DOWN"))
        params.setdefault("edgeRouting", params.get("edgeRouting", "POLYLINE"))
        params.setdefault(
            "nodeSize.constraints",
            params.pop("nodeSizeConstraints", "NODE_LABELS"),
        )
        params.setdefault(
            "partitioning.activate", params.pop("partitioning", False)
        )
        params.setdefault(
            "layered.edgeLabels.sideSelection",
            params.pop("edgeLabelsSide", "SMART_DOWN"),
        )
        data, legend = tree_view.collector(self, params)
        params["elkdata"] = data
        class_diagram = super()._create_diagram(params)
        width, height = class_diagram.viewport.size
        axis: t.Literal["x", "y"]
        if params["elk.direction"] in {"DOWN", "UP"}:
            legend["layoutOptions"]["aspectRatio"] = width / height
            axis = "x"
        else:
            legend["layoutOptions"]["aspectRatio"] = width
            axis = "y"
        params["elkdata"] = legend
        params["is_legend"] = True
        legend_diagram = super()._create_diagram(params)
        stack_diagrams(class_diagram, legend_diagram, axis)
        return class_diagram

name: str property 🔗

Returns the name of the diagram.

uuid: str property 🔗

Returns the UUID of the diagram.

ContextAccessor 🔗

Bases: Accessor

Provides access to the context diagrams.

Source code in capellambse_context_diagrams/context.py
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
class ContextAccessor(common.Accessor):
    """Provides access to the context diagrams."""

    def __init__(
        self, dgcls: str, render_params: dict[str, t.Any] | None = None
    ) -> None:
        super().__init__()
        self._dgcls = dgcls
        self._default_render_params = render_params or {}

    @t.overload
    def __get__(self, obj: None, objtype: type[t.Any]) -> ContextAccessor: ...

    @t.overload
    def __get__(
        self,
        obj: common.T,
        objtype: type[common.T] | None = None,
    ) -> ContextDiagram: ...

    def __get__(
        self,
        obj: common.T | None,
        objtype: type | None = None,
    ) -> common.Accessor | ContextDiagram:
        """Make a ContextDiagram for the given model object."""
        del objtype
        if obj is None:  # pragma: no cover
            return self
        assert isinstance(obj, common.GenericElement)
        return self._get(obj, ContextDiagram)

    def _get(
        self,
        obj: common.GenericElement,
        diagram_class: type[ContextDiagram],
        diagram_id: str = "{}_context",
    ) -> common.Accessor | ContextDiagram:
        try:
            cache = getattr(
                obj._model, ".".join((__name__, diagram_class.__qualname__))
            )
        except AttributeError:
            cache = {}
            setattr(
                obj._model,
                ".".join((__name__, diagram_class.__qualname__)),
                cache,
            )
        diagram_id = diagram_id.format(obj.uuid)
        try:
            return cache[diagram_id]
        except KeyError:
            pass

        new_diagram = diagram_class(
            self._dgcls, obj, **self._default_render_params
        )
        new_diagram.filters.add(filters.NO_UUID)
        cache[diagram_id] = new_diagram
        return new_diagram

__get__(obj, objtype=None) 🔗

Make a ContextDiagram for the given model object.

Source code in capellambse_context_diagrams/context.py
60
61
62
63
64
65
66
67
68
69
70
def __get__(
    self,
    obj: common.T | None,
    objtype: type | None = None,
) -> common.Accessor | ContextDiagram:
    """Make a ContextDiagram for the given model object."""
    del objtype
    if obj is None:  # pragma: no cover
        return self
    assert isinstance(obj, common.GenericElement)
    return self._get(obj, ContextDiagram)

ContextDiagram 🔗

Bases: AbstractDiagram

An automatically generated context diagram.

Attributes🔗

target The common.GenericElement from which the context is collected from. styleclass The diagram class (for e.g. [LAB]). render_styles Dictionary with the ElkChildType in str format as keys and styling.Styler functions as values. An example is given by: styling.BLUE_ACTOR_FNCS display_symbols_as_boxes Display objects that are normally displayed as symbol as a simple box instead, with the symbol being the box' icon. This avoids the object of interest to become one giant, oversized symbol in the middle of the diagram, and instead keeps the symbol small and only enlarges the surrounding box. display_parent_relation Display objects with a parent relationship to the object of interest as the parent box. display_derived_interfaces Display derived objects collected from additional collectors beside the main collector for building the context. slim_center_box Minimal width for the center box, containing just the icon and the label. This is False if hierarchy was identified. serializer The serializer builds a diagram.Diagram via serializers.DiagramSerializer.make_diagram by converting every _elkjs.ELKOutputChild into a diagram.Box, diagram.Edge or diagram.Circle. filters A list of filter names that are applied during collection of context. Currently this is only done in collectors.exchange_data_collector.

Source code in capellambse_context_diagrams/context.py
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
244
245
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
class ContextDiagram(diagram.AbstractDiagram):
    """An automatically generated context diagram.

    Attributes
    ----------
    target
        The `common.GenericElement` from which the context is collected
        from.
    styleclass
        The diagram class (for e.g. [LAB]).
    render_styles
        Dictionary with the `ElkChildType` in str format as keys and
        `styling.Styler` functions as values. An example is given by:
        [`styling.BLUE_ACTOR_FNCS`][capellambse_context_diagrams.styling.BLUE_ACTOR_FNCS]
    display_symbols_as_boxes
        Display objects that are normally displayed as symbol as a
        simple box instead, with the symbol being the box' icon. This
        avoids the object of interest to become one giant, oversized
        symbol in the middle of the diagram, and instead keeps the
        symbol small and only enlarges the surrounding box.
    display_parent_relation
        Display objects with a parent relationship to the object of
        interest as the parent box.
    display_derived_interfaces
        Display derived objects collected from additional collectors
        beside the main collector for building the context.
    slim_center_box
        Minimal width for the center box, containing just the icon and
        the label. This is False if hierarchy was identified.
    serializer
        The serializer builds a `diagram.Diagram` via
        [`serializers.DiagramSerializer.make_diagram`][capellambse_context_diagrams.serializers.DiagramSerializer.make_diagram]
        by converting every
        [`_elkjs.ELKOutputChild`][capellambse_context_diagrams._elkjs.ELKOutputChild]
        into a `diagram.Box`, `diagram.Edge` or `diagram.Circle`.
    filters
        A list of filter names that are applied during collection of
        context. Currently this is only done in
        [`collectors.exchange_data_collector`][capellambse_context_diagrams.collectors.generic.exchange_data_collector].
    """

    def __init__(
        self,
        class_: str,
        obj: common.GenericElement,
        *,
        render_styles: dict[str, styling.Styler] | None = None,
        display_symbols_as_boxes: bool = False,
        display_parent_relation: bool = False,
        display_derived_interfaces: bool = False,
        slim_center_box: bool = True,
    ) -> None:
        super().__init__(obj._model)
        self.target = obj
        self.styleclass = class_

        self.render_styles = render_styles or {}
        self.serializer = serializers.DiagramSerializer(self)
        self.__filters: cabc.MutableSet[str] = self.FilterSet(self)
        self.display_symbols_as_boxes = display_symbols_as_boxes
        self.display_parent_relation = display_parent_relation
        self.display_derived_interfaces = display_derived_interfaces
        self.slim_center_box = slim_center_box

        if standard_filter := STANDARD_FILTERS.get(class_):
            self.filters.add(standard_filter)
        if standard_styles := STANDARD_STYLES.get(class_):
            self.render_styles = standard_styles

    @property
    def uuid(self) -> str:  # type: ignore
        """Returns diagram UUID."""
        return f"{self.target.uuid}_context"

    @property
    def name(self) -> str:  # type: ignore
        """Returns the diagram name."""
        return f"Context of {self.target.name.replace('/', '- or -')}"

    @property
    def type(self) -> modeltypes.DiagramType:
        """Return the type of this diagram."""
        try:
            return modeltypes.DiagramType(self.styleclass)
        except ValueError:  # pragma: no cover
            logger.warning("Unknown diagram type %r", self.styleclass)
            return modeltypes.DiagramType.UNKNOWN

    class FilterSet(cabc.MutableSet):
        """A set that stores filter_names and invalidates diagram cache."""

        def __init__(
            self,
            diagram: diagram.AbstractDiagram,  # pylint: disable=redefined-outer-name
        ) -> None:
            self._set: set[str] = set()
            self._diagram = diagram

        def add(self, value: str) -> None:
            if value not in filters.FILTER_LABEL_ADJUSTERS:
                logger.error("The filter '%s' is not yet supported.", value)
                return
            if value not in self._set:
                self._set.add(value)
                self._diagram.invalidate_cache()

        def discard(self, value: str) -> None:
            if value in self._set:
                self._diagram.invalidate_cache()
            return self._set.discard(value)

        def __contains__(self, x: object) -> bool:
            return self._set.__contains__(x)

        def __iter__(self) -> cabc.Iterator[str]:
            return self._set.__iter__()

        def __len__(self) -> int:
            return self._set.__len__()

    def render(self, fmt: str | None, /, **params) -> t.Any:
        """Render the diagram in the given format."""
        rparams = params.copy()
        for attr, value in params.items():
            attribute = getattr(self, attr, "NOT_FOUND")
            if attribute not in {"NOT_FOUND", value}:
                self.invalidate_cache()

                setattr(self, attr, value)
                del rparams[attr]
        return super().render(fmt, **rparams)

    def _create_diagram(self, params: dict[str, t.Any]) -> cdiagram.Diagram:
        transparent_background = params.pop("transparent_background", False)
        self.display_parent_relation = params.pop(
            "display_parent_relation", self.display_parent_relation
        )
        self.display_derived_interfaces = params.pop(
            "display_derived_interfaces", self.display_derived_interfaces
        )
        self.display_symbols_as_boxes = params.pop(
            "display_symbols_as_boxes", self.display_symbols_as_boxes
        )
        self.slim_center_box = params.pop(
            "slim_center_box", self.slim_center_box
        )
        data = params.get("elkdata") or get_elkdata(self, params)
        layout = try_to_layout(data)
        add_context(layout, params.get("is_legend", False))
        return self.serializer.make_diagram(
            layout,
            transparent_background=transparent_background,
        )

    @property  # type: ignore
    def filters(self) -> cabc.MutableSet[str]:  # type: ignore
        return self.__filters

    @filters.setter
    def filters(self, value: cabc.Iterable[str]) -> None:
        self.__filters.clear()
        self.__filters |= set(value)

name: str property 🔗

Returns the diagram name.

type: modeltypes.DiagramType property 🔗

Return the type of this diagram.

uuid: str property 🔗

Returns diagram UUID.

FilterSet 🔗

Bases: MutableSet

A set that stores filter_names and invalidates diagram cache.

Source code in capellambse_context_diagrams/context.py
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
class FilterSet(cabc.MutableSet):
    """A set that stores filter_names and invalidates diagram cache."""

    def __init__(
        self,
        diagram: diagram.AbstractDiagram,  # pylint: disable=redefined-outer-name
    ) -> None:
        self._set: set[str] = set()
        self._diagram = diagram

    def add(self, value: str) -> None:
        if value not in filters.FILTER_LABEL_ADJUSTERS:
            logger.error("The filter '%s' is not yet supported.", value)
            return
        if value not in self._set:
            self._set.add(value)
            self._diagram.invalidate_cache()

    def discard(self, value: str) -> None:
        if value in self._set:
            self._diagram.invalidate_cache()
        return self._set.discard(value)

    def __contains__(self, x: object) -> bool:
        return self._set.__contains__(x)

    def __iter__(self) -> cabc.Iterator[str]:
        return self._set.__iter__()

    def __len__(self) -> int:
        return self._set.__len__()

render(fmt, /, **params) 🔗

Render the diagram in the given format.

Source code in capellambse_context_diagrams/context.py
332
333
334
335
336
337
338
339
340
341
342
def render(self, fmt: str | None, /, **params) -> t.Any:
    """Render the diagram in the given format."""
    rparams = params.copy()
    for attr, value in params.items():
        attribute = getattr(self, attr, "NOT_FOUND")
        if attribute not in {"NOT_FOUND", value}:
            self.invalidate_cache()

            setattr(self, attr, value)
            del rparams[attr]
    return super().render(fmt, **rparams)

DataFlowAccessor 🔗

Bases: ContextAccessor

Source code in capellambse_context_diagrams/context.py
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
class DataFlowAccessor(ContextAccessor):
    # pylint: disable=super-init-not-called
    def __init__(
        self, diagclass: str, render_params: dict[str, t.Any] | None = None
    ) -> None:
        self._dgcls = diagclass
        self._default_render_params = render_params or {}

    def __get__(  # type: ignore
        self,
        obj: common.T | None,
        objtype: type | None = None,
    ) -> common.Accessor | ContextDiagram:
        """Make a DataFlowViewDiagram for the given model object."""
        del objtype
        if obj is None:  # pragma: no cover
            return self
        assert isinstance(obj, common.GenericElement)
        return self._get(obj, DataFlowViewDiagram, "{}_data_flow_view")

__get__(obj, objtype=None) 🔗

Make a DataFlowViewDiagram for the given model object.

Source code in capellambse_context_diagrams/context.py
199
200
201
202
203
204
205
206
207
208
209
def __get__(  # type: ignore
    self,
    obj: common.T | None,
    objtype: type | None = None,
) -> common.Accessor | ContextDiagram:
    """Make a DataFlowViewDiagram for the given model object."""
    del objtype
    if obj is None:  # pragma: no cover
        return self
    assert isinstance(obj, common.GenericElement)
    return self._get(obj, DataFlowViewDiagram, "{}_data_flow_view")

DataFlowViewDiagram 🔗

Bases: ContextDiagram

An automatically generated DataFlowViewDiagram.

Source code in capellambse_context_diagrams/context.py
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
class DataFlowViewDiagram(ContextDiagram):
    """An automatically generated DataFlowViewDiagram."""

    def __init__(self, class_: str, obj: common.GenericElement, **kw) -> None:
        super().__init__(class_, obj, **kw, display_symbols_as_boxes=True)

    @property
    def uuid(self) -> str:  # type: ignore
        """Returns the UUID of the diagram."""
        return f"{self.target.uuid}_data_flow_view"

    @property
    def name(self) -> str:  # type: ignore
        """Returns the name of the diagram."""
        return f"DataFlow view of {self.target.name}"

    def _create_diagram(self, params: dict[str, t.Any]) -> cdiagram.Diagram:
        params["elkdata"] = dataflow_view.collector(self, params)
        return super()._create_diagram(params)

name: str property 🔗

Returns the name of the diagram.

uuid: str property 🔗

Returns the UUID of the diagram.

FunctionalContextAccessor 🔗

Bases: ContextAccessor

Source code in capellambse_context_diagrams/context.py
129
130
131
132
133
134
135
136
137
138
139
140
141
142
class FunctionalContextAccessor(ContextAccessor):
    def __get__(  # type: ignore
        self,
        obj: common.T | None,
        objtype: type | None = None,
    ) -> common.Accessor | ContextDiagram:
        """Make a ContextDiagram for the given model object."""
        del objtype
        if obj is None:  # pragma: no cover
            return self
        assert isinstance(obj, common.GenericElement)
        return self._get(
            obj, FunctionalContextDiagram, "{}_functional_context"
        )

__get__(obj, objtype=None) 🔗

Make a ContextDiagram for the given model object.

Source code in capellambse_context_diagrams/context.py
130
131
132
133
134
135
136
137
138
139
140
141
142
def __get__(  # type: ignore
    self,
    obj: common.T | None,
    objtype: type | None = None,
) -> common.Accessor | ContextDiagram:
    """Make a ContextDiagram for the given model object."""
    del objtype
    if obj is None:  # pragma: no cover
        return self
    assert isinstance(obj, common.GenericElement)
    return self._get(
        obj, FunctionalContextDiagram, "{}_functional_context"
    )

FunctionalContextDiagram 🔗

Bases: ContextDiagram

An automatically generated Context Diagram exclusively for Components.

Source code in capellambse_context_diagrams/context.py
402
403
404
405
406
407
408
409
410
411
412
413
414
415
class FunctionalContextDiagram(ContextDiagram):
    """An automatically generated Context Diagram exclusively for
    Components.
    """

    @property
    def name(self) -> str:  # type: ignore
        return f"Interface Context of {self.target.name}"

    def _create_diagram(self, params: dict[str, t.Any]) -> cdiagram.Diagram:
        params["elkdata"] = exchanges.get_elkdata_for_exchanges(
            self, exchanges.FunctionalContextCollector, params
        )
        return super()._create_diagram(params)

InterfaceContextAccessor 🔗

Bases: ContextAccessor

Provides access to the interface context diagrams.

Source code in capellambse_context_diagrams/context.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
class InterfaceContextAccessor(ContextAccessor):
    """Provides access to the interface context diagrams."""

    def __init__(  # pylint: disable=super-init-not-called
        self,
        diagclass: dict[type[common.GenericElement], str],
        render_params: dict[str, t.Any] | None = None,
    ) -> None:
        self.__dgclasses = diagclass
        self._default_render_params = render_params or {}

    def __get__(  # type: ignore
        self,
        obj: common.T | None,
        objtype: type | None = None,
    ) -> common.Accessor | ContextDiagram:
        """Make a ContextDiagram for the given model object."""
        del objtype
        if obj is None:  # pragma: no cover
            return self
        assert isinstance(obj, common.GenericElement)
        assert isinstance(obj.parent, common.GenericElement)
        self._dgcls = self.__dgclasses[obj.parent.__class__]
        return self._get(obj, InterfaceContextDiagram, "{}_interface_context")

__get__(obj, objtype=None) 🔗

Make a ContextDiagram for the given model object.

Source code in capellambse_context_diagrams/context.py
114
115
116
117
118
119
120
121
122
123
124
125
126
def __get__(  # type: ignore
    self,
    obj: common.T | None,
    objtype: type | None = None,
) -> common.Accessor | ContextDiagram:
    """Make a ContextDiagram for the given model object."""
    del objtype
    if obj is None:  # pragma: no cover
        return self
    assert isinstance(obj, common.GenericElement)
    assert isinstance(obj.parent, common.GenericElement)
    self._dgcls = self.__dgclasses[obj.parent.__class__]
    return self._get(obj, InterfaceContextDiagram, "{}_interface_context")

InterfaceContextDiagram 🔗

Bases: ContextDiagram

An automatically generated Context Diagram exclusively for ComponentExchanges.

Source code in capellambse_context_diagrams/context.py
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
class InterfaceContextDiagram(ContextDiagram):
    """An automatically generated Context Diagram exclusively for
    ``ComponentExchange``s.
    """

    def __init__(
        self,
        class_: str,
        obj: common.GenericElement,
        include_interface: bool = False,
        **kw,
    ) -> None:
        self.include_interface = include_interface
        super().__init__(class_, obj, **kw, display_symbols_as_boxes=True)

    @property
    def name(self) -> str:  # type: ignore
        return f"Interface Context of {self.target.name}"

    def _create_diagram(self, params: dict[str, t.Any]) -> cdiagram.Diagram:
        params["elkdata"] = exchanges.get_elkdata_for_exchanges(
            self, exchanges.InterfaceContextCollector, params
        )
        return super()._create_diagram(params)

RealizationViewContextAccessor 🔗

Bases: ContextAccessor

Provides access to the realization view diagrams.

Source code in capellambse_context_diagrams/context.py
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
class RealizationViewContextAccessor(ContextAccessor):
    """Provides access to the realization view diagrams."""

    # pylint: disable=super-init-not-called
    def __init__(
        self, diagclass: str, render_params: dict[str, t.Any] | None = None
    ) -> None:
        self._dgcls = diagclass
        self._default_render_params = render_params or {}

    def __get__(  # type: ignore
        self,
        obj: common.T | None,
        objtype: type | None = None,
    ) -> common.Accessor | ContextDiagram:
        """Make a RealizationViewDiagram for the given model object."""
        del objtype
        if obj is None:  # pragma: no cover
            return self
        assert isinstance(obj, common.GenericElement)
        return self._get(obj, RealizationViewDiagram, "{}_realization_view")

__get__(obj, objtype=None) 🔗

Make a RealizationViewDiagram for the given model object.

Source code in capellambse_context_diagrams/context.py
178
179
180
181
182
183
184
185
186
187
188
def __get__(  # type: ignore
    self,
    obj: common.T | None,
    objtype: type | None = None,
) -> common.Accessor | ContextDiagram:
    """Make a RealizationViewDiagram for the given model object."""
    del objtype
    if obj is None:  # pragma: no cover
        return self
    assert isinstance(obj, common.GenericElement)
    return self._get(obj, RealizationViewDiagram, "{}_realization_view")

RealizationViewDiagram 🔗

Bases: ContextDiagram

An automatically generated RealizationViewDiagram Diagram.

This diagram is exclusively for Activity, Functions, Entity and Components of all layers.

Source code in capellambse_context_diagrams/context.py
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
class RealizationViewDiagram(ContextDiagram):
    """An automatically generated RealizationViewDiagram Diagram.

    This diagram is exclusively for ``Activity``, ``Function``s,
    ``Entity`` and ``Components`` of all layers.
    """

    def __init__(self, class_: str, obj: common.GenericElement, **kw) -> None:
        super().__init__(class_, obj, **kw, display_symbols_as_boxes=True)

    @property
    def uuid(self) -> str:  # type: ignore
        """Returns the UUID of the diagram."""
        return f"{self.target.uuid}_realization_view"

    @property
    def name(self) -> str:  # type: ignore
        """Returns the name of the diagram."""
        return f"Realization view of {self.target.name}"

    def _create_diagram(self, params: dict[str, t.Any]) -> cdiagram.Diagram:
        params.setdefault("depth", params.get("depth", 1))
        params.setdefault(
            "search_direction", params.get("search_direction", "ALL")
        )
        params.setdefault("show_owners", True)
        params.setdefault("layer_sizing", "WIDTH")
        data, edges = realization_view.collector(self, params)
        layout = try_to_layout(data)
        adjust_layer_sizing(data, layout, params["layer_sizing"])
        layout = try_to_layout(data)
        for edge in edges:
            layout["children"].append(
                {
                    "id": edge["id"],
                    "type": "edge",
                    "sourceId": edge["sources"][0],
                    "targetId": edge["targets"][0],
                    "routingPoints": [],
                    "styleclass": "Realization",
                }  # type: ignore[arg-type]
            )
        self._add_layer_labels(layout)
        return self.serializer.make_diagram(
            layout,
            transparent_background=params.get("transparent_background", False),
        )

    def _add_layer_labels(self, layout: _elkjs.ELKOutputData) -> None:
        for layer in layout["children"]:
            if layer["type"] != "node":
                continue

            layer_obj = self.serializer.model.by_uuid(layer["id"])
            _, layer_name = realization_view.find_layer(layer_obj)
            pos = layer["position"]["x"], layer["position"]["y"]
            size = layer["size"]["width"], layer["size"]["height"]
            width, height = helpers.get_text_extent(layer_name)
            x, y, tspan_y = calculate_label_position(*pos, *size)
            label_box: _elkjs.ELKOutputChild = {
                "type": "label",
                "id": "None",
                "text": layer_name,
                "position": {"x": x, "y": y},
                "size": {"width": width, "height": height},
                "style": {
                    "text_transform": f"rotate(-90, {x}, {y}) {tspan_y}",
                    "text_fill": "grey",
                },
            }
            layer["children"].insert(0, label_box)
            layer["style"] = {"stroke": "grey", "rx": 5, "ry": 5}

name: str property 🔗

Returns the name of the diagram.

uuid: str property 🔗

Returns the UUID of the diagram.

add_context(data, is_legend=False) 🔗

Add all connected nodes as context to all elements.

Source code in capellambse_context_diagrams/context.py
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
def add_context(data: _elkjs.ELKOutputData, is_legend: bool = False) -> None:
    """Add all connected nodes as context to all elements."""
    if is_legend:
        for child in data["children"]:
            if child["type"] == "node":
                child["context"] = [child["id"]]  # type: ignore[typeddict-unknown-key]
        return

    ids: set[str] = set()

    def get_ids(
        obj: (
            _elkjs.ELKOutputNode
            | _elkjs.ELKOutputPort
            | _elkjs.ELKOutputJunction
            | _elkjs.ELKOutputEdge
        ),
    ) -> None:
        if obj["id"] and not obj["id"].startswith("g_"):
            ids.add(obj["id"])
        for child in obj.get("children", []):
            if child["type"] in {"node", "port", "junction", "edge"}:
                assert child["type"] != "label"
                get_ids(child)

    def set_ids(
        obj: _elkjs.ELKOutputChild,
        ids: set[str],
    ) -> None:
        obj["context"] = list(ids)  # type: ignore[typeddict-unknown-key]
        for child in obj.get("children", []):  # type: ignore[attr-defined]
            set_ids(child, ids)

    for child in data["children"]:
        if child["type"] in {"node", "port", "junction", "edge"}:
            assert child["type"] != "label"
            get_ids(child)

    for child in data["children"]:
        set_ids(child, ids)

adjust_layer_sizing(data, layout, layer_sizing) 🔗

Set nodeSize.minimum config in the layoutOptions.

Source code in capellambse_context_diagrams/context.py
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
def adjust_layer_sizing(
    data: _elkjs.ELKInputData,
    layout: _elkjs.ELKOutputData,
    layer_sizing: t.Literal["UNION", "WIDTH", "HEIGHT"],
) -> None:
    """Set `nodeSize.minimum` config in the layoutOptions."""

    def calculate_min(key: t.Literal["width", "height"] = "width") -> float:
        return max(child["size"][key] for child in layout["children"])  # type: ignore[typeddict-item]

    if layer_sizing not in {"UNION", "WIDTH", "HEIGHT", "INDIVIDUAL"}:
        raise NotImplementedError(
            "For ``layer_sizing`` only UNION, WIDTH, HEIGHT or INDIVIDUAL is supported"
        )

    min_w = calculate_min() + 15.0 if layer_sizing in {"UNION", "WIDTH"} else 0
    min_h = (
        calculate_min("height") if layer_sizing in {"UNION", "HEIGHT"} else 0
    )
    for layer in data["children"]:
        layer["layoutOptions"]["nodeSize.minimum"] = f"({min_w},{min_h})"

calculate_label_position(x, y, width, height, padding=10) 🔗

Calculate the position of the label and tspan.

The function calculates the center of the rectangle and uses the rectangle's width and height to adjust its position within it. The text is assumed to be horizontally and vertically centered within the rectangle. The tspan y coordinate is for positioning the label right under the left side of the rectangle.

Parameters🔗

Returns🔗

tuple A tuple containing the x- and y-coordinate for the text element and the adjusted y-coordinate for the tspan element.

Source code in capellambse_context_diagrams/context.py
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
def calculate_label_position(
    x: float,
    y: float,
    width: float,
    height: float,
    padding: float = 10,
) -> tuple[float, float, float]:
    """Calculate the position of the label and tspan.

    The function calculates the center of the rectangle and uses the
    rectangle's width and height to adjust its position within it. The
    text is assumed to be horizontally and vertically centered within
    the rectangle. The tspan y coordinate is for positioning the label
    right under the left side of the rectangle.

    Parameters
    ----------

    Returns
    -------
    tuple
        A tuple containing the x- and y-coordinate for the text element
        and the adjusted y-coordinate for the tspan element.
    """
    center_y = y + height / 2
    tspan_y = center_y - width / 2 + padding
    return (x + width / 2, center_y, tspan_y)

stack_diagrams(first, second, axis='x') 🔗

Add the diagram elements from right to left inline.

Source code in capellambse_context_diagrams/context.py
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
def stack_diagrams(
    first: cdiagram.Diagram,
    second: cdiagram.Diagram,
    axis: t.Literal["x", "y"] = "x",
) -> None:
    """Add the diagram elements from ``right`` to left inline."""
    if first.viewport:
        offset = first.viewport.pos + first.viewport.size
        offset @= (1, 0) if axis == "x" else (0, 1)
        for element in second:
            new = copy.deepcopy(element)
            new.move(offset)
            first += new
    else:
        for element in second:
            new = copy.deepcopy(element)
            first += new

try_to_layout(data) 🔗

Try calling elkjs, raise a JSONDecodeError if it fails.

Source code in capellambse_context_diagrams/context.py
607
608
609
610
611
612
613
def try_to_layout(data: _elkjs.ELKInputData) -> _elkjs.ELKOutputData:
    """Try calling elkjs, raise a JSONDecodeError if it fails."""
    try:
        return _elkjs.call_elkjs(data)
    except json.JSONDecodeError as error:
        logger.error(json.dumps(data, indent=4))
        raise error