Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [Acceleo] Transversal tree generation
[Acceleo] Transversal tree generation [message #871515] Mon, 14 May 2012 10:02 Go to next message
CRASNIER Stéphane is currently offline CRASNIER StéphaneFriend
Messages: 13
Registered: May 2012
Junior Member
Hi all,

I have written a little recursive algorithm to generate text transversally to my tree and I feel that the way I've done that is neither efficient nore in the OCL/Acceleo spirit.

More precisely
Here is an example of the structure of my tree (i.e. : model) :
1    2    3    4    5
A -> B -> C -> D -> E         
       -> F
  -> G -> H


Where A, C, E, F and H inherit from the same type, let's say Leaf, and B, D and G from another one, let's say Node. 1.. 5 represent the deepness of my tree. The max deepness is known... Which has saved me !

To start, I want to generate some text for each Leaf instance in this order :
A -> C -> F -> H -> E... In fact, level by level.

I thought of using the flatten operation but the return order seems to not fit my need.

So, I've used a recursive template with a simple counter like that (I have summarized the code because the type hierarchy is quite large...) :
[template public main(a: Leaf) post (trim()) {indexes: Sequence(Integer) = Sequence {1, 2 , 3, 4};}]
 
 [comment @main /]
 [file (...)]
  [for (it : Integer | indexes)]
    [test(a, it, 1)/]
  [/for]
 [/file]
[/template]

[template public test(leaf : Leaf, displayedIndex : Integer, currentIndex : Integer)  post (trim()) { nextIndex : Integer = currentIndex + 1; }]
 [if (displayedIndex = currentIndex)]
  [the generated text form leaf]
 [/if]
 [if (currentIndex < displayedIndex) ]  
    [for (node : Node | leaf.nodes)]
     [let childLeaf : Leaf = node.leaf]
       [test(childLeaf, displayedIndex, nextIndex)/]
     [/let]
    [/for]
 [/if]
[/template]


May be this code could also be useful for somebody else who is beginning with Acceleo like me : I was'nt able to find any post relating to this kind of "transversal" generation... It's certainly a newbie stuff !Smile

So, feel free to give any feeling/better solution/suggestion !

Thank you !

Stéphane

Re: [Acceleo] Transversal tree generation [message #874609 is a reply to message #871515] Mon, 21 May 2012 08:43 Go to previous messageGo to next message
Laurent Goubet is currently offline Laurent GoubetFriend
Messages: 1902
Registered: July 2009
Senior Member
Hi Stéphane,

I'll admit I do not really understand what was the problem and how you solved it. Basically, you want to generate different things for the same "type" of Objects, depending on how deep they are nested? It might be easier to read if you compute the nesting level for each "leaf" instead of passing in an index as argument.

You can get the nesting level through "getAncestors()->size()".

Laurent Goubet
Obeo
Re: [Acceleo] Transversal tree generation [message #874625 is a reply to message #874609] Mon, 21 May 2012 09:17 Go to previous message
CRASNIER Stéphane is currently offline CRASNIER StéphaneFriend
Messages: 13
Registered: May 2012
Junior Member
Hi Laurent !

Thank you for the reply !

Yes, you've perfectly understood my need and expressed it better than me in a one single sentence :
Quote:
Basically, you want to generate different things for the same "type" of Objects, depending on how deep they are nested


... And you're right : I missed the "getAncestors()->size()" operation to get the nesting level... It's going to simplify the code : Thank you !

I'm also wondering if there is not a more efficient and elegant way to achieve this through queries for instance... I don't like recursively browsing the tree to reach a level for each generated level... May be it's the only way...

Stéphane
Previous Topic:[Acceleo] Unstable behavior when using a query from an imported module
Next Topic:Error when parsing an external data type in the xmi model
Goto Forum:
  


Current Time: Wed Sep 25 22:18:31 GMT 2024

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

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

Back to the top