Skip to main content



      Home
Home » Modeling » ATL » ATL Helper Context Assignment
ATL Helper Context Assignment [message #1852710] Wed, 01 June 2022 19:53 Go to next message
Eclipse UserFriend
Dear all,

I managed to have some String operations in do {


thisModule.var2<-thisModule.var;
thisModule.var2<-thisModule.var.split('[.]');
thisModule.var2.debug();


}

How can I create a helper context rule that does the same assignment and operations? I inspected some helper contexts, but most of them run with if/else.

Thank you.
Best regards
Re: ATL Helper Context Assignment [message #1852717 is a reply to message #1852710] Thu, 02 June 2022 03:07 Go to previous messageGo to next message
Eclipse UserFriend
Short answer: you can't.

The do blocks in ATL are imperative, while helpers are declarative. And even though imperative code blocks in ATL also allow for embedding of declarative expressions, you cannot embed an imperative statement in a declarative expression. Examples of statements in ATL are assignments, loops, and also expressions. Statements always end with ';'. The expressions in ATL are basically defined by OCL. Examples are helper invocations, if-then-else-endif blocks, let-in blocks, and basically everything that results in some return value.

Your example code could use a helper method as follows:
...
do {
  thisModule.var2<-thisModule.var;
  thisModule.var2<-thisModule.var.splitDot();
  thisModule.var2.debug();
}

helper context String def : splitDot() : String =
  self.split('[.]');


As you can see, very little is gained for simple expressions.
Re: ATL Helper Context Assignment [message #1852720 is a reply to message #1852717] Thu, 02 June 2022 04:34 Go to previous messageGo to next message
Eclipse UserFriend
Hi Dennis,

Thank you so much for your answer. Actually, I try to achieve some steps of string operation and I need to apply this to 4-5 transformations. Using their do {} blocks will not be reasonable because the code will be the same for all. Instead, a helper rule could help, but the declarative type limits my approach I think.

thisModule.var <-pim.eContainer().refGetValue('GetLine');
thisModule.var2<-thisModule.var;
thisModule.var2<-thisModule.var.split('[.]');
thisModule.var2<- thisModule.var2.insertAt(thisModule.var2.size(),thisModule.var2->last().toInteger()+1);
thisModule.count <- thisModule.count +1;
thisModule.var2<-thisModule.var2.insertAt(thisModule.var2.size(),thisModule.var2->last().toInteger()+thisModule.count).subSequence(1, thisModule.var2.size());
pim.Line<thisModule.var2.at(1).concat('.')+thisModule.var2.at(2).concat('.')+thisModule.var2.at(3).concat('.')+thisModule.var2.at(4);

The idea is that modify a string in form of 1.2.3.4, parsing it based on '.' then incrementing the last digit, which is 4 to 5 and merging it again as 1.2.3.5. However, even though I achieved this in the imperative do{} block. I think I used so helper context, otherwise, I have to repeat the same code for 4-5 transformations, which is not conventional.

Thank you.
Best regards.


Re: ATL Helper Context Assignment [message #1852751 is a reply to message #1852720] Thu, 02 June 2022 15:05 Go to previous messageGo to next message
Eclipse UserFriend
The problem you're trying to solve can also be solved in a declarative way, but you have to change your way of thinking.

You're trying to increment the last number in a sequence of numbers separated by dots (a version number?), given as a string, and then return the result again as a string.

You started off well by splitting the input string on each dot in a sequence of numbers. Then, the whole sequence except the last number can just by kept as is, and we append the incremented last number to it. Then we need to convert the resulting sequence of strings back into a single, dotted string, which can be done with iterate():

helper context String def : incrementVersion : String =
  let numbers : Sequence(String) = self.split('[.]')
  in numbers
    ->subSequence(1, numbers->size() - 1)
    ->append((numbers->last().toInteger() + 1).toString())
    ->iterate(e; acc : String = OclUndefined |
      if acc.oclIsUndefined() then
        e
      else
        acc + '.' + e
    );
Re: ATL Helper Context Assignment [message #1852865 is a reply to message #1852751] Wed, 08 June 2022 17:50 Go to previous messageGo to next message
Eclipse UserFriend
Hi Dennis,

Thank you so much, as always I owe you once again :)

I modified it a bit and it works now.

Best regards.
Re: ATL Helper Context Assignment [message #1852867 is a reply to message #1852865] Wed, 08 June 2022 18:21 Go to previous messageGo to next message
Eclipse UserFriend
Hi Dennis,

BTW, is it possible to use any debug or print operation inside of helper context? especially while iterating.

Best regards.
Re: ATL Helper Context Assignment [message #1852894 is a reply to message #1852867] Thu, 09 June 2022 13:13 Go to previous message
Eclipse UserFriend
The debug() function returns the value on which is it invoked, so you can always insert it in any expression.
Previous Topic:Default value serialization in XMI
Next Topic:How to reference external modules?
Goto Forum:
  


Current Time: Sat Nov 01 19:24:10 EDT 2025

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

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

Back to the top