Home » Archived » M2M (model-to-model transformation) » [ATL] Does ATL support type casting?
|
Re: [ATL] Does ATL support type casting? [message #71228 is a reply to message #71169] |
Wed, 09 January 2008 16:15 |
Eclipse User |
|
|
|
Originally posted by: mikael.barbero.gmail.com
The OCL cast (oclAsType) operation is not supported by ATL but I do not
ever met a situation where type casting is unavoidable ;)
Post your situation there, we may help you!
Best regards,
Mikael
Michael wrote:
> Hi,
> I am wandering ATL support type casting or not. If so, how can we cast
> a variable to another type, which is a sub-type of the original type. If
> not, how can we handle the situation in which type casting is unavoided?
> Thanks.
> Best regards.
> Michael.
>
>
--
Mikaël Barbero - PhD Candidate
ATLAS Group (INRIA & LINA) - University of Nantes
2, rue de la Houssinière
44322 Nantes Cedex 3 - France
tel. +33 2 51 12 58 08 /\ cell.+33 6 07 63 19 00
email: Mikael.Barbero@{gmail.com, univ-nantes.fr}
http://www.sciences.univ-nantes.fr/lina/atl/
|
|
|
Re: [ATL] Does ATL support type casting? [message #71345 is a reply to message #71228] |
Wed, 09 January 2008 17:26 |
Mikai Yang Messages: 149 Registered: July 2009 |
Senior Member |
|
|
This is a multi-part message in MIME format.
------=_NextPart_000_00E6_01C852E4.B99303E0
Content-Type: text/plain;
charset="ISO-8859-1"
Content-Transfer-Encoding: quoted-printable
Hi,=20
For example, we have the following metamodel.=20
class Car {
color : Color=20
}=20
=20
abstract class Color{
}=20
class Red extends Color {
date: Java.util.Date // the purchase =
date. =20
person: String // The name of =
the person who painted this color.=20
}
class Blue extends Color{=20
date: Java.util.Date =20
}
class Green extends Color {
date: Java.util.Date
}
=20
From above metamodel, we know Car is associated with Color. =
Color has three sub-classes: Red, Blue, Green.=20
Now, we would like to use a contextual hepler of Car to get =
the painting record of one car.=20
We first define a contextual attribute of Color, like:=20
helper context Color def:colorRecord:String =3D =
if self.oclIsTypeOf(Red) then 'Red ' + =
'Painted by ' + self.person + ' in ' + self.date=20
else
if self.oclIsTypeOf(Blue) then 'Blue ' + =
'Painted by ' + self.person + ' in ' + self.date
else=20
if self.oclIsTypeOf(Green) then ''Green ' =
+ 'Painted by ' + self.person + ' in ' + self.date
endif =20
Then we get color history of a car, like=20
=20
helper context Car def: history(): String =3D =
let color: Color =3D self.color in=20
color.colorRecord
;=20
=20
In above, we hope to use 'self' to access attributes =
defined in the sub-classes of its type. This is illegal, unless we have =
cast the variable to a type of the corresponding subclass. This is why I =
raise the question about the type cast of ATL.=20
=20
I hope my example makes some sense. Thanks.=20
Best regards.=20
Michael.=20
=20
"Mika=EBl Barbero" <mikael.barbero@gmail.com> wrote in message =
news:fm2rvb$qg1$3@build.eclipse.org...
> The OCL cast (oclAsType) operation is not supported by ATL but I do =
not=20
> ever met a situation where type casting is unavoidable ;)
> Post your situation there, we may help you!
>=20
> Best regards,
> Mikael
>=20
> Michael wrote:
>> Hi,
>> I am wandering ATL support type casting or not. If so, how can =
we cast=20
>> a variable to another type, which is a sub-type of the original type. =
If=20
>> not, how can we handle the situation in which type casting is =
unavoided?
>> Thanks.
>> Best regards.
>> Michael.=20
>>=20
>>=20
>=20
>=20
>=20
> --=20
> Mika=EBl Barbero - PhD Candidate
> ATLAS Group (INRIA & LINA) - University of Nantes
> 2, rue de la Houssini=E8re
> 44322 Nantes Cedex 3 - France
> tel. +33 2 51 12 58 08 /\ cell.+33 6 07 63 19 00
> email: Mikael.Barbero@{gmail.com, univ-nantes.fr}
> http://www.sciences.univ-nantes.fr/lina/atl/
------=_NextPart_000_00E6_01C852E4.B99303E0
Content-Type: text/html;
charset="ISO-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2900.3243" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>Hi, </FONT></DIV>
<DIV><FONT face=3DArial size=3D2> For example, =
we have the=20
following metamodel. </FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; =20
class Car {</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; &nbs p; =20
color : Color </FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; }=20
</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; =20
</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; abstract class=20
Color{</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; =20
} </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; =20
class Red extends Color {</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; &nbs p; &nbs p=
;   ; date:=20
Java.util.Date // the purchase date.=20
</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; &nbs p; &nbs p=
;   ; person:=20
String & ;nbsp; // =
The name=20
of the person who painted this color. </FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; &nbs p; &nbs p=
; =20
}</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; =20
class Blue extends Color{ </FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; &nbs p; &nbs p=
;   ; =20
date: Java.util.Date </FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; =20
}</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; =20
class Green extends Color {</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; &nbs p; &nbs p=
;   ; date:=20
Java.util.Date</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; =20
}</FONT></DIV>
<DIV><FONT face=3DArial =
size=3D2> =20
</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> From above =
metamodel, we=20
know Car is associated with Color. Color has three =
sub-classes: Red,=20
Blue, Green. </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial=20
size=3D2> Now, we =
would like=20
to use a contextual hepler of Car to get the painting record =
of=20
one car. </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial=20
size=3D2> We first =
define a=20
contextual attribute of Color, like: </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; &nbs p; =20
helper context Color def:colorRecord:String =3D=20
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; &nbs p; &nbs p=
;   ; if=20
self.oclIsTypeOf(Red) then 'Red ' + 'Painted by ' +<U>=20
self.person</U> + ' in ' + <U>self.date</U>=20
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; &nbs p; &nbs p=
;   ; =20
else</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; &nbs p; &nbs p=
;   ; =20
if self.oclIsTypeOf(Blue) then 'Blue ' + 'Painted by ' +=20
<U>self.person</U> + ' in ' + <U>self.date</U></FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; &nbs p; &nbs p=
;   ; =20
else </FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; &nbs p; &nbs p=
;   ; if=20
self.oclIsTypeOf(Green) then ''Green ' + 'Painted by ' +=20
<U>self.person</U> + ' in ' + <U>self.date</U></FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; &nbs p; &nbs p=
;   ;=20
endif </FONT></DIV>
<DIV> </DIV>
<DIV> Then we=20
get color history of a car, like </DIV>
<DIV> </DIV>
<DIV> &n=
bsp; &n bsp; &n b=
sp; helper=20
context Car def: history(): String =3D </DIV>
<DIV> &n=
bsp; &n bsp; &n b=
sp; &nb sp; =20
let color: Color =3D self.color in </DIV>
<DIV> &n=
bsp; &n bsp; &n b=
sp; &nb sp; &nb s=
p; &nbs p;=20
color.colorRecord</DIV>
<DIV> &n=
bsp; &n bsp; &n b=
sp; =20
; </DIV>
<DIV> </DIV></FONT></DIV></FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; </FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; In=20
above, we hope to use 'self' to access attributes defined in the=20
sub-classes of its type. This is illegal, unless we have cast the =
variable to a=20
type of the corresponding subclass. This is why I raise the question =
about the=20
type cast of ATL. </FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p;=20
</FONT></DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; =20
I hope my example makes some sense. Thanks. </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Best regards. </FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Michael. </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial=20
size=3D2> &nbs=
p; </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>"Mika=EBl Barbero" <</FONT><A=20
href=3D"mailto:mikael.barbero@gmail.com"><FONT face=3DArial=20
size=3D2>mikael.barbero@gmail.com</FONT></A><FONT face=3DArial =
size=3D2>> wrote in=20
message </FONT><A href=3D"news:fm2rvb$qg1$3@build.eclipse.org"><FONT =
face=3DArial=20
size=3D2>news:fm2rvb$qg1$3@build.eclipse.org</FONT></A><FONT =
face=3DArial=20
size=3D2>...</FONT></DIV><FONT face=3DArial size=3D2>> The OCL cast =
(oclAsType)=20
operation is not supported by ATL but I do not <BR>> ever met a =
situation=20
where type casting is unavoidable ;)<BR>> Post your situation there, =
we may=20
help you!<BR>> <BR>> Best regards,<BR>> Mikael<BR>> <BR>> =
Michael=20
wrote:<BR>>> Hi,<BR> >> & ;nbsp; I am =
wandering=20
ATL support type casting or not. If so, how can we cast <BR>>> a =
variable=20
to another type, which is a sub-type of the original type. If =
<BR>>> not,=20
how can we handle the situation in which type casting is=20
unavoided?<BR> >> & ;nbsp; Thanks.<BR>>> =
Best=20
regards.<BR>>> Michael. <BR>>> <BR>>> <BR>> =
<BR>>=20
<BR>> <BR>> -- <BR>> Mika=EBl Barbero - PhD Candidate<BR>> =
ATLAS Group=20
(INRIA & LINA) - University of Nantes<BR>> 2, rue de la=20
Houssini=E8re<BR>> 44322 Nantes Cedex 3 - France<BR>> tel. +33 2 =
51 12 58 08=20
/\ cell.+33 6 07 63 19 00<BR>> email: </FONT><A=20
href=3D"mailto:Mikael.Barbero@{gmail.com"><FONT face=3DArial=20
size=3D2>Mikael.Barbero@{gmail.com</FONT></A><FONT face=3DArial =
size=3D2>,=20
univ-nantes.fr}<BR>> </FONT><A=20
href=3D"http://www.sciences.univ-nantes.fr/lina/atl/"><FONT face=3DArial =
size=3D2>http://www.sciences.univ-nantes.fr/lina/atl/</FONT></A></BODY></=
HTML>
------=_NextPart_000_00E6_01C852E4.B99303E0--
|
|
|
Re: [ATL] Does ATL support type casting? [message #71441 is a reply to message #71345] |
Thu, 10 January 2008 10:03 |
Eclipse User |
|
|
|
Originally posted by: mikael.barbero.gmail.com
Hi,
First of all, I don't see why you used let expression in the history
helper. Why don't you do simply:
helper context MM!Car def: history: String =
self.color.colorRecord;
Note that I removed "()" to make it an attribute helper, i.e. its
results is cached during the transformation execution.
Now, regarding your helper colorRecord, it should be illegal at
compile-time if ATL had a static typing systems similar to Java's. But
ATL is dynamically typed and then it will not throw any error at
compile-time for your helper but will fail at runtime (for the Blue
self.person and the Green self.person call).
Then, your helper should be rewritten like this (with a bit of
refactoring to make it cleaner ;) ):
helper context MM!Color def: colorRecord : String =
if self.oclIsTypeOf(MM!Red) then
'Red Painted by ' + self.person
else if self.oclIsTypeOf(MM!Blue) then
'Blue'
else if self.oclIsTypeOf(MM!Green) then
'Green'
endif endif endif + ' in ' + self.date;
And this is legal at runtime and will not fail at compile-time! It may
be harder to code and debug but it also offer more flexibility in your
ATL code.
Best regards,
Mikael
Michael wrote:
> Hi,
> For example, we have the following metamodel.
> class Car {
> color : Color
> }
>
> abstract class Color{
> }
>
> class Red extends Color {
> date: Java.util.Date // the purchase
> date.
> person: String // The name of
> the person who painted this color.
> }
>
> class Blue extends Color{
> date: Java.util.Date
> }
>
> class Green extends Color {
> date: Java.util.Date
> }
>
> From above metamodel, we know Car is associated with Color.
> Color has three sub-classes: Red, Blue, Green.
>
> Now, we would like to use a contextual hepler of Car to get
> the painting record of one car.
>
> We first define a contextual attribute of Color, like:
>
> helper context Color def:colorRecord:String =
> if self.oclIsTypeOf(Red) then 'Red ' +
> 'Painted by ' +_ self.person_ + ' in ' + _self.date_
> else
> if self.oclIsTypeOf(Blue) then 'Blue ' +
> 'Painted by ' + _self.person_ + ' in ' + _self.date_
> else
> if self.oclIsTypeOf(Green) then ''Green '
> + 'Painted by ' + _self.person_ + ' in ' + _self.date_
> endif
>
> Then we get color history of a car, like
>
> helper context Car def: history(): String =
> let color: Color = self.color in
> color.colorRecord
> ;
>
>
> In above, we hope to use 'self' to access attributes
> defined in the sub-classes of its type. This is illegal, unless we have
> cast the variable to a type of the corresponding subclass. This is why I
> raise the question about the type cast of ATL.
>
> I hope my example makes some sense. Thanks.
>
> Best regards.
> Michael.
>
>
>
> "Mikaël Barbero" <mikael.barbero@gmail.com
> <mailto:mikael.barbero@gmail.com>> wrote in message
> news:fm2rvb$qg1$3@build.eclipse.org...
> > The OCL cast (oclAsType) operation is not supported by ATL but I do not
> > ever met a situation where type casting is unavoidable ;)
> > Post your situation there, we may help you!
> >
> > Best regards,
> > Mikael
> >
> > Michael wrote:
> >> Hi,
> >> I am wandering ATL support type casting or not. If so, how can
> we cast
> >> a variable to another type, which is a sub-type of the original
> type. If
> >> not, how can we handle the situation in which type casting is unavoided?
> >> Thanks.
> >> Best regards.
> >> Michael.
> >>
> >>
> >
> >
> >
> > --
> > Mikaël Barbero - PhD Candidate
> > ATLAS Group (INRIA & LINA) - University of Nantes
> > 2, rue de la Houssinière
> > 44322 Nantes Cedex 3 - France
> > tel. +33 2 51 12 58 08 /\ cell.+33 6 07 63 19 00
> > email: Mikael.Barbero@{gmail.com <mailto:Mikael.Barbero@{gmail.com>,
> univ-nantes.fr}
> > http://www.sciences.univ-nantes.fr/lina/atl/
--
Mikaël Barbero - PhD Candidate
ATLAS Group (INRIA & LINA) - University of Nantes
2, rue de la Houssinière
44322 Nantes Cedex 3 - France
tel. +33 2 51 12 58 08 /\ cell.+33 6 07 63 19 00
email: Mikael.Barbero@{gmail.com, univ-nantes.fr}
http://www.sciences.univ-nantes.fr/lina/atl/
|
|
|
Goto Forum:
Current Time: Tue Apr 23 12:46:08 GMT 2024
Powered by FUDForum. Page generated in 0.03891 seconds
|