Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » OCL » OCL and Xtext (OCL statement for primitive datatypes)
OCL and Xtext [message #1257619] Wed, 26 February 2014 16:30 Go to next message
Günther Fiedler is currently offline Günther Fiedler
Messages: 21
Registered: February 2013
Junior Member
Hello!

I´m currently working on a semantic validation of my Xtext grammar by using OCL.

In my grammar i made the initialization of attributes optional.

But i still allow something like

public boolean flag = 5;

Therefore i was trying to write an OCL-Statement.

context Variable_Base inv DataTypeValueInt ('Type mismatch'):
self.param.paramType.datType.oclAsType(DataType).datType = 'int' implies self.value.val.oclIsTypeOf(IntLiteral)

This Statement works fine checking the values of the attribute. The problem now is, that the initialization of an attribute isn´t optional any more.

Thanks in advance,
Günther
Re: OCL and Xtext [message #1257711 is a reply to message #1257619] Wed, 26 February 2014 18:16 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4110
Registered: July 2009
Senior Member
Hi

If you want it to be optional you shouldn't navigate on from the value.

You must guard it just as you would a null in Java.

.... implies self.value.val.oclIsTypeOf(IntLiteral)

should perhaps be

.... implies self.value<> null implies
self.value.val.oclIsTypeOf(IntLiteral)

It is likely that OCL 2.5 will introduce the safe navigation operator
allowing

.... implies self.value?.val.oclIsTypeOf(IntLiteral)

Regards

Ed Willink


On 26/02/2014 16:30, Günther Fiedler wrote:
> Hello!
>
> I´m currently working on a semantic validation of my Xtext grammar by
> using OCL.
>
> In my grammar i made the initialization of attributes optional.
>
> But i still allow something like
> public boolean flag = 5;
>
> Therefore i was trying to write an OCL-Statement.
> context Variable_Base inv DataTypeValueInt ('Type mismatch'):
> self.param.paramType.datType.oclAsType(DataType).datType = 'int'
> implies self.value.val.oclIsTypeOf(IntLiteral)
>
> This Statement works fine checking the values of the attribute. The
> problem now is, that the initialization of an attribute isn´t optional
> any more.
>
> Thanks in advance,
> Günther
Re: OCL and Xtext [message #1258316 is a reply to message #1257711] Thu, 27 February 2014 09:04 Go to previous message
Günther Fiedler is currently offline Günther Fiedler
Messages: 21
Registered: February 2013
Junior Member
Hi Ed,

thank you very much, it works perfectly fine!

I managed to find another solution:

inv DataTypeValueInt ('Assigned value is not Integer'):
if self.param.paramType.datType.oclAsType(DataType).datType = 'int' and ((self.value <> null and self.value.val.oclIsTypeOf(IntLiteral)) or self.value = null) then
true
else
if self.param.paramType.datType.oclAsType(DataType).datType <> 'int' then
true
else
false
endif
endif


But your´s is way easier, thank you again for the lesson!

Kind regards,
Günther
Previous Topic:USE_TYPE_CACHES and DYNAMIC_DISPATCH break basic OCL operations?
Next Topic:Enum value not recognized?
Goto Forum:
  


Current Time: Thu Oct 30 16:01:04 GMT 2014

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

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