Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Dynamic extension of EMF model and getEAllStructuralFeatures
Dynamic extension of EMF model and getEAllStructuralFeatures [message #686800] Thu, 19 May 2011 12:32 Go to next message
Daniel Selman is currently offline Daniel SelmanFriend
Messages: 14
Registered: July 2009
Junior Member
I am trying to debug an issue I am seeing with our application (based on
EMF 2.6.1). We generate Java code from Ecore and then have config files
that dynamically add properties to EClasses at runtime.

What I am seeing is that new dynamic properties show up on the EClasses
but are not visible on derived classes (using getEAllStructureFeatures).

E.g. Derived -> MyClass

package mypackage;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.EcorePackage;

public class Main {
public static void main( String[] args ) {

IlrMypackagePackage pkg = IlrMypackagePackage.eINSTANCE;
EClass clazz = pkg.getMyClass();

EcorePackage theCorePackage = EcorePackage.eINSTANCE;
EcoreFactory theCoreFactory = EcoreFactory.eINSTANCE;

EAttribute bookStoreOwner = theCoreFactory.createEAttribute();
bookStoreOwner.setName("owner");
bookStoreOwner.setEType(theCorePackage.getEString());

clazz.getEStructuralFeatures().add(bookStoreOwner);
dumpEClass(clazz);

EClass derivedClazz = pkg.getDerived();
dumpEClass(derivedClazz);
}

private static void dumpEClass( EClass clazz ) {
System.out.println( "======" );
System.out.println( clazz.getName() );
EList<EStructuralFeature> list = clazz.getEAllStructuralFeatures();
for (EStructuralFeature eStructuralFeature : list) {
System.out.println( " " + eStructuralFeature.getName() );
}
System.out.println( "======" );
}
}

private static void dumpEClass( EClass clazz ) {
System.out.println( "======" );
System.out.println( clazz.getName() );
EList<EStructuralFeature> list = clazz.getEAllStructuralFeatures();
for (EStructuralFeature eStructuralFeature : list) {
System.out.println( " " + eStructuralFeature.getName() );
}
System.out.println( "======" );
}

Output:

======
MyClass
secondClass
Name
owner
======
======
Derived
secondClass
Name
======

Anyone have any ideas?

Thanks,
Dan
Re: Dynamic extension of EMF model and getEAllStructuralFeatures [message #686801 is a reply to message #686800] Thu, 19 May 2011 13:41 Go to previous messageGo to next message
Daniel Selman is currently offline Daniel SelmanFriend
Messages: 14
Registered: July 2009
Junior Member
So, it looks like this is not supported OOTB by EMF. We were using
custom Jet templates and our own class derived from EClassImpl to manage
this. If there are easier ways of achieving this I'd be interested to
hear of them.

Dan

On 5/19/11 2:32 PM, Daniel Selman wrote:
> I am trying to debug an issue I am seeing with our application (based on
> EMF 2.6.1). We generate Java code from Ecore and then have config files
> that dynamically add properties to EClasses at runtime.
>
> What I am seeing is that new dynamic properties show up on the EClasses
> but are not visible on derived classes (using getEAllStructureFeatures).
>
> E.g. Derived -> MyClass
>
> package mypackage;
>
> import org.eclipse.emf.common.util.EList;
> import org.eclipse.emf.ecore.EAttribute;
> import org.eclipse.emf.ecore.EClass;
> import org.eclipse.emf.ecore.EStructuralFeature;
> import org.eclipse.emf.ecore.EcoreFactory;
> import org.eclipse.emf.ecore.EcorePackage;
>
> public class Main {
> public static void main( String[] args ) {
>
> IlrMypackagePackage pkg = IlrMypackagePackage.eINSTANCE;
> EClass clazz = pkg.getMyClass();
>
> EcorePackage theCorePackage = EcorePackage.eINSTANCE;
> EcoreFactory theCoreFactory = EcoreFactory.eINSTANCE;
>
> EAttribute bookStoreOwner = theCoreFactory.createEAttribute();
> bookStoreOwner.setName("owner");
> bookStoreOwner.setEType(theCorePackage.getEString());
>
> clazz.getEStructuralFeatures().add(bookStoreOwner);
> dumpEClass(clazz);
>
> EClass derivedClazz = pkg.getDerived();
> dumpEClass(derivedClazz);
> }
>
> private static void dumpEClass( EClass clazz ) {
> System.out.println( "======" );
> System.out.println( clazz.getName() );
> EList<EStructuralFeature> list = clazz.getEAllStructuralFeatures();
> for (EStructuralFeature eStructuralFeature : list) {
> System.out.println( " " + eStructuralFeature.getName() );
> }
> System.out.println( "======" );
> }
> }
>
> private static void dumpEClass( EClass clazz ) {
> System.out.println( "======" );
> System.out.println( clazz.getName() );
> EList<EStructuralFeature> list = clazz.getEAllStructuralFeatures();
> for (EStructuralFeature eStructuralFeature : list) {
> System.out.println( " " + eStructuralFeature.getName() );
> }
> System.out.println( "======" );
> }
>
> Output:
>
> ======
> MyClass
> secondClass
> Name
> owner
> ======
> ======
> Derived
> secondClass
> Name
> ======
>
> Anyone have any ideas?
>
> Thanks,
> Dan
Re: Dynamic extension of EMF model and getEAllStructuralFeatures [message #686811 is a reply to message #686801] Thu, 19 May 2011 17:44 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Rich Kulp

Hi,

Was the derived class generated as frozen? By default the
ePackageImpl.init() method for your EMF package freezes the classes
generated. This means that once the package is initialized the structure
is frozen. It doesn't listen for changes from parent classes.

Rich Kulp
Re: Dynamic extension of EMF model and getEAllStructuralFeatures [message #686812 is a reply to message #686800] Thu, 19 May 2011 17:45 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33107
Registered: July 2009
Senior Member
Daniel,

Comments below.

Daniel Selman wrote:
> I am trying to debug an issue I am seeing with our application (based
> on EMF 2.6.1). We generate Java code from Ecore and then have config
> files that dynamically add properties to EClasses at runtime.
Changing the generated model in any way isn't supported. Things like
feature IDs would just end up being wrong...
>
> What I am seeing is that new dynamic properties show up on the
> EClasses but are not visible on derived classes (using
> getEAllStructureFeatures).
Because that's assumed to be static for a generated model (after freeze
is called during initialization).
>
> E.g. Derived -> MyClass
>
> package mypackage;
>
> import org.eclipse.emf.common.util.EList;
> import org.eclipse.emf.ecore.EAttribute;
> import org.eclipse.emf.ecore.EClass;
> import org.eclipse.emf.ecore.EStructuralFeature;
> import org.eclipse.emf.ecore.EcoreFactory;
> import org.eclipse.emf.ecore.EcorePackage;
>
> public class Main {
> public static void main( String[] args ) {
>
> IlrMypackagePackage pkg = IlrMypackagePackage.eINSTANCE;
> EClass clazz = pkg.getMyClass();
>
> EcorePackage theCorePackage = EcorePackage.eINSTANCE;
> EcoreFactory theCoreFactory = EcoreFactory.eINSTANCE;
>
> EAttribute bookStoreOwner = theCoreFactory.createEAttribute();
> bookStoreOwner.setName("owner");
> bookStoreOwner.setEType(theCorePackage.getEString());
>
> clazz.getEStructuralFeatures().add(bookStoreOwner);
> dumpEClass(clazz);
>
> EClass derivedClazz = pkg.getDerived();
> dumpEClass(derivedClazz);
> }
>
> private static void dumpEClass( EClass clazz ) {
> System.out.println( "======" );
> System.out.println( clazz.getName() );
> EList<EStructuralFeature> list =
> clazz.getEAllStructuralFeatures();
> for (EStructuralFeature eStructuralFeature : list) {
> System.out.println( " " + eStructuralFeature.getName()
> );
> }
> System.out.println( "======" );
> }
> }
>
> private static void dumpEClass( EClass clazz ) {
> System.out.println( "======" );
> System.out.println( clazz.getName() );
> EList<EStructuralFeature> list =
> clazz.getEAllStructuralFeatures();
> for (EStructuralFeature eStructuralFeature : list) {
> System.out.println( " " + eStructuralFeature.getName()
> );
> }
> System.out.println( "======" );
> }
>
> Output:
>
> ======
> MyClass
> secondClass
> Name
> owner
> ======
> ======
> Derived
> secondClass
> Name
> ======
>
> Anyone have any ideas?
If anything, you should be using a dynamic model that extends the
generated model, rather than modifying the generated model directly
(much like you'd extend a Java class, rather than go in and try to munge
the byte code dynamically at runtime).
>
> Thanks,
> Dan


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Dynamic extension of EMF model and getEAllStructuralFeatures [message #686976 is a reply to message #686800] Thu, 19 May 2011 13:41 Go to previous message
Daniel Selman is currently offline Daniel SelmanFriend
Messages: 14
Registered: July 2009
Junior Member
So, it looks like this is not supported OOTB by EMF. We were using
custom Jet templates and our own class derived from EClassImpl to manage
this. If there are easier ways of achieving this I'd be interested to
hear of them.

Dan

On 5/19/11 2:32 PM, Daniel Selman wrote:
> I am trying to debug an issue I am seeing with our application (based on
> EMF 2.6.1). We generate Java code from Ecore and then have config files
> that dynamically add properties to EClasses at runtime.
>
> What I am seeing is that new dynamic properties show up on the EClasses
> but are not visible on derived classes (using getEAllStructureFeatures).
>
> E.g. Derived -> MyClass
>
> package mypackage;
>
> import org.eclipse.emf.common.util.EList;
> import org.eclipse.emf.ecore.EAttribute;
> import org.eclipse.emf.ecore.EClass;
> import org.eclipse.emf.ecore.EStructuralFeature;
> import org.eclipse.emf.ecore.EcoreFactory;
> import org.eclipse.emf.ecore.EcorePackage;
>
> public class Main {
> public static void main( String[] args ) {
>
> IlrMypackagePackage pkg = IlrMypackagePackage.eINSTANCE;
> EClass clazz = pkg.getMyClass();
>
> EcorePackage theCorePackage = EcorePackage.eINSTANCE;
> EcoreFactory theCoreFactory = EcoreFactory.eINSTANCE;
>
> EAttribute bookStoreOwner = theCoreFactory.createEAttribute();
> bookStoreOwner.setName("owner");
> bookStoreOwner.setEType(theCorePackage.getEString());
>
> clazz.getEStructuralFeatures().add(bookStoreOwner);
> dumpEClass(clazz);
>
> EClass derivedClazz = pkg.getDerived();
> dumpEClass(derivedClazz);
> }
>
> private static void dumpEClass( EClass clazz ) {
> System.out.println( "======" );
> System.out.println( clazz.getName() );
> EList<EStructuralFeature> list = clazz.getEAllStructuralFeatures();
> for (EStructuralFeature eStructuralFeature : list) {
> System.out.println( " " + eStructuralFeature.getName() );
> }
> System.out.println( "======" );
> }
> }
>
> private static void dumpEClass( EClass clazz ) {
> System.out.println( "======" );
> System.out.println( clazz.getName() );
> EList<EStructuralFeature> list = clazz.getEAllStructuralFeatures();
> for (EStructuralFeature eStructuralFeature : list) {
> System.out.println( " " + eStructuralFeature.getName() );
> }
> System.out.println( "======" );
> }
>
> Output:
>
> ======
> MyClass
> secondClass
> Name
> owner
> ======
> ======
> Derived
> secondClass
> Name
> ======
>
> Anyone have any ideas?
>
> Thanks,
> Dan
Re: Dynamic extension of EMF model and getEAllStructuralFeatures [message #686987 is a reply to message #686801] Thu, 19 May 2011 17:44 Go to previous message
Eclipse UserFriend
Originally posted by: Rich Kulp

Hi,

Was the derived class generated as frozen? By default the
ePackageImpl.init() method for your EMF package freezes the classes
generated. This means that once the package is initialized the structure
is frozen. It doesn't listen for changes from parent classes.

Rich Kulp
Re: Dynamic extension of EMF model and getEAllStructuralFeatures [message #686988 is a reply to message #686800] Thu, 19 May 2011 17:45 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33107
Registered: July 2009
Senior Member
Daniel,

Comments below.

Daniel Selman wrote:
> I am trying to debug an issue I am seeing with our application (based
> on EMF 2.6.1). We generate Java code from Ecore and then have config
> files that dynamically add properties to EClasses at runtime.
Changing the generated model in any way isn't supported. Things like
feature IDs would just end up being wrong...
>
> What I am seeing is that new dynamic properties show up on the
> EClasses but are not visible on derived classes (using
> getEAllStructureFeatures).
Because that's assumed to be static for a generated model (after freeze
is called during initialization).
>
> E.g. Derived -> MyClass
>
> package mypackage;
>
> import org.eclipse.emf.common.util.EList;
> import org.eclipse.emf.ecore.EAttribute;
> import org.eclipse.emf.ecore.EClass;
> import org.eclipse.emf.ecore.EStructuralFeature;
> import org.eclipse.emf.ecore.EcoreFactory;
> import org.eclipse.emf.ecore.EcorePackage;
>
> public class Main {
> public static void main( String[] args ) {
>
> IlrMypackagePackage pkg = IlrMypackagePackage.eINSTANCE;
> EClass clazz = pkg.getMyClass();
>
> EcorePackage theCorePackage = EcorePackage.eINSTANCE;
> EcoreFactory theCoreFactory = EcoreFactory.eINSTANCE;
>
> EAttribute bookStoreOwner = theCoreFactory.createEAttribute();
> bookStoreOwner.setName("owner");
> bookStoreOwner.setEType(theCorePackage.getEString());
>
> clazz.getEStructuralFeatures().add(bookStoreOwner);
> dumpEClass(clazz);
>
> EClass derivedClazz = pkg.getDerived();
> dumpEClass(derivedClazz);
> }
>
> private static void dumpEClass( EClass clazz ) {
> System.out.println( "======" );
> System.out.println( clazz.getName() );
> EList<EStructuralFeature> list =
> clazz.getEAllStructuralFeatures();
> for (EStructuralFeature eStructuralFeature : list) {
> System.out.println( " " + eStructuralFeature.getName()
> );
> }
> System.out.println( "======" );
> }
> }
>
> private static void dumpEClass( EClass clazz ) {
> System.out.println( "======" );
> System.out.println( clazz.getName() );
> EList<EStructuralFeature> list =
> clazz.getEAllStructuralFeatures();
> for (EStructuralFeature eStructuralFeature : list) {
> System.out.println( " " + eStructuralFeature.getName()
> );
> }
> System.out.println( "======" );
> }
>
> Output:
>
> ======
> MyClass
> secondClass
> Name
> owner
> ======
> ======
> Derived
> secondClass
> Name
> ======
>
> Anyone have any ideas?
If anything, you should be using a dynamic model that extends the
generated model, rather than modifying the generated model directly
(much like you'd extend a Java class, rather than go in and try to munge
the byte code dynamically at runtime).
>
> Thanks,
> Dan


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Keeping different in-memory instances of same model synchronized
Next Topic:emf over web service
Goto Forum:
  


Current Time: Tue Mar 19 04:26:50 GMT 2024

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

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

Back to the top