Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Adding classes to generated Package
Adding classes to generated Package [message #903945] Mon, 27 August 2012 11:38 Go to next message
Dominik Muttenthaler is currently offline Dominik MuttenthalerFriend
Messages: 5
Registered: August 2012
Junior Member
Hi,

What is the best way to add classes dynamically to a running editor.

Let me specify this a little bit more:
I have a base model, that contains a EClass named ResourceContainer (containing a zero to many EReference called resources of EType Resource) and a EClass named Resource (containing a EAttribute called id).

I created the model, edit and editor code out of that model.
EClass Resource has a overridden constructor that creates a value for EAttribute id.

That works fine. Its possible to create a model where the root is a ResourceContainer and its possible to add Resources. (The ECLass Resource should be abstract later)

What i wanna do is to enable the user to create his own Resources (ESuperType Resource) in a extended model and load them into the editor. I already have a Button to (re)load a file (so the editor need not get restarted - later a FileChangeListener should do that).

Unfortunately till now i didn't find a way to add those new EClasses to the EPackage the editor is using.

-----------------------

So what i wanna do is the following:
When starting the editor (and when the user is pressing the reload button), i want to add EClasses specified in a ecore file to the base EPackage.
Re: Adding classes to generated Package [message #903957 is a reply to message #903945] Mon, 27 August 2012 12:42 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Dominik,

Comments below.

On 27/08/2012 1:38 PM, Dominik Muttenthaler wrote:
> Hi,
>
> What is the best way to add classes dynamically to a running editor.
It depends on what you mean by dynamically...
>
> Let me specify this a little bit more:
> I have a base model, that contains a EClass named ResourceContainer
> (containing a zero to many EReference called resources of EType
> Resource) and a EClass named Resource (containing a EAttribute called
> id).
>
> I created the model, edit and editor code out of that model.
> EClass Resource has a overridden constructor that creates a value for
> EAttribute id.
It's generally a bad idea for the constructor to populate features...
>
> That works fine. Its possible to create a model where the root is a
> ResourceContainer and its possible to add Resources. (The ECLass
> Resource should be abstract later)
>
> What i wanna do is to enable the user to create his own Resources
> (ESuperType Resource) in a extended model and load them into the
> editor. I already have a Button to (re)load a file (so the editor need
> not get restarted - later a FileChangeListener should do that).
That sounds complex for the user to manage. Probably better is that you
provide some type of mechanism whereby the user can annotate resources
with additional properties (much like EModelElement can be annotated).
>
> Unfortunately till now i didn't find a way to add those new EClasses
> to the EPackage the editor is using.
You can't modify a generated package at all. You can create new dynamic
packages and use those. ReflectiveItemProviderAdapter shows how such
newly discovered packages can contribute to the child creation menu.
>
> -----------------------
>
> So what i wanna do is the following:
> When starting the editor (and when the user is pressing the reload
> button), i want to add EClasses specified in a ecore file to the base
> EPackage.
You can only do that if your base package itself is dynamic. Is that
the case?


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Adding classes to generated Package [message #903967 is a reply to message #903957] Mon, 27 August 2012 13:14 Go to previous messageGo to next message
Dominik Muttenthaler is currently offline Dominik MuttenthalerFriend
Messages: 5
Registered: August 2012
Junior Member
Thanks for the fast answer(s),

> It's generally a bad idea for the constructor to populate features...
What i do is the following:
id = eClass().getName() + EcoreUtil.generateUUID();

I already see that this isn't good, as soon as i have a dynamic class with supertype Resource since EMF creates the Resource Class and then sets the eClass Property.
Where else would u suggest to do this?


> That sounds complex for the user to manage. Probably better is that you
> provide some type of mechanism whereby the user can annotate resources
> with additional properties (much like EModelElement can be annotated).
My users are software-developers, and they would like to edit a ecore-file.


> You can't modify a generated package at all. You can create new dynamic
> packages and use those. ReflectiveItemProviderAdapter shows how such
> newly discovered packages can contribute to the child creation menu.
Manged to do that already
Is it possible that classes created out of the dynamic package have the same xmlns as classes created out of the genereated package?
ATM i have 2 xmlns that correspond to the URIs of the Resources


> You can only do that if your base package itself is dynamic. Is that
> the case?
Not sure where to set that. But the base package is generated code so i guess its not.
Re: Adding classes to generated Package [message #903979 is a reply to message #903967] Mon, 27 August 2012 13:38 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Dominik,

Comments below.


On 27/08/2012 3:14 PM, Dominik Muttenthaler wrote:
> Thanks for the fast answer(s),
>
>> It's generally a bad idea for the constructor to populate features...
> What i do is the following:
> id = eClass().getName() + EcoreUtil.generateUUID();
>
> I already see that this isn't good, as soon as i have a dynamic class
> with supertype Resource since EMF creates the Resource Class and then
> sets the eClass Property.
> Where else would u suggest to do this?
Presumably your derived class extends Resource and then the ResourceImpl
is still constructed. The problem is that when you deserialize or copy
some instance, you needlessly create IDs...

Probably better is to compute and cache the ID in the getter. That way
there's an ID there is anything asks for it, but until then, there isn't
one and when you deserialize, the value from the serialization will be set.
>
>
>> That sounds complex for the user to manage. Probably better is that
>> you provide some type of mechanism whereby the user can annotate
>> resources with additional properties (much like EModelElement can be
>> annotated).
> My users are software-developers, and they would like to edit a
> ecore-file.
:-P
>
>
>> You can't modify a generated package at all. You can create new
>> dynamic packages and use those. ReflectiveItemProviderAdapter shows
>> how such newly discovered packages can contribute to the child
>> creation menu.
> Manged to do that already
> Is it possible that classes created out of the dynamic package have
> the same xmlns as classes created out of the genereated package?
No, each package must have a unique nsURI.
> ATM i have 2 xmlns that correspond to the URIs of the Resources
>
>
>> You can only do that if your base package itself is dynamic. Is that
>> the case?
> Not sure where to set that. But the base package is generated code so
> i guess its not.
Yes, that should have been obvious to me given you talked about
specializing the constructor.

So you need to create new packages with new classes.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Adding classes to generated Package [message #904833 is a reply to message #903979] Wed, 29 August 2012 08:37 Go to previous messageGo to next message
Dominik Muttenthaler is currently offline Dominik MuttenthalerFriend
Messages: 5
Registered: August 2012
Junior Member
Thanks a lot, your tip's really helped me.

Just one more question (I guess Smile):

As written in the first post i have the following structure:
Base.ecore (generated):
  EClass ResourceContainer
    EReference Resource (0 .. many)
  EClass Resource
    EAttribute id
  EClass SomeResource1 (superType = Resource)
    ...

Extended.ecore (dynamically loaded):
  EClass SomeResource2 (superType = Resource)
    ...
  EClass SomeResource2 (superType = Resource)
    ...

I already have a file with the following Structure:
ResourceContainer
  SomeResource1
  SomeResource2
  SomeResource3

When i start and right-click on ResourceContainer i can only add SomeResource1-childs.
But as soon as i select SomeResource2 or SomeResource3 i can also add all the Resources defined in Extended.ecore. It looks like they get loaded after the first selection.
I already checked the ResourceSet (it is completely loaded before i select SomeResource2 or SomeResource3).
Re: Adding classes to generated Package [message #904844 is a reply to message #904833] Wed, 29 August 2012 09:02 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Dominik,

Are you using ReflectiveItemProviderAdapter? It sounds like an issue of
having populated the cache of allEClasses. A brute for approach would
be to call IEditingDomainItemProvider.getNewChildDescriptors for each
EObject in the tree (or at least once for each EObject with a different
eClass()).


On 29/08/2012 10:37 AM, Dominik Muttenthaler wrote:
> Thanks a lot, your tip's really helped me.
>
> Just one more question (I guess :)):
>
> As written in the first post i have the following structure:
>
> Base.ecore (generated):
> EClass ResourceContainer
> EReference Resource (0 .. many)
> EClass Resource
> EAttribute id
> EClass SomeResource1 (superType = Resource)
> ...
>
>
> Extended.ecore (dynamically loaded):
> EClass SomeResource2 (superType = Resource)
> ...
> EClass SomeResource2 (superType = Resource)
> ...
>
> I already have a file with the following Structure:
>
> ResourceContainer
> SomeResource1
> SomeResource2
> SomeResource3
>
> When i start and right-click on ResourceContainer i can only add
> SomeResource1-childs.
> But as soon as i select SomeResource2 or SomeResource3 i can also add
> all the Resources defined in Extended.ecore. It looks like they get
> loaded after the first selection.
> I already checked the ResourceSet (it is completely loaded before i
> select SomeResource2 or SomeResource3).


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Adding classes to generated Package [message #904896 is a reply to message #904844] Wed, 29 August 2012 10:42 Go to previous message
Dominik Muttenthaler is currently offline Dominik MuttenthalerFriend
Messages: 5
Registered: August 2012
Junior Member
> Are you using ReflectiveItemProviderAdapter? It sounds like an issue of
> having populated the cache of allEClasses. A brute for approach would
> be to call IEditingDomainItemProvider.getNewChildDescriptors for each
> EObject in the tree (or at least once for each EObject with a different
> eClass()).

Thank you very much.
It looks like the "magic" happens in ReflectiveItemProvider.gatherMetaData.
Since its going up to the package and adds all Classes of that Package i thinks its enough to call IEditingDomainItemProvider.getNewChildDescriptors for one EObject (doesn't matter witch) in EPackage.

[Updated on: Wed, 29 August 2012 10:58]

Report message to a moderator

Previous Topic:Resource sync issue
Next Topic:[Teneo] Parent/Child/Child cascade merging error
Goto Forum:
  


Current Time: Thu Apr 18 13:56:17 GMT 2024

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

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

Back to the top