Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [Acceleo] In search of a clean solution for namespace/block generation
[Acceleo] In search of a clean solution for namespace/block generation [message #632220] Tue, 12 October 2010 07:34 Go to next message
Kirsten M. Z. is currently offline Kirsten M. Z.Friend
Messages: 132
Registered: July 2010
Senior Member
Hello,

I am searching for the best way for generating code like the following in Acceleo. Unfortunately, I haven't found a good solution/example:

namespace N1
{
        namespace N2
        {
                namespace N3
                {
                        ...specific-code...
                }
        }
}


Especially concerning reusability of the "namespace generation", a perfect indentation and clean code, I want to use a construct as follows. Unfortunately, something like this, does not exist in Acceleo (I think?)

[generateNamespaces(baseClass)]
        ...specific-code...
[generateNamespaces /]


Has anybody some advice? How to solve my use case in a good way?

Thanks!

Regards,
Kirsten
Re: [Acceleo] In search of a clean solution for namespace/block generation [message #632229 is a reply to message #632220] Tue, 12 October 2010 08:26 Go to previous messageGo to next message
Laurent Goubet is currently offline Laurent GoubetFriend
Messages: 1902
Registered: July 2009
Senior Member
Hi Kirsten,

You won't be able to have a perfect indentation in both generated code and template, you'll have to make compromises Razz.

The best way of handling this problem is if your namespaces and "baseclass" are of two different kind. For example if your namespaces are "EPackage"s and baseclass are "EClass"es, you could then use code like this :

[template public generate(element : EObject)]
  [comment this will not generate anything/]
[/template]

[template public generate(pack : EPackage)]
  namespace [name/]
  {
  [pack.eContents().generate()/]
  }
[/template]

[template public generate(class : EClass)]
  ...specific code...
[/template]


The three templates share the same name, so Acceleo will check the actual runtime type of the parameter in order to know which it should call. As you start your generation from the namespace (package "N1"), it will first generate the "namespace N1" part, then as it goes down the content encountering more packages or classes, generate the remainder. As soon as we find anything than a package or class, the placeholder "generate(EObject)" will be called and won't generate a thing.

There are more than a single way to do this, you could also iterate over the result of "baseClass.ancestors(EPackage)->reverse()" and generate all namespaces at once from this list ... You'll have to experiment to see which you prefer.

Regards,

Laurent Goubet
Obeo
Re: [Acceleo] In search of a clean solution for namespace/block generation [message #632238 is a reply to message #632229] Tue, 12 October 2010 08:54 Go to previous message
Kirsten M. Z. is currently offline Kirsten M. Z.Friend
Messages: 132
Registered: July 2010
Senior Member
Hi Laurent,

thanks for your solution. It contains some interesting hints. However, the overall solution is not useful for me. The problem is the reusability of the "namespace generation" code. BTW, I need the searched construct not only for namespace generation, but it is a neat example everyone knows.

Imagine, you have to generate:

namespace N1
{
        namespace N2
        {
                namespace N3
                {
                        ... cheese ...
                }
        }
}
namespace N1
{
        namespace N2
        {
                namespace N3
                {
                        ... onions ...
                }
        }
}


In "my preferred solution", this would still look like:

[generateNamespaces(baseClass)]
        ... cheese ...
[generateNamespaces /]
[generateNamespaces(baseClass)]
        ... onions ...
[generateNamespaces /]



BTW: I know that indention in Acceleo code is... well... I gave up Smile At least the resulting code must be indented perfectly, and this is, what I meant.
Previous Topic:[Xpand] EvaluationException, no main found in xpand template
Next Topic:[Xpand/Xtend] Type problem with packages
Goto Forum:
  


Current Time: Thu Mar 28 15:49:01 GMT 2024

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

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

Back to the top