Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [Acceleo] Assigning a Template Result to a Variable
[Acceleo] Assigning a Template Result to a Variable [message #1808381] Sun, 23 June 2019 05:38 Go to next message
Elder Rodrigues is currently offline Elder RodriguesFriend
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.
Re: [Acceleo] Assigning a Template Result to a Variable [message #1808492 is a reply to message #1808381] Tue, 25 June 2019 15:40 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7680
Registered: July 2009
Senior Member
Hi

(Sorry, it's taken 2 days for the RDF to propagate your message.)

You need to use reflection.

You can either autogenerate your *.mtl from your *.ecore so that you have as many if's as you need.

Or, in Acceleo using the Ecore embedding, you loop over eClass().eStructuralFeatures to embed your if's as a loop body.

Regards

Ed Willink
Previous Topic:p.type.name is not transforming in the code
Next Topic:Attributes of Custom Datatype is not seen in papyrus
Goto Forum:
  


Current Time: Sat Dec 07 02:37:46 GMT 2024

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

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

Back to the top