Question

I am trying to programmatically load an e4 application model in order to be able to iterate on the model elements. Currently I am facing the problem, that I don't know on how to correctly load it.

Given an Application.e4xmi I tried to simply load the file using simple load an existing model which however did not result in a populated Resource (null).

Then I found out about org.eclipse.e4.tools.emf.ui.common.XMIModelResource, I am however not able to instantiate the model using the following code

URI uriNew = URI.createURI("file:///Users/marco/github-clones/osara/at.osara.rcp/Application.e4xmi");
XMIModelResource xmimr = new XMIModelResource(uriNew);

as I get Package with uri 'http://www.eclipse.org/ui/2010/UIModel/application' not found. This ecore however is located in the already imported org.eclipse.e4.ui.model.workbench

Anybody got a hint on this? Thanks!

Was it helpful?

Solution 2

The reason is quite simple; the respective EMF model is not yet registered in the workspace. In order to do so, the following code has to be executed before loading the model:

import org.eclipse.e4.ui.model.application.impl.ApplicationPackageImpl;
ApplicationPackageImpl.init();

Here goes a full code sample for loading an Eclipse 4 Application model in a standalone main method:

import org.eclipse.e4.ui.internal.workbench.E4XMIResourceFactory;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.impl.ApplicationPackageImpl;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;

public class LoadAppModel {

    private static ResourceSet resourceSet = new ResourceSetImpl();

    public static void main(String[] args) {
        ApplicationPackageImpl.init();
        URI uri = URI
                .createURI("file:///Users/marco/git/pharmacy_at/at.medevit.ecrit.pharmacy_at.application/Application.e4xmi");

        resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
                .put("e4xmi", new E4XMIResourceFactory());

        Resource res = resourceSet.getResource(uri, true);
        MApplication app = (MApplication) res.getContents().get(0);
        System.out.println(app.getElementId());
    }

}

OTHER TIPS

You could use the injected EModelService or MApplication classes. Here is more info.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top