Model Validation

AMALTHEA uses for custom model validations the Sphinx validation project which is based on EMF validation .

Understanding Check Catalogs

A Sphinx validator optionally makes use of a Catalog. When a catalog is used, each @Check annotations should have a constraint name (which can be seen as unique id) to match a constraint in the catalog. So there is a logical mapping from methods to constraints. For a constraint to be applicable within the scope of a validator, the set of categories specified in its @Check annotation should be a subset of the set of categories referenced by the constraint in the check catalog. In other words, categories are used to restrict applicability to check methods.

Following catalog is going to demonstrate the relationship between categories and constraint.

Except for constraint check 5 each other constraint is assigned to at least one category. Constraints check 4 and check 5 are linked to multiple categories.

Hint: The assignment from constraint to one or more categories could also be done in the @Check annotation itself.

Adding own Validations

Adding an own validation to the AMALTHEA framework can be done by the following steps:

Plugin dependencies

org.eclipse.core.runtime,
org.eclipse.sphinx.emf,
org.eclipse.sphinx.emf.check,
org.eclipse.app4mc.amalthea.model

Implementation

Create a class which is extending org.eclipse.sphinx.emf.check.AbstractCheckValidator and implement a method of arbitrary name and tag it with the check annotation. At this point it is important to know that at least one parameter is needed for the check method. For instance, if you want to perform a check related to the whole AMALTHEA model you should consider org.eclipse.app4mc.amalthea.model.Amalthea as parameter type. An empty parameter list will never activate the check for your AMALTHEA model. This raises the capability of an so called out-of-the-box scoping which means that the validation checks only appears on the relevant model elements
(e.g. validations related to the AMALTHEA software model may only appear when right-clicking on an appropriate model element of type Software)

import org.eclipse.sphinx.emf.check.AbstractCheckValidator;
import org.eclipse.sphinx.emf.check.Check;
import org.eclipse.app4mc.amalthea.model.Amalthea;


public class BasicExampleConstraint extends AbstractCheckValidator {

	/**
	 *
	 * constraint: 
	 *   Must match at least to one existing constraint from the check catalog
	 *
	 * categories: 
	 *   Is of type array, thus constraint can be assigned to multiple categories
	 *
	 */
	@Check(constraint = "BasicExampleConstraint", categories = { "BasicCategory", ...and many more ... })
	void checkVeryImportantConstraint(final Amalthea ama)
	{

		/**
		 * Perform your check
		 */

		error("Error occurs", ama, ama.eContainingFeature());
		warning("Warning occurs", ama, ama.eContainingFeature());
		info("Info occurs", ama, ama.eContainingFeature());
		// Error type information is taken directly from catalog
		issue(ama, ama.eContainingFeature()); 
	}
}

Create your own class and insert at least one method extended by the check Sphinx annotation. The method should contain exactly one parameter of at least the type of the most generic model element of the AMALTHEA model (i.e. org.eclipse.app4mc.amalthea.model.Amalthea).
Implement the validate method by adding your own custom validation.

Plugin configuration

Add an extension point called org.eclipse.sphinx.emf.check.checkvalidators and for each implemented constraint a child entry for your class to the plugin.xml to register it in the Sphinx Validation framework. For each child entry you must specify your constraint class (including @Check annotated methods) and optionally a catalog which contains more detailed information about the composition and relationship between constraints.

The current configuration of the AMALTHEA model validations can be found in the plugin org.eclipse.app4mc.amalthea.validation in the plugin.xml.

There the constraints section includes the list of available validations and their corresponding implementation class.