Home » Modeling » EMF » [EMF Transaction] How to use it?
[EMF Transaction] How to use it? [message #540080] |
Mon, 14 June 2010 19:35 |
Oliver Messages: 20 Registered: July 2009 |
Junior Member |
|
|
Hi,
I'am new to EMF Transaction. If I understood it right, the use case of
EMF Transaction is modifying the same model/IEditingDomain with
different editor instances at the same time without the "dirty read"
problem.
In my case I wan't to modify my model (e.g. model transformation) and
after that the modified model has to validate. If the validation fails,
the model changes have to undone (i.e. rollback).
So I think this needs this steps:
1. Creating a ITransactionalEditingDomain for the ResourceSet where the
model lives:
TransactionalEditingDomain editingDomain =
TransactionalEditingDomain.Factory.INSTANCE.createEditingDom ain(resourceSet);
2. Getting a TransactionalCommandStack of the editing domain:
TransactionalCommandStack commandStack = (TransactionalCommandStack)
editingDomain.getCommandStack();
Does the TransactionalCommandStack represents a root transaction with
commands as child transactions?
3. Modify my model. If I understood it right, I have two ways to modify
my model:
a) executing some command (e.g. AddCommand, SetCommand etc.) on the
command stack - preferred for editors.
b) create a RecordingCommand, modifying my model inside the doExecute
method (e.g. myLibrary.setName("MyLib")) and execute the
RecordingCommand on the command stack - preferred for model transformations.
4. All changes are done, so I wan't to validate my model. Does EMF
Transaction provide validation with EMF Validating automatically? If
yes, when get validation invoked? I don't wan't to validate on every
model changes, I want to validate when all changes are done.
5. Is there an explicit commit method?
6. How to rollback a transaction? It is done by:
while (commandStack.canUndo()) {
commandStack.undo();
}
or is there an explicit rollback method?
7. Dispose the editing domain of the resource set:
editingDomain.dispose();
(It would be nice if all can done programmatically and without extending
some plug-in extension points)
ciao
Olli
|
|
|
Re: [EMF Transaction] How to use it? [message #540257 is a reply to message #540080] |
Tue, 15 June 2010 13:11 |
|
Hi, Olli,
See some replies in-line, below.
HTH,
Christian
On 14/06/10 03:35 PM, Oliver wrote:
> Hi,
>
> I'am new to EMF Transaction. If I understood it right, the use case of
> EMF Transaction is modifying the same model/IEditingDomain with
> different editor instances at the same time without the "dirty read"
> problem.
Yes, that's one of its benefits.
> In my case I wan't to modify my model (e.g. model transformation) and
> after that the modified model has to validate. If the validation fails,
> the model changes have to undone (i.e. rollback).
Well, that's another of its benefits: Transactions automatically run
data integrity constraints to validate changes that have occurred and
roll back those changes if integrity is violated.
> So I think this needs this steps:
> 1. Creating a ITransactionalEditingDomain for the ResourceSet where the
> model lives:
> TransactionalEditingDomain editingDomain =
> TransactionalEditingDomain.Factory.INSTANCE.createEditingDom ain(resourceSet);
So far, so good. Note that if you need to share an editing domain
amongst multiple editors, you way want to consider registering an
editing domain by ID for those editors to find it. The programmer's
guide documentation should have an explanation of that.
> 2. Getting a TransactionalCommandStack of the editing domain:
> TransactionalCommandStack commandStack = (TransactionalCommandStack)
> editingDomain.getCommandStack();
> Does the TransactionalCommandStack represents a root transaction with
> commands as child transactions?
No, not at all. It's just a command stack as in any EMF editing domain.
From a client API perspective, it just has some extended protocol for
command execution that allows to tweak the semantics of the transaction
that is used for the execution of the command.
> 3. Modify my model. If I understood it right, I have two ways to modify
> my model:
> a) executing some command (e.g. AddCommand, SetCommand etc.) on the
> command stack - preferred for editors.
These have nothing to do with editors, really. These commands are
building-blocks for more complex changes.
> b) create a RecordingCommand, modifying my model inside the doExecute
> method (e.g. myLibrary.setName("MyLib")) and execute the
> RecordingCommand on the command stack - preferred for model
> transformations.
These have nothing to do with model transformations. They are equally
well suited to use in editors.
> 4. All changes are done, so I wan't to validate my model. Does EMF
> Transaction provide validation with EMF Validating automatically? If
> yes, when get validation invoked? I don't wan't to validate on every
> model changes, I want to validate when all changes are done.
Yes, constraints marked for "live mode" evaluation are invoked
automatically when a transaction commits. If they turn up errors, then
the commit fails and the changes are rolled back.
You are, of course, free to listen to the editing domain's notification
that a transaction has completed and perform any kind of validation that
you need (in a transaction, of course) and then take any action that is
required. If you listen to the command-stack for notification that a
command was executed, then you could undo it if you desire.
So much is possible with EMF ... :-)
> 5. Is there an explicit commit method?
Yes, transactions have an explicit commit protocol, but clients very
rarely need to use it. If all of your changes are performed via
Commands on the stack (highly recommended), then the command stack takes
care of opening and committing read/write transactions for you.
For read-only access to the model, the editing domain automatically
wraps Runnable code fragments in transactions for you, too.
> 6. How to rollback a transaction? It is done by:
> while (commandStack.canUndo()) {
> commandStack.undo();
> }
> or is there an explicit rollback method?
No, the command stack is not a transaction. The Transaction interface
has a rollback() method, but you will never use it. A transaction
usually either rolls itself back if it fails validation, or a post hoc
validation listener might undo the last command on the stack. Once
committed, a transaction cannot be rolled back, but you can, of course,
perform opposite changes to counter its effect.
> 7. Dispose the editing domain of the resource set:
> editingDomain.dispose();
Well, don't do that until your editors have all finished using it.
> (It would be nice if all can done programmatically and without extending
> some plug-in extension points)
Of course. In the fine EMF tradition, their are APIs to perform any of
the registrations that you might otherwise do statically on an extension
point.
>
> ciao
> Olli
|
|
| |
Re: [EMF Transaction] How to use it? [message #541515 is a reply to message #541393] |
Mon, 21 June 2010 12:49 |
|
Hi, Olli,
An EMF transaction only performs validation on commit of the entire
transaction. If your transaction performs a number of updates to the
same object, your constraint needs only to check the sum once and simply
ignore the redundant notifications.
A simple name constraint would have the same problem if the name is set
multiple times within a transaction.
Note that even in the case of large nested transactions, validation is
done only when the top-most transaction commits. You'll see this, for
example, in the composition of operations in a GMF diagram editor.
HTH,
Christian
On 20/06/10 04:25 PM, Oliver wrote:
> Thank you very much!
>
> Quote:
>> Yes, constraints marked for "live mode" evaluation are invoked
>> automatically when a transaction commits. If they turn up errors, then
>> the commit fails and the changes are rolled back.
>
>
> I think the fact, that only live constraints checked in a transaction
> its good for small commands like SetCommand etc. but for big
> transactions not.
> For example: the sum of an integer list have to zero (i.e. constraint).
> In a big transaction some values are appended to the list. So there are
> two alternatives to check this constraint: check the sum on every insert
> or check at the end of the transaction. So for the first one you need n
> checks for n inserts for the last one only one check. I my view such
> heavy checks are batch constraints and live constraints are only used
> for example to check, if an name is not null and begins with a letter.
>
> ciao
> Olli
|
|
|
Goto Forum:
Current Time: Thu Sep 26 20:10:10 GMT 2024
Powered by FUDForum. Page generated in 0.03776 seconds
|