[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Newsgroup Home]
|
[news.eclipse.modeling.m2t] Re: [Acceleo] Code Beautifying
|
Hi Timothy,
Comments inlined below.
Timothy Marc a écrit :
Hey,
just started yesterday my first steps with Acceleo. I've some general
questions concerning the beautification of the generated code.
1. Is there a common rule for indentation? When i write my templates
with normal indentation, like this
[file (testContext.name.concat('.ttcn3'), false)]
module [testContext.name/]{
[for (dPool : DataPool | testContext.allDataPool)]
[for (dp:DataPartition|dPool.ownedPartition)]
[genTypes(dp)/]
[/for]
[/for]
}
[/file]
the outcoming is very ugly aligned, since the whole [genTypes/] part is
too much indented.
We've implemented this as the specification shows it in its examples :
WYSIWYG. You've set three tabulations for the line "genTypes", and three
tabulations is what you get. We expect to have the specification here
change a little and set as many tabulations for _each_ line of
"genTypes" as what you have before its call, so that something like :
----------8<----------
[template public generateClass(c : EClass)]
class [c.name/] {
[for (attr : EAttribute | c.eAttributes())]
[generateAccessors(attr)/]
[/for]
}
[/template]
[template private generateAccessors(a : EAttribute)]
public [a.eType.name/] get[a.name.toUpperFirst()/]() {
return a.name;
}
[/template]
---------->8----------
properly generates something like :
----------8<----------
class MyClass {
public Object getObject() {
return object;
}
}
---------->8----------
Though for now, only the very first line of "generateAccessors" would
get indented with the tabulation preceding the call. This is still a
work in progress.
2. How to use the seperator(',') construct correctly in a for-loop.
Currentlty, the , will be inserted in the next line, not after the
generated line
boolean confirmationExpected
, integer data optional
The loop looks like
[for (p:Property | c.oclAsType(Classifier).attribute) separator(',')]
[if (p.type.oclIsTypeOf(PrimitiveType))][iterateBasicType(p)/]
[else]
Complex
[/if]
[/for]
The problem here is with your [if/] construct. The rules for whitespace
handling in 8.4 are too ambiguous to be properly implemented, and we've
yet to fully implement our own rules. Indeed, in the case you show here
(single-line then and else), the if shouldn't generate new lines, yet it
does for now. You can workaround this new line character by using a
single-line 'if' instead :
----------8<----------
[for (p:Property | c.oclAsType(Classifier).attribute) separator(',')]
[if
(p.type.oclIsTypeOf(PrimitiveType))][iterateBasicType(p)/][else]Complex[/if]
[/for]
---------->8----------
3. Is it possible to pass a Set as a template parameter?
Not with the current implementation of OCL as you cannot use constructs
like "Set(String)" in other places than variable declaration, thus
something like "let s : Set(String) = Set{'a', 'b'}" is legit, yet
"x.oclAsType(Set(String))" is not (and doesn't parse for that matter).
The incoming version 3.0 of MDT OCL should fix that issue since it
should follow the incoming OCL 2.1 specification.
4. I've tried to pass a parameter like c.oclAsKindOf(Class), where c is
a Classifier, but Acceleo told me, that such an template doesn't exist.
Well, the template is there.
[template public class2type(c:Class)]
Is it possible to downcast an object during the invocation of a template?
"oclAsKindOf" doesn't exist; and that's what the error message should
tell you. Use "oclAsType" to change the static type of your objects, and
"oclIsKindOf" to check if the cast is legit.
Take note that with the current MDT OCL implementation,
"'a'.oclIsKindOf(Integer)" will return false (that's what's expected),
but "'a'.oclAsType(Integer)" will properly change the static type of 'a'
so that "'a'.oclAsType(Integer)" will parse ... and won't fail at
evaluation time except if you try to use an Integer method on it. You
should always check before casting ;).
Thanks a lot
/Timothy
begin:vcard
fn:Laurent Goubet
n:Goubet;Laurent
org:<a href="http://www.obeo.fr/">Obeo</a>
email;internet:laurent.goubet@xxxxxxx
url:http://www.obeo.fr
version:2.1
end:vcard