[Acceleo] Transversal tree generation [message #871515] |
Mon, 14 May 2012 10:02 |
CRASNIER Stéphane 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 !
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 |
|
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
|
|
|
|
Powered by
FUDForum. Page generated in 0.03131 seconds