Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Resourceset Change Location at Runtime but not updated while saving
Resourceset Change Location at Runtime but not updated while saving [message #1759308] Mon, 10 April 2017 13:00 Go to next message
Kanwal Gill is currently offline Kanwal GillFriend
Messages: 16
Registered: February 2017
Junior Member
I have this problem of saving a file which has a reference to another file, and the other file was just saved at a different place and now the reference isn't updated...

so basically the location of file is not updated in resource...

as I understand and have discussed that there is basically a problem in resourceset, because when you inspect the resource tree basically at runtime, you see that the node changes its location at the runtime from A to B, but at the time of SAVING the resourceset thinks that the file is still in A.
SO THE LOCATION OF RESOURCE IS NOT UPDATED.
Kindly can anyone help???


index.php/fa/29018/0/

  • Attachment: res.JPG
    (Size: 54.78KB, Downloaded 282 times)
Re: Resourceset Change Location at Runtime but not updated while saving [message #1759316 is a reply to message #1759308] Mon, 10 April 2017 05:27 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 29545
Registered: July 2009
Senior Member
Without any code to show what you're actually doing, it's pretty much impossible to comment in any informative way. Here's what you need to do if you're going to rename a resource. Load all resources referring to the resource you're about to rename into a single resource set. Ensure that all proxies have been resolved (EcoreUtil.resolveAll). Change the URI of the resource you want to rename/move. Save all resources in the resource set. Delete the old contents at the old location in the underlying storage (workspace/filesystem).

Of course you're showing pictures of Henshin, do I don't know if you're specifically asking about that, but given they are EMF resources, the same approach applies.
Re: Resourceset Change Location at Runtime but not updated while saving [message #1759324 is a reply to message #1759308] Mon, 10 April 2017 14:38 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 6040
Registered: July 2009
Senior Member
Hi

If all resources are saveable, a save should save everything necessary.

BUT, if any of your resources have a smart save that loads/resolves/reorganises or otherwise unifies model elements, you must complete any load/resolve/reorganisation/unification for all resources before you save any resource. (OK if you are really smart you can load/resolve/reorganise/unify and save in reverse referencing order).

I suggest you force a second save to slightly different file names so that you can diff to see what has evolved.

Regards

Ed Willink

Re: Resourceset Change Location at Runtime but not updated while saving [message #1759327 is a reply to message #1759316] Mon, 10 April 2017 15:09 Go to previous messageGo to next message
Kanwal Gill is currently offline Kanwal GillFriend
Messages: 16
Registered: February 2017
Junior Member
URI uriDiagram = URI.createFileURI(new File(path + pair.getKey().toString() + ".henshin_diagram").getAbsolutePath());
Resource res = resourceSet.createResource(uriDiagram);
XMIResourceImpl xmiResource = (XMIResourceImpl) res;
xmiResource.setEncoding("UTF-8");
EObject rootElement = xmiResource.getEObject("/");
xmiResource.setID(rootElement, UUID.randomUUID().toString());
res.getContents().add(cssdiagramnew);
try {
res.save(Collections.EMPTY_MAP);
} catch (IOException e) {
e.printStackTrace();
}



this is a code snippet of saving the resource... I guess there should be something to make the resource refer to or point to the changed file location now... something like refresh function or something... maybe you can tell while seeing the code
Re: Resourceset Change Location at Runtime but not updated while saving [message #1759341 is a reply to message #1759327] Mon, 10 April 2017 08:45 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 29545
Registered: July 2009
Senior Member
I only see one resource involved. I see you do a ResourceSet.create, but not a load, so I don't know how an object can be in that resource, so no doubt rootElement is null. Of course I don't know where cssdiagramnew comes from either. Somewhere I'm expect to see a resource you've loaded with one URI be modified with a setURI and then saved at that new location, along with saving the resource that reference objects in that "renamed" resource.
Re: Resourceset Change Location at Runtime but not updated while saving [message #1759356 is a reply to message #1759341] Mon, 10 April 2017 20:57 Go to previous messageGo to next message
Kanwal Gill is currently offline Kanwal GillFriend
Messages: 16
Registered: February 2017
Junior Member
// Resources are being loaded here in this method...

public void loadComponents(URI uri) {
resourceSet = new HenshinResourceSet();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("henshin", new HenshinResourceFactory());
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("henshin_diagram", new CSSNotationResourceFactory());

String filename = uri.segmentsList().get(uri.segmentsList().size()-1).replace(".henshin", ".henshin_diagram");
URI diagramUri = uri.trimSegments(1).appendSegment(filename);
diagramResource = (GMFResource) resourceSet.getResource(diagramUri, true);

CSSDiagramImpl diag = (CSSDiagramImpl) diagramResource.getContents().get(0);
System.out.println(diag.getElement());
for (Object child : diag.getChildren()) {
if (child instanceof Node) {
Node node = (Node) child;
System.out.println(node.getElement());
}
}

Resource loadedResource = resourceSet.getResource(uri, true);
if (loadedResource != null) {
modelComponents = new ArrayList<Component>();
for (EObject object : loadedResource.getContents()) {
if (object instanceof Module) {
Module eModule = (Module) object;
Component component = new Component(eModule.toString());
component.addUnits(eModule.getUnits());
modelComponents.add(component);
}
}
}
System.out.println();
}
Re: Resourceset Change Location at Runtime but not updated while saving [message #1759357 is a reply to message #1759341] Mon, 10 April 2017 21:02 Go to previous messageGo to next message
Kanwal Gill is currently offline Kanwal GillFriend
Messages: 16
Registered: February 2017
Junior Member
// here after loading the file, I am splitting the diagram (or model) and changing the location of nodes

if (diagramResource != null) {
CSSDiagramImpl fullcssdiagram = (CSSDiagramImpl) diagramResource.getContents().get(0);

HashMap<Node, List<Edge>> node2edges = new HashMap<Node, List<Edge>>();

for (Object nodechild : fullcssdiagram.getChildren()){
List<Edge> listofedges = new ArrayList<Edge>();

for (Object edgechild : fullcssdiagram.getEdges()){
if (edgechild instanceof Edge){
Edge edge = (Edge) edgechild;
Node node = (Node) edge.getSource();
Node uppernode = (Node) node.eContainer().eContainer();
Module modulename = (Module) uppernode.getElement().eContainer();
Rule rulename = (Rule) uppernode.getElement();
if (nodechild.equals(uppernode)){
listofedges.add((Edge)edgechild);
}
}
node2edges.put((Node)nodechild,listofedges);
}
}

Iterator it = module2rules.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
//making the CSSDiagramFile
CSSDiagramImpl cssdiagramnew = new CSSDiagramImpl();
cssdiagramnew.setType(fullcssdiagram.getType());
cssdiagramnew.setVisible(fullcssdiagram.isVisible());
cssdiagramnew.setMeasurementUnit(fullcssdiagram.getMeasurementUnit());
cssdiagramnew.setElement(fullcssdiagram.getElement());

for (Object o : fullcssdiagram.getStyles()) {
EObject newStyle = EcoreUtil.copy((EObject)o);
cssdiagramnew.getStyles().add(newStyle);
}

cssdiagramnew.setName(pair.getKey().toString()+".henshin_diagram");

//checking for Nodes and putting in CSSDiagramFile
EList children = fullcssdiagram.getChildren();
for (Object child : children){
if (child instanceof Node)
{
Node node = (Node) child;
if(pair.getValue().toString().contains(node.getElement().toString()))
{
cssdiagramnew.insertChild(node);
//just to check the HashMap enteries
Set set = node2edges.entrySet();
Iterator cc = set.iterator();
while(cc.hasNext()) {
Map.Entry me = (Map.Entry)cc.next();
if(me.getKey().equals(node)){
List<Edge> edgestoinsert = (List<Edge>) me.getValue();

for (int i = 0; i < edgestoinsert.size(); i++) {
cssdiagramnew.insertEdge(edgestoinsert.get(i));
}

}
}
}
}
}

//making file with same name as module(s)
URI uriDiagram = URI.createFileURI(new File(path + pair.getKey().toString() + ".henshin_diagram").getAbsolutePath());
Resource res = resourceSet.createResource(uriDiagram);
XMIResourceImpl xmiResource = (XMIResourceImpl) res;
xmiResource.setEncoding("UTF-8");
EObject rootElement = xmiResource.getEObject("/");
xmiResource.setID(rootElement, UUID.randomUUID().toString());
res.getContents().add(cssdiagramnew);
try {
res.save(Collections.EMPTY_MAP);
} catch (IOException e) {
e.printStackTrace();
}
it.remove(); // avoids a ConcurrentModificationException
}
}
}
Re: Resourceset Change Location at Runtime but not updated while saving [message #1759358 is a reply to message #1759341] Mon, 10 April 2017 21:06 Go to previous messageGo to next message
Kanwal Gill is currently offline Kanwal GillFriend
Messages: 16
Registered: February 2017
Junior Member
well... I have given or pasted the code of LOADING and then SPLITTING the model or diagram file, where the problem is that the resource keeps on pointing to the older location...

maybe now you can tell more easily what I can do
Re: Resourceset Change Location at Runtime but not updated while saving [message #1759377 is a reply to message #1759358] Mon, 10 April 2017 22:52 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 29545
Registered: July 2009
Senior Member
You're basically asking me to try to debug your code. But of course I can't run it so I can only stare at it, and then I feel sad to see uses of CSSDiagramImpl where clearly there is API for it and a factory for creating a instance. You show a picture with four resources, but in your code I see two resources being loaded and one new one being created and saved, so I have no idea how a fourth resource enters this pictures. Should you be calling Resource.setURI on one of the loaded resources and saving it as well? Set a breakpoint in org.eclipse.emf.ecore.xmi.impl.XMLHelperImpl.getHREF(EObject) and during the save, monitor what URIs are involve. When you see the URI to what you believe in the wrong file, you'll know which objects is being saved that cases this. In this code you'll notice the URI that's saved depends on the URI of the object's containing resource (or it's proxy URI) and then you'll realize that while saving you are saving a reference to an object in the wrong resource (or to an unresolved proxy).
Re: Resourceset Change Location at Runtime but not updated while saving [message #1759416 is a reply to message #1759377] Tue, 11 April 2017 05:27 Go to previous messageGo to next message
Kanwal Gill is currently offline Kanwal GillFriend
Messages: 16
Registered: February 2017
Junior Member
well thanks for the tip... the problem got solved Smile
Re: Resourceset Change Location at Runtime but not updated while saving [message #1759420 is a reply to message #1759377] Tue, 11 April 2017 06:45 Go to previous messageGo to next message
Kanwal Gill is currently offline Kanwal GillFriend
Messages: 16
Registered: February 2017
Junior Member
and btw... can you tell what is the other API that can be used instead of CSSDiagramImpl ???
Re: Resourceset Change Location at Runtime but not updated while saving [message #1759429 is a reply to message #1759420] Tue, 11 April 2017 16:16 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 29545
Registered: July 2009
Senior Member
Isn't there a CSSDiagram interface? And given it's a model, there must be a factory and you should be using XyzFactory.eINSTANCE.createCSSDiagram(). (I'm not sure why the constructors are even public; by default generates models have protected constructors to discourage the use of the implementation classes.)
Re: Resourceset Change Location at Runtime but not updated while saving [message #1759900 is a reply to message #1759429] Wed, 19 April 2017 16:41 Go to previous message
Kanwal Gill is currently offline Kanwal GillFriend
Messages: 16
Registered: February 2017
Junior Member
well no I have not found any CSSDiagram interface... Plus, can you also tell. That I am copying the Nodes from one file to the other (with all corresponding edges also)... and at the time of OPENING them in the GMF Editor, the Diagram Components are messy. Like they are all present in the diagram, but the nodes and edges are just messed up - any idea?
Previous Topic:[CDO] MYSQL-OCL SemanticException
Next Topic:the diagram components messy when opened in editor
Goto Forum:
  


Current Time: Fri Sep 21 21:08:24 GMT 2018

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

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

Back to the top