2012-11-06 55 views
0

我試圖以編程方式加載e4應用程序模型以便能夠在模型元素上進行迭代。目前我面臨的問題是,我不知道如何正確加載它。以編程方式加載e4應用程序模型

鑑於Application.e4xmi我試圖簡單地加載文件,使用簡單的load an existing model,但是沒有導致填充資源(null)。

然後我發現了org.eclipse.e4.tools.emf.ui.common.XMIModelResource,不過,我不能夠使用下面的代碼

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

實例化模型,我得到Package with uri 'http://www.eclipse.org/ui/2010/UIModel/application' not found.此的Ecore但是位於已經導入org.eclipse.e4.ui.model.workbench

任何人對此有何暗示?謝謝!

回答

2

原因很簡單;相應的EMF模型尚未在工作區中註冊。爲了做到這一點,下面的代碼具有加載模型前要執行的:

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

這裏不用一個全碼樣本在一個獨立的主方法加載一個Eclipse 4應用模型:

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()); 
    } 

} 
2

您可以使用注入的EModelServiceMApplication類。 Here是更多信息。

+0

我很抱歉,那不是問題!在你的信息中加載一個已經可用的應用程序的應用程序模型!我正試圖從文件中加載應用程序模型而不使用它! –

+0

奇怪的是,我得到以下內容: URI uriNew = URI.createURI(「file:///Users/kirill/Documents/Dev/DW/DomainWorkbench/org.domainworkbench/Application.e4xmi」); \t \t XMIModelResource xmimr = new XMIModelResource(uriNew); \t \t assertNotNull(xmimr); assertEquals(1,xmimr.getRoot()。size()); 依賴關係: 要求-軟件包:org.eclipse.core.runtime, org.junit, org.eclipse.e4.tools.emf.ui;捆綁版本= 「0.12.0」, org.eclipse.platform ; bundle-version =「4.3.0」, org.eclipse.core.databinding.observable; bundle-version =「1.4.1」 green ... – Kyrill

+0

而對於org.eclipse.platform; bundle-version =「 4.2.1「它也起作用。 (當然,對於沒有DynamicMenuContributions的模型...)。 – Kyrill

相關問題