objectmergeeclipse-emfemfeclipse-emf-ecore

EMF Merging Two Objects


I have model generated by EMF.

I am writing API over it to provide easier CRUD operation to the users.

For this purpose in constructor of my API classes, I create a working-copy of my ECore Object using EcoreUtil.copy. Then all the operation occur over this working copy.

In case the user calls abandon change. I again create the copy of my original object and re-initialize the working copy object.

In case the user calls the save, I can't do a direct copy of working copy to original, as it won't change the model (copy's eContainer will be null and original model will be intact).

Hence, I want to merge my working copy to original. One of the possible solution for it is to set all the fields of original one by one. But, it can be lengthy and error-prone in case of large number of fields.

What can I do to easily perform merge operation? What are other possible approaches to tackle this problem?


Solution

  • I'm assuming this data can't be edited or even accessed by several users/threads at same time. If so, then the easiest way to implement such behavior is to use the Change Recorder which is part of the EMF framework.

    When the user starts editing the data, you simply attach the Change Recorder to the most outer object in the tree what you want to track (it may be the entire model), and then start recording. The changes will actually be done in the original objects, but in case user calls "abandon change" then you perform the rollback (undo) using the changes that the Change Recorder have collected. In case user calls "save" then you don't need to do anything else since the original objects were already changed, simply dispose the Change Recorder.

    Actually there is already the EMF Transactions framework that provides a Transactional Command Stack, which uses internal the Change Recorder to provide the undo and redo functionalities. In your case, you just need to make use of the "undo" when user calls "abandon change".