Skip to main content



      Home
Home » Modeling » EMF » Changing EType of an EAttribute @ runtime
Changing EType of an EAttribute @ runtime [message #416601] Thu, 07 February 2008 09:26 Go to next message
Eclipse UserFriend
Hi guys,

Before talking about my problem i'll give a brief background.
I have an EMF model (M1 say) for which i have model and edit code
generated. Now, M1 is volatile, meaning addition of new attributes can
happen or change of existing attributes can happen on M1.

Keeping the volatility in mind, i let the user add new attributes to the
model (M1) and the ReflectiveProviders take care of bringing them on the
UI.
In brief, @ run-time i create a copy of my M1 EPackage, and add any new
attributes to that in memory EPackage (M2 say). And any further processing
is done through the M2 EPackage.

the problem:
now the problem comes when there is a change in data-type of an existing
attribute. (lets consider Integer to Float). Although in the M2 EPacakge
the existing attribute's data-type is changed from Integer to Float... if
i do an eSet on the attribute (after data-type change) setters from the
generated code are invoked which still take in an Integer and hence i get
a ClassCastException.

when i do an eSet or an eGet for a feature thru my in memory M2 EPackage
... i don't want the existing generated code to be invoked. coz its
obsolete ... is there any way to achieve this.

this is the most brief i could get.

cheers,
Chetan Kumar
Re: Changing EType of an EAttribute @ runtime [message #416603 is a reply to message #416601] Thu, 07 February 2008 10:13 Go to previous messageGo to next message
Eclipse UserFriend
Hi Chetan,

I see two possible issues:

1. I'm not sure how you're copying your M1 EPackage. But, if whatever
you're doing yields another EPackage that uses your generated M1
factory, then it will create instances of your generated implementation
classes.

Try calling getEFactoryInstance() to obtain the factory from your M2
package. If it's just an instance of EFactoryImpl, not your generated
factory class, this isn't the problem.

2. Dynamic EMF does not support changing the model after you've started
instantiating it. EObjectImpl allocates typed setting holders for its
features, so it's just not possible to add, remove, or change the
attributes of classes that have already been instantiated. If this is
what you're trying to do, it just won't work.

It might be possible for you to derive an EObject implementation that is
more tolerant of changes in the model (for example, by using a map,
instead of an array, to store settings). But, I'd still expect you'll
face some nasty problems, such as how to update existing instances when
a feature changes or is removed, which is why we've never done that.

One other potential problem I see looming is that you shouldn't really
be using two different packages with the same namespace URI. Having
multiple copies, even identical, of metadata tends to get people in trouble.

Cheers,
Dave


Chetan Kumar wrote:
> Hi guys,
>
> Before talking about my problem i'll give a brief background.
> I have an EMF model (M1 say) for which i have model and edit code
> generated. Now, M1 is volatile, meaning addition of new attributes can
> happen or change of existing attributes can happen on M1.
>
> Keeping the volatility in mind, i let the user add new attributes to
> the model (M1) and the ReflectiveProviders take care of bringing them on
> the UI. In brief, @ run-time i create a copy of my M1 EPackage, and add
> any new attributes to that in memory EPackage (M2 say). And any further
> processing is done through the M2 EPackage.
>
> the problem:
> now the problem comes when there is a change in data-type of an
> existing attribute. (lets consider Integer to Float). Although in the M2
> EPacakge the existing attribute's data-type is changed from Integer to
> Float... if i do an eSet on the attribute (after data-type change)
> setters from the generated code are invoked which still take in an
> Integer and hence i get a ClassCastException.
>
> when i do an eSet or an eGet for a feature thru my in memory M2
> EPackage .. i don't want the existing generated code to be invoked. coz
> its obsolete ... is there any way to achieve this.
>
> this is the most brief i could get.
>
> cheers,
> Chetan Kumar
>
>
>
Re: Changing EType of an EAttribute @ runtime [message #416604 is a reply to message #416601] Thu, 07 February 2008 10:23 Go to previous messageGo to next message
Eclipse UserFriend
Hi Chetan,

We don't recommend changing a class after creating an instance of it.
This may work in some scenarios, but will probably cause problems in others.

Here's a list of newsgroup posts that address this issue (certainly
there are more posts). As you will see, the common solutions for this
are to model wildcards features (like the ones available in XML schema),
and to dynamically extend an existing class.

"dynamic attributes" from 2/22/2006 5:32 AM
"Dynamically extending the Ecore model?" from 6/29/2007 3:15 AM
"dynamic extension of model" from 7/10/2007 11:16 AM

Cheers,
Marcelo

Chetan Kumar wrote:
> Hi guys,
>
> Before talking about my problem i'll give a brief background.
> I have an EMF model (M1 say) for which i have model and edit code
> generated. Now, M1 is volatile, meaning addition of new attributes can
> happen or change of existing attributes can happen on M1.
>
> Keeping the volatility in mind, i let the user add new attributes to
> the model (M1) and the ReflectiveProviders take care of bringing them on
> the UI. In brief, @ run-time i create a copy of my M1 EPackage, and add
> any new attributes to that in memory EPackage (M2 say). And any further
> processing is done through the M2 EPackage.
>
> the problem:
> now the problem comes when there is a change in data-type of an
> existing attribute. (lets consider Integer to Float). Although in the M2
> EPacakge the existing attribute's data-type is changed from Integer to
> Float... if i do an eSet on the attribute (after data-type change)
> setters from the generated code are invoked which still take in an
> Integer and hence i get a ClassCastException.
>
> when i do an eSet or an eGet for a feature thru my in memory M2
> EPackage .. i don't want the existing generated code to be invoked. coz
> its obsolete ... is there any way to achieve this.
>
> this is the most brief i could get.
>
> cheers,
> Chetan Kumar
>
>
>
Re: Changing EType of an EAttribute @ runtime [message #416607 is a reply to message #416603] Thu, 07 February 2008 11:09 Go to previous messageGo to next message
Eclipse UserFriend
Hi Dave,

1. i create a deep copy of my existing M1 package in memory, keep the
same nsURI but register the M2 EPackage for that nsURI. there by i am able
to have 2 EPackages for the same nsURI. I checked that factory instance
part, objects created from M2 package use factory of type EFactoryImpl.

2. Model is not changed after it is instantiated. I have taken care that
model changes are done before any instantiation happens from the model.


cheers,
Chetan Kumar
Re: Changing EType of an EAttribute @ runtime [message #416610 is a reply to message #416607] Thu, 07 February 2008 12:10 Go to previous messageGo to next message
Eclipse UserFriend
Chetan Kumar wrote:
> Hi Dave,
>
> 1. i create a deep copy of my existing M1 package in memory, keep the
> same nsURI but register the M2 EPackage for that nsURI. there by i am
> able to have 2 EPackages for the same nsURI. I checked that factory
> instance part, objects created from M2 package use factory of type
> EFactoryImpl.

Hi again,

I can think of just a few possibilities:

1. The classes in your copied package extend the corresponding ones in
the original. Then, the EFactoryImpl implementation of create() will
intentionally instantiate the generated base.

2. The copy of the package is still returning the generated factory,
yielding the generated implementations. Apparently you've already ruled
this one out.

3. The generated factory is being obtained directly, not from the
package. This can be done by registering a factory override via the
org.eclipse.emf.ecore.factory_override extension point. This wraps the
registered EPackage.Descriptor for the factory with a special descriptor
that yields the factory directly. However, if you're replacing the
package registration in the registry, this shouldn't be a problem.

What I'm really thinking you should do is put a breakpoint in your
M1FactoryImpl.create(EClass) method and see exactly what's invoking it.

By the way, having two EPackages for the same namespace URI still seems
questionable to me, but if you constrain your usage sufficiently, I
suppose it could be possible to avoid any problems.

Cheers,
Dave
Re: Changing EType of an EAttribute @ runtime [message #416622 is a reply to message #416610] Fri, 08 February 2008 08:36 Go to previous messageGo to next message
Eclipse UserFriend
Hi Dave,

Hi again,

I can think of just a few possibilities:

1. The classes in your copied package extend the corresponding ones in the
original. Then, the EFactoryImpl implementation of create() will
intentionally instantiate the generated base. - yes ... because of that
inheritance, generated base is instantiated.

I put a breakpoint in my M1FactoryImpl.create() ... and its being
invoked thru my in memory copy EPackage. And the superTypes' factory is
being used.
Re: Changing EType of an EAttribute @ runtime [message #416630 is a reply to message #416622] Fri, 08 February 2008 10:06 Go to previous messageGo to next message
Eclipse UserFriend
Chetan Kumar wrote:
>
> I put a breakpoint in my M1FactoryImpl.create() ... and its being
> invoked thru my in memory copy EPackage. And the superTypes' factory is
> being used.

Hi again Chetan,

So, you are making the classes in the dynamic EPackage extend those in
the generated one? That explains it.

Based on what you've said you're trying to do, that doesn't sound right
to me. You certainly can't remove or change any features inherited from
a generated base.

Cheers,
Dave
Re: Changing EType of an EAttribute @ runtime [message #416654 is a reply to message #416630] Mon, 11 February 2008 05:15 Go to previous messageGo to next message
Eclipse UserFriend
Dave Steinberg wrote:

> Based on what you've said you're trying to do, that doesn't sound right
> to me. You certainly can't remove or change any features inherited from
> a generated base.

Hi Dave,

I wish it was the case in my requirement ... but attributes used in the
M1 model tend to be volatile. Instead of re-generating model for each such
change, we've used Dynamic capabilities of EMF.
Is there no work around from the EMF perspective for this problem
(change of data-type).

cheers,
Chetan Kumar
Re: Changing EType of an EAttribute @ runtime [message #416658 is a reply to message #416654] Mon, 11 February 2008 10:18 Go to previous messageGo to next message
Eclipse UserFriend
Chetan Kumar wrote:
>
> I wish it was the case in my requirement ... but attributes used in the
> M1 model tend to be volatile. Instead of re-generating model for each
> such change, we've used Dynamic capabilities of EMF. Is there no work
> around from the EMF perspective for this problem (change of data-type).

Hi Chetan,

Sorry, but class extension just doesn't allow you to do the kind of
thing you're trying to do.

If you want to try to continue down the path you're on, I'd suggest
cloning, instead of extending, the generated classes. That would allow
you to make arbitrary changes to their features before instantiating
them. Of course, it would also mean that EMF won't consider the new
dynamic types to be type-compatible with the generated ones.

Cheers,
Dave
Re: Changing EType of an EAttribute @ runtime [message #416696 is a reply to message #416658] Wed, 13 February 2008 04:47 Go to previous messageGo to next message
Eclipse UserFriend
Dave Steinberg wrote:

> If you want to try to continue down the path you're on, I'd suggest
> cloning, instead of extending, the generated classes. That would allow
> you to make arbitrary changes to their features before instantiating
> them. Of course, it would also mean that EMF won't consider the new
> dynamic types to be type-compatible with the generated ones.

Hi Dave,

Well, that was the reason why we went to extending rather than cloning.
looks like we're back then.
Re: Changing EType of an EAttribute @ runtime [message #416699 is a reply to message #416696] Wed, 13 February 2008 09:05 Go to previous message
Eclipse UserFriend
Chetan Kumar wrote:
>
> Well, that was the reason why we went to extending rather than cloning.
> looks like we're back then.

Hi Chetan,

You could consider base types that declare no features (or only the
features that you know won't change). Basically you don't get to have it
both ways: you can't say type B inherits type A's features (that's part
of what makes them type compatible, after all) and then change those
features.

Cheers,
Dave
Previous Topic:Not able to persist Enum types
Next Topic:OutOfMemoryError during EMF model code generation from XSD source
Goto Forum:
  


Current Time: Mon Sep 15 11:55:28 EDT 2025

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

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

Back to the top