Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Epsilon » Working with enumerations and casting to subclass
Working with enumerations and casting to subclass [message #479087] Sun, 09 August 2009 00:36 Go to next message
Eclipse UserFriend
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 Go to previous messageGo to next message
Louis Rose is currently offline Louis RoseFriend
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 Go to previous messageGo to next message
Eclipse UserFriend
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 #479468 is a reply to message #479414] Tue, 11 August 2009 09:05 Go to previous message
Louis Rose is currently offline Louis RoseFriend
Messages: 440
Registered: July 2009
Location: York, United Kingdom
Senior Member
Hey Lukman,

Reply hidden below :)

Lukman wrote:
> 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

I found the above description a little ambiguous. I think the IF
statement says the following:

If transition.source.kind is not junction and not initial and if
transition.target.kind is not junction, then generate transitions for
transition.

Is that what you wanted to express?

>
> But the generateTransitions operation is still being called even when
> transition.source is initial.

Ok, one approach is to debug this using println() statements. Try
adjusting the above code to read:

else{
('Source: ' + transition.source.kind).println();
('Target: ' + transition.target.kind).println();

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);
}
}

>
> Can anyone point out what is wrong with my IF statement.
>

The IF statement looks ok. I suspect that the kind property might not be
of type PseudoStateKind for some reason. I might be wrong though!

If debugging with println() statements doesn't help, would you attach
your project to another post, and I'll take a closer look. Thanks!

Cheers,
Louis.
Re: Working with enumerations and casting to subclass [message #573256 is a reply to message #479087] Sun, 09 August 2009 10:25 Go to previous message
Louis Rose is currently offline Louis RoseFriend
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 #573592 is a reply to message #479110] Mon, 10 August 2009 22:45 Go to previous message
Lukman  is currently offline Lukman Friend
Messages: 8
Registered: August 2010
Junior Member
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 #573614 is a reply to message #479414] Tue, 11 August 2009 09:05 Go to previous message
Louis Rose is currently offline Louis RoseFriend
Messages: 440
Registered: July 2009
Location: York, United Kingdom
Senior Member
Hey Lukman,

Reply hidden below :)

Lukman wrote:
> 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

I found the above description a little ambiguous. I think the IF
statement says the following:

If transition.source.kind is not junction and not initial and if
transition.target.kind is not junction, then generate transitions for
transition.

Is that what you wanted to express?

>
> But the generateTransitions operation is still being called even when
> transition.source is initial.

Ok, one approach is to debug this using println() statements. Try
adjusting the above code to read:

else{
('Source: ' + transition.source.kind).println();
('Target: ' + transition.target.kind).println();

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);
}
}

>
> Can anyone point out what is wrong with my IF statement.
>

The IF statement looks ok. I suspect that the kind property might not be
of type PseudoStateKind for some reason. I might be wrong though!

If debugging with println() statements doesn't help, would you attach
your project to another post, and I'll take a closer look. Thanks!

Cheers,
Louis.
Previous Topic:Preconditions and postconditions for operations and ErlParserRules.g
Next Topic:Re: Epsilon book: EOL Assertion operations not mentioned?
Goto Forum:
  


Current Time: Fri Mar 29 04:38:44 GMT 2024

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

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

Back to the top