Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Constraints Using OCL (Dave S. please help me)
Constraints Using OCL (Dave S. please help me) [message #417908] Fri, 28 March 2008 14:23 Go to next message
Greg Fox is currently offline Greg FoxFriend
Messages: 39
Registered: July 2009
Member
(Dave S: I told you I would call you out in the forums. Ha! I am the guy
that sat next to you at the EMF table at EclipseCon.)

Is there a clear example of setting up a constraint using OCL.

I have added the following to my ecore model:

<eClassifiers xsi:type="ecore:EClass" name="MyThing"
eSuperTypes="#//MyBaseObject">
<eAnnotations source="http://www.eclipse.org/emf/2002/Ecore">
<details key="constraints" value="NameLengthTest"/>
</eAnnotations>
<eAnnotations source="http://www.eclipse.org/emf/2002/OCL">
<details key="NameLengthTest" value="name.size() &lt;20"/>
</eAnnotations>

This seems to generate a "validateMyThing_NameLengthTest" method which
requires hand coding. I was expecting that the OCL annotation would
remove the need for adding any code. Am I setting this up incorrectly?

Also, what is the trick to making these validations occur for each
transaction that attempts to update my EMF objects?

I was hoping that by setting up the constraint, the EMF object would never
be able to contain a value that violates that constraint. What am I
missing in this picture?

Thanks,
Greg
Re: Constraints Using OCL (Dave S. please help me) [message #417912 is a reply to message #417908] Fri, 28 March 2008 14:43 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------070006030809090406070202
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Greg,

I have Dave locked away until the book is done. :-P

Likely this article will be useful

http://www.eclipse.org/articles/article.php?file=Article-EMF -Codegen-with-OCL/index.html

I'm sure that the transaction framework validated constraints and rolls
back if there are violations. But I'm not sure you need to take
explicit action for that to happen. Christian can provide some details
when he has some time... The pressures of M6 are keeping us busy...


Greg Fox wrote:
> (Dave S: I told you I would call you out in the forums. Ha! I am the
> guy that sat next to you at the EMF table at EclipseCon.)
>
> Is there a clear example of setting up a constraint using OCL.
>
> I have added the following to my ecore model:
>
> <eClassifiers xsi:type="ecore:EClass" name="MyThing"
> eSuperTypes="#//MyBaseObject">
> <eAnnotations source="http://www.eclipse.org/emf/2002/Ecore">
> <details key="constraints" value="NameLengthTest"/>
> </eAnnotations>
> <eAnnotations source="http://www.eclipse.org/emf/2002/OCL">
> <details key="NameLengthTest" value="name.size() &lt;20"/>
> </eAnnotations>
>
> This seems to generate a "validateMyThing_NameLengthTest" method which
> requires hand coding. I was expecting that the OCL annotation would
> remove the need for adding any code. Am I setting this up incorrectly?
>
> Also, what is the trick to making these validations occur for each
> transaction that attempts to update my EMF objects?
> I was hoping that by setting up the constraint, the EMF object would
> never be able to contain a value that violates that constraint. What
> am I missing in this picture?
>
> Thanks,
> Greg
>


--------------070006030809090406070202
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Greg,<br>
<br>
I have Dave locked away until the book is done.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Constraints Using OCL (Dave S. please help me) [message #417916 is a reply to message #417912] Fri, 28 March 2008 16:13 Go to previous messageGo to next message
Greg Fox is currently offline Greg FoxFriend
Messages: 39
Registered: July 2009
Member
Thanks Ed! Please at least let Dave have bread and water. He was already
looking a little thin ;)

I actually had used that article when I was trying to figure this out but
I was a little confused. It appeared that the article was using
customized templates to cause the OCL to work.

The annotation that they added to Employee.ecore for the OCL was:

<eAnnotations source="http://www.eclipse.org/ocl/examples/OCL">
<details key="deptHasEmployees" value="not manager.oclIsUndefined()
implies employees->notEmpty()" />
<details key="positiveID" value="self.deptID > 0" />
<details key="validName" value="not self.name.oclIsUndefined() and
self.name <> ''" />
</eAnnotations>

Note that the source is specific to the example
"http://www.eclipse.org/ocl/examples/OCL".

I used ""http://www.eclipse.org/emf/2002/OCL"", but perhaps this is wrong.
Re: Constraints Using OCL (Dave S. please help me) [message #417917 is a reply to message #417916] Fri, 28 March 2008 16:35 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Greg,

A strict diet is just one of the added bonus. If you review the
literature carefully, under-nutrition is the only proven way to extend
life expectancy. And since the book is taking so long, that's starting
to seem important. So we don't feed him much, but we give him vitamin
pills. He can have all the water needs though. :-P

I think it's important to match the namespace used by the example since
I think that's how the templates find the annotations...

Of course I'd love to find time to be able to add hooks so that all this
kind of cool stuff could be supported via extension points, but good
ideas are cheap, while execution is difficult...


Greg Fox wrote:
> Thanks Ed! Please at least let Dave have bread and water. He was
> already looking a little thin ;)
>
> I actually had used that article when I was trying to figure this out
> but I was a little confused. It appeared that the article was using
> customized templates to cause the OCL to work.
>
> The annotation that they added to Employee.ecore for the OCL was:
>
> <eAnnotations source="http://www.eclipse.org/ocl/examples/OCL">
> <details key="deptHasEmployees" value="not manager.oclIsUndefined()
> implies employees->notEmpty()" /> <details key="positiveID"
> value="self.deptID > 0" /> <details key="validName" value="not
> self.name.oclIsUndefined() and self.name <> ''" /> </eAnnotations>
>
> Note that the source is specific to the example
> "http://www.eclipse.org/ocl/examples/OCL".
>
> I used ""http://www.eclipse.org/emf/2002/OCL"", but perhaps this is
> wrong.
>
>
>
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Constraints Using OCL (Dave S. please help me) [message #417919 is a reply to message #417912] Fri, 28 March 2008 17:25 Go to previous messageGo to next message
Greg Fox is currently offline Greg FoxFriend
Messages: 39
Registered: July 2009
Member
Thanks Ed. I didn't realize it was not implemented in EMF itself yet. I
pointed at the templates that came from the example and it generates fine
now.
Re: Constraints Using OCL (Dave S. please help me) [message #417923 is a reply to message #417919] Fri, 28 March 2008 17:59 Go to previous messageGo to next message
Greg Fox is currently offline Greg FoxFriend
Messages: 39
Registered: July 2009
Member
So what is the key to getting these validations to be called automatically
for a transaction? Is there a recommended way to make this happen?
(Note: I am not using the generated Editor)

Also, are contraints defined in this way related to the EMF Validation
framework? I see in the EMF Validation help that they specify contraints
in the plugin.xml using OCL.
Re: Constraints Using OCL (Dave S. please help me) [message #417929 is a reply to message #417923] Fri, 28 March 2008 22:01 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cdamus.ca.ibm.com

Hi, Greg,

The article to which Ed referred you uses EMF's intrinsic validation
framework. This has no relation to the (extensible) EMF Validation
Framework component, except inasmuch as the latter defines a "constraint
language" extension that bridges to the invariant operations in your
EMF-generated model.

There are, also, more ways than plugin.xml by which you can specify
constraints. In fact, this is a default constraint provider implementation
that doesn't scale very well to large numbers of constraints (it rather
overwhelms Eclipse's extension registry). A more flexible approach
involves the definition of dynamic constraint providers, of which an
example is provided in the org.eclipse.emf.validation.examples.ocl plug-in
that loads constraints from *.ocl text files.

Getting back to your original question, the constraint meta-data related to
triggering (live mode vs. batch mode, events, etc.) can possibly be
specified using annotations in your Ecore model. None of the SDK examples
or articles on Eclipse.org goes into this.

HTH,

Christian


Greg Fox wrote:

> So what is the key to getting these validations to be called automatically
> for a transaction? Is there a recommended way to make this happen?
> (Note: I am not using the generated Editor)
>
> Also, are contraints defined in this way related to the EMF Validation
> framework? I see in the EMF Validation help that they specify contraints
> in the plugin.xml using OCL.
Re: Constraints Using OCL (Dave S. please help me) [message #417930 is a reply to message #417917] Fri, 28 March 2008 22:02 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: cdamus.ca.ibm.com

Hi, Ed, Greg,

Note that the namespace URI used by my article deliberate is an "examples"
namespace because the article doesn't purport to be an authoritative or
complete implementation. There is much more that could be done to improve
the efficiency and organization of the code for "production readiness."

Cheers,

Christian

Ed Merks wrote:

> Greg,
>
> A strict diet is just one of the added bonus. If you review the
> literature carefully, under-nutrition is the only proven way to extend
> life expectancy. And since the book is taking so long, that's starting
> to seem important. So we don't feed him much, but we give him vitamin
> pills. He can have all the water needs though. :-P
>
> I think it's important to match the namespace used by the example since
> I think that's how the templates find the annotations...
>
> Of course I'd love to find time to be able to add hooks so that all this
> kind of cool stuff could be supported via extension points, but good
> ideas are cheap, while execution is difficult...
>

-----8<-----
Re: Constraints Using OCL (Dave S. please help me) [message #422532 is a reply to message #417929] Mon, 08 September 2008 16:56 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ccoslet.gmail.com

Dear Christian, dear all

> There are, also, more ways than plugin.xml by which you can specify
> constraints. In fact, this is a default constraint provider implementation
> that doesn't scale very well to large numbers of constraints (it rather
> overwhelms Eclipse's extension registry). A more flexible approach
> involves the definition of dynamic constraint providers, of which an
> example is provided in the org.eclipse.emf.validation.examples.ocl plug-in
> that loads constraints from *.ocl text files.

I have a question about this since it looks very complicated and I'm not
sure whether this solution will do what I need. See, normally
constraints and invariants should be kept with the model, and instead of
storing them as annotations in natural language (which would mean I have
to write the same thing twice, once in natural language in the model and
once in Java in the generated code), it would be nice if I could do
something like the article you posted previously, just defining
constraints and implementing them in some sort of inline OCL, all in one
file.

I have seven models to implement and I do not want to do all the things
described in the article for each of them since as far as I can see
there will be a plugin for that in the future anyway and after that it
will be harder to remove the changes I made. So is there any way,
without doing anything to plugin.xml or something, only with Eclipse out
of the box, to put constraints together with the model, or at least in
some OCL file and referencing it using an annotation?

> Getting back to your original question, the constraint meta-data related to
> triggering (live mode vs. batch mode, events, etc.) can possibly be
> specified using annotations in your Ecore model. None of the SDK examples
> or articles on Eclipse.org goes into this.

Yeah...

Best regards
Cristina
Re: Constraints Using OCL (Dave S. please help me) [message #422537 is a reply to message #422532] Mon, 08 September 2008 19:24 Go to previous message
Eclipse UserFriend
Originally posted by: cdamus.zeligsoft.com

Hi, Cristina,

I expect that it would be relatively straight-forward to implement a
dynamic constraint provider that looks for EAnnotations in the Ecore
model (probably much like the invariant-costraint annotations in the
article) and parses them on-the-fly (probably with caching).

Like you, I am a firm believer in defining the constraints in the model;
it isn't complete without them. However, sometimes a specialized use of
a model needs to add more (domain-specific) constraints, which is the
reason for the extensibility offered by the validation component.

Starting with the example that reads constraints from the *.ocl files,
it should not require much effort to adapt it to finding and parsing
constraints in annotations on the EClasses in your Ecore model. The
major difference is that the OCLHelper API will be more convenient for
parsing these constraints, because you won't have the constraint-context
syntax; the context is implicitly the EClass that owns the annotation.

Follow up on the eclipse.modeling.mdl.ocl newsgroup, if you have
specific questions about working with the OCL parsing and evaluation API.

HTH,

Christian

Cristina Coslet wrote:
> Dear Christian, dear all
>
>> There are, also, more ways than plugin.xml by which you can specify
>> constraints. In fact, this is a default constraint provider
>> implementation
>> that doesn't scale very well to large numbers of constraints (it rather
>> overwhelms Eclipse's extension registry). A more flexible approach
>> involves the definition of dynamic constraint providers, of which an
>> example is provided in the org.eclipse.emf.validation.examples.ocl
>> plug-in
>> that loads constraints from *.ocl text files.
>
> I have a question about this since it looks very complicated and I'm not
> sure whether this solution will do what I need. See, normally
> constraints and invariants should be kept with the model, and instead of
> storing them as annotations in natural language (which would mean I have
> to write the same thing twice, once in natural language in the model and
> once in Java in the generated code), it would be nice if I could do
> something like the article you posted previously, just defining
> constraints and implementing them in some sort of inline OCL, all in one
> file.
>
> I have seven models to implement and I do not want to do all the things
> described in the article for each of them since as far as I can see
> there will be a plugin for that in the future anyway and after that it
> will be harder to remove the changes I made. So is there any way,
> without doing anything to plugin.xml or something, only with Eclipse out
> of the box, to put constraints together with the model, or at least in
> some OCL file and referencing it using an annotation?
>
>> Getting back to your original question, the constraint meta-data
>> related to
>> triggering (live mode vs. batch mode, events, etc.) can possibly be
>> specified using annotations in your Ecore model. None of the SDK
>> examples
>> or articles on Eclipse.org goes into this.
>
> Yeah...
>
> Best regards
> Cristina
Previous Topic:Problem wrapping OCL result in SimpleAnyType
Next Topic:Should reference to EDataType be containment or not?
Goto Forum:
  


Current Time: Fri Apr 26 14:29:08 GMT 2024

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

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

Back to the top