Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [Dynamic EMF] adding features to generated EClass at runtime
[Dynamic EMF] adding features to generated EClass at runtime [message #1254551] Sun, 23 February 2014 14:27 Go to next message
Bertrand Quenin is currently offline Bertrand QueninFriend
Messages: 70
Registered: July 2009
Member
Hi,

I was wondering if it is possible to add new structural features to a generated EClass.

My goal is to "extend" at runtime a generated EClass by adding attributes or references (themselves created using dynamic emf).

I don't know if it is allowed by the framework or not.

I have read here: http://www.eclipsezone.com/eclipse/forums/t70357.html

that the recommended way was to subclass a generated EClass with a dynamic EClass.

I tried this use case and it works fine in deed.

However, it doesn't seem performance efficient.

Here is my test code to illustrate my use case using the recommended approach (sorry it's Xtend and not Java):

    @Test
    def void testSimpleSubclassingExtension() {
        // Create an Organization
        val organization = pb.organization.name("Axway").build

        // Create Organization EClass subclass named DynamicEClass in a new EPackage(extended) 
        val account = eb.eAttribute.name("account").eType(EcorePackage.Literals.ESTRING).build
        val dynamicEClass = eb.eClass.name("DynamicEClass").eStructuralFeatures(account).eSuperTypes(ParticipantPackage.eINSTANCE.organization).build
        val extendedPackage = eb.ePackage.name("extended").eClassifiers(dynamicEClass).build

        // Create an instance of the "extendedOrganization"
        val extendedOrganization = extendedPackage.EFactoryInstance.create(dynamicEClass)

        organization.eClass.EAllStructuralFeatures.forEach [
            extendedOrganization.eSet(it, organization.eGet(it))
        ]

        // Set value for the extension
        extendedOrganization.eSet(account, "an account")
        assertEquals("an account", ms.oclEvaluate("self.account", extendedOrganization))

        extendedOrganization.eSet(account, "another account")
        assertEquals("another account", ms.oclEvaluate("self.account", extendedOrganization))
    }


This approach works fine, however it seems limited to a leaf EClass. I'd like to create an extension anywhere in the model (I tried another approach unsuccessfully)

I'd like to understand if what I'm trying to do is possible/recommended/discouraged/etc

Best Regards,
BQ.
Re: [Dynamic EMF] adding features to generated EClass at runtime [message #1254584 is a reply to message #1254551] Sun, 23 February 2014 15:18 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Bertrand,

Comments below.

On 23/02/2014 3:27 PM, Bertrand Quenin wrote:
> Hi,
>
> I was wondering if it is possible to add new structural features to a
> generated EClass.
No.
>
> My goal is to "extend" at runtime a generated EClass by adding
> attributes or references (themselves created using dynamic emf).
>
> I don't know if it is allowed by the framework or not.
>
> I have read here: http://www.eclipsezone.com/eclipse/forums/t70357.html
>
> that the recommended way was to subclass a generated EClass with a
> dynamic EClass.
Yes, you can have a dynamic extension of a generated class but you can't
modify a generated class.
>
> I tried this use case and it works fine in deed.
>
> However, it doesn't seem performance efficient.
How so.
>
> Here is my test code to illustrate my use case using the recommended
> approach (sorry it's Xtend and not Java):
>
>
> @Test
> def void testSimpleSubclassingExtension() {
> // Create an Organization
> val organization = pb.organization.name("Axway").build
>
> // Create Organization EClass subclass named DynamicEClass in a
> new EPackage(extended) val account =
> eb.eAttribute.name("account").eType(EcorePackage.Literals.ESTRING).build
> val dynamicEClass =
> eb.eClass.name("DynamicEClass").eStructuralFeatures(account).eSuperTypes(ParticipantPackage.eINSTANCE.organization).build
> val extendedPackage =
> eb.ePackage.name("extended").eClassifiers(dynamicEClass).build
>
> // Create an instance of the "extendedOrganization"
> val extendedOrganization =
> extendedPackage.EFactoryInstance.create(dynamicEClass)
>
> organization.eClass.EAllStructuralFeatures.forEach [
> extendedOrganization.eSet(it, organization.eGet(it))
> ]
>
> // Set value for the extension
> extendedOrganization.eSet(account, "an account")
> assertEquals("an account", ms.oclEvaluate("self.account",
> extendedOrganization))
>
> extendedOrganization.eSet(account, "another account")
> assertEquals("another account", ms.oclEvaluate("self.account",
> extendedOrganization))
> }
>
>
> This approach works fine, however it seems limited to a leaf EClass.
> I'd like to create an extension anywhere in the model (I tried another
> approach unsuccessfully)
Of course an extension must be a new class that other existing
(generated) classes can know nothing about.
>
> I'd like to understand if what I'm trying to do is
> possible/recommended/discouraged/etc
Not possible.
>
> Best Regards,
> BQ.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: [Dynamic EMF] adding features to generated EClass at runtime [message #1254601 is a reply to message #1254584] Sun, 23 February 2014 15:44 Go to previous messageGo to next message
Bertrand Quenin is currently offline Bertrand QueninFriend
Messages: 70
Registered: July 2009
Member
Hi Ed,

Thanks for this prompt answer!

By your answers, I understand that I can't achieve this goal using generated code.

Another question then: Is the only way to manage dynamic model extensions is to go fully dynamic EMF and not relying at all on generated classes (i.e. using a ResourceSet and dynamic EMF?)?

Regards,
BQ.
Re: [Dynamic EMF] adding features to generated EClass at runtime [message #1255145 is a reply to message #1254601] Mon, 24 February 2014 06:08 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Bertrand,

Comments below.

On 23/02/2014 4:44 PM, Bertrand Quenin wrote:
> Hi Ed,
>
> Thanks for this prompt answer!
>
> By your answers, I understand that I can't achieve this goal using
> generated code.
Yes, you can't modify the model corresponding to unmodifiable generated
code.
>
> Another question then: Is the only way to manage dynamic model
> extensions is to go fully dynamic EMF and not relying at all on
> generated classes (i.e. using a ResourceSet and dynamic EMF?)?
Yes, if you want to modify a model it must be a dynamic model (and you
can't modify the model for existing already-in-memory instances because
they'll no longer conform to their model).
>
> Regards,
> BQ.
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: [Dynamic EMF] adding features to generated EClass at runtime [message #1255301 is a reply to message #1255145] Mon, 24 February 2014 09:50 Go to previous message
Bertrand Quenin is currently offline Bertrand QueninFriend
Messages: 70
Registered: July 2009
Member
Thanks for your answer, it greatly helps.

I'm going to prototype something in this way. I'll probably have some other questions along the way.

Regards,
BQ.
Previous Topic:How to import UML?
Next Topic:[ChangeRecoder] Unexpected behavior
Goto Forum:
  


Current Time: Fri Apr 19 21:21:54 GMT 2024

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

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

Back to the top