Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » A newbie question of EMF transaction
A newbie question of EMF transaction [message #1014163] Mon, 25 February 2013 09:29 Go to next message
sakop qq is currently offline sakop qqFriend
Messages: 29
Registered: January 2013
Junior Member
The RecordingCommand is said to support the undo function.

below is the code that makes me confused.

package test;


import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalCommandStack;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.example.library.Book;
import org.eclipse.example.library.BookCategory;
import org.eclipse.example.library.Library;
import org.eclipse.example.library.LibraryFactory;
import org.eclipse.example.library.Writer;

public class TransactionalEditingDomainTest {
public static void main(String[] args) {
LibraryFactory factory = LibraryFactory.eINSTANCE;

final Library library = factory.createLibrary();
final Writer sakop = factory.createWriter();

ResourceSet rset = new ResourceSetImpl();
rset.getResourceFactoryRegistry().getExtensionToFactoryMap()
.put("xmi", new XMIResourceFactoryImpl());
final Resource file1 = rset.createResource(URI.createFileURI("test.xmi"));

TransactionalEditingDomain domain = TransactionalEditingDomain.Factory.INSTANCE
.createEditingDomain(rset);

TransactionalCommandStack stack = (TransactionalCommandStack) domain.getCommandStack();

RecordingCommand command = new RecordingCommand(domain) {

@Override
protected void doExecute() {
file1.getContents().add(library);
library.getWriters().add(sakop);
}

public boolean canUndo(){
return true;
}
};

System.out.println(library.getWriters());
stack.execute(command);
System.out.println(library.getWriters());
stack.undo();
System.out.println(library.getWriters());//still prints the writer list
}
}

I suppose that after undo library.getWriters() should be an empty EList,can sb tell me why?

Thanks
Re: A newbie question of EMF transaction [message #1014286 is a reply to message #1014163] Mon, 25 February 2013 14:20 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33136
Registered: July 2009
Senior Member
You're not using the editing domain's resource set so it's not listening
to your objects being changed.

On 25/02/2013 10:29 AM, sakop qq wrote:
> The RecordingCommand is said to support the undo function.
>
> below is the code that makes me confused.
>
> package test;
>
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
> import org.eclipse.emf.transaction.RecordingCommand;
> import org.eclipse.emf.transaction.TransactionalCommandStack;
> import org.eclipse.emf.transaction.TransactionalEditingDomain;
> import org.eclipse.example.library.Book;
> import org.eclipse.example.library.BookCategory;
> import org.eclipse.example.library.Library;
> import org.eclipse.example.library.LibraryFactory;
> import org.eclipse.example.library.Writer;
>
> public class TransactionalEditingDomainTest {
> public static void main(String[] args) {
> LibraryFactory factory = LibraryFactory.eINSTANCE;
>
> final Library library = factory.createLibrary();
> final Writer sakop = factory.createWriter();
>
> ResourceSet rset = new ResourceSetImpl();
> rset.getResourceFactoryRegistry().getExtensionToFactoryMap()
> .put("xmi", new XMIResourceFactoryImpl());
> final Resource file1 =
> rset.createResource(URI.createFileURI("test.xmi"));
>
> TransactionalEditingDomain domain =
> TransactionalEditingDomain.Factory.INSTANCE
> .createEditingDomain(rset);
>
> TransactionalCommandStack stack = (TransactionalCommandStack)
> domain.getCommandStack();
>
> RecordingCommand command = new RecordingCommand(domain) {
>
> @Override
> protected void doExecute() {
> file1.getContents().add(library);
> library.getWriters().add(sakop);
> }
>
> public boolean canUndo(){
> return true;
> }
> };
>
> System.out.println(library.getWriters());
> stack.execute(command);
> System.out.println(library.getWriters());
> stack.undo();
> System.out.println(library.getWriters());//still prints the
> writer list
> }
> }
>
> I suppose that after undo library.getWriters() should be an empty
> EList,can sb tell me why?
>
> Thanks
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: A newbie question of EMF transaction [message #1014320 is a reply to message #1014286] Mon, 25 February 2013 15:35 Go to previous messageGo to next message
sakop qq is currently offline sakop qqFriend
Messages: 29
Registered: January 2013
Junior Member
I made some changes,but still no use

TransactionalEditingDomain domain = TransactionalEditingDomain.Factory.INSTANCE
.createEditingDomain();
ResourceSet rset = domain.getResourceSet();

would you please tell me how to do it ,just a tiny example please,thanks!
Re: A newbie question of EMF transaction [message #1014406 is a reply to message #1014163] Mon, 25 February 2013 18:56 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

Hi,

I'm mildly surprised that you didn't get some kind of exception in
trying to run this stand-alone test application. The Transaction API
wasn't designed to work without the Eclipse framework running; I might
have expected the transaction in which the RecordingCommand executes to
fail to commit because of a problem validating it (the Validation API
also wasn't designed to work without Eclipse).

Can you confirm in the debugger that the top command on the undo stack
is the RecordingCommand? That is, is "stack.undo()" actually undoing
what you think it's undoing?

If so, then what does the ChangeDescription look like that
RecordingCommand.undo() is undoing?

As far as examples are concerned, the EMF Transaction SDK includes
tutorial documentation in the Eclipse Help centre and also an Examples
wizard that installs one or more example projects in your workspace.
These examples are documented in the help.

HTH,

Christian


On 2013-02-25 09:29:52 +0000, sakop qq said:

> The RecordingCommand is said to support the undo function.
>
> below is the code that makes me confused.
>
> package test;
>
>
> import org.eclipse.emf.common.util.URI;
> import org.eclipse.emf.ecore.resource.Resource;
> import org.eclipse.emf.ecore.resource.ResourceSet;
> import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
> import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
> import org.eclipse.emf.transaction.RecordingCommand;
> import org.eclipse.emf.transaction.TransactionalCommandStack;
> import org.eclipse.emf.transaction.TransactionalEditingDomain;
> import org.eclipse.example.library.Book;
> import org.eclipse.example.library.BookCategory;
> import org.eclipse.example.library.Library;
> import org.eclipse.example.library.LibraryFactory;
> import org.eclipse.example.library.Writer;
>
> public class TransactionalEditingDomainTest {
> public static void main(String[] args) {
> LibraryFactory factory = LibraryFactory.eINSTANCE;
>
> final Library library = factory.createLibrary();
> final Writer sakop = factory.createWriter();
>
> ResourceSet rset = new ResourceSetImpl();
> rset.getResourceFactoryRegistry().getExtensionToFactoryMap()
> .put("xmi", new XMIResourceFactoryImpl());
> final Resource file1 = rset.createResource(URI.createFileURI("test.xmi"));
>
> TransactionalEditingDomain domain =
> TransactionalEditingDomain.Factory.INSTANCE
> .createEditingDomain(rset);
>
> TransactionalCommandStack stack = (TransactionalCommandStack)
> domain.getCommandStack();
>
> RecordingCommand command = new RecordingCommand(domain) {
>
> @Override
> protected void doExecute() {
> file1.getContents().add(library);
> library.getWriters().add(sakop);
> }
>
> public boolean canUndo(){
> return true;
> }
> };
>
> System.out.println(library.getWriters());
> stack.execute(command);
> System.out.println(library.getWriters());
> stack.undo();
> System.out.println(library.getWriters());//still prints the writer list
> }
> }
>
> I suppose that after undo library.getWriters() should be an empty
> EList,can sb tell me why?
>
> Thanks
Re: A newbie question of EMF transaction [message #1014595 is a reply to message #1014406] Tue, 26 February 2013 05:53 Go to previous messageGo to next message
sakop qq is currently offline sakop qqFriend
Messages: 29
Registered: January 2013
Junior Member
Yes,an error was thrown saying that the "EvaluationContext.class" is not found,so I added "org.eclipse.core.expressions" to the dependecy list and the error was suppressed
Re: A newbie question of EMF transaction [message #1014800 is a reply to message #1014595] Tue, 26 February 2013 13:38 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

OK, that makes sense.

So, the problem is resolved? Or it still occurs but now there are no
errors in the log?

cW


On 2013-02-26 05:53:06 +0000, sakop qq said:

> Yes,an error was thrown saying that the "EvaluationContext.class" is
> not found,so I added "org.eclipse.core.expressions" to the dependecy
> list and the error was suppressed
Re: A newbie question of EMF transaction [message #1015164 is a reply to message #1014800] Wed, 27 February 2013 15:45 Go to previous message
sakop qq is currently offline sakop qqFriend
Messages: 29
Registered: January 2013
Junior Member
In fact, I was trying to do some experiments using TransactionalEditingDomain in graphiti. But I didn't know where to use it,so I quitted.Thanks anyway.
Previous Topic:Cross referencing model
Next Topic:ecore XMLHandler barfing on transient and derived member - why?
Goto Forum:
  


Current Time: Fri Apr 19 01:52:20 GMT 2024

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

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

Back to the top