Skip to content

Commit

Permalink
feat(validation): Enable validation of an entire layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Wuestengecko committed Jul 11, 2024
1 parent 73b3973 commit 4f68718
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
6 changes: 6 additions & 0 deletions capellambse/extensions/validation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def init() -> None:
# pylint: disable=redefined-outer-name # false-positive
import capellambse
from capellambse.model import common as c
from capellambse.model import crosslayer as xl

c.set_accessor(
capellambse.MelodyModel,
Expand All @@ -35,3 +36,8 @@ def init() -> None:
c.GenericElement.validate = property( # type: ignore[attr-defined]
lambda self: self.validation.validate
)
c.set_accessor(
xl.BaseArchitectureLayer,
"validation",
c.AlternateAccessor(LayerValidation),
)
34 changes: 33 additions & 1 deletion capellambse/extensions/validation/_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
__all__ = [
"Category",
"ElementValidation",
"LayerValidation",
"ModelValidation",
"RealType",
"Result",
Expand Down Expand Up @@ -441,8 +442,39 @@ def search(self, /, *typenames: str) -> c.ElementList[t.Any]:
return c.MixedElementList(self._model, list(found.values()))


class LayerValidation(Validation):
"""Provides access to the layer's validation rules and results."""

@property
def rules(self) -> Rules:
"""Return all registered validation rules."""
return _VALIDATION_RULES

def validate(self) -> Results:
"""Execute all registered validation rules and store results."""
all_results = []
for rule_ in _VALIDATION_RULES.values():
for obj in rule_.find_objects(self._model):
if obj.layer != self.parent:
continue
all_results.append(
(
(rule_, obj.uuid),
Result(rule_, obj, rule_.validate(obj)),
)
)
return Results(all_results)

def search(self, /, *typenames: str) -> c.ElementList[t.Any]:
found: dict[str, t.Any] = {}
for i in typenames:
objs = _types_registry[i].search(self._model).by_layer(self.parent)
found.update((o.uuid, o._element) for o in objs)
return c.MixedElementList(self._model, list(found.values()))


class ElementValidation(Validation):
"""Provides access to the model's validation rules and results."""
"""Provides access to the element's validation rules and results."""

@property
def rules(self) -> list[Rule]:
Expand Down

0 comments on commit 4f68718

Please sign in to comment.