Home » Modeling » EMF » ChangeRecorder on new ecore tools project/diagram
| | | | | | |
Re: ChangeRecorder on new ecore tools project/diagram [message #689390 is a reply to message #688785] |
Mon, 27 June 2011 14:55 |
Jean-Philippe Brabant Messages: 17 Registered: May 2011 |
Junior Member |
|
|
Ok so I made some huge progress by following those two tutorials :
http://www.vogella.de/articles/EclipseEMF/article.html
http://www.vogella.de/articles/EclipseEMFPersistence/article.html#javacode_load
I didn't want to understand everything, I just wanted to make things works but it was obviously a bad decision. Now that I understand a little bit more what I'm doing it going a lot better.
Those two tutorials explain who to create a meta-model, implement it, and then load it. I knew that ecore was itself a meta-model, so I just had to modify the code a little bit. After creating a Diagram.ecore file in the model folder, here how I did to load it (in case someone else need it someday) :
// Initialize the model
EcorePackage.eINSTANCE.eClass();
// Register the XMI resource factory for the .ecore extension
Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
Map<String, Object> m = reg.getExtensionToFactoryMap();
m.put("ecore", new XMIResourceFactoryImpl());
// Obtain a new resource set
ResourceSet resSet = new ResourceSetImpl();
// Get the resource
Resource resource = resSet.getResource(URI.createURI("model/Diagram.ecore"), true);
EPackage ePackage = (EPackage)resource.getContents().get(0);
System.out.println(ePackage.getName());
TreeIterator<EObject> iterator = ePackage.eAllContents();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
Now that I have an appropriate ressourceSet I'ill go an try to use the ChangeRecorder. I'll post result here.
[Updated on: Mon, 27 June 2011 14:56] Report message to a moderator
|
|
|
Re: ChangeRecorder on new ecore tools project/diagram [message #692488 is a reply to message #688432] |
Mon, 04 July 2011 14:44 |
Jean-Philippe Brabant Messages: 17 Registered: May 2011 |
Junior Member |
|
|
Ok, I got a new problem. By looking at the JUnit test of the change recorder I build this snippet of code :
public class Main {
public static void main(String[] args) {
// Initialize the model.
EcorePackage.eINSTANCE.eClass();
// Register the XMI resource factory for the .ecore extension.
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("ecore", new XMIResourceFactoryImpl());
// Obtain a new resource set.
ResourceSet resourceSet = new ResourceSetImpl();
// Get the resource.
Resource resource = resourceSet.getResource(URI.createURI("model/Diagram.ecore"), true);
// Get the root of the resource.
EPackage ePackage = (EPackage)resource.getContents().get(0);
// Start a change recorder.
ChangeRecorder changeRecorder = new ChangeRecorder(resourceSet);
// Make a change to the class name.
TreeIterator<EObject> iterator = ePackage.eAllContents();
while(iterator.hasNext()) {
EObject obj = iterator.next();
if (obj instanceof EClass) {
((EClass)obj).setName("Class_2");
}
}
// End the change recording.
ChangeDescription changeDescription = changeRecorder.endRecording();
// Add the result to our resouce.
resource.getContents().add(changeDescription);
// Print the resource.
try {
resource.save(System.out, null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
What this code did is this : It loaded the resource of a diagram already created using ecoretools, it put a ChangeRecorder on it, made a change, then displayed the result.
Now what I wanted to do is record the change made while using the graphic tool to create diagram. So I made a small eclipse plugin which added to button to the Eclipse UI, one "start recording change" and the other "stop recording change". The first button did the first part of the code until "// Make a change to the class name" and the 2nd one did the last part of the code starting at "// End the change recording".
So I clicked the first button, made a change using the graphical tool, and then clicked the 2nd button but the change recorder showed nothing. Furthermore, my code is printing the whole diagram before printing the result of the change recorder, and the diagram part showed the old model. It look like the resource I used (the .ecore file) didn't get updated. If I reattached a change recorder and then printed it, it would show the new diagram, but the change recorder never record any change.
Do you guys have any idea where I might have gone wrong ?
Thank you.
Sources :
[Updated on: Mon, 04 July 2011 14:46] Report message to a moderator
|
|
|
Re: ChangeRecorder on new ecore tools project/diagram [message #692979 is a reply to message #692488] |
Tue, 05 July 2011 15:10 |
Ed Merks Messages: 33142 Registered: July 2009 |
Senior Member |
|
|
Comments below.
> Ok, I got a new problem. By looking at the JUnit test of the change
> recorder I build this snippet of code :
>
> public class Main {
>
> public static void main(String[] args) {
> // Initialize the model.
> EcorePackage.eINSTANCE.eClass();
>
> // Register the XMI resource factory for the .ecore extension.
>
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("ecore",
> new XMIResourceFactoryImpl());
>
> // Obtain a new resource set.
> ResourceSet resourceSet = new ResourceSetImpl();
>
> // Get the resource.
> Resource resource =
> resourceSet.getResource(URI.createURI("model/Diagram.ecore"), true);
>
> // Get the root of the resource.
> EPackage ePackage = (EPackage)resource.getContents().get(0);
>
> // Start a change recorder.
> ChangeRecorder changeRecorder = new ChangeRecorder(resourceSet);
>
> // Make a change to the class name.
> TreeIterator<EObject> iterator = ePackage.eAllContents();
> while(iterator.hasNext()) {
> EObject obj = iterator.next();
> if (obj instanceof EClass) {
> ((EClass)obj).setName("Class_2");
> }
> }
>
> // End the change recording.
> ChangeDescription changeDescription =
> changeRecorder.endRecording();
>
> // Add the result to our resouce.
> resource.getContents().add(changeDescription);
>
> // Print the resource.
> try {
> resource.save(System.out, null);
> } catch (IOException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> }
> }
>
>
> What this code did is this : It loaded the resource of a diagram
> already created using ecoretools, it put a ChangeRecorder on it, made
> a change, then displayed the result.
>
> Now what I wanted to do is record the change made while using the
> graphic tool to create diagram. So I made a small eclipse plugin which
> added to button to the Eclipse UI, one "start recording change" and
> the other "stop recording change". The first button did the first part
> of the code until "// Make a change to the class name" and the 2nd one
> did the last part of the code starting at "// End the change recording".
> So I clicked the first button, made a change using the graphical tool,
> and then clicked the 2nd button but the change recorder showed nothing.
Did you consult with your debugger?
> Furthermore, my code is printing the whole diagram before printing the
> result of the change recorder, and the diagram part showed the old
> model. It look like the resource I used (the .ecore file) didn't get
> updated. If I reattached a change recorder and then printed it, it
> would show the new diagram, but the change recorder never record any
> change.
How are you attaching the change recorder to the copy of the model being
editing by the diagram?
> Do you guys have any idea where I might have gone wrong ?
It sounds like perhaps you're monitoring your own separate copy of the
model, not the one in the diagram editor.
> Thank you.
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Re: ChangeRecorder on new ecore tools project/diagram [message #694000 is a reply to message #692979] |
Thu, 07 July 2011 15:54 |
Jean-Philippe Brabant Messages: 17 Registered: May 2011 |
Junior Member |
|
|
You asked 3 questions :
1- Did you consult with your debugger?
2- How are you attaching the change recorder to the copy of the model being
editing by the diagram?
3- It sounds like perhaps you're monitoring your own separate copy of the
model, not the one in the diagram editor.
For question 2, I attach the change recorder as follow :
ResourceSet resourceSet = new ResourceSetImpl();
Resource resource = resourceSet.getResource(URI.createURI("c:\file.ecore"), true);
ChangeRecorder changeRecorder = new ChangeRecorder(resourceSet);
For question 1 and 3, here is what I found out by putting a break point in org.eclipse.emf.ecore.resource.impl.ResourceSetImpl in the method createResource : When creating a new diagram, 2 files, and 2 resources are created, a .ecore one and a .ecorediag one. But the interesting part is after making a modification to the .ecore file using the graphical editor, and then saving it, a new .ecore resource is created with the same name as the previous one.
I did an OS course recently and we learned that the safest way to save a document is not to modify the original, but instead, to create a copy with the modification, and then to override the original. By doing that you don't corrupt the original file if something goes wrong in the process. So I guess the ecore diagram editor does this to. It probably recreate the original, add the modification to it and then override the original file. That would explain why I don't get anything with the change recorder ; it does not modify my resource at all.
Now I'm lost, I don't know what I will do. I'll think about it.
Is there a sort of diff tools to compare 2 .ecore file and get the Change description that would transform the first one in the second one ? Or do you think that the ecore diagram editor is creating a list of operation to create the new .ecore file and that I could access it ?
|
|
|
Re: ChangeRecorder on new ecore tools project/diagram [message #694043 is a reply to message #694000] |
Thu, 07 July 2011 17:06 |
Ed Merks Messages: 33142 Registered: July 2009 |
Senior Member |
|
|
Jean-Philippe,
Comments below.
On 07/07/2011 8:54 AM, Jean-Philippe Brabant wrote:
> You asked 3 questions :
> 1- Did you consult with your debugger?
This is one of my favorite questions to ask. :-P
>
> 2- How are you attaching the change recorder to the copy of the model
> being editing by the diagram?
>
> 3- It sounds like perhaps you're monitoring your own separate copy of
> the model, not the one in the diagram editor.
>
>
> For question 2, I attach the change recorder as follow :
>
> ResourceSet resourceSet = new ResourceSetImpl();
> Resource resource =
> resourceSet.getResource(URI.createURI("c:\file.ecore"), true);
This worked? I'd expect you to use createFileURI when using an
OS-specific file path.
> ChangeRecorder changeRecorder = new ChangeRecorder(resourceSet);
So you're listening to all resources in that resource set...
>
>
> For question 1 and 3, here is what I found out by putting a break
> point in org.eclipse.emf.ecore.resource.impl.ResourceSetImpl in the
> method createResource : When creating a new diagram, 2 files, and 2
> resources are created, a .ecore one and a .ecorediag one.
Yes, I'd expect that.
> But the interesting part is after making a modification to the .ecore
> file using the graphical editor, and then saving it, a new .ecore
> resource is created with the same name as the previous one.
And both separate resources with the same URI exist in the resource
set? That sound bad...
>
> I did an OS course recently and we learned that the safest way to save
> a document is not to modify the original, but instead, to create a
> copy with the modification, and then to override the original.
Yes, that's a good point. Keep in mind thought that saves are done
through the Eclipse workspace, and it's capable or keeping backup copies
you can restore...
> By doing that you don't corrupt the original file if something goes
> wrong in the process. So I guess the ecore diagram editor does this to.
It wouldn't need to create multiple resources in the resource set though...
> It probably recreate the original, add the modification to it and then
> override the original file. That would explain why I don't get
> anything with the change recorder ; it does not modify my resource at
> all.
> Now I'm lost, I don't know what I will do. I'll think about it.
If you've set a breakpoint in createResource you should see on the stack
why it's getting there a third time...
>
> Is there a sort of diff tools to compare 2 .ecore file and get the
> Change description that would transform the first one in the second one ?
EMF Compare definitely does something like this and creates a Diff model
> Or do you think that the ecore diagram editor is creating a list of
> operation to create the new .ecore file and that I could access it ?
There will be commands on the command stack, but it's hard to figure out
what's happened by inspecting the commands.
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| | | |
Re: ChangeRecorder on new ecore tools project/diagram [message #694617 is a reply to message #694529] |
Sat, 09 July 2011 01:46 |
Ed Merks Messages: 33142 Registered: July 2009 |
Senior Member |
|
|
Jean-Philippe,
Producing a ChangeDescription (summarize or endRecording), putting it in
a resource, and serializing that to System.out is what I usually do.
On 08/07/2011 12:13 PM, Jean-Philippe Brabant wrote:
> I'm currently applying some modifications to my code as you suggested.
> My new change recorder implementation seem to record some stuff but it
> hard to print the results. Do you know any easy way to print it on the
> console ? I only have access to the change recorder object.
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Goto Forum:
Current Time: Fri Apr 26 09:07:21 GMT 2024
Powered by FUDForum. Page generated in 0.04214 seconds
|