[Acceleo] Assigning a Template Result to a Variable [message #1808381] |
Sun, 23 June 2019 05:38 |
Elder Rodrigues Messages: 7 Registered: June 2019 |
Junior Member |
|
|
Hello everyone,
I've started using acceleo recently and my problem is this:
Given an instance of a metaclass of my metamodel, I need to generate a specific text for each value of the properties and then link each text generated using the keyword 'and'.
For example, let's say there is a MetaClassX that has three propeties: propA, propB, and propC. What I need to do is basically:
1) For each property check if its value is undefined. If it is not, then a text must be generated.
2) Link all generated texts using the word 'and'
One possible solution could be:
[template public generateText (class: MetaClassX)]
[if not class.propA.oclIsUndefined ()] <text of a> [/if]
[if not class.propB.oclIsUndefined ()] <text of b> [if not class.propA.oclIsUndefined ()] AND [/if] [/if]
[if not class.propC.oclIsUndefined ()] <text of c> [if (not class.propA.oclIsUndefined ()) or (not class.propB.oclIsUndefined ())] AND [/if]
[/template]
The problem with the above code is that it is not scalable in the sense that if a metaclass has too many properties, the "if code" will be huge.
Another option could be to create a template for each property and do the following:
[template public generateTextPropA (class: MetaClassX)]
[if not class.propA.oclIsUndefined ()] <text of a> [/if]
[/template]
[template public generateTextPropB (class: MetaClassX)]
[if not class.propB.oclIsUndefined ()] <text of b> [/if]
[/template]
[template public generateTextPropC (class: MetaClassX)]
[if not class.propC.oclIsUndefined ()] <text of c> [/if]
[/template]
[template public generateText (class: MetaClassX)]
[let texts: Set (String) = Set (String) {
class.generateTextPropA,
class.generateTextPropB,
class.generateTextPropC
} -> select (s | s.size > 0)]
[for (text: String | texts) separator('AND')]
text
[/for][/let]
[/template]
The advantage of the above code is that it is more scalable than the first one in the sense that if a class has too many properties, you just need to add the text in the set. On the other hand, I noticed that there is a difference between i) calling the template directly (first example) and ii) assigning the result of a template to a variable, and then printing the variable (second example). In the last one if the string size is larger than a row, then the indentation will not work as expected. Is there any sophisticated way to solve this problem?
Thanks.
|
|
|
|
Powered by
FUDForum. Page generated in 0.03837 seconds