Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Defining OCL-Constraints on user EDataTypes
Defining OCL-Constraints on user EDataTypes [message #548753] Thu, 22 July 2010 21:01 Go to next message
Oliver is currently offline OliverFriend
Messages: 13
Registered: January 2010
Location: Germany
Junior Member
Hi,

the new OCL delegates are very cool and usefull!
I'am interested in the OCL validation delgate for user defined EDataTypes.

For example, I wan't to have an EDataType that represents a normal String with a maximum length of 30 characters:
EDataType {
  name := ThirtyCharString
  instanceTypeName := java.lang.String
  serializable := true
  eAnnotations += EAnnotation {
    source := http://www.eclipse.org/emf/2002/Ecore
    details += EStringToStringMapEntry {
      key := constraints
      value := maxLengthConstraint
    }
  }
  eAnnotations += EAnnotation {
    source := http://www.eclipse.org/emf/2002/Ecore/OCL
    details += EStringToStringMapEntry {
      key := maxLengthConstraint
      value := ???.oclIsUndefined() or ???.size() <= 30
    }
  }
}


Is this possible or is it necessary to define on each EAttribute that uses the EDataType a constraint on it?

(btw. is there a naming convention for constraints? should constraint names start with upper or lower case? etc.)

ciao Olli
Re: Defining OCL-Constraints on user EDataTypes [message #548761 is a reply to message #548753] Thu, 22 July 2010 21:36 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Olivier,

Comments below.

Oliver wrote:
> Hi,
>
> the new OCL delegates are very cool and usefull!
> I'am interested in the OCL validation delgate for user defined
> EDataTypes.
>
> For example, I wan't to have an EDataType that represents a normal
> String with a maximum length of 30 characters:
> EDataType {
> name := ThirtyCharString
> instanceTypeName := java.lang.String
> serializable := true
> eAnnotations += EAnnotation {
> source := http://www.eclipse.org/emf/2002/Ecore
> details += EStringToStringMapEntry {
> key := constraints
> value := maxLengthConstraint
> }
> }
> eAnnotations += EAnnotation {
> source := http://www.eclipse.org/emf/2002/Ecore/OCL
> details += EStringToStringMapEntry {
> key := maxLengthConstraint
> value := ???.oclIsUndefined() or ???.size() <= 30
> }
> }
> }
>
> Is this possible or is it necessary to define on each EAttribute that
> uses the EDataType a constraint on it?
Yes, you can define constraints directly on EDataTypes.
>
> (btw. is there a naming convention for constraints? should constraint
> names start with upper or lower case? etc.)
They should start with an upper case letter and use Camel case.
>
> ciao Olli


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Defining OCL-Constraints on user EDataTypes [message #548769 is a reply to message #548753] Thu, 22 July 2010 22:40 Go to previous messageGo to next message
Oliver is currently offline OliverFriend
Messages: 13
Registered: January 2010
Location: Germany
Junior Member
Thanks Ed!
I've found out "???" have to replaced by "self" so a valid EDataType is:
EDataType {
  name := ThirtyCharString
  instanceTypeName := java.lang.String
  serializable := true
  eAnnotations += EAnnotation {
    source := http://www.eclipse.org/emf/2002/Ecore
    details += EStringToStringMapEntry {
      key := constraints
      value := MaxLengthConstraint
    }
  }
  eAnnotations += EAnnotation {
    source := http://www.eclipse.org/emf/2002/Ecore/OCL
    details += EStringToStringMapEntry {
      key := MaxLengthConstraint
      value := self.oclIsUndefined() or self.size() <= 30
    }
  }
}


I had never expected that this is possible, but it works Smile

Ciao Olli
Re: Defining OCL-Constraints on user EDataTypes [message #548799 is a reply to message #548769] Fri, 23 July 2010 06:33 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi Olli

I've no idea how you think this works.

a) self.oclIsUndefined() is always false for EDataType since by
definition it exists, unless you've overridden it, which is not
necessarily permitted in OCL at present (<= 2.2) and not supported in
MDT/OCL 3.0.0.

b) the java.lang.String is not visible to OCL, so there is no
self.size() operation.

As well as leading uppercase camelcase it is good to be readable for
polarity, so LessThanThirtyCharacters or NotTooLong or LengthOk would be
better.

If you use the OCLinEcore editor
(http://wiki.eclipse.org/MDT/OCLinEcore) you don't have to worry about
the EAnnotation scaffolding and you get OCL syntax validation:

package Example : ex = 'http://example'
{
datatype ThirtyCharString : 'java.lang.String'
{
invariant MaxLengthConstraint: oclIsUndefined() or self.size() <= 30;
}
}

Unfortunately, while checking your example I discovered that,
https://bugs.eclipse.org/bugs/show_bug.cgi?id=320689, the editor does
not currently consider OclAny methods for DataTypes or Enums, so you
will get an erroneous red squiggle on oclIsUndefined() and a correct red
squiggle on size().

Regards

Ed Willink

On 22/07/2010 23:40, Oliver wrote:
> Thanks Ed!
> I've found out "???" have to replaced by "self" so a valid EDataType is:
> EDataType {
> name := ThirtyCharString
> instanceTypeName := java.lang.String
> serializable := true
> eAnnotations += EAnnotation {
> source := http://www.eclipse.org/emf/2002/Ecore
> details += EStringToStringMapEntry {
> key := constraints
> value := MaxLengthConstraint
> }
> }
> eAnnotations += EAnnotation {
> source := http://www.eclipse.org/emf/2002/Ecore/OCL
> details += EStringToStringMapEntry {
> key := MaxLengthConstraint
> value := self.oclIsUndefined() or self.size() <= 30
> }
> }
> }
>
> I had never expected that this is possible, but it works :)
>
> Ciao Olli
Re: Defining OCL-Constraints on user EDataTypes [message #548812 is a reply to message #548799] Fri, 23 July 2010 07:06 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi Olli

Try one of

package example : ex = 'http://example'
{
class ThirtyCharString1 : 'java.lang.String'
{
operation size() : Integer; -- requires custom Java code
invariant MaxLengthConstraint: size() <= 30;
}
class ThirtyCharString2
{
attribute s : String;
operation size() : Integer {
body: s.size();
}
invariant MaxLengthConstraint: size() <= 30;
}
}

Regards

Ed Willink
Re: Defining OCL-Constraints on user EDataTypes [message #548841 is a reply to message #548753] Fri, 23 July 2010 09:24 Go to previous messageGo to next message
Oliver is currently offline OliverFriend
Messages: 13
Registered: January 2010
Location: Germany
Junior Member
Thank you Edward!
OK, I'am not an expert in OCL so I've used oclIsDefined() without an understanding what it does on EDataTypes. If I understand it right, the OCL expressions on EDataTypes are only evaluated when there in instance of a EDataType exists i. e. the value of EAttribute on runtime is not null. So it is sufficient to use self.size() <= 30

I've tried many many times to use the OCLinEcore editor, but the Eclipse crashes when I would create a dynamic instance of the meta class.

What was your intention with ThirtyCharString1 and ThirtyCharString2? Both are EClasses and can't use as eType of an EAttribute.

Ciao Olli

Re: Defining OCL-Constraints on user EDataTypes [message #548850 is a reply to message #548841] Fri, 23 July 2010 09:46 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi Olli

On 23/07/2010 10:24, Oliver wrote:
> Thank you Edward!
> OK, I'am not an expert in OCL so I've used oclIsDefined() without an
> understanding what it does on EDataTypes.

That is youir problem. You think the method is "oclIsDefined" but it is
spelled "oclIsUndefined" which is what it does.

> If I understand it right, the
> OCL expressions on EDataTypes are only evaluated when there in instance
> of a EDataType exists i. e. the value of EAttribute on runtime is not
> null. So it is sufficient to use self.size() <= 30

The self is redundant.

But EDataType do not have attributes; they are atomic types.

In simpler Java it is pointless to test if ("aString".equals(null))
because "aString" necessarily exists before you can consider invoking an
operation on it.

> I've tried many many times to use the OCLinEcore editor, but the Eclipse
> crashes when I would create a dynamic instance of the meta class.

Bug treports are always welcome so that we can try to prevent other
users sharing your problems. However this is not a bug report.

'Eclipse crashes' seems very very unlikely, since once Eclipse starts
nearly everything is caught as an error rather than a crash.

> What was your intention with ThirtyCharString1 and ThirtyCharString2?
> Both are EClasses and can't use as eType of an EAttribute.

Exactly. You want your ThirtySomething class to have an operation
"size()". Only EClasses can have user-defined operations.

If you want to have an opaque datatype then you cannot use OCL since OCL
needs to see the datatype content to operate on it. Therefore for your
EDataType example you must implement the invariant body in Java.

Regards

Ed Willink
Re: Defining OCL-Constraints on user EDataTypes [message #548874 is a reply to message #548753] Fri, 23 July 2010 11:06 Go to previous messageGo to next message
Oliver is currently offline OliverFriend
Messages: 13
Registered: January 2010
Location: Germany
Junior Member
Thanks for reply, Edward!

Yes, the if ("aString".equals(null)) was an good example to explain it.

If you're interested in the problem with "eclipse crashes": At this system (Windows XP 32 Bit on a 64 Bit CPU and multiuser enviroment) crashes sometimes. If I right click on the EClass in the outline view "Create Dynamic Instance..." Wizard comes corrctly and when I click on "Finish" Eclipse opens a XML Editor and popup an error dialog "Problem occured". It isn't possible to look in details. When I click on OK than "Out of memory" comes.

See
http://www.bilder-space.de/bilder/475af1-1279882518.png and http://www.bilder-space.de/bilder/1cb68c-1279882553.png

But I expect thats a problem of my current system not of the Eclipse. On my other system I've never see that the Eclipse crashes. So when I can test it on my other system and it would crashes too, than I will submit a bug report.

Back to the topic:
Quote:
If you want to have an opaque datatype then you cannot use OCL since OCL
needs to see the datatype content to operate on it. Therefore for your
EDataType example you must implement the invariant body in Java.

Conflicts this to use an OCL invariant on an EDataType? It works with self.size() <= 30 respectively size() <= 30. In the generated editor, I get an error message, when this constraint is violated. This is what I expected so everythign is fine Smile

Ciao Olli
Re: Defining OCL-Constraints on user EDataTypes [message #548931 is a reply to message #548874] Fri, 23 July 2010 13:35 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi Olli

The OCLinEcore tutorial in common with many other EMF-related documents
warns you to make sure that you configure your JVM correctly otherwise
you get out of memory errors with anything more than trivial EMF usage.

Have you read <http://wiki.eclipse.org/IRC_FAQ#How_do_I_start_Eclipse.3F> ?

Regards

Ed Willink


On 23/07/2010 12:06, Oliver wrote:
> Thanks for reply, Edward!
>
> Yes, the if ("aString".equals(null)) was an good example to explain it.
>
> If you're interested in the problem with "eclipse crashes": At this
> system (Windows XP 32 Bit on a 64 Bit CPU and multiuser enviroment)
> crashes sometimes. If I right click on the EClass in the outline view
> "Create Dynamic Instance..." Wizard comes corrctly and when I click on
> "Finish" Eclipse opens a XML Editor and popup an error dialog "Problem
> occured". It isn't possible to look in details. When I click on OK than
> "Out of memory" comes.
>
> See
> and
> But I expect thats a problem of my current system not of the Eclipse. On
> my other system I've never see that the Eclipse crashes. So when I can
> test it on my other system and it would crashes too, than I will submit
> a bug report.
>
> Back to the topic:
> Quote:
>> If you want to have an opaque datatype then you cannot use OCL since OCL
>> needs to see the datatype content to operate on it. Therefore for your
>> EDataType example you must implement the invariant body in Java.
>
> Conflicts this to use an OCL invariant on an EDataType? It works with
> self.size() <= 30 respectively size() <= 30. In the generated editor, I
> get an error message, when this constraint is violated. This is what I
> expected so everythign is fine :)
>
> Ciao Olli
Previous Topic:XSD -> Ecore: Elements not recognized without insertion of namespace
Next Topic:Patching of org.eclipse.emf.ant plug-in
Goto Forum:
  


Current Time: Thu Apr 25 11:10:36 GMT 2024

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

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

Back to the top