Working with enumerations and casting to subclass [message #479087] |
Sun, 09 August 2009 00:36 |
Eclipse User |
|
|
|
Originally posted by: lukmanabrahim.gmail.com
Hi,
Right now I'm working with EGL and I have a problem with enumerations in
the metamodel. How do I refer to elements in the enumeration in IF
statement?
Another problem that I got is with subclasses in the metamodel. This is
the snippet of my EGL template.
if(transition.source.isKindOf(Pseudostate)=false){
generateTransitions();
}else{
------
}
In the ELSE section I want to add another IF statement that will check
certain properties of Pseudostate. In the metamodel transition.source can
be either Pseudostate or SimpleState. Do I need to cast transition.source
to Pseudostate in the ELSE section? If I do, how do I do it?
Cheers,
Lukman
|
|
|
Re: Working with enumerations and casting to subclass [message #479110 is a reply to message #479087] |
Sun, 09 August 2009 10:25 |
Louis Rose Messages: 440 Registered: July 2009 Location: York, United Kingdom |
Senior Member |
|
|
Hi Lukman,
Lukman wrote:
> Hi,
>
> Right now I'm working with EGL and I have a problem with enumerations in
> the metamodel. How do I refer to elements in the enumeration in IF
> statement?
EOL provides the # operator for accessing enumeration literals. For
example, the VisibilityEnum#vk_public expression returns the value of
the literal vk_public of the VisibilityEnum enumeration.
Attribute values can be tested against enumeration values in if
statements, like this:
if (method.visiblity = VisibilityEnum#vk_public) {
-- do something specific to public methods
} else {
-- do something else
}
I've taken this information from the Epsilon book (Section 4.4.3), which
can be downloaded for free here:
http://www.eclipse.org/gmt/epsilon/doc/book/
>
> Another problem that I got is with subclasses in the metamodel. This is
> the snippet of my EGL template.
>
> if(transition.source.isKindOf(Pseudostate)=false){
> generateTransitions();
> }else{
> ------
> }
>
> In the ELSE section I want to add another IF statement that will check
> certain properties of Pseudostate. In the metamodel transition.source
> can be either Pseudostate or SimpleState. Do I need to cast
> transition.source to Pseudostate in the ELSE section? If I do, how do I
> do it?
>
No, casting is not necessary because the Epsilon languages use dynamic
typing. Attempting to call an undefined feature / method results in an
error at runtime, not at compile time.
In the else part above, you can simply call the features defined by
Pseudostate -- no casting necessary.
Best,
Louis.
----
Louis Rose
Research Student
Department of Computer Science,
University of York,
Heslington, York, YO10 5DD, United Kingdom.
+44 1904 434762
Twitter: @louismrose
|
|
|
Re: Working with enumerations and casting to subclass [message #479414 is a reply to message #479110] |
Mon, 10 August 2009 22:45 |
Eclipse User |
|
|
|
Originally posted by: lukmanabrahim.gmail.com
Thank you for the post. The solution that you gave me works.
Now the code in the ELSE section looks like this
else{
if((not transition.source.kind=PseudostateKind#junction.instance) and
(not transition.target.kind=PseudostateKind#junction.instance) and
(not transition.source.kind=PseudostateKind#initial.instance)){
generateTransitions(transition);
}
}
This doesn't work. The condition that I want to write is call
generateTransitions operation when
1) transition.source is not a junction or initial
2) transition.target is not a junction
But the generateTransitions operation is still being called even when
transition.source is initial.
Can anyone point out what is wrong with my IF statement.
Lukman
|
|
|
|
Re: Working with enumerations and casting to subclass [message #573256 is a reply to message #479087] |
Sun, 09 August 2009 10:25 |
Louis Rose Messages: 440 Registered: July 2009 Location: York, United Kingdom |
Senior Member |
|
|
Hi Lukman,
Lukman wrote:
> Hi,
>
> Right now I'm working with EGL and I have a problem with enumerations in
> the metamodel. How do I refer to elements in the enumeration in IF
> statement?
EOL provides the # operator for accessing enumeration literals. For
example, the VisibilityEnum#vk_public expression returns the value of
the literal vk_public of the VisibilityEnum enumeration.
Attribute values can be tested against enumeration values in if
statements, like this:
if (method.visiblity = VisibilityEnum#vk_public) {
-- do something specific to public methods
} else {
-- do something else
}
I've taken this information from the Epsilon book (Section 4.4.3), which
can be downloaded for free here:
http://www.eclipse.org/gmt/epsilon/doc/book/
>
> Another problem that I got is with subclasses in the metamodel. This is
> the snippet of my EGL template.
>
> if(transition.source.isKindOf(Pseudostate)=false){
> generateTransitions();
> }else{
> ------
> }
>
> In the ELSE section I want to add another IF statement that will check
> certain properties of Pseudostate. In the metamodel transition.source
> can be either Pseudostate or SimpleState. Do I need to cast
> transition.source to Pseudostate in the ELSE section? If I do, how do I
> do it?
>
No, casting is not necessary because the Epsilon languages use dynamic
typing. Attempting to call an undefined feature / method results in an
error at runtime, not at compile time.
In the else part above, you can simply call the features defined by
Pseudostate -- no casting necessary.
Best,
Louis.
----
Louis Rose
Research Student
Department of Computer Science,
University of York,
Heslington, York, YO10 5DD, United Kingdom.
+44 1904 434762
Twitter: @louismrose
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02592 seconds