[Acceleo] Transversal tree generation [message #871515] |
Mon, 14 May 2012 06:02  |
Eclipse User |
|
|
|
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 !
So, feel free to give any feeling/better solution/suggestion !
Thank you !
Stéphane
|
|
|
|
Re: [Acceleo] Transversal tree generation [message #874625 is a reply to message #874609] |
Mon, 21 May 2012 05:17  |
Eclipse User |
|
|
|
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
|
|
|
Powered by
FUDForum. Page generated in 0.04114 seconds