|
Re: ATL Helper Context Assignment [message #1852717 is a reply to message #1852710] |
Thu, 02 June 2022 07:07 |
|
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.
Cheers,
Dennis
|
|
|
Re: ATL Helper Context Assignment [message #1852720 is a reply to message #1852717] |
Thu, 02 June 2022 08:34 |
Burak Karaduman Messages: 84 Registered: July 2018 |
Member |
|
|
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 19:05 |
|
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
);
Cheers,
Dennis
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02039 seconds