Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [DynamicEMF] How to set an unchangeable using DynamicEMF
[DynamicEMF] How to set an unchangeable using DynamicEMF [message #1403701] Fri, 25 July 2014 12:54 Go to next message
Bertrand Quenin is currently offline Bertrand QueninFriend
Messages: 70
Registered: July 2009
Member
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 #1403707 is a reply to message #1403701] Fri, 25 July 2014 13:09 Go to previous messageGo to next message
Felix Dorner is currently offline Felix DornerFriend
Messages: 295
Registered: March 2012
Senior Member
Why not just make it read/write in your model? Otherwise you won't ever
be able to save/load instances and conserve the identifier. I don't
think you're supposed to change a metaattribute at runtime
(EPackage.freeze())?

Felix
Re: [DynamicEMF] How to set an unchangeable using DynamicEMF [message #1403710 is a reply to message #1403707] Fri, 25 July 2014 13:23 Go to previous messageGo to next message
Bertrand Quenin is currently offline Bertrand QueninFriend
Messages: 70
Registered: July 2009
Member
Hi Felix,

That's what I'm currently doing, but I'm using the model to generate various things and I'm currently handling the "identifier" as a special case. It's not really handy and I was wondering if there was another way.

Thanks,
BQ.
Re: [DynamicEMF] How to set an unchangeable using DynamicEMF [message #1403715 is a reply to message #1403701] Fri, 25 July 2014 14:03 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 30372
Registered: July 2009
Senior Member
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.
>
Previous Topic:[XCORE] Loosing Memory/Memory Exhaustion
Next Topic:[CDO] Unable to launch our product due to CDO issues
Goto Forum:
  


Current Time: Fri Aug 23 13:29:13 GMT 2019

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

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

Back to the top