Skip to content

Commit

Permalink
merge: Fix SA interface context
Browse files Browse the repository at this point in the history
  • Loading branch information
ewuerger committed Sep 9, 2022
2 parents 5ba0aa0 + 4209fe9 commit 8cfdca3
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 55 deletions.
42 changes: 18 additions & 24 deletions capellambse_context_diagrams/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@
"""
from __future__ import annotations

import collections.abc as cabc
import logging
import typing as t
from importlib import metadata

from capellambse.aird import COLORS, CSSdef, capstyle
from capellambse.model import common
from capellambse.model.crosslayer import fa
from capellambse.model.layers import ctx, la, oa, pa
from capellambse.model.modeltypes import DiagramType

from . import context

try:
Expand All @@ -29,6 +37,8 @@
__version__ = "0.0.0+unknown"
del metadata

ClassPair = tuple[type[common.GenericElement], DiagramType]

logger = logging.getLogger(__name__)

ATTR_NAME = "context_diagram"
Expand All @@ -43,13 +53,7 @@ def init() -> None:

def register_classes() -> None:
"""Add the `context_diagram` property to the relevant model objects."""
from capellambse.model.layers import ctx, la, oa, pa
from capellambse.model.modeltypes import DiagramType

patch_styles()
supported_classes: list[
tuple[type[common.GenericElement], DiagramType]
] = [
supported_classes: list[ClassPair] = [
(oa.Entity, DiagramType.OAB),
(oa.OperationalActivity, DiagramType.OAIB),
(oa.OperationalCapability, DiagramType.OCB),
Expand All @@ -62,23 +66,22 @@ def register_classes() -> None:
(pa.PhysicalComponent, DiagramType.PAB),
(pa.PhysicalFunction, DiagramType.PDFB),
]
patch_styles(supported_classes)
class_: type[common.GenericElement]
for class_, dgcls in supported_classes:
common.set_accessor(
class_, ATTR_NAME, context.ContextAccessor(dgcls.value)
)


def patch_styles() -> None:
def patch_styles(classes: cabc.Iterable[ClassPair]) -> None:
"""Add missing default styling to default styles.
See Also
--------
[capstyle.get_style][capellambse.aird.capstyle.get_style] : Default
style getter.
"""
from capellambse.aird import COLORS, CSSdef, capstyle

cap: dict[str, CSSdef] = {
"fill": [COLORS["_CAP_Entity_Gray_min"], COLORS["_CAP_Entity_Gray"]],
"stroke": COLORS["dark_gray"],
Expand All @@ -90,17 +93,13 @@ def patch_styles() -> None:
capstyle.STYLES["Operational Capabilities Blank"][
"Box.OperationalCapability"
] = cap
capstyle.STYLES["System Data Flow Blank"]["Circle.FunctionalExchange"] = {
"fill": COLORS["_CAP_xAB_Function_Border_Green"]
}
circle_style = {"fill": COLORS["_CAP_xAB_Function_Border_Green"]}
for _, dt in classes:
capstyle.STYLES[dt.value]["Circle.FunctionalExchange"] = circle_style


def register_interface_context() -> None:
"""Add the `context_diagram` property to interface model objects."""
from capellambse.model.crosslayer import fa
from capellambse.model.layers import ctx, la, oa, pa
from capellambse.model.modeltypes import DiagramType

common.set_accessor(
oa.CommunicationMean,
ATTR_NAME,
Expand Down Expand Up @@ -134,9 +133,7 @@ def register_functional_context() -> None:
The functional context diagrams will be available soon.
"""
from capellambse.model.layers import ctx, la, oa, pa
from capellambse.model.modeltypes import DiagramType

attr_name = f"functional_{ATTR_NAME}"
supported_classes: list[
tuple[type[common.GenericElement], DiagramType]
] = [
Expand All @@ -149,9 +146,6 @@ def register_functional_context() -> None:
for class_, dgcls in supported_classes:
common.set_accessor(
class_,
f"functional_{ATTR_NAME}",
attr_name,
context.FunctionalContextAccessor(dgcls.value),
)


from capellambse.model import common
15 changes: 8 additions & 7 deletions capellambse_context_diagrams/collectors/exchanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class ExchangeCollector(metaclass=abc.ABCMeta):
"""Base class for context collection on Exchanges."""

intermap = {
intermap: dict[str, DT] = {
DT.OAB: ("source", "target", "allocated_interactions", "activities"),
DT.SAB: (
"source.parent",
Expand All @@ -34,13 +34,13 @@ class ExchangeCollector(metaclass=abc.ABCMeta):
"source.parent",
"target.parent",
"allocated_functional_exchanges",
"functions",
"allocated_functions",
),
DT.PAB: (
"source.parent",
"target.parent",
"allocated_functional_exchanges",
"functions",
"allocated_functions",
),
}

Expand All @@ -57,7 +57,7 @@ def __init__(
self.get_source = operator.attrgetter(src)
self.get_target = operator.attrgetter(trg)
self.get_alloc_fex = operator.attrgetter(alloc_fex)
self.get_functions = operator.attrgetter(fncs)
self.get_alloc_functions = operator.attrgetter(fncs)

def get_functions_and_exchanges(
self, comp: common.GenericElement, interface: common.GenericElement
Expand All @@ -70,7 +70,7 @@ def get_functions_and_exchanges(
`FunctionalExchange`s for given `Component` and `interface`.
"""
functions, outgoings, incomings = [], [], []
alloc_functions = self.get_functions(comp)
alloc_functions = self.get_alloc_functions(comp)
for fex in self.get_alloc_fex(interface):
source = self.get_source(fex)
if source in alloc_functions:
Expand Down Expand Up @@ -170,7 +170,8 @@ def get_left_and_right(self) -> None:
def get_capella_order(
comp: common.GenericElement, functions: list[common.GenericElement]
) -> list[common.GenericElement]:
return [fnc for fnc in comp.functions if fnc in functions]
alloc_functions = self.get_alloc_functions(comp)
return [fnc for fnc in alloc_functions if fnc in functions]

def make_boxes(
comp: common.GenericElement, functions: list[common.GenericElement]
Expand All @@ -180,7 +181,7 @@ def make_boxes(
box["children"] = [
makers.make_box(c)
for c in functions
if c in self.get_functions(comp)
if c in self.get_alloc_functions(comp)
]
self.data["children"].append(box)
made_children.add(comp.uuid)
Expand Down
42 changes: 22 additions & 20 deletions capellambse_context_diagrams/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ def __get__( # type: ignore


class ContextDiagram(diagram.AbstractDiagram):
"""
An automatically generated context diagram.
"""An automatically generated context diagram.
Attributes
----------
Expand Down Expand Up @@ -152,6 +151,23 @@ class ContextDiagram(diagram.AbstractDiagram):
[`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,
) -> None:
super().__init__(obj._model)
self.target = obj
self.styleclass = class_

self.render_styles = render_styles or styling.BLUE_ACTOR_FNCS
self.serializer = serializers.DiagramSerializer(self)
self.__filters: cabc.MutableSet[str] = self.FilterSet(self)
self.display_symbols_as_boxes = display_symbols_as_boxes

@property
def uuid(self) -> str: # type: ignore
"""Returns diagram UUID."""
Expand Down Expand Up @@ -228,23 +244,6 @@ def __iter__(self) -> cabc.Iterator[str]:
def __len__(self) -> int:
return self._set.__len__()

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

self.render_styles = render_styles or styling.BLUE_ACTOR_FNCS
self.serializer = serializers.DiagramSerializer(self)
self.__filters: cabc.MutableSet[str] = self.FilterSet(self)
self.display_symbols_as_boxes = display_symbols_as_boxes

def _create_diagram(
self,
params: dict[str, t.Any],
Expand All @@ -270,9 +269,12 @@ def filters(self, value: cabc.Iterable[str]) -> None:

class InterfaceContextDiagram(ContextDiagram):
"""An automatically generated Context Diagram exclusively for
ComponentExchanges.
``ComponentExchange``s.
"""

def __init__(self, class_: str, obj: common.GenericElement, **kw) -> None:
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}"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ classifiers = [
"Topic :: Software Development :: Libraries :: Python Modules",
]
dependencies = [
"capellambse",
"capellambse>=0.4.19.dev12",
"typing_extensions",
]

Expand Down
Loading

0 comments on commit 8cfdca3

Please sign in to comment.