Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [XCORE] how to implement validation
[XCORE] how to implement validation [message #1253803] Sat, 22 February 2014 18:18 Go to next message
Roger Gilliar is currently offline Roger GilliarFriend
Messages: 40
Registered: March 2010
Member
Since I'm just starting out with emf and xcore I was wondering how to validate my model and add custom validation rules. For example how do I specify that a string field must not be empty and must contain at least 2 characters or that a numeric field must be in the range -127 to 128 ?

Would it be possible to configure the validation rules at runtime ?

Is the EMF validation framework the right place to look ? At first sight I would have expected some declarative approach to specify the constraints.

[Updated on: Sat, 22 February 2014 18:34]

Report message to a moderator

Re: [XCORE] how to implement validation [message #1254406 is a reply to message #1253803] Sun, 23 February 2014 10:32 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Roger,

I've not added anything special to Xcore to help generate the validator,
so while you can add @Ecore annotations to specify the named constraints:

http://help.eclipse.org/kepler/index.jsp?topic=/org.eclipse.emf.doc/references/overview/EMF.Validation.html

I.e., you can specify @Ecore(contraints="A B" on any classifier

The generated validator will have methods in which you can implement
your constraints, but you can't specify the constraint logic in
Xcore/Xbase yet (as you can do for derived features and operations).

Some of the simple constraints you mention can already be specified via
extended meta data annotations on the specialized EDataTypes because EMF
supports capturing all the XML Schema simple type facets as
annotations. So you might try specifying an XML Schema that captures
the intent of your constraints, import that as an Ecore/GenModel, and
export that to Xcore to see what these constraints look like as
@ExtendedMetadata annotations.

None of EMF's instrinsic validation support facilitates runtime
configuration. The extended validation framework supports such things:

http://www.eclipse.org/modeling/emf/?project=validation



On 22/02/2014 7:18 PM, Roger Gilliar wrote:
> Since I'm just starting out with emf and xcore I was wondering how to
> validate my model and add custom validation rules. For example how do
> I specify that a string field must not be empty and must contain at
> least 2 characters or that a numeric field must be in the range -127
> to 128 ?
>
> Would it be possible to configure the validation rules at runtime ?


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: [XCORE] how to implement validation [message #1256720 is a reply to message #1254406] Tue, 25 February 2014 18:34 Go to previous messageGo to next message
Roger Gilliar is currently offline Roger GilliarFriend
Messages: 40
Registered: March 2010
Member
I can still can figure out how to implement validation.

I tried:

@ExtendedMetaData(name="moduleNo",
baseType="http://www.eclipse.org/emf/2003/XMLType#integer",
minInclusive="0",
maxInclusive="65534")
unique Integer moduleNo

That gives me a comment at the interface but no code seems to be generated that does the check. I think I'm totally misunderstanding how validation is supposed to work in an EMF context. With Bean validation I would write something like

class Test {
@Min(0)
@Max(65534)
Integer moduleNo;
}

validator.validate(testObject);

But I have no idea how to do this with EMF.
Re: [XCORE] how to implement validation [message #1256761 is a reply to message #1256720] Tue, 25 February 2014 19:31 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Roger,

Comments below.

On 25/02/2014 7:35 PM, Roger Gilliar wrote:
> I can still can figure out how to implement validation.
> I tried:
>
> @ExtendedMetaData(name="moduleNo",
> baseType="http://www.eclipse.org/emf/2003/XMLType#integer",
> minInclusive="0",
> maxInclusive="65534")
> unique Integer moduleNo
>
> That gives me a comment at the interface but no code seems to be
> generated that does the check.
That's because it's all done in EObjectValidator based on the
annotations in the Ecore model.

Note however that I mentioned that such annotations are on a classifier,
but above you show it on a structural feature. I.e., it should be like this

@ExtendedMetaData
(baseType="http://www.eclipse.org/emf/2003/XMLType#integer",
minInclusive="0",
maxInclusive="65534")
type PositiveInt wraps int

class Example
{
PositiveInt feature
}

Think of it like in XML Schema, i.e., you define a new simple type with
facet constraints and then use that type for elements or attributes.
> I think I'm totally misunderstanding how validation is supposed to
> work in an EMF context. With Bean validation I would write something
> like
>
> class Test {
> @Min(0)
> @Max(65534)
> Integer moduleNo;
> }
>
> validator.validate(testObject);
Diagnostician.INSTANCE.validate(eObject) does the trick.
>
> But I have no idea how to do this with EMF.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: [XCORE] how to implement validation [message #1256787 is a reply to message #1256761] Tue, 25 February 2014 20:14 Go to previous messageGo to next message
Roger Gilliar is currently offline Roger GilliarFriend
Messages: 40
Registered: March 2010
Member
Cool,

it's working now.

BTW:
The following code causes an error:

@ExtendedMetaData
(baseType="http://www.eclipse.org/emf/2003/XMLType#integer",
...

but the following works:
(baseType="http://www.eclipse.org/emf/2003/XMLType#Integer",

The first one tries to create a BigInteger and the generated source does not compile.
Re: [XCORE] how to implement validation [message #1257151 is a reply to message #1256787] Wed, 26 February 2014 05:43 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Roger,

Comments below.

On 25/02/2014 9:14 PM, Roger Gilliar wrote:
> Cool,
>
> it's working now.
> BTW:
> The following code causes an error:
>
> @ExtendedMetaData
> (baseType="http://www.eclipse.org/emf/2003/XMLType#integer",
> ..
>
> but the following works:
> (baseType="http://www.eclipse.org/emf/2003/XMLType#Integer",
Sorry, I should have looked at the generated code to see it doesn't
compile.
>
> The first one tries to create a BigInteger and the generated source
> does not compile.
Yes you have to make sure that the wrapped type of what you extend and
the wrapped type you define match. You can leave out the baseType as
well; that annotation just ensures that you "inherit" the constraints of
the specified base type.


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Valid instances of models - interesting perspective
Next Topic:XCore to Ecore conversion problem with ECore references
Goto Forum:
  


Current Time: Wed Apr 24 20:28:02 GMT 2024

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

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

Back to the top