Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Xcore how to define the value of an EnumLiterals
Xcore how to define the value of an EnumLiterals [message #893435] Wed, 04 July 2012 07:37 Go to next message
René Brandstetter is currently offline René BrandstetterFriend
Messages: 17
Registered: July 2012
Junior Member
Hi,

does anbody know how I can define the value of an EnumLiteral in a .xcore-File?

For example if I use ecore I was able to define the following properties on an EnumLiteral


Literal: ">"
Name: "GREATER_THAN"
Value: 2


and in xcore can only define the literal and the name like:


enum Operator
{
GREATER_THAN as ">"
LESS_THAN as "<"
...
}


In the generated java code the value of all enum literals is always 0 how can I specify this?

I already checked http://wiki.eclipse.org/Xcore#Specifying_an_Enumeration but there they didn't even explain the "as" keyword.

Btw does anybody knows hot to define the default value for attributes too for example how do I define null, "test" or what ever on an string attribute?

Regards

[Updated on: Wed, 04 July 2012 12:42]

Report message to a moderator

Re: Xcore how to define the value of an EnumLiterals [message #893440 is a reply to message #893435] Wed, 04 July 2012 07:59 Go to previous messageGo to next message
René Brandstetter is currently offline René BrandstetterFriend
Messages: 17
Registered: July 2012
Junior Member
I suddendly found the answer for the literal value in the example of http://wiki.eclipse.org/Xcore#Converting_a_GenModel_to_an_Xcore_Model


enum Operator
{
GREATER_THAN as ">" = 2
LESS_THAN as "<" = 3
...
}


This does also work with defining different default values for string attributes


class Test {
String testStr = "test"
}


But until now I can't find a way to define null as the default value and how to define a different default value for enums.

e.g.


class TestMyEnum{
Operator op
}


This will always use the first one as default which is GREATER_THAN but how can I define null or LESS_THAN as default?

[Updated on: Wed, 04 July 2012 12:42]

Report message to a moderator

Re: Xcore hwo to define the value of an EnumLiterals [message #893469 is a reply to message #893440] Wed, 04 July 2012 08:05 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Ren,

Comments below.

On 04/07/2012 9:59 AM, René Brandstetter wrote:
> I suddendly found the answer for the literal value in the example of
> http://wiki.eclipse.org/Xcore#Converting_a_GenModel_to_an_Xcore_Model
>
>
> enum Operator
> {
> GREATER_THAN as ">" = 2
> LESS_THAN as "<" = 3
> ..
> }
>
>
> This does also work with defining different default values for string
> attributes
>
>
> class Test {
> String testStr = "test"
> }
>
>
> But until now I can't find a way to define null as the default value
Null is the implicit default for all non-primitives.
> and how to define a different default value for enums.
Enums are handled as primitives so there is always a non-null default
value, i.e., the first enumerator. The default value is specified using
the literal form...
>
> e.g.
>
>
> class TestMyEnum{
> Operator op
> }
>
>
> This will always use the first one as default which is GREATER_THAN
> but how can I define null or LESS_THAN as default?
Operator op = "<" should work.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Xcore how to define the value of an EnumLiterals [message #893516 is a reply to message #893469] Wed, 04 July 2012 12:26 Go to previous messageGo to next message
René Brandstetter is currently offline René BrandstetterFriend
Messages: 17
Registered: July 2012
Junior Member
So it's better to make the property unsettable to have something like a null value.

Thanks for your help Ed.

BTW is there a chance to get an update (concerning the missing keywords like "as", "unsettable", ...) on the Xcore wiki page. (If you want I can add a section to the Enumeration Specification which mentions the "as" keyword.)

[Updated on: Wed, 04 July 2012 12:42]

Report message to a moderator

Re: Xcore hwo to define the value of an EnumLiterals [message #893527 is a reply to message #893516] Wed, 04 July 2012 13:01 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
René,<br>
<br>
Comments below.<br>
<br>
<div class="moz-cite-prefix">On 04/07/2012 2:26 PM, René
Brandstetter wrote:<br>
</div>
<blockquote cite="mid:jt1cq0$gf6$1@xxxxxxxxe.org" type="cite">So
it's better to make the property unsettable to have something like
a null value.
<br>
</blockquote>
EEnums are always treated as primitives so null is not possible.  
Making the feature unsettable lets you distinguish between just
having a default value and being explicitly set to a value that
happens to be the default value.<br>
<br>
If you absolutely need null values, you need to create a wrapper
EDataType and use that EDataType in place of the EEnum.<br>
<blockquote>enum NodeKind<br>
{<br>
    Singleton<br>
    Root<br>
    Intermediate<br>
    Leaf<br>
}<br>
<br>
@ExtendedMetaData(baseType="NodeKind")<br>
type NodeKindObject wraps Enumerator<br>
</blockquote>
<br>
<blockquote cite="mid:jt1cq0$gf6$1@xxxxxxxxe.org" type="cite">
<br>
Thanks for your help Ed.
<br>
<br>
BTW is there a chance to get an update (concerning the missing
keywords like "as", "unsettable", ...) on the Xcore wiki page.
<br>
</blockquote>
The wiki isn't aimed at being a comprehensive definitive reference
manual.  I'm not sure when I'll find time for that.<br>
<br>
<br>
</body>
</html>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Xcore how to define the value of an EnumLiterals [message #893533 is a reply to message #893527] Wed, 04 July 2012 13:18 Go to previous message
René Brandstetter is currently offline René BrandstetterFriend
Messages: 17
Registered: July 2012
Junior Member
Thx for your help.

BTW if you want you can copy the following text and but it to the the "Specifing an Enumeration" section of your wiki. (It's already encoded in the eclipse wiki style)
===== Original ecore properties and their xcore counterpart  =====

On the original ecore enumeration literal you can define the following properties:<br>

*Name<br>
*Literal<br>
*Value<br>

This can be defined in Xcore as<br>

''Name '''as''' "Literal" '''=''' Value''<br>

<u>Example:</u><br>
<pre>enum BookCategory
{
   Mystery as "MYSTERY" = 2
   ScienceFiction as "SiFi" = 1
   Biography as "Bio" = 0
}
</pre> 
The default value for literal and value are:<br>

*literal would be the same as the name<br>
*value would be 0<br>

===== Usage in xcore<br> =====

If you want to define a different enum default value for your attribute (e.g. ScienceFiction instead of Mystery) you can do this with an assignment of the literal.<br>

<u>Example:</u><br>
<pre>class Book
{
    String title
    int pages
    BookCategory bookCategory = "SiFi"
    container Library library opposite books
    referes Writer[] authors opposite books
}
</pre>
It's not possible to define null as the default value for an enum because EMF treats enums as primitve datatypes and those can't be null. But you can make the attribute "unsettable" and this will generate an isSet...() and an unset...() method for your enum. Those to methods can be used to distinguish between just having a default value and being explicitly set to a value that happens to be the default value. If you absolutly need null values, you need to create a wrapper EDataType and use EDataType in place of the Enum.<br>

<u>Example for unsettable:</u>
<pre>class Book
{
    String title
    int pages
    unsettable BookCategory bookCategory
    container Library library opposite books
    referes Writer[] authors opposite books
}</pre>
<br>

<u>Example for EDataType wrapping:</u>
<pre>@ExtendedMetaData(baseType="BookCategory")
type BookCategoryObject wraps Enumerator

class Book
{
    String title
    int pages
    BookCategoryObject bookCategory
    container Library library opposite books
    referes Writer[] authors opposite books
}</pre>
<br>

[Updated on: Wed, 04 July 2012 13:18]

Report message to a moderator

Previous Topic:Gen. Editor: Change Font Color
Next Topic:Xcore Validation
Goto Forum:
  


Current Time: Wed Apr 24 23:01:19 GMT 2024

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

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

Back to the top