Validating a Model through Java [message #1746818] |
Sat, 05 November 2016 00:41 |
Eclipse User |
|
|
|
Hello,
I have a metamodel and OCL constraints are defined inside this metamodel using OCLinECore. What I want to achieve is, to write a Java program, that reads the model file and the metamodel; then validates the OCL constraints and if there are any violations, the user should be informed through a standard console output.
I have tried to understand the document http://download.eclipse.org/ocl/doc/5.0.0/ocl.pdf but I am not sure what I am looking for is there. I have generated the Java code of the metamodel by enabling "Operation Reflection". The validator class is generated but one needs to get all the EClass instances in the model and validate each instance through related Java method for validation. What I am looking for is a method that is behind the "Validate" button in the validation view of Eclipse that validates all the OCL constraints together.
Could you please help me with this issue?
|
|
|
|
Re: Validating a Model through Java [message #1746834 is a reply to message #1746822] |
Sat, 05 November 2016 11:18 |
Eclipse User |
|
|
|
Sorry Ed, I don't get it well. I don't know what you refer by "default validation", "OCL interpreter being slow", "OCL2Java code generator". I already stated what I want to achieve, let me describe the steps that I followed:
- I have defined a metamodel using OCLinECore editor. So an example entity from this metamodel is as follows:
/*
* OCL Constraint: NoNullName
* The name attribute can't be null.
*
*/
abstract class Nameable { interface }
{
attribute name : String[1];
attribute description : String[?];
invariant NoNullName:
self.name <> null and self.name.size() > 0;
}
- I have generated genmodel of this metamodel and enabled "Operation Reflection" option of the genmodel.
- Then, I have generated the model code from this genmodel. I have one automatically generated class now that extends EObjectValidator. This class has all methods for validation, that validates both the structural properties of an ECore model and also OCL constraints. An example method is
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public boolean validateNameable(Nameable nameable, DiagnosticChain diagnostics, Map<Object, Object> context) {
if (!validate_NoCircularContainment(nameable, diagnostics, context)) return false;
boolean result = validate_EveryMultiplicityConforms(nameable, diagnostics, context);
if (result || diagnostics != null) result &= validate_EveryDataValueConforms(nameable, diagnostics, context);
if (result || diagnostics != null) result &= validate_EveryReferenceIsContained(nameable, diagnostics, context);
if (result || diagnostics != null) result &= validate_EveryBidirectionalReferenceIsPaired(nameable, diagnostics, context);
if (result || diagnostics != null) result &= validate_EveryProxyResolves(nameable, diagnostics, context);
if (result || diagnostics != null) result &= validate_UniqueID(nameable, diagnostics, context);
if (result || diagnostics != null) result &= validate_EveryKeyUnique(nameable, diagnostics, context);
if (result || diagnostics != null) result &= validate_EveryMapEntryUnique(nameable, diagnostics, context);
if (result || diagnostics != null) result &= validateNameable_NoNullName(nameable, diagnostics, context);
return result;
}
If you want to use this class this validation, you have to write some code that takes very element in your model and uses these methods for validation. Also, one needs to write their custom validation messages.
Is there any easier way like a validate method that can run only on the root of your model that traces all your model elements and returns the list of violations with default messages?
|
|
|
|
Powered by
FUDForum. Page generated in 0.02785 seconds