Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [Acceleo 3] Iteration over the types of a model(How-to specify MTL to get statistics on a model)
[Acceleo 3] Iteration over the types of a model [message #640004] Thu, 18 November 2010 16:17 Go to next message
Cedric Gava is currently offline Cedric GavaFriend
Messages: 41
Registered: July 2009
Member
Hello

Lets suppose we have a metamodel
CV contains Experience, Training, and Skill wich may, in turn contains Experience, training and Skill....

I would like to generate some statistics like

"How many Training are contained in Experience objects"
So, I would like to do the following :

1 Iterate over a ParentType sequence of Types
2 For each parentIterator object of this ParentType contained in my model
3 Iterate over a childType sequence of Types
4 print the number of object of childType contained by parentIterator

It should look like this :

[template public StatisticCSV(c : TOP)]
	[comment @main /]
	[file ('stat.txt', false, 'UTF-8')]
	Parent;Experience;Training;Skill
	[for (parentType | Sequence{Experience,Training,Skill})]
		[for (parentIterator | c.eAllContents()->select(e | e.oclIsKindOf(parentType)))]
 			[parentType.oclAsType(EClass).name/];[for (childType | Sequence{Experience,Training,Skill}) separator(';') after ('\n')]
 			[parentIterator.eContents()->select(e | e.oclIsKindOf(childType))/]
 			[/for]
        [/for]
    [/for]
	[/file]
[/template]


Unfortunately, This has plenty of mistakes.
I canc iterate over a sequence doing :
[for (Sequence{Experience,Training,Skill})]

But as my Iterator has no name, I can't use it in subsequent iterations !!!

I'm thinking of generating a huge mtl directly from ecore, given my metamodel as input model... but I wonder If I can do this directly with a simple MTL...

Any Idea ?

Thank you for your help
Kind regards


sooo lonely friends of eclipse on forum
http://www.eclipse.org/donate/images/friendslogo160.jpg
Re: [Acceleo 3] Iteration over the types of a model [message #640119 is a reply to message #640004] Fri, 19 November 2010 08:10 Go to previous messageGo to next message
Stephane Begaudeau is currently offline Stephane BegaudeauFriend
Messages: 413
Registered: April 2010
Location: Nantes (France)
Senior Member

Hi Cedric,

"[for (Sequence{Experience,Training,Skill})] But as my Iterator has no name, I can't use it in subsequent iterations !!!"
You can access the counter of this iteration with the variable named "i", and you can access the current object of the iteration with the variable named "self".

"1 Iterate over a ParentType sequence of Types
2 For each parentIterator object of this ParentType contained in my model
3 Iterate over a childType sequence of Types
4 print the number of object of childType contained by parentIterator"

As for your problem, I'm not sure I've understood everything but you probably could solve it with something like this:
Sequence{ParentType1, ParentType2, ParentType3}.eAllContents().size()

If you want to give your metamodel as an input for the generation, then the generation needs to be configured with your meta-metamodel which means that your templates need to be applied on EObjets not on your own concept.


Stephane Begaudeau, Obeo



Re: [Acceleo 3] Iteration over the types of a model [message #640374 is a reply to message #640119] Sun, 21 November 2010 09:46 Go to previous messageGo to next message
Cedric Gava is currently offline Cedric GavaFriend
Messages: 41
Registered: July 2009
Member
Hi Stephane,

And Thank you for your answer

Quote:
"[for (Sequence{Experience,Training,Skill})] But as my Iterator has no name, I can't use it in subsequent iterations !!!"
You can access the counter of this iteration with the variable named "i", and you can access the current object of the iteration with the variable named "self".


The problem there is that I cannot use nested loops, and acces the counter( or current object) of the higher level loop :
[for (SequenceA)]
    [for (SequenceB)]
        [comment How can I acces counter of Sequence A there?/]
    [/for]
[/for]


So I wanted to give a name to my higher level iterator, like the code below, but it is a syntax error since "a" must be typed... :
[for (a|SequenceA)]
    [for (SequenceB)]


So I tried to say that a is an Eclass, but "Eclass" is not recognized as a valid type :
[for (a : EClass|SequenceA)]
    [for (SequenceB)]


Am I clearer ?

Is it possible to give 2 metamodels (the metamodel and the meta-metamodel) as input to a module to define EClass symbol to the compiler ?
My goal indeed is to name an iterator over "class" and not an iterator over "object".

Quote:
If you want to give your metamodel as an input for the generation, then the generation needs to be configured with your meta-metamodel which means that your templates need to be applied on EObjets not on your own concept.


This is what I will do If I have no other option.

Kind regards


sooo lonely friends of eclipse on forum
http://www.eclipse.org/donate/images/friendslogo160.jpg
Re: [Acceleo 3] Iteration over the types of a model [message #640388 is a reply to message #640374] Sun, 21 November 2010 14:37 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 4150
Registered: July 2009
Senior Member
Hi Cedric

You can use a [let variable to save the outer i under another name.

Regards

Ed Willink

On 21/11/2010 09:47, Cedric Gava wrote:
> Hi Stephane,
>
> And Thank you for your answer
>
> Quote:
>> "[for (Sequence{Experience,Training,Skill})] But as my Iterator has
>> no name, I can't use it in subsequent iterations !!!"
>> You can access the counter of this iteration with the variable named
>> "i", and you can access the current object of the iteration with the
>> variable named "self".
>
>
> The problem there is that I cannot use nested loops, and acces the
> counter( or current object) of the higher level loop :
>
> [for (SequenceA)]
> [for (SequenceB)]
> [comment How can I acces counter of Sequence A there?/]
> [/for]
> [/for]
>
>
> So I wanted to give a name to my higher level iterator, like the code
> below, but it is a syntax error since "a" must be typed... :
>
> [for (a|SequenceA)]
> [for (SequenceB)]
>
>
> So I tried to say that a is an Eclass, but "Eclass" is not recognized
> as a valid type :
>
> [for (a : EClass|SequenceA)]
> [for (SequenceB)]
>
>
> Am I clearer ?
>
> Is it possible to give 2 metamodels (the metamodel and the
> meta-metamodel) as input to a module to define EClass symbol to the
> compiler ?
> My goal indeed is to name an iterator over "class" and not an iterator
> over "object".
>
> Quote:
>> If you want to give your metamodel as an input for the generation,
>> then the generation needs to be configured with your meta-metamodel
>> which means that your templates need to be applied on EObjets not on
>> your own concept.
>
>
> This is what I will do If I have no other option.
>
> Kind regards
Re: [Acceleo 3] Iteration over the types of a model [message #640457 is a reply to message #640374] Mon, 22 November 2010 08:32 Go to previous messageGo to next message
Laurent Goubet is currently offline Laurent GoubetFriend
Messages: 1638
Registered: July 2009
Senior Member
Hi Cedric,

Ed's answer is true : you could use a let to remember the "i" iterator variable ... though I don't think this is what you seek.

Quote:
Is it possible to give 2 metamodels (the metamodel and the meta-metamodel) as input to a module to define EClass symbol to the compiler ?


It is.

[module myModule('myMetamodel', 'http://www.eclipse.org/emf/2002/Ecore')/]

Of course, you could also use the qualified name of your type. If your metamodel depends on Ecore, OCL should be able to resolve it :

[for (a : ecore::EClass|SequenceA)]

(Qualified names in OCL are separated with "::", "ecore" is the name of the package containing the type "EClass".)

However, I believe "[for (a|SequenceA)]" should have worked... (tries it on his own)... and it does not Sad. OCL doesn't force users into typing their iterators, Acceleo shouldn't either. Could you raise an enhancement request against M2T/Acceleo so that we allow for such shortcuts in our "for" loops?

Laurent Goubet
Obeo
icon14.gif  [SOLVED] [Acceleo 3] Iteration over the types of a model [message #640624 is a reply to message #640457] Mon, 22 November 2010 16:06 Go to previous messageGo to next message
Cedric Gava is currently offline Cedric GavaFriend
Messages: 41
Registered: July 2009
Member
Hi Laurent, Hi Ed...

Ok a managed to set ecore namespace's types, and got the following template :

[template public StatisticCSV(c : CV)]
    [comment @main /]
    [file ('stat.txt', false, 'UTF-8')]
    [let seqClass : Sequence(ecore::EClass) = Sequence{A,B,C,D,E,F,G}]
[comment/]Parent;size;[for (seqClass) separator(';')][name/][/for]
        [for (parentType : ecore::EClass | seqClass)]
            [comment]Iterate over every instance of the parent Class[/comment]
            [for (parentIterator : ecore::EObject | c.eAllContents()->select(eClass().name = parentType.name) )]
[comment      /][parentType.name/];[parentIterator.eContents()->size()/];[for (childType : ecore::EClass | seqClass) separator(';') after ('\n')]
[comment          /][parentIterator.eContents()->select(eClass().name = childType.name)
                                               ->size()/][/for]
            [/for]
        [/for]
    [/let]
    [/file]
[/template]

And it works

Thank you for the support !!


sooo lonely friends of eclipse on forum
http://www.eclipse.org/donate/images/friendslogo160.jpg
Re: [SOLVED] [Acceleo 3] Iteration over the types of a model [message #640783 is a reply to message #640624] Tue, 23 November 2010 07:33 Go to previous message
Laurent Goubet is currently offline Laurent GoubetFriend
Messages: 1638
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------080004050705000705060801
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Hi Cedric,

And thanks for the solution and the raised bug, this may help others :).

Laurent Goubet
Obeo

On 22/11/2010 17:06, Cedric Gava wrote:
> Hi Laurent, Hi Ed...
> Ok a managed to set ecore namespace's types, and got the following
> template :
>
>
> [template public StatisticCSV(c : CV)]
> [comment @main /]
> [file ('stat.txt', false, 'UTF-8')]
> [let seqClass : Sequence(ecore::EClass) = Sequence{A,B,C,D,E,F,G}]
> [comment/]Parent;size;[for (seqClass) separator(';')][name/][/for]
> [for (parentType : ecore::EClass | seqClass)]
> Iterate over every instance of the parent Class[/comment]
> [for (parentIterator : ecore::EObject |
> c.eAllContents()->select(eClass().name = parentType.name) )]
> [comment /][parentType.name/];[parentIterator.eContents()->size()/];[for
> (childType : ecore::EClass | seqClass) separator(';') after ('\n')]
> [comment /][parentIterator.eContents()->select(eClass().name =
> childType.name)
> ->size()/][/for]
> [/for]
> [/for]
> [/let]
> [/file]
> [/template]
>
> And it works
>
> Thank you for the support !!
>


--------------080004050705000705060801
Content-Type: text/x-vcard; charset=utf-8;
name="laurent_goubet.vcf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="laurent_goubet.vcf"

YmVnaW46dmNhcmQNCmZuOkxhdXJlbnQgR291YmV0DQpuOkdvdWJldDtMYXVy ZW50DQpvcmc6
PGEgaHJlZj0iaHR0cDovL3d3dy5vYmVvLmZyIj5PYmVvPC9hPg0KZW1haWw7 aW50ZXJuZXQ6
bGF1cmVudC5nb3ViZXRAb2Jlby5mcg0KdXJsOmh0dHA6Ly93d3cub2Jlby5m cg0KdmVyc2lv
bjoyLjENCmVuZDp2Y2FyZA0KDQo=
--------------080004050705000705060801--
Previous Topic:Class with any operation with more of one parameter
Next Topic:[acceleo] Strange behavior when accessing name field
Goto Forum:
  


Current Time: Sun Nov 23 13:41:38 GMT 2014

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

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