December 11, 2007

Validation of Models with Acceleo (Part 2)

The second one (Part 2) consists in creating Acceleo services.

This solution provides additionnal functionalities in relation to the first one. Indeed, as the rules are written in Java language, you can decide that a specific rule won't overload a generic one for example...

I've created a "ValidatorServices" class which implements a mechanism to validate a model and get the report from templates, thanks to the services that it provides. The end-user has just to extend this one and to implement the "checkRules" abstract protected method in order to define his own rules.
He can use "validate", "isErrorChecking" or "isWarningChecking" services to validate his model.

But the main new feature consists in giving the possibility to use the result of the validation report and to act on the generation or not. Let's interest by this feature:

The "isErrorChecking" and "isWarningChecking" are able to notify if an error/warning at least has been detected. So, "isErrorChecking" may be used for example to forbid the generation if it returns true.

The "ValidatorServices" class manages a "singleton" behaviour in order to have only one validation launch per generation and to optimize the process.
Moreover, it manages a workaround about Acceleo architecture limitation on the instanciating of custom services between templates...

Indeed, the services don't share the same context between each template. So, let's take an example with two templates where each of them generates a file. We knows that the generation depends on the "file" parameter. Consequently, it's in this place that you have to use "isExistingError" if you want to control the generation. Not to have as many validation launches as templates which generate files, it is required to keep report in memory. To do that, the "ValidatorServices" retrieve data from a temporary file, in the temporary directory of the OS (Java environment variable). You can use a custom action to delete this file (clean data context) at the end of the generation process.
IsXXXChecking use temporary file only if it is called in different templates which generate files.

The main benefit of this way is to be able to cancel the generation if the validation process has thrown errors or warnings... according to your rules.
Then, it provides a flexibility for the use of the validation mechanism (priority of rules, control generation...) thanks to the Java language. So, applying a rule on a "super" meta-class and an other one on a "child" meta-class, you can decide to display the both respective messages if these rules are not checked (and not only the "child" one).

The concern is the technical heaviness of the solution which might be managed, in a more transparent way, by the acceleo core.
Then, the use of Java language involves a loss of the Acceleo features (debugging, traceability...).