From 4f687186350263314e9db959b3fc972718e6f808 Mon Sep 17 00:00:00 2001 From: Martin Lehmann Date: Thu, 11 Jul 2024 10:50:27 +0200 Subject: [PATCH] feat(validation): Enable validation of an entire layer --- capellambse/extensions/validation/__init__.py | 6 ++++ .../extensions/validation/_validate.py | 34 ++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/capellambse/extensions/validation/__init__.py b/capellambse/extensions/validation/__init__.py index 60800915..2167febc 100644 --- a/capellambse/extensions/validation/__init__.py +++ b/capellambse/extensions/validation/__init__.py @@ -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, @@ -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), + ) diff --git a/capellambse/extensions/validation/_validate.py b/capellambse/extensions/validation/_validate.py index c975f9ad..a38fcc49 100644 --- a/capellambse/extensions/validation/_validate.py +++ b/capellambse/extensions/validation/_validate.py @@ -5,6 +5,7 @@ __all__ = [ "Category", "ElementValidation", + "LayerValidation", "ModelValidation", "RealType", "Result", @@ -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]: