Only one file for many classes, here my code. [message #780668] |
Wed, 18 January 2012 23:06 |
Davide Perini Messages: 28 Registered: January 2012 |
Junior Member |
|
|
Hi all...
I am trying to generate only one file for model. (One model contains a class diagram with many classes).
For example, I want to create a txt file with all the names of the classes.
I have done this:
[comment encoding = UTF-8 /]
[module generate('.../uml2/3.0.0/UML')]
[template public Main(aClass : Class)]
[comment @main/]
[file (aClass.name.concat('.txt'), false)]
[printNameClass(aClass)/]
[/file]
If I have a model with 4 classes, this code generates four different file with the name of their class.
I want to create only one file with all the four names. How can I do it?
Thanks.
[Updated on: Thu, 19 January 2012 00:51] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
Re: Only one file for many classes, here my code. [message #780807 is a reply to message #780802] |
Thu, 19 January 2012 12:58 |
|
Hi,
still the same.
(1) how to traverse. you need to know the metamodel. in Xpand such a traversation would look like
«IMPORT uml»
«DEFINE main FOR Model»
«FILE "test.txt"»
«EXPAND handlePackageElement FOREACH this.packagedElement»
«ENDFILE»
«ENDDEFINE»
«REM»Just a Fallback«ENDREM»
«DEFINE handlePackageElement FOR PackageableElement»
«ENDDEFINE»
«DEFINE handlePackageElement FOR Package»
«EXPAND handlePackageElement FOREACH this.packagedElement»
«ENDDEFINE»
«DEFINE handlePackageElement FOR Class»
Class: «this.name»
«ENDDEFINE»
(2) how to technically traverse with acceleo you can find here
[template public umlToBeans(aPackage : Package)]
[comment @main /]
[for (aClass : Class | aPackage.packagedElement->filter(Class))]
[aClass.generate()/]
[/for]
[/template]
and how to get everything into one file:
do the same in acceleo than you do in xpand.
the tutorial contains an example for even this too
[template public generate(aClass : Class)]
[file (aClass.name.concat('.java'), false)]
public class [aClass.name.toUpperFirst()/] {
[for (p: Property | aClass.attribute) separator('\n')]
private [p.type.name/] [p.name/];
[/for]
[for (p: Property | aClass.attribute) separator('\n')]
public [p.type.name/] get[p.name.toUpperFirst()/]() {
return this.[p.name/];
}
[/for]
[for (o: Operation | aClass.ownedOperation) separator('\n')]
public [o.type.name/] [o.name/]() {
// TODO should be implemented
}
[/for]
}
[/file]
[/template]
the properties go into the same file as the class does. what a miracle.
~Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
|
|
|
Re: Only one file for many classes, here my code. [message #780862 is a reply to message #780807] |
Thu, 19 January 2012 17:39 |
Davide Perini Messages: 28 Registered: January 2012 |
Junior Member |
|
|
Christian Dietrich wrote on Thu, 19 January 2012 07:58
the properties go into the same file as the class does. what a miracle.
~Christian
For first thanks for the answer, I really appreciate your help but
that code does not solve the problem since it generates n file for n classes.
the code you have posted:
[template public umlToBeans(aPackage : Package)]
[comment @main /]
[for (aClass : Class | aPackage.packagedElement->filter(Class))]
[aClass.generate()/]
[/for]
[/template]
[template public generate(aClass : Class)]
[file (aClass.name.concat('.java'), false)]
public class [aClass.name.toUpperFirst()/] {
[for (p: Property | aClass.attribute) separator('\n')]
private [p.type.name/] [p.name/];
[/for]
[for (p: Property | aClass.attribute) separator('\n')]
public [p.type.name/] get[p.name.toUpperFirst()/]() {
return this.[p.name/];
}
[/for]
[for (o: Operation | aClass.ownedOperation) separator('\n')]
public [o.type.name/] [o.name/]() {
// TODO should be implemented
}
[/for]
}
[/file]
[/template]
generates exactly the same things that my original code generates with "different" requests.
This is my original code:
[template public Main(aClass : Class)]
[comment @main/]
[file (aClass.name.concat('.txt'), false, 'UTF-8')]
[print(aClass)/]
[/file]
[/template]
[template public print(aClass : Class)]
Class name = [aClass.name/]
[/template]
As I asked, I need to generate only one output file with the names of the classes contained in the model.
If my model have 4 classes for example, I need to create only one output file containing the name of the four classes.
With mine and yours code, acceleo generates four output file for four classes.
Any help is much appreciated.
Thanks.
[Updated on: Thu, 19 January 2012 17:40] Report message to a moderator
|
|
|
|
Re: Only one file for many classes, here my code. [message #780870 is a reply to message #780864] |
Thu, 19 January 2012 18:36 |
Davide Perini Messages: 28 Registered: January 2012 |
Junior Member |
|
|
Christian Dietrich wrote on Thu, 19 January 2012 12:48HI,
i wont do your homework. i wanted to tell you that you should create a file like
THIS IS A TEMPLATE FOR A UML::MODEL
FILE "FILENAME.TXT"
TRAVERSE THE MODEL AND SREACH THE CLASSES AND OOUTPUT THEIR NAME
ENDFILE
ENDOFTHETEMPLATE
I have a degree in computer science, don't call it homework
The homework time is finished, now I need to choose if use acceleo in my company or not so I am studying it
In any case, your suggestion is really much appreciated and I thank you for your help/patience.
I tought you gived me the solution but, I understood the code now and "adjusted" the shot.
Now I can print all the class names in one file. Thanks.
I will continue my excercise. I will post some more question here, if I'll have questions, hope you will be here
Again, many thanks.
|
|
|
|
|
|
|
Re: Only one file for many classes, here my code. [message #781108 is a reply to message #781107] |
Fri, 20 January 2012 14:53 |
|
Hi,
i am still not sure if you understood the problem.
consider you have following Java Interfaces
public interface Relationship {
Element getTarget();
}
public interface Element {
}
public interface NamedElement extends Element {
String getName();
}
public interface Class extends NamedElement{
}
so now you should know/understand the problem
how to solve it
(1) use some if else and typechecks (ocliskindof)
(2) Acceleo (as Xpand) supports Polymorphic Dispatch
this fact should help you to solve the problem
thus the solution for your last problem could have looked like
[template public generateElement(model : Model)]
[comment @main/]
[file ('test.txt', false, 'UTF-8')]
[for (e : Element | model.ownedElement)]
[gen(e)/]
[/for]
[/file]
[/template]
[template public gen(c : Class)]
Class: [c.name/]
[/template]
[template public gen(p : Package)]
[for (e : Element | p.ownedElement)]
[gen(e)/]
[/for]
[/template]
[template public gen(c : Element)]
[/template]
this should help you to handle only elements that are of type Class and throw the other ones to /dev/null
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
|
|
|
|
|
|
Re: Only one file for many classes, here my code. [message #781137 is a reply to message #781123] |
Fri, 20 January 2012 16:47 |
|
Hi,
this is once more a question on the uml metamodel and not one on acceleo. so asking the uml forum or having a look at the uml metamodel would
once more help. ( i know that uml is ugly but you have to blame the OMG for that
[template public generateElement(aModel : Model)]
[comment @main /]
[file ('test.txt', false, 'Cp1252')]
[for (a : Association | aModel.eAllContents(Association))]
A [a.memberEnd->at(1).type.name/] has [a.memberEnd->at(2).lower/] [a.memberEnd->at(2).upper/] [a.memberEnd->at(2).type.name/].
A [a.memberEnd->at(2).type.name/] has [a.memberEnd->at(1).lower/] [a.memberEnd->at(1).upper/] [a.memberEnd->at(1).type.name/].
[/for]
[/file]
[/template]
~Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Day Job: https://www.everest-systems.com
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|