[Dynamic EMF] adding features to generated EClass at runtime [message #1254551] |
Sun, 23 February 2014 14:27 |
Bertrand Quenin 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 |
Ed Merks Messages: 33140 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/
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02662 seconds