Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [Acceleo] A switch in an Acceleo query
[Acceleo] A switch in an Acceleo query [message #1244594] Wed, 12 February 2014 15:18 Go to next message
Tatiana Fesenko is currently offline Tatiana Fesenko
Messages: 61
Registered: July 2009
Member
Hi,
I have to implement an Acceleo query which should return a string value based on the input. It can be implemented using an equivalent of a Java switch statement. Unfortunately, I didn't find anything similar to the switch in the specification. Acceleo/OCL doesn't support the elseif statement that's why this simple code contains many nested 'if's and its readability suffers:

    if aResponseCode = 200 then 'OK' 
      else if aResponseCode = 403 then 'Forbidden' 
        else if aResponseCode = 500 then 'Internal Server Error' 
           else '' 
        endif 
      endif 
    endif


A good way to implement it would be using an associative array (table), but again, it wasn't mentioned anywhere in the specification. Therefore, the only alternative to multiple nested 'if's that I can see is to use a Java service which delegates this functionality to a Java method.

Is there an equivalent of a switch or associative array in Acceleo? Can you suggest better alternatives to using nested ifs or a java helper?

Thanks,
Tanya
Re: [Acceleo] A switch in an Acceleo query [message #1244676 is a reply to message #1244594] Wed, 12 February 2014 18:22 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4046
Registered: July 2009
Senior Member
Hi

Acceleo has elif so you don't have to nest.

You can exploit the cached queries and so teach Acceleo your lookups, so
the slow if chain only gets executed once per value.

However you'll find that learning to call out to Java is often helpful
for hard problems.

Regards

Ed Willink


On 12/02/2014 15:18, Tatiana Fesenko wrote:
> Hi, I have to implement an Acceleo query which should return a string
> value based on the input. It can be implemented using an equivalent of
> a Java switch statement. Unfortunately, I didn't find anything similar
> to the switch in the specification. Acceleo/OCL doesn't support the
> elseif statement that's why this simple code contains many nested
> 'if's and its readability suffers:
>
>
> if aResponseCode = 200 then 'OK' else if aResponseCode = 403
> then 'Forbidden' else if aResponseCode = 500 then 'Internal
> Server Error' else '' endif endif endif
>
>
> A good way to implement it would be using an associative array
> (table), but again, it wasn't mentioned anywhere in the specification.
> Therefore, the only alternative to multiple nested 'if's that I can
> see is to use a Java service which delegates this functionality to a
> Java method.
> Is there an equivalent of a switch or associative array in Acceleo?
> Can you suggest better alternatives to using nested ifs or a java helper?
> Thanks,
> Tanya
Re: [Acceleo] A switch in an Acceleo query [message #1244706 is a reply to message #1244676] Wed, 12 February 2014 19:28 Go to previous messageGo to next message
Tatiana Fesenko is currently offline Tatiana Fesenko
Messages: 61
Registered: July 2009
Member
I am using Acceleo 3.4.0 and it shows the '"else" expected instead of "elif"' error on an attempt to use 'elif'. I tried 'elsif' - the same result.

I believe Acceleo uses OCL for queries. The 9.4.46 section of the OCL 2.3.1 specification defines the syntax for the "if" conditions that doesn't support 'elsifs' :

Quote:
IfExpCS ::= 'if' OclExpression[1]
'then' OclExpression[2]
'else' OclExpression[3]
'endif'


I try to avoid using Java helpers in Acceleo as navigating from Acceleo templates to Java code is not good for readability of the code.
Re: [Acceleo] A switch in an Acceleo query [message #1244713 is a reply to message #1244706] Wed, 12 February 2014 19:43 Go to previous messageGo to next message
Ed Willink is currently offline Ed Willink
Messages: 4046
Registered: July 2009
Senior Member
HI

Acceleo has its own syntax for [if ...]...[/if] but indeed OCL for if
.... endif

Only Acceleo has elif at present, although we agreed t propose elif as
part of the OCL 2.5 submission.

Regards

Ed Willink


On 12/02/2014 19:28, Tatiana Fesenko wrote:
> I am using Acceleo 3.4.0 and it shows the '"else" expected instead of
> "elif"' error on an attempt to use 'elif'. I tried 'elsif' - the same
> result.
>
> I believe Acceleo uses OCL for queries. The 9.4.46 section of the OCL
> 2.3.1 specification defines the syntax for the "if" conditions that
> doesn't support 'elsifs' :
>
> Quote:
>> IfExpCS ::= 'if' OclExpression[1]
>> 'then' OclExpression[2]
>> 'else' OclExpression[3]
>> 'endif'
>
>
> I try to avoid using Java helpers in Acceleo as navigating from
> Acceleo templates to Java code is not good for readability of the code.
Re: [Acceleo] A switch in an Acceleo query [message #1244719 is a reply to message #1244713] Wed, 12 February 2014 19:54 Go to previous message
Tatiana Fesenko is currently offline Tatiana Fesenko
Messages: 61
Registered: July 2009
Member
Ed, an "elif" would be a nice feature of OCL 2.5. Looking forward to seeing it!
Meanwhile, I'll use a Java helper for this particular situation.
Previous Topic:Change in Profile Causes Stereotype to not be found
Next Topic:Eclipse UML Profiles Repository now available!
Goto Forum:
  


Current Time: Tue Sep 23 08:22:47 GMT 2014

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

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