Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » Best way to use "separator" in an if chain
Best way to use "separator" in an if chain [message #1808561] Wed, 26 June 2019 20:57
Elder Rodrigues is currently offline Elder RodriguesFriend
Messages: 7
Registered: June 2019
Junior Member
Hello everyone,

I'm wondering how is the best way to implement the code that I will explain below:

Given an instance of MetaclassX, I need to generate specific text for each of its non-null properties and then merge all the texts using a 'and' keyword as a separator.
So let's say that MetaClassX has three properties: propA, propB, and propC. There are at least two approaches that I have thinked:

Approach #1:
[template public generateText(anElement: MetaClassX])]
  [if not anElement.propA.oclIsUndefined] <text of propA>[/if]
  [if not anElement.propB.oclIsUndefined] [if not anElement.propA.oclIsUndefined] and [/if] <text of propB>[/if]
  [if not anElement.propC.oclIsUndefined] [if (not anElement.propA.oclIsUndefined) or (not anElement.propB.oclIsUndefined)] and [/if] <text of propC>[/if]

Approach #2:
[template public generateText(anElement: MetaClassX])]
[let allTexts: Set(String) = Set(String){
[for (text: String | allTexts) separator(' and ')]
[text /] [/for]

[template public generateTextPropA(anElement: MetaClassX])]
[if not anElement.propA.oclIsUndefined] <text of propA>[/if]

[template public generateTextPropB(anElement: MetaClassX])]
[if not anElement.propB.oclIsUndefined] <text of propB>[/if]

[template public generateTextPropC(anElement: MetaClassX])]
[if not anElement.propC.oclIsUndefined] <text of propC>[/if]

The Appraoch#1 is not scalable in the sense that if the MetaClassX has many properties, the if statement will have many conditions and will leave the code difficult to maintain.
The Approach#2 is more scalable in the sense that if MetaClassX has many properties, you just need to create templates for each property to generate the specific texts and then include
the results of each template within the set. However, I'm not sure if it's the best way. Any suggestion?


[Updated on: Wed, 26 June 2019 21:48]

Report message to a moderator

Previous Topic:Attributes of Custom Datatype is not seen in papyrus
Next Topic:[Acceleo] For loop on Set<Class> is not working
Goto Forum:

Current Time: Thu Dec 07 07:46:10 GMT 2023

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

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

Back to the top