Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Sirius » Adding a "Load Resources" tool to Sirius diagram viewpoint, unsure how to do so.(Looking for help with creating a "Load Resources" equivalent in a Sirius diagram viewpoint.)
Adding a "Load Resources" tool to Sirius diagram viewpoint, unsure how to do so. [message #1854517] Tue, 23 August 2022 14:28 Go to next message
Joost Mertens is currently offline Joost MertensFriend
Messages: 4
Registered: August 2022
Junior Member
Hello all,

Preface: this is the first Sirius editor I'm creating after following the family and mindstorms tutorials so I might not get all the terminology right. Feel free to correct me when necessary. Links to must read documentation are always welcome as well.

Some context: I have two classes in my metamodel, say A and B. Class B has a variable that refers to an instance of type A. Simplified metamodel in emfatic below:

	class A{
		attr String name;
	}
	
	class B{
		attr String name;
		ref A referenceToA;
	}



To assign a value to this variable I:

    1. Instantiate a model for each class, let's say instanceOfA and instanceOfB
    2. Open instanceOfB in the model editor.
    3. Use the "Load Resource" option from the right click context menu in the EMF model editor to load instanceOfA from my workspace.
    4. Set the property referenceToA to the value instanceOfA.


For my actual metamodel this looks something like the attached screenshot (I've replaced specific names with A and B in the post, I figured that'd be clearer).

index.php/fa/42434/0/

Main Question

I'm now looking to do the same operations in the Sirius diagram editor of instanceOfB, but I don't really know how to tackle it.

What I would like to create is a wizard through which I can select the resource to load from the model explorer (similar to the popup you receive when pressing "Load Resource"), and then set to the [i]referenceToA[i/] property upon completing the wizard.

I know setting the property is as straightforward as using the "set X" operation in the tools in the .odesign file, but I'm quite lost when it comes to the wizard/popup.

I have found one post (https://www.eclipse.org/forums/index.php/t/1078013/) that has this same question, but this user is clearly more experienced with EMF and Sirius and unfortunately did not post their solution. The post is also from 2016, so I figured I'd better create a new thread, rather than necro the old one.

Does anyone who has experience mind shining a light on this topic? Links to must read documentation or source code are welcome as answers, I'd love to read up on them to learn how to do this.


Re: Adding a "Load Resources" tool to Sirius diagram viewpoint, unsure how to do so. [message #1854556 is a reply to message #1854517] Thu, 25 August 2022 09:42 Go to previous messageGo to next message
Joost Mertens is currently offline Joost MertensFriend
Messages: 4
Registered: August 2022
Junior Member
Quick update to my own post:

Figured out that I can create the dialog I need by creating and running a new LoadResourceAction (in a function in the Services.java file):

    public void loadResource(EObject obj) {
    	LoadResourceAction loadResourceAction = new LoadResourceAction();
    	loadResourceAction.run();
    }


Not quite sure where to go from here, I see that the loadResourceAction instance has an editing domain, which stores a resourceset, so I think I'd be able to get the loaded object from there, but so far no success.

[Updated on: Thu, 25 August 2022 10:15]

Report message to a moderator

Re: Adding a "Load Resources" tool to Sirius diagram viewpoint, unsure how to do so. [message #1854560 is a reply to message #1854556] Thu, 25 August 2022 12:53 Go to previous messageGo to next message
Joost Mertens is currently offline Joost MertensFriend
Messages: 4
Registered: August 2022
Junior Member
A bit more progress, after some looking around in the generated editor code and referencing the other topic I linked in the original message I can now open the dialog box, register the resource (I think) and have the URI for getting the EObject to return in the function.

public EObject loadResource(EObject obj) {
    	//Session session = SessionManager.INSTANCE.getSession(obj);
    	// Get session and domain
    	Session session = new EObjectQuery(obj).getSession();
    	EditingDomain domain = session.getTransactionalEditingDomain();
    	
    	// create and open Dialog
    	LoadResourceDialog loadResourceDialog = new LoadResourceDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), domain);
        loadResourceDialog.open();
        
        // get selected URI
        List<URI> URIList = loadResourceDialog.getURIs();
        URI uri = URIList.get(0);
        
        System.out.println(uri.toString());
        
        // load resource through its uri
        AddSemanticResourceCommand cmd = new AddSemanticResourceCommand(session, uri, null);
        cmd.execute();
        
    	//get root object fom URI to return
        EObject ret_obj = domain.getResourceSet().getEObject(uri, false);
        return ret_obj;
    }


Now I do have a problem with getting the ret_obj EObject, the line throws an InvocationTargetException

Problem code line is the
uri.fragment()
, which returns null in the following code:

/*
   * Javadoc copied from interface.
   */
  public EObject getEObject(URI uri, boolean loadOnDemand)
  {
    Resource resource = getResource(uri.trimFragment(), loadOnDemand);
    if (resource != null)
    {
      X return resource.getEObject(uri.fragment());
    }
    else
    {
      return null;
    }
  }


The resource is not null though, so I suppose the URI parses fine.

Re: Adding a "Load Resources" tool to Sirius diagram viewpoint, unsure how to do so. [message #1854637 is a reply to message #1854517] Wed, 31 August 2022 13:25 Go to previous message
Joost Mertens is currently offline Joost MertensFriend
Messages: 4
Registered: August 2022
Junior Member
Final update, didn't get around to looking into this again till today, but my question is resolved.

I figured I could also get the object by getting the root EObject of the resource contents, as this is the object I'd need to return (instead of trying to get the URI fragments to work).

The code below opens the dialog box, loads the resource and returns the EObject:

    public EObject loadResource(EObject obj) {

    	// Get session and domain
    	//Session session = SessionManager.INSTANCE.getSession(obj);
    	Session session = new EObjectQuery(obj).getSession();
    	EditingDomain domain = session.getTransactionalEditingDomain();
    	//EditingDomain domain = AdapterFactoryEditingDomain.getEditingDomainFor(obj);
    	
    	// create and open Dialog
    	LoadResourceDialog loadResourceDialog = new LoadResourceDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), domain);
        loadResourceDialog.open();
        
        // get selected URI
        List<URI> URIList = loadResourceDialog.getURIs();
        URI uri = URIList.get(0);
        
        Resource resource;
        // code taken from "createModel" function in Ess_coreEditor.java in the editor project.
        try {
			// Load the resource through the editing domain.
			resource = domain.getResourceSet().getResource(uri, true);
		}
		catch (Exception e) {
			resource = domain.getResourceSet().getResource(uri, false);
		}
        
        //get root object from the resource to set.
        EObject retobj = resource.getContents().get(0);
        
        return retobj;
    }


The returned object then gets set in the tool:
index.php/fa/42446/0/
index.php/fa/42449/0/

I believe the code in comments are different ways of obtaining the domain, I tried both the domain methods and both results work (didn't try both sessions though).

The field gets properly set, and the Container shows up as expected:
(viewpoint)
index.php/fa/42447/0/
(semantic element)
index.php/fa/42445/0/

And the external model is loaded in the project dependencies as well:
index.php/fa/42448/0/

That's exactly the behavior I wanted, so problem resolved.

[Updated on: Wed, 31 August 2022 13:26]

Report message to a moderator

Previous Topic:Usage of java services defined inside a different viewpoint - is that possible?
Next Topic:[Sirius] Access to local resources of modeling project
Goto Forum:
  


Current Time: Sat Apr 27 01:10:14 GMT 2024

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

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

Back to the top