Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
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 Go to next message
Gregoire Dupe is currently offline Gregoire DupeFriend
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 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
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/
Re: How to create a cluster of model elements with EMF commands [message #774228 is a reply to message #774078] Tue, 03 January 2012 14:54 Go to previous messageGo to next message
Gregoire Dupe is currently offline Gregoire DupeFriend
Messages: 75
Registered: September 2009
Location: France
Member
Hello,

Thank you for your answer.

I've added the following test before the call of the method 'canExecute()':

	private static void checkResourceSet(final ResourceSet resourceSet) {
		for (Resource resource : resourceSet.getResources()) {
			Assert.assertNotNull("The resource's URI must not be null.", resource.getURI()); //$NON-NLS-1$
		}
	}


No failure appends.

I've debugged the following method: org.eclipse.emf.ecore.presentation.new AdapterFactoryEditingDomain() {...}.isReadOnly(Resource)

        public boolean isReadOnly(Resource resource)
        {
          return 
            "java".equals(resource.getURI().scheme()) || 
            "xcore".equals(resource.getURI().fileExtension()) ||
            super.isReadOnly(resource);
        }


This is the parameter 'resource' which is null and not its URI. This not surprising because the method 'isReadOnly' is called by the following expression:

domain.isReadOnly(owner.eResource())


The variable 'owner' contains the EClass that I've just created and not attached yet: I'm trying to execute the command which has to attached my EClass to its container.

Should I add all the newly created EObjects in resource.getContents() before to execute any command ?

Regards,
Grégoire
Re: How to create a cluster of model elements with EMF commands [message #774272 is a reply to message #774228] Tue, 03 January 2012 16:09 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Gregoire,

Comments below.

On 03/01/2012 3:54 PM, Gregoire Dupe wrote:
> Hello,
>
> Thank you for your answer.
>
> I've added the following test before the call of the method
> 'canExecute()':
>
>
> private static void checkResourceSet(final ResourceSet resourceSet) {
> for (Resource resource : resourceSet.getResources()) {
> Assert.assertNotNull("The resource's URI must not be
> null.", resource.getURI()); //$NON-NLS-1$
> }
> }
>
>
> No failure appends.
>
> I've debugged the following method:
> org.eclipse.emf.ecore.presentation.new AdapterFactoryEditingDomain()
> {...}.isReadOnly(Resource)
>
>
> public boolean isReadOnly(Resource resource)
> {
> return "java".equals(resource.getURI().scheme())
> || "xcore".equals(resource.getURI().fileExtension()) ||
> super.isReadOnly(resource);
> }
>
>
> This is the parameter 'resource' which is null and not its URI. This
> not surprising because the method 'isReadOnly' is called by the
> following expression:
>
> domain.isReadOnly(owner.eResource())
I didn't expect that it was ever called with null.
>
>
> The variable 'owner' contains the EClass that I've just created and
> not attached yet: I'm trying to execute the command which has to
> attached my EClass to its container.
No, we should fix this recently changed override to be null tolerant.
Please open a bugzilla.
>
> Should I add all the newly created EObjects in resource.getContents()
> before to execute any command ?
>
> Regards,
> Grégoire
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to create a cluster of model elements with EMF commands [message #774659 is a reply to message #774272] Wed, 04 January 2012 11:03 Go to previous message
Gregoire Dupe is currently offline Gregoire DupeFriend
Messages: 75
Registered: September 2009
Location: France
Member
Hello,

Ed Merks wrote on Tue, 03 January 2012 11:09


Please open a bugzilla.



Done: Bug 367828 - NullPointerException in EcoreEditor$5.isReadOnly

Thank you.
Grégoire
Previous Topic:[CDO] ID generation location
Next Topic:One way cross document reference to any EObject
Goto Forum:
  


Current Time: Sat Apr 20 03:34:24 GMT 2024

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

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

Back to the top