Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » EMF Command for updating EMaps?
EMF Command for updating EMaps? [message #394459] Wed, 20 July 2005 19:08 Go to next message
Ed Snible is currently offline Ed SnibleFriend
Messages: 34
Registered: July 2009
Member
I'd like to set an EAnnotation detail through an AddCommand/SetCommand.

I have an editingDomain that contains an EAnnotation. I know about
EcorePackage.eINSTANCE.getEAnnotation_Details(). What I can't figure out
is how to add the entry "Foo" -> "Bar" through this mechanism.

AddCommand has a constructor whose second parameter is an EList -- and an
EMap is an EList -- but the EntryImpls that EMap uses are protected so I
can't simply make one myself and add it to the EMap-as-an-EList.

I could use the SetCommand() with the EAnnotation and it's
EcorePackage.eINSTANCE.getEAnnotation_Details(). To do that I'd need to
make a copy of the EMap, update the copy, and use the copy for SetCommand
(right?) I don't know how to duplicate an EMap -- EcoreUtil.copy() only
copies EObjects.

Please advise.
Re: EMF Command for updating EMaps? [message #394461 is a reply to message #394459] Wed, 20 July 2005 19:58 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 29538
Registered: July 2009
Senior Member
Ed,

You can create a map entry using
EcoreFactory.eINSTANCE.create(EcorePackage.eINSTANCE.getEStr ingToStringMapEntry())),
you can create a copy of a collection of EObjects (including an EMap)
using EcoreUtil.copyAll, and you can convert a collection of Map.Entry
values to an EMap using new BasicEMap() and applying addAll...


Ed Snible wrote:

> I'd like to set an EAnnotation detail through an AddCommand/SetCommand.
>
> I have an editingDomain that contains an EAnnotation. I know about
> EcorePackage.eINSTANCE.getEAnnotation_Details(). What I can't figure
> out is how to add the entry "Foo" -> "Bar" through this mechanism.
>
> AddCommand has a constructor whose second parameter is an EList -- and
> an EMap is an EList -- but the EntryImpls that EMap uses are protected
> so I can't simply make one myself and add it to the EMap-as-an-EList.
>
> I could use the SetCommand() with the EAnnotation and it's
> EcorePackage.eINSTANCE.getEAnnotation_Details(). To do that I'd need
> to make a copy of the EMap, update the copy, and use the copy for
> SetCommand (right?) I don't know how to duplicate an EMap --
> EcoreUtil.copy() only copies EObjects.
>
> Please advise.
>
Re: EMF Command for updating EMaps? [message #1719082 is a reply to message #394461] Tue, 05 January 2016 01:25 Go to previous messageGo to next message
Jim Boone is currently offline Jim BooneFriend
Messages: 15
Registered: August 2015
Location: North Carolina
Junior Member
Ed,

I stumbled across this posting because I basically want to do the same thing that the other Ed wanted to. I want to programmatically add or update the documentation text used when generating XML (see below):

source: http://www.eclipse.org/emf/2002/GenModel
keys: documentation and appinfo

I have searched for coding examples and snippets that would show me how to do this using an editing domain without any luck. Your posting is interesting, but still vague to me. Can you point to a complete example of how this is done? I tried to find where in the code this is done in the generated editor but could not find it. Thanks in advance!

--Jim
Re: EMF Command for updating EMaps? [message #1719140 is a reply to message #1719082] Tue, 05 January 2016 15:20 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 29538
Registered: July 2009
Senior Member
Jim,

And you want to do this as commands?

In the Ecore editor, a new details entry is created because of this code
in
org.eclipse.emf.ecore.provider.EAnnotationItemProvider.collectNewChildDescriptors(Collection<Object>,
Object)

protected void collectNewChildDescriptors(Collection<Object>
newChildDescriptors, Object object)
{
// super.collectNewChildDescriptors(newChildDescriptors, object);

newChildDescriptors.add
(createChildParameter
(EcorePackage.Literals.EANNOTATION__DETAILS,
EcoreFactory.eINSTANCE.create(EcorePackage.Literals.ESTRING_TO_STRING_MAP_ENTRY)));
}

So that's how you create a new Detail entry if you need to add
something, and you can add it using an AddCommand. If you want to
update the value of an entry, you can do that with a SetCommand.

On 05/01/2016 2:25 AM, Jim Boone wrote:
> Ed,
>
> I stumbled across this posting because I basically want to do the same
> thing that the other Ed wanted to. I want to programmatically add or
> update the documentation text used when generating XML (see below):
>
> source: http://www.eclipse.org/emf/2002/GenModel
> keys: documentation and appinfo
>
> I have searched for coding examples and snippets that would show me
> how to do this using an editing domain without any luck. Your posting
> is interesting, but still vague to me. Can you point to a complete
> example of how this is done? I tried to find where in the code this
> is done in the generated editor but could not find it. Thanks in
> advance!
>
> --Jim
Re: EMF Command for updating EMaps? [message #1719175 is a reply to message #1719140] Tue, 05 January 2016 20:28 Go to previous messageGo to next message
Jim Boone is currently offline Jim BooneFriend
Messages: 15
Registered: August 2015
Location: North Carolina
Junior Member
I am still struggling. Perhaps a simple illustration is in order.

Let's say that I have an eClass with an annotation as shown below. Then update the value of as shown. Now I want to use the editing domain to update it. How would I do that?

....
		String documentationValue = EcoreUtil.getAnnotation(clazz, "Test", "documentation");
		System.out.println("Original Value: " + documentationValue);

		String newValue = "This is updated documentation";
		EcoreUtil.setAnnotation(clazz, "Test", "documentation", newValue);
		System.out.println("Updated Value: " + newValue);

		/*
		 * Command cmd = SetCommand.create(editingDomain, clazz, feature???, value??); 
		 * editingDomain.getCommandStack().execute(cmd);
		 */
....
Output:
Original Value: this is documentation text
Updated Value: This is updated documentation
Re: EMF Command for updating EMaps? [message #1719197 is a reply to message #1719175] Wed, 06 January 2016 07:36 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 29538
Registered: July 2009
Senior Member
Jim,

You need to consider what the implementation normally does:

public static void setAnnotation(EModelElement eModelElement, String
sourceURI, String key, String value)
{
EAnnotation eAnnotation = eModelElement.getEAnnotation(sourceURI);
if (value == null)
{
if (eAnnotation != null)
{
eAnnotation.getDetails().removeKey(key);
}
}
else
{
if (eAnnotation == null)
{
eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation();
eAnnotation.setSource(sourceURI);
eModelElement.getEAnnotations().add(eAnnotation);
}
eAnnotation.getDetails().put(key, value);
}
}

So one brute force approach is to use a ChangeCommand and just invoke
that method. The other approach is to consider all the cases. If there
isn't an EAnnotation with the expected source URI present, you need to
create one and add it to the EModelElement.eAnnotations feature. The
next case to consider is trickier. The EAnnotation.details feature
might contain an entry for the key; in that case, you need to find that
entry and set the value feature. If it doesn't contain an entry, you
need to create an entry and add it. All this can be done using add and
set commands. A ChangeCommand wouldn't be so bad either. You only
need to attach the change recorder to the EModelElement because it's the
only thing you'll change.



On 05/01/2016 9:28 PM, Jim Boone wrote:
> I am still struggling. Perhaps a simple illustration is in order.
>
> Let's say that I have an eClass with an annotation as shown below.
> Then update the value of as shown. Now I want to use the editing
> domain to update it. How would I do that?
>
>
> ...
> String documentationValue = EcoreUtil.getAnnotation(clazz,
> "Test", "documentation");
> System.out.println("Original Value: " + documentationValue);
>
> String newValue = "This is updated documentation";
> EcoreUtil.setAnnotation(clazz, "Test", "documentation",
> newValue);
> System.out.println("Updated Value: " + newValue);
>
> /*
> * Command cmd = SetCommand.create(editingDomain, clazz,
> feature???, value??); *
> editingDomain.getCommandStack().execute(cmd);
> */
> ...
> Output:
> Original Value: this is documentation text
> Updated Value: This is updated documentation
>
Previous Topic:Where can I find Ecore.ecore
Next Topic:[CDO] ClassCastException for StaleReferenceProxy
Goto Forum:
  


Current Time: Wed Sep 19 10:49:18 GMT 2018

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

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

Back to the top