Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Rollback to a previous state of the model
Rollback to a previous state of the model [message #1792571] Thu, 19 July 2018 15:10 Go to next message
Parsa Pourali is currently offline Parsa PouraliFriend
Messages: 197
Registered: February 2014
Senior Member
Hello,

[I have already asked this question on XText and Sirius forums but didn't get the answer I wanted, so I thought maybe someone here could help].

I was wondering if there is a way to get a snapshot of the model at some point and then revert back everything to it. So, basically, I need to do:
1- keeping the snapshot in the memory,
2- do sth with the model and modify it.
3- if the modification is not what I wanted, then revert everything back to the snapshot that is taken in the step 1.

I can't use RecordingCommand because there is not a single change that I roll back. I will show a dialog to the user where the user can do whatever he wants to do with the model and all these changes are within their own recording command. Then he can either accept or cancel the changes. By default, the Accept will do nothing because the changes that he made in the dialog are already saved to the original model at the time of the change. But if he selects the Cancel button, I need to revert back to the time before he opens the dialog.

I tried to create an element (i.e. snapshot resource) in the memory and use ecoreutils.copy to copy everything from the original resource to the snapshot resource. But the copy back from the snapshot to the original resource does not seem to work :(

Any help would be appreciated.

Thanks,
Parsa
Re: Rollback to a previous state of the model [message #1792593 is a reply to message #1792571] Fri, 20 July 2018 03:31 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 29653
Registered: July 2009
Senior Member
You have two approaches you can take. One would be to make a copy, have the dialog operate on that copy (not the original), and then reconcile the changes in the copy against the state of the original when the user presses OK. The other approach is to allow the changes to be directly made to the model, using commands, and then to undo the commands if they press Cancel. At the start of the editing, you could use CommandStack.getUndoCommand() to remember the command stack's state, and if they press Cancel, you can call undo() until the getUndoCommand() is the same as it originally was before the dialog started. If all changes are properly done with commands, that should restore the model to the original state.
Re: Rollback to a previous state of the model [message #1792643 is a reply to message #1792593] Fri, 20 July 2018 12:35 Go to previous messageGo to next message
Parsa Pourali is currently offline Parsa PouraliFriend
Messages: 197
Registered: February 2014
Senior Member
Hi Ed.,

Thanks a lot for your beautiful explanation. I just have a few questions on copying:
1) The actual resource is an XText resource. I don't think that makes any difference in my case, right?

2) should I use ecoreutils.copy to copy or there is a better way such as root.getcontents().add(originalRoot.getallcontents() ? These copies do not point to the same element address (i.e., changing one does not affect the other), right ?

3) If there is an element e.g., root.statemachine1.transition1 in the original model, how can I get it from the copied model? should I get its uri fragment from the original and call it on the coped version?

I'd appreciate it if you could kindly make some notes on the above questions.

Best,
Parsa
Re: Rollback to a previous state of the model [message #1792644 is a reply to message #1792643] Fri, 20 July 2018 13:24 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 6111
Registered: July 2009
Senior Member
Hi

I would caution against using the CommandStack for roll-back since any imperfection in the coding of any undo (or redo) will cause the roll-back to at least partially deficient.

Since you only want the current/saved state you never really need to roll-back; you just point the UI at the one or other of two alternate complete models assigned to Resource.getContents().

You should certainly use the EcoreUtils.copier. The copier maintains a nice map from source to target, but from your description you should never need to use it.

Regards

Ed Willink
Re: Rollback to a previous state of the model [message #1792669 is a reply to message #1792644] Fri, 20 July 2018 18:11 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 29653
Registered: July 2009
Senior Member
1) Well yes it makes a difference, because an Xtext resource is a text file, and if you modify directly the model, you would need to destroy (recreate from scratch) the underlying node representation and its textual representation. So direct editing of the model seems not ideal for this case.

2) A Copier is a map. You can create one, call copyAll, and then copyReferences an you'll have a complete map from originals to copies.

3) A copier is a map. You can create the inverse of this map to go from copy to original.
Previous Topic:Extending Ecore models
Next Topic:[SOLVED] Hello, I have hit the "...exceeding the 65535 bytes limit". Any help?
Goto Forum:
  


Current Time: Sat Nov 17 22:06:47 GMT 2018

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

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

Back to the top