Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Deleting many eobjects
Deleting many eobjects [message #1703745] Tue, 04 August 2015 15:56 Go to next message
Andreas Graf is currently offline Andreas GrafFriend
Messages: 211
Registered: July 2009
Senior Member
Dear EMF gurus,

we have the situation that we need to delete more than one element from an EMF model with EcoreUtil.delete(). However, we would like to avoid that EcoreUtil tries to find the references again for each invocation. Since EcoreUtil.delete with boolean flag recursive set to true, it seems that the code can handle more than one object.

The idea is to have a custom implementation of EcoreUtil.delete() that starts like this:

	/**
	 * Warning: ASSUMPTION is that all objects are either under the same root Object
	 * or in the same resource set.
	 * 
	 * @param eObjects
	 * @param recursive
	 */
	public static void delete(List<EObject> inList, boolean recursive) {
		if (inList.isEmpty())
			return;
		if (recursive) {
			EObject rootEObject = EcoreUtil.getRootContainer(inList.get(0));
			Resource resource = rootEObject.eResource();

			Set<EObject> eObjects = new HashSet<EObject>();
			Set<EObject> crossResourceEObjects = new HashSet<EObject>();
			eObjects.addAll(inList);
			for(EObject eObject : inList ) {
				for (@SuppressWarnings("unchecked")
				TreeIterator<InternalEObject> j = (TreeIterator<InternalEObject>) (TreeIterator<?>) eObject
						.eAllContents(); j.hasNext();) {
					InternalEObject childEObject = j.next();
					if (childEObject.eDirectResource() != null) {
						crossResourceEObjects.add(childEObject);
					} else {
						eObjects.add(childEObject);
					}
				}
			}


That basically means, not only collecting the descendents of one object, but of a list of objects that is being passed as argument.

Am I missing any inner workings of EMF that would break this?

Regards,

Andreas
Re: Deleting many eobjects [message #1703827 is a reply to message #1703745] Wed, 05 August 2015 08:47 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33141
Registered: July 2009
Senior Member
Andreas,

I suppose a deleteAll method would be good. Of course one would want it
to be more general without assumptions. What you show below should be
okay given the assumption holds. Of course you'd want to augment the
"remove(eObject);" call to remove all the objects from their container...


On 04/08/2015 5:56 PM, Andreas Graf wrote:
> Dear EMF gurus,
>
> we have the situation that we need to delete more than one element
> from an EMF model with EcoreUtil.delete(). However, we would like to
> avoid that EcoreUtil tries to find the references again for each
> invocation. Since EcoreUtil.delete with boolean flag recursive set to
> true, it seems that the code can handle more than one object.
>
> The idea is to have a custom implementation of EcoreUtil.delete() that
> starts like this:
>
> /**
> * Warning: ASSUMPTION is that all objects are either under the
> same root Object
> * or in the same resource set.
> * * @param eObjects
> * @param recursive
> */
> public static void delete(List<EObject> inList, boolean recursive) {
> if (inList.isEmpty())
> return;
> if (recursive) {
> EObject rootEObject =
> EcoreUtil.getRootContainer(inList.get(0));
> Resource resource = rootEObject.eResource();
>
> Set<EObject> eObjects = new HashSet<EObject>();
> Set<EObject> crossResourceEObjects = new HashSet<EObject>();
> eObjects.addAll(inList);
> for(EObject eObject : inList ) {
> for (@SuppressWarnings("unchecked")
> TreeIterator<InternalEObject> j =
> (TreeIterator<InternalEObject>) (TreeIterator<?>) eObject
> .eAllContents(); j.hasNext();) {
> InternalEObject childEObject = j.next();
> if (childEObject.eDirectResource() != null) {
> crossResourceEObjects.add(childEObject);
> } else {
> eObjects.add(childEObject);
> }
> }
> }
>
> That basically means, not only collecting the descendents of one
> object, but of a list of objects that is being passed as argument.
>
> Am I missing any inner workings of EMF that would break this?
>
> Regards,
>
> Andreas
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:[CDO]is the new client UI available?
Next Topic:[EMF Transaction] Exception during rollback
Goto Forum:
  


Current Time: Fri Apr 26 02:41:12 GMT 2024

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

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

Back to the top