Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Default value of EAttribute typed to an Enumeration
Default value of EAttribute typed to an Enumeration [message #431752] Thu, 23 July 2009 16:52 Go to next message
John T.E. Timm is currently offline John T.E. TimmFriend
Messages: 161
Registered: July 2009
Senior Member
I noticed that the default value of an attribute that has been typed to a
Java enumeration ends up being the first member of that enumeration and
that isESet checks whether the attribute is different from the default
value (as opposed to checking against null).

The problem I am facing is if I have an attribute that is required
(cardinality 1..1), it doesn't have a default literal value, and I want to
set it to the first member of an enumeration. It turns out not getting
serialized because both cases fail (isESet || (keepDefaults &&
getDefaultLiteral() != null).

Is there a way to override this behavior (other than doing so in codegen)
to solve my problem?

Thanks,

JT
Re: Default value of EAttribute typed to an Enumeration [message #431758 is a reply to message #431752] Thu, 23 July 2009 21:03 Go to previous messageGo to next message
John T.E. Timm is currently offline John T.E. TimmFriend
Messages: 161
Registered: July 2009
Senior Member
If I make an EEnum "unsettable" in the model, shouldn't it allow me to set
it to null? If this was possible, I think it would resolve the issue
below, otherwise, I think this could be a legitimate bug.

JT

John T.E. Timm wrote:

> I noticed that the default value of an attribute that has been typed to a
> Java enumeration ends up being the first member of that enumeration and
> that isESet checks whether the attribute is different from the default
> value (as opposed to checking against null).

> The problem I am facing is if I have an attribute that is required
> (cardinality 1..1), it doesn't have a default literal value, and I want to
> set it to the first member of an enumeration. It turns out not getting
> serialized because both cases fail (isESet || (keepDefaults &&
> getDefaultLiteral() != null).

> Is there a way to override this behavior (other than doing so in codegen)
> to solve my problem?

> Thanks,

> JT
Re: Default value of EAttribute typed to an Enumeration [message #431759 is a reply to message #431758] Thu, 23 July 2009 22:07 Go to previous messageGo to next message
John T.E. Timm is currently offline John T.E. TimmFriend
Messages: 161
Registered: July 2009
Senior Member
In Class.javajet, I ended up changing this line:

String staticDefaultValue = genFeature.getStaticDefaultValue();

to this:

String staticDefaultValue = (genFeature.isEnumType()&&
!genFeature.isSetDefaultValue()) ? "null" :
genFeature.getStaticDefaultValue();

And it appears to have solved my problem. Now to figure out what else gets
broken...

JT
Re: Default value of EAttribute typed to an Enumeration [message #431760 is a reply to message #431759] Thu, 23 July 2009 22:53 Go to previous messageGo to next message
John T.E. Timm is currently offline John T.E. TimmFriend
Messages: 161
Registered: July 2009
Senior Member
I also ended up modifying this part of setter generation in Class.javajet:
<%if (genFeature.isEnumType()) {%>
<%if (genModel.isVirtualDelegation()) {%>
<%=genFeature.getImportedType(genClass)%> <%=genFeature.getSafeName()%> =
new<%=genFeature.getCapName()%>;
<%} else {%>
<%=genFeature.getSafeName()%> = new<%=genFeature.getCapName()%>;
<%}%>
<%} else {%>

John T.E. Timm wrote:

> In Class.javajet, I ended up changing this line:

> String staticDefaultValue = genFeature.getStaticDefaultValue();

> to this:

> String staticDefaultValue = (genFeature.isEnumType()&&
> !genFeature.isSetDefaultValue()) ? "null" :
> genFeature.getStaticDefaultValue();

> And it appears to have solved my problem. Now to figure out what else gets
> broken...

> JT
Re: Default value of EAttribute typed to an Enumeration [message #431761 is a reply to message #431760] Thu, 23 July 2009 23:57 Go to previous messageGo to next message
John T.E. Timm is currently offline John T.E. TimmFriend
Messages: 161
Registered: July 2009
Senior Member
Of course after spending all this time investigating the code templates
and obsessing over getting an Enumeration based attribute to be null, I
realized that I all really need is for the attribute to be marked as
"unsettable" to add unset to the value space. How embarrasing. :) Anyways,
I see now that regardless of what I set the value to, if I call setXXX, it
will mark it as set and will be serialized as such. Unfortunately, I have
literally hundreds of attributes in my model that need to be changed.

Thakns,

JT
Re: Default value of EAttribute typed to an Enumeration [message #431767 is a reply to message #431761] Fri, 24 July 2009 11:30 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

--=-/Riwv3yOaf5JPGdE7NHz
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hi, John,

Myself, I always include an explicit "none" value in my enumerations, as
the first literal, when the enumeration needs to have optional
semantics. Sometimes I even go so far is implementing the "Null Object"
pattern by mixing in a common interface that defines an isUndefined()
operation, which usually is implemented in an enum as "this == NONE".

Would that help your situation? It would mean only updating your enums,
not your attributes of those types.

Cheers,

Christian


On Thu, 2009-07-23 at 23:57 +0000, John T.E. Timm wrote:

> Of course after spending all this time investigating the code templates
> and obsessing over getting an Enumeration based attribute to be null, I
> realized that I all really need is for the attribute to be marked as
> "unsettable" to add unset to the value space. How embarrasing. :) Anyways,
> I see now that regardless of what I set the value to, if I call setXXX, it
> will mark it as set and will be serialized as such. Unfortunately, I have
> literally hundreds of attributes in my model that need to be changed.
>
> Thakns,
>
> JT
>

--=-/Riwv3yOaf5JPGdE7NHz
Content-Type: text/html; charset="utf-8"

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
<META NAME="GENERATOR" CONTENT="GtkHTML/3.24.1.1">
</HEAD>
<BODY>
Hi, John,<BR>
<BR>
Myself, I always include an explicit &quot;none&quot; value in my enumerations, as the first literal, when the enumeration needs to have optional semantics.&nbsp; Sometimes I even go so far is implementing the &quot;Null Object&quot; pattern by mixing in a common interface that defines an isUndefined() operation, which usually is implemented in an enum as &quot;this == NONE&quot;.<BR>
<BR>
Would that help your situation?&nbsp; It would mean only updating your enums, not your attributes of those types.<BR>
<BR>
Cheers,<BR>
<BR>
Christian<BR>
<BR>
<BR>
On Thu, 2009-07-23 at 23:57 +0000, John T.E. Timm wrote:
<BLOCKQUOTE TYPE=CITE>
<PRE>
Of course after spending all this time investigating the code templates
and obsessing over getting an Enumeration based attribute to be null, I
realized that I all really need is for the attribute to be marked as
&quot;unsettable&quot; to add unset to the value space. How embarrasing. :) Anyways,
I see now that regardless of what I set the value to, if I call setXXX, it
will mark it as set and will be serialized as such. Unfortunately, I have
literally hundreds of attributes in my model that need to be changed.

Thakns,

JT

</PRE>
</BLOCKQUOTE>
</BODY>
</HTML>

--=-/Riwv3yOaf5JPGdE7NHz--
Re: Default value of EAttribute typed to an Enumeration [message #431774 is a reply to message #431761] Fri, 24 July 2009 15:20 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33136
Registered: July 2009
Senior Member
John,

I'm not sure in the end if you got what you wanted. You could make it
unsettable and you could specify an explicit default. If you really
want null to be a value, you have to create another EDataType that wraps
the generated enumeration type; any model with EEnums generated from XML
Schemas does this automatically to support nillable elements...


John T.E. Timm wrote:
> Of course after spending all this time investigating the code
> templates and obsessing over getting an Enumeration based attribute to
> be null, I realized that I all really need is for the attribute to be
> marked as "unsettable" to add unset to the value space. How
> embarrasing. :) Anyways, I see now that regardless of what I set the
> value to, if I call setXXX, it will mark it as set and will be
> serialized as such. Unfortunately, I have literally hundreds of
> attributes in my model that need to be changed.
>
> Thakns,
>
> JT
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Default value of EAttribute typed to an Enumeration [message #431776 is a reply to message #431774] Fri, 24 July 2009 16:03 Go to previous messageGo to next message
John T.E. Timm is currently offline John T.E. TimmFriend
Messages: 161
Registered: July 2009
Senior Member
Ed:

What it came down to was that I actually didn't need null. What I really
needed was the "unset" value. The isUnsettable property takes care of this
for me.

Thanks,

JT

Ed Merks wrote:

> John,

> I'm not sure in the end if you got what you wanted. You could make it
> unsettable and you could specify an explicit default. If you really
> want null to be a value, you have to create another EDataType that wraps
> the generated enumeration type; any model with EEnums generated from XML
> Schemas does this automatically to support nillable elements...


> John T.E. Timm wrote:
>> Of course after spending all this time investigating the code
>> templates and obsessing over getting an Enumeration based attribute to
>> be null, I realized that I all really need is for the attribute to be
>> marked as "unsettable" to add unset to the value space. How
>> embarrasing. :) Anyways, I see now that regardless of what I set the
>> value to, if I call setXXX, it will mark it as set and will be
>> serialized as such. Unfortunately, I have literally hundreds of
>> attributes in my model that need to be changed.
>>
>> Thakns,
>>
>> JT
>>
Re: Default value of EAttribute typed to an Enumeration [message #431777 is a reply to message #431767] Fri, 24 July 2009 16:09 Go to previous message
John T.E. Timm is currently offline John T.E. TimmFriend
Messages: 161
Registered: July 2009
Senior Member
Hi Christian,

Thank you for your suggestions. I am modeling various controlled
vocabularies that are part of a standard with these enumerations. I think
that adding an explicit "none" value could work well in my situation but I
would like to leave the actual enumerations alone if possible. In my
application, I decided that I care more about the "unset" value. At first
I thought that getting the Enumeration to be null was the only way, but
with the isUnsettable option, the generated code keeps track of a separate
"unset" value which I feel should work well.

Thanks,

JT

Christian W. Damus wrote:

> Hi, John,

> Myself, I always include an explicit "none" value in my enumerations, as
> the first literal, when the enumeration needs to have optional
> semantics. Sometimes I even go so far is implementing the "Null Object"
> pattern by mixing in a common interface that defines an isUndefined()
> operation, which usually is implemented in an enum as "this == NONE".

> Would that help your situation? It would mean only updating your enums,
> not your attributes of those types.

> Cheers,

> Christian


> On Thu, 2009-07-23 at 23:57 +0000, John T.E. Timm wrote:

>> Of course after spending all this time investigating the code templates
>> and obsessing over getting an Enumeration based attribute to be null, I
>> realized that I all really need is for the attribute to be marked as
>> "unsettable" to add unset to the value space. How embarrasing. :) Anyways,
>> I see now that regardless of what I set the value to, if I call setXXX, it
>> will mark it as set and will be serialized as such. Unfortunately, I have
>> literally hundreds of attributes in my model that need to be changed.
>>
>> Thakns,
>>
>> JT
>>
Previous Topic:Dynamic insertion of a Attribute
Next Topic:[CDO] Column name generation
Goto Forum:
  


Current Time: Thu Apr 18 16:52:21 GMT 2024

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

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

Back to the top