Skip to main content



      Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » Custom validation
Custom validation [message #58097] Wed, 18 October 2006 23:45 Go to next message
Eclipse UserFriend
Originally posted by: hocnt.cybersoft-vn.com

Dear all,

I'm using EMFT validation framework in my project. New constraints are
declared in the plugin.xml and it requires a validation class which
extends org.eclipse.emf.validation.AbstractModelConstraint. This class
is where I write my validation code. The method is

public IStatus validate(IValidationContext ctx) {}

I get the object being validated by calling ctx.getTarget().

Sometime, this is a formula object which has an expression, something
like (a * b + c), where a, b and c are name of other objects. I can
parse the expression to get the list of objects composing the
expression. Now I want to validate also these objects.

To validate a, b and c here, I write an util class as follow:

public class ModelDomainValidationUtil {

public synchronized static boolean validate(ModelDomain modelDomain){
EValidatorAdapter validateAdapt = new EValidatorAdapter(){
@Override
public synchronized boolean validate(EClass eClass, EObject eObject,
DiagnosticChain diagnostics, Map context) {
IBatchValidator batchValidator;
batchValidator =
(IBatchValidator) ModelValidationService.getInstance().newValidator(
EvaluationMode.BATCH);
batchValidator.setIncludeLiveConstraints(true);
batchValidator.setReportSuccesses(false);
IStatus status = Status.OK_STATUS;
status = batchValidator.validate(eObject,
new NullProgressMonitor() );
return status.isOK();
}
};

return validateAdapt.validate(modelDomain.eClass(), modelDomain, null,
null);
}

}

then, from within my "public IStatus validate(IValidationContext ctx)
{}" above, I call

ModelDomainValidationUtil.validate(a);
ModelDomainValidationUtil.validate(b);
ModelDomainValidationUtil.validate(c);

to validate a, b and c.

It is ok, if there is no reference cycle, e.g. a refers to b, b refers
to c, and c refers to a, a refers to b, etc.

To resolve this issue, I expect to be able to validate an object and
also specify a list of excluded objects. Something like:

validate(eObject, excludedList)

if eObject refers to any object in the excluded list, it won't validate
that object. The excluded list here contains a list of objects which
have been already validated. I think this can resolve the reference cycle.

I don't see how I can do that.

So my question is:
1. Is the ModelDomainValidationUtil class above a proper way to call
validation in code? If not, what is the alternative?

2. How to validate an object with some additional conditions?

Any idea will be appreciated.

Regards,
Hoc Nguyen
Re: Custom validation [message #58278 is a reply to message #58097] Thu, 19 October 2006 10:12 Go to previous message
Eclipse UserFriend
Originally posted by: cdamus.ca.ibm.com

Hi, Hoc,

To answer question (1), first: the ModelValidationService is not intended
to be called re-entrantly by constraints. I'm not sure whether that will
cause you problems. In any case, wrapping the invocation of the
ModelValidationService in an EValidatorAdapter seems superfluous. Why not
just call it directly?

Regarding question (2): the IValidationContext has putConstraintData() and
getConstraintData() methods that are intended for a constraint to store any
cache of information that it needs. The constraint that checks the formula
and the objects related to it can use this to store (e.g.) the set of
objects that it has visited. Any object that it is called upon to validate
that is already in the set, it skips. Also, to ensure that a, b, and c are
validated when the formula is, you can define a custom ITraversalStrategy
and register it against your metamodel on the
org.eclipse.emf.validation.traversalStrategies extension point. The
traversal strategy is responsible for the iteration of the model during a
batch validation. This might eliminate the need for this recursive
invocation of the ModelValidationService.

What may be of help, also, in reporting multiple problems (on related
objects) from a single constraint is
https://bugs.eclipse.org/bugs/show_bug.cgi?id=161558.

Cheers,

Christian


Hoc Nguyen wrote:

> Dear all,
>
> I'm using EMFT validation framework in my project. New constraints are
> declared in the plugin.xml and it requires a validation class which
> extends org.eclipse.emf.validation.AbstractModelConstraint. This class
> is where I write my validation code. The method is
>
> public IStatus validate(IValidationContext ctx) {}
>
> I get the object being validated by calling ctx.getTarget().
>
> Sometime, this is a formula object which has an expression, something
> like (a * b + c), where a, b and c are name of other objects. I can
> parse the expression to get the list of objects composing the
> expression. Now I want to validate also these objects.
>
> To validate a, b and c here, I write an util class as follow:
>
> public class ModelDomainValidationUtil {
>
> public synchronized static boolean validate(ModelDomain modelDomain){
> EValidatorAdapter validateAdapt = new EValidatorAdapter(){
> @Override
> public synchronized boolean validate(EClass eClass, EObject eObject,
> DiagnosticChain diagnostics, Map context) {
> IBatchValidator batchValidator;
> batchValidator =
> (IBatchValidator) ModelValidationService.getInstance().newValidator(
> EvaluationMode.BATCH);
> batchValidator.setIncludeLiveConstraints(true);
> batchValidator.setReportSuccesses(false);
> IStatus status = Status.OK_STATUS;
> status = batchValidator.validate(eObject,
> new NullProgressMonitor() );
> return status.isOK();
> }
> };
>
> return validateAdapt.validate(modelDomain.eClass(), modelDomain, null,
> null);
> }
>
> }
>
> then, from within my "public IStatus validate(IValidationContext ctx)
> {}" above, I call
>
> ModelDomainValidationUtil.validate(a);
> ModelDomainValidationUtil.validate(b);
> ModelDomainValidationUtil.validate(c);
>
> to validate a, b and c.
>
> It is ok, if there is no reference cycle, e.g. a refers to b, b refers
> to c, and c refers to a, a refers to b, etc.
>
> To resolve this issue, I expect to be able to validate an object and
> also specify a list of excluded objects. Something like:
>
> validate(eObject, excludedList)
>
> if eObject refers to any object in the excluded list, it won't validate
> that object. The excluded list here contains a list of objects which
> have been already validated. I think this can resolve the reference cycle.
>
> I don't see how I can do that.
>
> So my question is:
> 1. Is the ModelDomainValidationUtil class above a proper way to call
> validation in code? If not, what is the alternative?
>
> 2. How to validate an object with some additional conditions?
>
> Any idea will be appreciated.
>
> Regards,
> Hoc Nguyen
Re: Custom validation [message #594387 is a reply to message #58097] Thu, 19 October 2006 10:12 Go to previous message
Eclipse UserFriend
Originally posted by: cdamus.ca.ibm.com

Hi, Hoc,

To answer question (1), first: the ModelValidationService is not intended
to be called re-entrantly by constraints. I'm not sure whether that will
cause you problems. In any case, wrapping the invocation of the
ModelValidationService in an EValidatorAdapter seems superfluous. Why not
just call it directly?

Regarding question (2): the IValidationContext has putConstraintData() and
getConstraintData() methods that are intended for a constraint to store any
cache of information that it needs. The constraint that checks the formula
and the objects related to it can use this to store (e.g.) the set of
objects that it has visited. Any object that it is called upon to validate
that is already in the set, it skips. Also, to ensure that a, b, and c are
validated when the formula is, you can define a custom ITraversalStrategy
and register it against your metamodel on the
org.eclipse.emf.validation.traversalStrategies extension point. The
traversal strategy is responsible for the iteration of the model during a
batch validation. This might eliminate the need for this recursive
invocation of the ModelValidationService.

What may be of help, also, in reporting multiple problems (on related
objects) from a single constraint is
https://bugs.eclipse.org/bugs/show_bug.cgi?id=161558

Cheers,

Christian


Hoc Nguyen wrote:

> Dear all,
>
> I'm using EMFT validation framework in my project. New constraints are
> declared in the plugin.xml and it requires a validation class which
> extends org.eclipse.emf.validation.AbstractModelConstraint. This class
> is where I write my validation code. The method is
>
> public IStatus validate(IValidationContext ctx) {}
>
> I get the object being validated by calling ctx.getTarget().
>
> Sometime, this is a formula object which has an expression, something
> like (a * b + c), where a, b and c are name of other objects. I can
> parse the expression to get the list of objects composing the
> expression. Now I want to validate also these objects.
>
> To validate a, b and c here, I write an util class as follow:
>
> public class ModelDomainValidationUtil {
>
> public synchronized static boolean validate(ModelDomain modelDomain){
> EValidatorAdapter validateAdapt = new EValidatorAdapter(){
> @Override
> public synchronized boolean validate(EClass eClass, EObject eObject,
> DiagnosticChain diagnostics, Map context) {
> IBatchValidator batchValidator;
> batchValidator =
> (IBatchValidator) ModelValidationService.getInstance().newValidator(
> EvaluationMode.BATCH);
> batchValidator.setIncludeLiveConstraints(true);
> batchValidator.setReportSuccesses(false);
> IStatus status = Status.OK_STATUS;
> status = batchValidator.validate(eObject,
> new NullProgressMonitor() );
> return status.isOK();
> }
> };
>
> return validateAdapt.validate(modelDomain.eClass(), modelDomain, null,
> null);
> }
>
> }
>
> then, from within my "public IStatus validate(IValidationContext ctx)
> {}" above, I call
>
> ModelDomainValidationUtil.validate(a);
> ModelDomainValidationUtil.validate(b);
> ModelDomainValidationUtil.validate(c);
>
> to validate a, b and c.
>
> It is ok, if there is no reference cycle, e.g. a refers to b, b refers
> to c, and c refers to a, a refers to b, etc.
>
> To resolve this issue, I expect to be able to validate an object and
> also specify a list of excluded objects. Something like:
>
> validate(eObject, excludedList)
>
> if eObject refers to any object in the excluded list, it won't validate
> that object. The excluded list here contains a list of objects which
> have been already validated. I think this can resolve the reference cycle.
>
> I don't see how I can do that.
>
> So my question is:
> 1. Is the ModelDomainValidationUtil class above a proper way to call
> validation in code? If not, what is the alternative?
>
> 2. How to validate an object with some additional conditions?
>
> Any idea will be appreciated.
>
> Regards,
> Hoc Nguyen
Previous Topic:EMFT Transaction 1.0.1: Cannot modify resource set without a write transaction
Next Topic:Teneo : persistence of objects URI
Goto Forum:
  


Current Time: Fri May 23 17:34:39 EDT 2025

Powered by FUDForum. Page generated in 0.03466 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top