Validating Aspect Models
Aspect Models are validated using the AspectModelValidator. Validation returns a list of
Violation`s. A violation has a human-readable message and a unique error code and provides access
to the `EvaluationContext which contains references to the model element that caused the
violation and the SHACL shape that triggered the violation.
Each possible type of violation is a subtype of the Violation interface and provides additional
context information specific to this type, for example, the MinLengthViolation provides int min
(the allowed length) and int actual (the length that was encountered in the model).
Consider the following example:
Show used imports
import java.io.File;
import org.eclipse.esmf.aspectmodel.loader.AspectModelLoader;
import org.eclipse.esmf.aspectmodel.Violation;
import org.eclipse.esmf.aspectmodel.ViolationReport;
import org.eclipse.esmf.aspectmodel.validation.services.AspectModelValidator;
import org.eclipse.esmf.aspectmodel.validation.services.DetailedViolationFormatter;
import org.eclipse.esmf.aspectmodel.validation.services.ViolationFormatter;
import org.eclipse.esmf.metamodel.AspectModel;
// AspectModel as returned by the AspectModelLoader
final AspectModel aspectModel = // ...
| 1 | To format the validation result into an human-readable form, use the ViolationFormatter or the
DetailedViolationFormatter. Note that those are only intended for text-based interfaces such as
CLIs. |
The AspectModelValidator also provides the loadModel(Supplier<AspectModel>) method that can be used in
conjunction with () → new AspectModeLoader().load(…) to have exceptions that might be created during the
Aspect Model loading process, for example due to model resolution failures or syntax errors in the input
files, be turned into a List<Violation> that can be passed to the aforementioned violation formatters. This
allows for custom structured handling of problems that occur during model loading.
To include the model validator, use the following dependencies:
- Maven
-
<dependency> <groupId>org.eclipse.esmf</groupId> <artifactId>esmf-aspect-model-validator</artifactId> <version>2.16.0</version> </dependency> - Gradle Groovy DSL
-
implementation 'org.eclipse.esmf:esmf-aspect-model-validator:2.16.0' - Gradle Kotlin DSL
-
implementation("org.eclipse.esmf:esmf-aspect-model-validator:2.16.0")
Programmatic Access
When using the Java API, errors are available through the Violation interface:
Show used imports
import java.io.File;
import org.eclipse.esmf.aspectmodel.loader.AspectModelLoader;
import org.eclipse.esmf.aspectmodel.Violation;
import org.eclipse.esmf.aspectmodel.ViolationReport;
import org.eclipse.esmf.aspectmodel.validation.services.AspectModelValidator;
import org.eclipse.esmf.aspectmodel.validation.services.DetailedViolationFormatter;
import org.eclipse.esmf.aspectmodel.validation.services.ViolationFormatter;
import org.eclipse.esmf.metamodel.AspectModel;