[DynamicEMF] How to set an unchangeable using DynamicEMF [message #1403701] |
Fri, 25 July 2014 08:54  |
Eclipse User |
|
|
|
Hi guys,
I have an interface in my model defined like this:
abstract class Identifiable { interface }
{
attribute identifier : String { id readonly };
}
When I generate the code, I need to change the generated code in the following manner to set the "identifier" attribute:
/**
* The default value of the '{@link #getIdentifier() <em>Identifier</em>}' attribute. <!-- begin-user-doc --> <!-- end-user-doc -->
*
* @see #getIdentifier()
* @generated
* @ordered
*/
protected static final String IDENTIFIER_EDEFAULT = null;
/**
* The cached value of the '{@link #getIdentifier() <em>Identifier</em>}' attribute. <!-- begin-user-doc --> <!-- end-user-doc -->
*
* @see #getIdentifier()
* @generated
* @ordered
*/
protected String identifier = UUID.randomUUID().tostring();
This works fine for instances created through the package factory.
However, sometimes I need to instantiate these objects with dynamic EMF:
// Here, the provided eClass has "Identifiable" as a super type
EObject instance = new DynamicEObjectImpl(eClass);
try {
EcoreUtil.setID(instance, UUID.randomUUID.toString)
} catch (IllegalArgumentException e) {
// Oops
}
And I can't set the identifier because the feature is not changeable. Is there a way to set "unchangeable" field there ?
Note that the provided EClass is created dynamically as well and inherits "Indentifiable":
val newClass = eb.eClass.name(userClass.name).build
newClass.EStructuralFeatures += userClass.fields.map [
...
]
// new eClass needs to have an identifier as well
newClass.ESuperTypes += IDENTIFIABLE
...
Regards,
BQ.
|
|
|
|
|
Re: [DynamicEMF] How to set an unchangeable using DynamicEMF [message #1403715 is a reply to message #1403701] |
Fri, 25 July 2014 10:03  |
Eclipse User |
|
|
|
Bertrand,
Comments below.
On 25/07/2014 2:54 PM, Bertrand Quenin wrote:
> Hi guys,
>
> I have an interface in my model defined like this:
>
>
> abstract class Identifiable { interface }
> {
> attribute identifier : String { id readonly };
> }
>
>
> When I generate the code, I need to change the generated code in the
> following manner to set the "identifier" attribute:
>
>
> /**
> * The default value of the '{@link #getIdentifier()
> <em>Identifier</em>}' attribute. <!-- begin-user-doc --> <!--
> end-user-doc -->
> * * @see #getIdentifier()
> * @generated
> * @ordered
> */
> protected static final String IDENTIFIER_EDEFAULT = null;
>
> /**
> * The cached value of the '{@link #getIdentifier()
> <em>Identifier</em>}' attribute. <!-- begin-user-doc --> <!--
> end-user-doc -->
> * * @see #getIdentifier()
> * @generated
> * @ordered
> */
> protected String identifier =
> UUID.randomUUID().tostring();
Is this feature also transient? When reading something in, you don't
generally want to do this.
>
>
>
> This works fine for instances created through the package factory.
>
> However, sometimes I need to instantiate these objects with dynamic EMF:
>
>
> // Here, the provided eClass has "Identifiable" as a super type
> EObject instance = new DynamicEObjectImpl(eClass);
>
> try {
> EcoreUtil.setID(instance, UUID.randomUUID.toString)
> } catch (IllegalArgumentException e) {
> // Oops
> }
>
Probably better to use
org.eclipse.emf.ecore.impl.MinimalEObjectImpl.Container.Dynamic, but
that's just a footprint consideration.
>
> And I can't set the identifier because the feature is not changeable.
> Is there a way to set "unchangeable" field there ?
If it's a dynamic Ecore model, you can make it changeable in that
model. Presumably it's not the generated model, otherwise you'd not be
trying to make a dynamic version of it.
>
> Note that the provided EClass is created dynamically as well and
> inherits "Indentifiable":
>
>
> val newClass = eb.eClass.name(userClass.name).build
> newClass.EStructuralFeatures += userClass.fields.map [
> ...
> ]
>
> // new eClass needs to have an identifier as well
> newClass.ESuperTypes += IDENTIFIABLE
>
> ...
Oh, so the unchangeable generated class already has the problem.
You really need to rethink how these IDs are managed. If you're going
to save instances, presumably you want the ID preserved when you read it
back in. That obviously don't work at all. So what's the purpose of
this "auto generating" of an ID? Is that something you need
programmatically or something you need to make the user's life easier?
Perhaps better would be for the resource to manage the assignment of
IDs, much as XMLResourceImpl manages extrinsic IDs, but perhaps melding
the two approaches and have the resource assign an ID to any object that
doesn't already have an ID that's set....
>
>
> Regards,
> BQ.
>
|
|
|
Powered by
FUDForum. Page generated in 0.02722 seconds