Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Add dynamic attribute to a static class
Add dynamic attribute to a static class [message #716882] Thu, 18 August 2011 16:40 Go to next message
Mickael Istria is currently offline Mickael IstriaFriend
Messages: 865
Registered: July 2009
Location: Grenoble, France
Senior Member

Hi,

I've been trying to add a dynamic attribute to a static class:

EAttribute extension = Cdk5Package.Literals.CDK5_CONSUMES__OPERATION;
JbiPackage.Literals.CONSUMES.getEStructuralFeatures().add(extension);

But I get an issue when trying to set this feature since the eIsSet
method does not see this attribute as a dynamic one (getFeatureCount
returns the number of attributes, including the one I just added). This
leads to an infinite call stack.
What is the correct way to achieve that and ensure that the attribute is
seen as dynamic from inside the class?

--
http://mickaelistria.wordpress.com
http://twitter.com/#!/mickaelistria
http://www.petalslink.com
Re: Add dynamic attribute to a static class [message #716889 is a reply to message #716882] Thu, 18 August 2011 17:21 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Mickael,

Comments below.

On 18/08/2011 9:40 AM, Mickael Istria wrote:
> Hi,
>
> I've been trying to add a dynamic attribute to a static class:
>
> EAttribute extension = Cdk5Package.Literals.CDK5_CONSUMES__OPERATION;
> JbiPackage.Literals.CONSUMES.getEStructuralFeatures().add(extension);
No, that's evil. That's like trying to add a field to a Java class
dynamically...
>
> But I get an issue when trying to set this feature since the eIsSet
> method does not see this attribute as a dynamic one (getFeatureCount
> returns the number of attributes, including the one I just added).
> This leads to an infinite call stack.
Yes, I'd expect all manner of problems.
> What is the correct way to achieve that and ensure that the attribute
> is seen as dynamic from inside the class?
You can only extend the class with a new dynamic class in a new dynamic
package and introduce things to that new class. Then you have to create
instances of that new class rather than of the base.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Add dynamic attribute to a static class [message #716895 is a reply to message #716889] Thu, 18 August 2011 17:40 Go to previous messageGo to next message
Mickael Istria is currently offline Mickael IstriaFriend
Messages: 865
Registered: July 2009
Location: Grenoble, France
Senior Member

>> I've been trying to add a dynamic attribute to a static class:
>>
>> EAttribute extension = Cdk5Package.Literals.CDK5_CONSUMES__OPERATION;
>> JbiPackage.Literals.CONSUMES.getEStructuralFeatures().add(extension);
> No, that's evil. That's like trying to add a field to a Java class
> dynamically...

Maybe evil, but for sure helpful! It is a kind of aspect-oriented
approach. My goal is to add 1 attribute to an existing "final" 3rd-party
model.

>> What is the correct way to achieve that and ensure that the attribute
>> is seen as dynamic from inside the class?
> You can only extend the class with a new dynamic class in a new dynamic
> package and introduce things to that new class. Then you have to create
> instances of that new class rather than of the base.

The issue is that I already have the object retrieved from 3rd-party
code. I am not responsible of its creation. Then I don't see how I could
"supertype" the provided item to my enriched type.


--
http://mickaelistria.wordpress.com
http://twitter.com/#!/mickaelistria
http://www.petalslink.com
Re: Add dynamic attribute to a static class [message #716897 is a reply to message #716895] Thu, 18 August 2011 17:49 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Mickael,

Comments below.

On 18/08/2011 10:40 AM, Mickael Istria wrote:
>>> I've been trying to add a dynamic attribute to a static class:
>>>
>>> EAttribute extension = Cdk5Package.Literals.CDK5_CONSUMES__OPERATION;
>>> JbiPackage.Literals.CONSUMES.getEStructuralFeatures().add(extension);
>> No, that's evil. That's like trying to add a field to a Java class
>> dynamically...
>
> Maybe evil, but for sure helpful! It is a kind of aspect-oriented
> approach.
Kind of, but I don't think even that allows you to add state to the
instances...
> My goal is to add 1 attribute to an existing "final" 3rd-party model.
>
>>> What is the correct way to achieve that and ensure that the attribute
>>> is seen as dynamic from inside the class?
>> You can only extend the class with a new dynamic class in a new dynamic
>> package and introduce things to that new class. Then you have to create
>> instances of that new class rather than of the base.
>
> The issue is that I already have the object retrieved from 3rd-party
> code. I am not responsible of its creation. Then I don't see how I
> could "supertype" the provided item to my enriched type.
This is no different than in Java where you can't add fields without
subclassing and if you don't control the instance creation you can't
ensure that your subclasses instance is created. In such a case, you
need to store any additional state in some other way, e.g., via an
external map.
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Add dynamic attribute to a static class [message #717068 is a reply to message #716897] Fri, 19 August 2011 08:03 Go to previous messageGo to next message
Mickael Istria is currently offline Mickael IstriaFriend
Messages: 865
Registered: July 2009
Location: Grenoble, France
Senior Member

>>>> I've been trying to add a dynamic attribute to a static class:
>>>>
>>>> EAttribute extension = Cdk5Package.Literals.CDK5_CONSUMES__OPERATION;
>>>> JbiPackage.Literals.CONSUMES.getEStructuralFeatures().add(extension);
>>> No, that's evil. That's like trying to add a field to a Java class
>>> dynamically...
>>
>> Maybe evil, but for sure helpful! It is a kind of aspect-oriented
>> approach.
> Kind of, but I don't think even that allows you to add state to the
> instances...

I spent some time tweaking the org.eclipse.emf.ecore plugin to support
such an injection of features in an existing object, and I got something
working as I'd like it to work. See
https://bugs.eclipse.org/bugs/show_bug.cgi?id=355188

Regards,
--
http://mickaelistria.wordpress.com
http://twitter.com/#!/mickaelistria
http://www.petalslink.com
Re: Add dynamic attribute to a static class [message #717202 is a reply to message #717068] Fri, 19 August 2011 15:08 Go to previous messageGo to next message
Gregoire Dupe is currently offline Gregoire DupeFriend
Messages: 75
Registered: September 2009
Location: France
Member
Hello,

If you only want to add derived attributes, EMF Facet could help you to reach your goal and conform to Ed's recommendations.

We plan to improve EMF Facet to be able to deal with non-derived attributes in the version 0.2.0.

Regards,
Grégoire

EMF Facet : http://www.eclipse.org/modeling/emft/facet/
Re: Add dynamic attribute to a static class [message #717205 is a reply to message #717202] Fri, 19 August 2011 15:15 Go to previous messageGo to next message
Mickael Istria is currently offline Mickael IstriaFriend
Messages: 865
Registered: July 2009
Location: Grenoble, France
Senior Member

I gave a try to EMF Facet, but I need my additional vendor-specific
features to be persisted (to XML). Also I was not able to get started.
Then I tried some other alternatives, and I think that there are a few
"bugs" in EMF - about detecting dynamicFeatures in a static class - that
can be solved in order to facilitate this use-case.
EMF-Facet could for sure leverage this too.

--
http://mickaelistria.wordpress.com
http://twitter.com/#!/mickaelistria
http://www.petalslink.com
Re: Add dynamic attribute to a static class [message #717207 is a reply to message #717205] Fri, 19 August 2011 15:31 Go to previous messageGo to next message
Gregoire Dupe is currently offline Gregoire DupeFriend
Messages: 75
Registered: September 2009
Location: France
Member
> but I need my additional vendor-specific features to be persisted (to XML).

Ok, I add this point in the EMF Facet requirements: "to be able to plug a vendor-specific persistence"

Thanks you,
Grégoire
Re: Add dynamic attribute to a static class [message #717229 is a reply to message #717207] Fri, 19 August 2011 16:10 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Sorry, but I returned the bug as won't fix. EMF's core is complicated
enough without adding yet another a new wrinkle, i.e., dynamically
changing "static" models....


On 19/08/2011 8:31 AM, Gregoire Dupe wrote:
>> but I need my additional vendor-specific features to be persisted (to
>> XML).
>
> Ok, I add this point in the EMF Facet requirements: "to be able to
> plug a vendor-specific persistence"
>
> Thanks you,
> Grégoire
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Add dynamic attribute to a static class [message #719967 is a reply to message #717207] Mon, 29 August 2011 15:05 Go to previous messageGo to next message
Mickael Istria is currently offline Mickael IstriaFriend
Messages: 865
Registered: July 2009
Location: Grenoble, France
Senior Member

Hi Gregoire,

Is there any user and developer documentation available that would help
me to get an idea of how EMF Facet could solve my use-case, and how
difficult it would be to have my additional attributes stored in my XML
file?
I did not succeed to find my path in the wiki.

Regards,

On 19/08/2011 17:31, Gregoire Dupe wrote:
>> but I need my additional vendor-specific features to be persisted (to
>> XML).
>
> Ok, I add this point in the EMF Facet requirements: "to be able to plug
> a vendor-specific persistence"
>
> Thanks you,
> Grégoire
>


--
http://mickaelistria.wordpress.com
http://twitter.com/#!/mickaelistria
http://www.petalslink.com
Re: Add dynamic attribute to a static class [message #719981 is a reply to message #717229] Mon, 29 August 2011 15:40 Go to previous messageGo to next message
Mickael Istria is currently offline Mickael IstriaFriend
Messages: 865
Registered: July 2009
Location: Grenoble, France
Senior Member

Guys,

Holidays helped me to clarify my needs, and I come with a workaround for
this use-case that does not imply modification of static models.
The idea is more to extend _instances_ of the metamodel rather than the
metamodel itself.


I found a kind of workaround:
Assuming I have a LegacyElement and I want to manipulate a ModernElement
that extends LegacyElement with an additional attibute modernAttribute.
Then I create a new ModernPackage that contains my ModernElement, and
when I retrieve an instanceof LegacyElement, I perform
((BasicEObjectImpl)element).eSetClass(ModernPackage.Literals.MODERN_ELEMENT).

Then the instance supports my new attribute, databinding works, and XML
serialization too. This is still static EMF.
The only issue I still have with this workaround is that, since it is
inheritance, I cannot add attributes from several independant sources.


Here is a new version of the issue: What I'd like to do now is to be
able to extend any _EObject_ (no more EClass) with any
EStructuralFeature, independently of its EClass. Is there something on
this topic in EMF, or in EMF-Facet ?

Regards,

--
http://mickaelistria.wordpress.com
http://twitter.com/#!/mickaelistria
http://www.petalslink.com
Re: Add dynamic attribute to a static class [message #720008 is a reply to message #719981] Mon, 29 August 2011 16:34 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Mickael,

Dynamic extensions are fine and are well supported. However, changing
the type after the instance has already been created and seen by other
parts of the framework may well get you into trouble. For example, I
doubt CDO or Teneo would be happy to have the type of an instance morph
dynamically.

You can always define an EClass that extends a bunch of other EClasses
(though not a bunch of static ones) and hence combines features from
several sources.

Note that for models like Ecore, Java, XML Schema, and so on, the model
itself defines an extensibility mechanism, i.e., annotations. That's a
far better approach.


On 29/08/2011 8:40 AM, Mickael Istria wrote:
> Guys,
>
> Holidays helped me to clarify my needs, and I come with a workaround
> for this use-case that does not imply modification of static models.
> The idea is more to extend _instances_ of the metamodel rather than
> the metamodel itself.
>
>
> I found a kind of workaround:
> Assuming I have a LegacyElement and I want to manipulate a
> ModernElement that extends LegacyElement with an additional attibute
> modernAttribute. Then I create a new ModernPackage that contains my
> ModernElement, and when I retrieve an instanceof LegacyElement, I
> perform
> ((BasicEObjectImpl)element).eSetClass(ModernPackage.Literals.MODERN_ELEMENT).
>
> Then the instance supports my new attribute, databinding works, and
> XML serialization too. This is still static EMF.
> The only issue I still have with this workaround is that, since it is
> inheritance, I cannot add attributes from several independant sources.
>
>
> Here is a new version of the issue: What I'd like to do now is to be
> able to extend any _EObject_ (no more EClass) with any
> EStructuralFeature, independently of its EClass. Is there something on
> this topic in EMF, or in EMF-Facet ?
>
> Regards,
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Add dynamic attribute to a static class [message #720232 is a reply to message #719967] Tue, 30 August 2011 07:48 Go to previous message
Gregoire Dupe is currently offline Gregoire DupeFriend
Messages: 75
Registered: September 2009
Location: France
Member
Hello,

We didn't have enough time to report all the documentation of the components that have been copied from MoDisco, that why the official documentation of EMF Facet (http://help.eclipse.org/indigo/nav/22) does not describe all the features. To get information about the components copied from MoDisco you should have a look to the MoDisco documentation (http://help.eclipse.org/indigo/topic/org.eclipse.gmt.modisco.infra.doc/doc/MoDisco/Components/FacetManager/FacetManager.html)

(I've add links to the MoDisco documentation in the EMF Facet wiki page: http://wiki.eclipse.org/EMF_Facet/)

Regards,
Gregoire Dupe
Previous Topic:[emf] question about ComposedSwitch
Next Topic:[CDO] folder concept
Goto Forum:
  


Current Time: Wed Apr 24 16:34:22 GMT 2024

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

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

Back to the top