Home » Modeling » EMF » How to create a cluster of model elements with EMF commands
How to create a cluster of model elements with EMF commands [message #771612] |
Tue, 27 December 2011 18:53 |
Gregoire Dupe Messages: 75 Registered: September 2009 Location: France |
Member |
|
|
Hello,
I need an advice about the use of the EMF commands.
I have to use an EMF commands to create a cluster of model elements. To link the newly created elements each other should I used commands or can I directly use the generated API ?
Here is two pieces of code illustrating what I'm trying to describe:
--------- should I used commands ---------
// create an EMF command to add an EClass to the root EPackage
final EClass newEClass = EcoreFactory.eINSTANCE.createEClass();
final CommandParameter classCmdParam = new CommandParameter(ePackage,EcorePackage.eINSTANCE.getEPackage_EClassifiers(), newEClass);
final Command createEClass = CreateChildCommand.create(editingDomain, ePackage, classCmdParam, Collections.EMPTY_LIST);
// create an EMF command to add an EReference to the new EClass
final EReference newEReference = EcoreFactory.eINSTANCE.createEReference();
final CommandParameter refCmdParam = new CommandParameter(ePackage, EcorePackage.eINSTANCE.getEClass_EStructuralFeatures(), newEReference);
final Command createEReference = CreateChildCommand.create(editingDomain, newEClass, refCmdParam, Collections.EMPTY_LIST);
// create a compound command
final List<Command> cmdList = new ArrayList<Command>();
cmdList.add(createEClass);
cmdList.add(createEReference);
final Command compoundCmd = new CompoundCommand(cmdList);
executeCommandAndCheck(ePackage, editingDomain, newEClass, newEReference, compoundCmd);
--------- or can I directly use the generated API ---------
// create an EMF command to add an EClass to the root EPackage
final EClass newEClass = EcoreFactory.eINSTANCE.createEClass();
final CommandParameter classCmdParam = new CommandParameter(ePackage, EcorePackage.eINSTANCE.getEPackage_EClassifiers(), newEClass);
final Command createEClass = CreateChildCommand.create(editingDomain, ePackage, classCmdParam, Collections.EMPTY_LIST);
// add an EReference to the new EClass
final EReference newEReference = EcoreFactory.eINSTANCE.createEReference();
newEClass.getEStructuralFeatures().add(newEReference);
executeCommandAndCheck(ePackage, editingDomain, newEClass, newEReference, createEClass);
I'm asking this question because I've a unit test in a regression status (EMF Facet project > org.eclipse.emf.facet.widgets.nattable.tests.swtbot.Bug344925Test.testBug344925). This test fails with the EMF 2.8 but not with EMF 2.7.
With EMF 2.8, I get the following exception:
java.lang.NullPointerException
at org.eclipse.emf.ecore.presentation.EcoreEditor$5.isReadOnly(EcoreEditor.java:853)
at org.eclipse.emf.edit.command.AddCommand.prepare(AddCommand.java:388)
at org.eclipse.emf.common.command.AbstractCommand.canExecute(AbstractCommand.java:114)
at org.eclipse.emf.edit.command.AbstractOverrideableCommand.doCanExecute(AbstractOverrideableCommand.java:120)
at org.eclipse.emf.edit.command.AbstractOverrideableCommand.canExecute(AbstractOverrideableCommand.java:113)
at org.eclipse.emf.common.command.CommandWrapper.prepare(CommandWrapper.java:169)
at org.eclipse.emf.common.command.AbstractCommand.canExecute(AbstractCommand.java:114)
at org.eclipse.emf.common.command.CompoundCommand.prepare(CompoundCommand.java:247)
at org.eclipse.emf.common.command.AbstractCommand.canExecute(AbstractCommand.java:114)
I'm wondering to know if I have to change the current implementation of the EMF Facet's component or if I mustn't (in the case of there is something wrong with EcoreEditor).
I've isolated the problem in the following unit test class: http://dev.eclipse.org/svnroot/modeling/org.eclipse.emft.facet/trunk/tests/org.eclipse.emf.facet.widgets.nattable.tests/src/org/eclipse/emf/facet/widgets/nattable/tests/swtbot/Bug365843.java
Regards,
Grégoire
|
|
|
Re: How to create a cluster of model elements with EMF commands [message #774078 is a reply to message #771612] |
Tue, 03 January 2012 08:19 |
Ed Merks Messages: 33137 Registered: July 2009 |
Senior Member |
|
|
Gregoire,
Comments below.
On 27/12/2011 7:53 PM, Gregoire Dupe wrote:
> Hello,
>
> I need an advice about the use of the EMF commands.
>
> I have to use an EMF commands to create a cluster of model elements.
> To link the newly created elements each other should I used commands
> or can I directly use the generated API ?
If you want this done in an editor such that undo is supported you have
to use commands of some sort. A change command is useful if you want to
record changes that result from directly using the API...
>
> Here is two pieces of code illustrating what I'm trying to describe:
>
>
> --------- should I used commands ---------
>
> // create an EMF command to add an EClass to the root EPackage
> final EClass newEClass = EcoreFactory.eINSTANCE.createEClass();
> final CommandParameter classCmdParam = new
> CommandParameter(ePackage,EcorePackage.eINSTANCE.getEPackage_EClassifiers(),
> newEClass);
> final Command createEClass = CreateChildCommand.create(editingDomain,
> ePackage, classCmdParam, Collections.EMPTY_LIST);
> // create an EMF command to add an EReference to the new EClass
> final EReference newEReference =
> EcoreFactory.eINSTANCE.createEReference();
> final CommandParameter refCmdParam = new CommandParameter(ePackage,
> EcorePackage.eINSTANCE.getEClass_EStructuralFeatures(), newEReference);
> final Command createEReference =
> CreateChildCommand.create(editingDomain, newEClass, refCmdParam,
> Collections.EMPTY_LIST);
> // create a compound command
> final List<Command> cmdList = new ArrayList<Command>();
> cmdList.add(createEClass);
> cmdList.add(createEReference);
> final Command compoundCmd = new CompoundCommand(cmdList);
> executeCommandAndCheck(ePackage, editingDomain, newEClass,
> newEReference, compoundCmd);
>
>
>
> --------- or can I directly use the generated API ---------
>
> // create an EMF command to add an EClass to the root EPackage
> final EClass newEClass = EcoreFactory.eINSTANCE.createEClass();
> final CommandParameter classCmdParam = new CommandParameter(ePackage,
> EcorePackage.eINSTANCE.getEPackage_EClassifiers(), newEClass);
> final Command createEClass = CreateChildCommand.create(editingDomain,
> ePackage, classCmdParam, Collections.EMPTY_LIST);
> // add an EReference to the new EClass
> final EReference newEReference =
> EcoreFactory.eINSTANCE.createEReference();
> newEClass.getEStructuralFeatures().add(newEReference);
> executeCommandAndCheck(ePackage, editingDomain, newEClass,
> newEReference, createEClass);
>
>
> I'm asking this question because I've a unit test in a regression
> status (EMF Facet project >
> org.eclipse.emf.facet.widgets.nattable.tests.swtbot.Bug344925Test.testBug344925).
> This test fails with the EMF 2.8 but not with EMF 2.7.
> With EMF 2.8, I get the following exception:
>
> java.lang.NullPointerException
> at
> org.eclipse.emf.ecore.presentation.EcoreEditor$5.isReadOnly(EcoreEditor.java:853)
Do you have resources without a URI in the resource set? That would
certainly cause this problem...
> at
> org.eclipse.emf.edit.command.AddCommand.prepare(AddCommand.java:388)
> at
> org.eclipse.emf.common.command.AbstractCommand.canExecute(AbstractCommand.java:114)
> at
> org.eclipse.emf.edit.command.AbstractOverrideableCommand.doCanExecute(AbstractOverrideableCommand.java:120)
> at
> org.eclipse.emf.edit.command.AbstractOverrideableCommand.canExecute(AbstractOverrideableCommand.java:113)
> at
> org.eclipse.emf.common.command.CommandWrapper.prepare(CommandWrapper.java:169)
> at
> org.eclipse.emf.common.command.AbstractCommand.canExecute(AbstractCommand.java:114)
> at
> org.eclipse.emf.common.command.CompoundCommand.prepare(CompoundCommand.java:247)
> at
> org.eclipse.emf.common.command.AbstractCommand.canExecute(AbstractCommand.java:114)
>
>
> I'm wondering to know if I have to change the current implementation
> of the EMF Facet's component or if I mustn't (in the case of there is
> something wrong with EcoreEditor).
>
> I've isolated the problem in the following unit test class:
> http://dev.eclipse.org/svnroot/modeling/org.eclipse.emft.facet/trunk/tests/org.eclipse.emf.facet.widgets.nattable.tests/src/org/eclipse/emf/facet/widgets/nattable/tests/swtbot/Bug365843.java
Probably the unit test is not creating a nicely formed resource set
where all resources have a URI...
>
> Regards,
> Grégoire
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| | | |
Goto Forum:
Current Time: Sat Apr 20 03:34:24 GMT 2024
Powered by FUDForum. Page generated in 0.02800 seconds
|