Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » ChangeRecorder on new ecore tools project/diagram
ChangeRecorder on new ecore tools project/diagram [message #688432] Fri, 24 June 2011 17:12 Go to next message
Jean-Philippe Brabant is currently offline Jean-Philippe BrabantFriend
Messages: 17
Registered: May 2011
Junior Member
I would like to add a ChangeRecorder (http://download.eclipse.org/modeling/emf/emf/javadoc/2.5.0/org/eclipse/emf/ecore/change/util/ChangeRecorder.html) automatically when a new Ecore tools Diagram is created.

The constructors of the ChangeRecorder need a sort of resource. When a ecore diagram is create, does it automatically create a default resource ? If yes, is there anyway to obtain it ?

Thank you,
JP Brabant.
Re: ChangeRecorder on new ecore tools project/diagram [message #688446 is a reply to message #688432] Fri, 24 June 2011 17:52 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
JP,

Are you looking to see changes made to the diagram, changes made to the
Ecore model, or both? What are you hoping to accomplish?


Jean-Philippe Brabant wrote:
> I would like to add a ChangeRecorder
> (http://download.eclipse.org/modeling/emf/emf/javadoc/2.5.0/org/eclipse/emf/ecore/change/util/ChangeRecorder.html)
> automatically when a new Ecore tools Diagram is created.
> The constructors of the ChangeRecorder need a sort of resource. When a
> ecore diagram is create, does it automatically create a default
> resource ? If yes, is there anyway to obtain it ?
>
> Thank you,
> JP Brabant.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: ChangeRecorder on new ecore tools project/diagram [message #688538 is a reply to message #688446] Fri, 24 June 2011 21:53 Go to previous messageGo to next message
Jean-Philippe Brabant is currently offline Jean-Philippe BrabantFriend
Messages: 17
Registered: May 2011
Junior Member
After the creation of an ecore diagram using ecore tools, I would like to get a trace of what the person using the diagram editor will do. Like if he added an EClass or an ERelation.
Re: ChangeRecorder on new ecore tools project/diagram [message #688561 is a reply to message #688538] Fri, 24 June 2011 22:38 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
JP,

I'm not sure that answer the question about which resources you'd like
to monitor. EMF generated editors implement IEditingDomainProvider
which gives access to the editing domain, and hence the resource set and
all resources in it. I don't know if the Ecore Tools editor implements
an interface that provides such access. You might want to look at the
implementation class to figure that out.


Jean-Philippe Brabant wrote:
> After the creation of an ecore diagram using ecore tools, I would like
> to get a trace of what the person using the diagram editor will do.
> Like if he added an EClass or an ERelation.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: ChangeRecorder on new ecore tools project/diagram [message #688750 is a reply to message #688561] Sat, 25 June 2011 15:38 Go to previous messageGo to next message
Jean-Philippe Brabant is currently offline Jean-Philippe BrabantFriend
Messages: 17
Registered: May 2011
Junior Member
Here is a screen shot of what happen after the creation of a diagram :

http://i.imgur.com/R22BU.png

I tried to do that :

ResourceSet resSet = new ResourceSetImpl();
Resource resource = resSet.getResource(URI.createURI("http://defaultname/1.0"), true);
System.out.println(resource.getContents().get(0));


But I get errors. I wanted to see if this resource was available. If so, I would have given it to the ChangeRecorder.

I also looked for something related to the IEditingDomainProvider but I didn't find anything that seemed relevant (no implementation).

Thank you for your fast responses.
Re: ChangeRecorder on new ecore tools project/diagram [message #688778 is a reply to message #688750] Sat, 25 June 2011 17:07 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
JP,

Comments below.

On 25/06/2011 8:38 AM, Jean-Philippe Brabant wrote:
> Here is a screen shot of what happen after the creation of a diagram :
The diagram doesn't seem to have come through.
>
>
> I tried to do that :
>
> ResourceSet resSet = new ResourceSetImpl();
> Resource resource =
> resSet.getResource(URI.createURI("http://defaultname/1.0"), true);
So you're trying to demand load the resource from an http location into
an empty new resource set... That's not likely to produce anything
useful...
> System.out.println(resource.getContents().get(0));
>
>
> But I get errors.
Probably it threw an exception because it wasn't able to create an input
stream to read what's at the URI...
> I wanted to see if this resource was available.
What's the significance of the URI you tried to use?
> If so, I would have given it to the ChangeRecorder.
You'd be monitoring your own local copy of it...
> I also looked for something related to the IEditingDomainProvider but
> I didn't find anything that seemed relevant (no implementation).
It might well be the case that the diagram editor provides no way of
getting at the resource set that it's using...
> Thank you for your fast responses.
I suppose if you extended their editor class, you could get at things
not available externally via public API...


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: ChangeRecorder on new ecore tools project/diagram [message #688785 is a reply to message #688778] Sat, 25 June 2011 18:14 Go to previous messageGo to next message
Jean-Philippe Brabant is currently offline Jean-Philippe BrabantFriend
Messages: 17
Registered: May 2011
Junior Member
> What's the significance of the URI you tried to use?

In the screen shot I highlighted the URI that seem to be automatically created. That why I was using this one.
Re: ChangeRecorder on new ecore tools project/diagram [message #689390 is a reply to message #688785] Mon, 27 June 2011 14:55 Go to previous messageGo to next message
Jean-Philippe Brabant is currently offline Jean-Philippe BrabantFriend
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 Go to previous messageGo to next message
Jean-Philippe Brabant is currently offline Jean-Philippe BrabantFriend
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 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
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 Go to previous messageGo to next message
Jean-Philippe Brabant is currently offline Jean-Philippe BrabantFriend
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 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
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 #694085 is a reply to message #694043] Thu, 07 July 2011 19:10 Go to previous messageGo to next message
Jean-Philippe Brabant is currently offline Jean-Philippe BrabantFriend
Messages: 17
Registered: May 2011
Junior Member
For the create URI I lied, I don't put specific path like this, I put a relative path from my project but I wanted to make sure you understood that I sort of put the file name as a URI. It worked when I made modification to my resource by hand. When you said "So you're listening to all resources in that resource set..." I would appreciate if you could provide more information. I thought it was the only way to attach a change recorder. It was the only one I saw in the examples.

I'm finding a lot of weird thing with the debugger, I'll continue investigating.
Re: ChangeRecorder on new ecore tools project/diagram [message #694126 is a reply to message #694085] Thu, 07 July 2011 21:07 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Jean-Philippe,

Comments below.

On 07/07/2011 12:10 PM, Jean-Philippe Brabant wrote:
> For the create URI I lied, I don't put specific path like this, I put
> a relative path from my project
So like platform:/resource/<project-name>/<path>/<file>? Using just
/<project-name>/<path>/<file> is likely to make a mess of cross resource
references...
> but I wanted to make sure you understood that I sort of put the file
> name as a URI. It worked when I made modification to my resource by
> hand. When you said "So you're listening to all resources in that
> resource set..."
You can see there are various constructors. Each of then calls
beginRecording with that argument and all things contained under those
arguments are monitored. So when a resource is created in the resource
set, the change recorder will automatically attach itself to that. As
such, what you've done will allow you to see all changes that happen in
the resource set.
> I would appreciate if you could provide more information. I thought it
> was the only way to attach a change recorder. It was the only one I
> saw in the examples.
It's okay what you're doing. I just wanted you to be aware of the
implication.
> I'm finding a lot of weird thing with the debugger, I'll continue
> investigating.
It seems like a simple problem, but it's actually quite a hard one.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: ChangeRecorder on new ecore tools project/diagram [message #694529 is a reply to message #694126] Fri, 08 July 2011 19:13 Go to previous messageGo to next message
Jean-Philippe Brabant is currently offline Jean-Philippe BrabantFriend
Messages: 17
Registered: May 2011
Junior Member
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.
Re: ChangeRecorder on new ecore tools project/diagram [message #694617 is a reply to message #694529] Sat, 09 July 2011 01:46 Go to previous message
Ed Merks is currently offline Ed MerksFriend
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/
Previous Topic:Generate Custom XML from Graphical Diagram / EMF Model
Next Topic:Update Viewer after CreateChildAction
Goto Forum:
  


Current Time: Fri Apr 26 09:07:21 GMT 2024

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

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

Back to the top