加载EMF实例模型,然后以编程方式在其中创建对象

时间:2018-09-29 07:38:27

标签: emf eclipse-emf

我需要加载EMF实例模型,然后仅通过在单独的插件中进行编码就可以在其中创建对象。

请通过代码片段向我说明如何创建此对象?

假设我们有简单的元模型:

2 个答案:

答案 0 :(得分:1)

如果要加载XML模型,只要在Eclipse插件中使用它,它就应该像这样简单:

CSS

要在模型中创建一些对象,您需要使用EMF从ResourceSet resourceSet = new ResourceSetImpl(); Resource resource = resourceSet.getResource(fileURI, true); 生成的gactory。如果您的.ecore描述了一个名为.ecore的软件包,则工厂将在Database中生成。

DatabaseFactory.java

要在此模型中检索您的User newUser = DatabaseFactory.INSTANCE.createUser(); newUser.setFirstName("xxxx"); Login login = DatabaseFactory.INSTANCE.createLogin(); login.setPassword("12345678"); newUser.getPasswords().add(login); resource.getContents().add(newUser);

User

(这只是一个例子,User user = (User) resource.eContents().get(0); 当然是有风险的。)

答案 1 :(得分:-1)

公共对象execute(ExecutionEvent事件)引发ExecutionException {                     IWorkbenchWindow窗口= HandlerUtil.getActiveWorkbenchWindowChecked(event);                     ISelection selection = window.getSelectionService()。getSelection(“ org.eclipse.jdt.ui.PackageExplorer”);                     MessageDialog.openInformation(window.getShell(),“ Test”,selection.toString());

                TreeSelection treeSelection = (TreeSelection) selection;
                TreePath[] treePaths = treeSelection.getPaths();
                TreePath treePath = treePaths[0];
                Object lastSegmentObj = treePath.getLastSegment();
                IFile file = (IFile) ((IAdaptable) lastSegmentObj).getAdapter(IFile.class);
                String path = file.getRawLocationURI().toString(); 
                URI uri = URI.createURI(path);

                User imodesene = UserFactory.eINSTANCE.createModesene();
                    imodesene.eAdapters().add(new EContentAdapter() {
                            @Override
                            public void notifyChanged(Notification notification) {
                                super.notifyChanged(notification);
                            }   
                        });

                ResourceSet resourceSet = new ResourceSetImpl();
                Resource resource = resourceSet.getResource(uri, true);
                imodesene = (User) resource.getContents().get(0);

                MessageDialog.openInformation(window.getShell(),"Test", "Load --- Name :"+imodesene.getName()+"\n");

                List<String> NetworkNodes = new ArrayList<>();
                EList<Network> listeNetwork = imodesene.getNetworkFacet().getNetwork();
                for (int it = 0; it < listeNetwork.size(); it++) {
                    EList<Node> listeNode = listeNetwork.get(it).getNodes();
                    for (int itt = 0; itt < listeNode.size(); itt++) {
                        NetworkNodes.add(listeNode.get(itt).getId());
                        }
                    }


                EList<PhysicalEnvironment> listeEnv = imodesene.getPhysicalEnvFacet().getPhysicalEnv();
                for (int it = 0; it < listeEnv.size(); it++) {
                    for (int itt = 0; itt < NetworkNodes.size(); itt++) {
                        NodeInstance nodeInstance = ModeseneFactory.eINSTANCE.createNodeInstance();
                        nodeInstance.setID(NetworkNodes.get(itt));
                        listeEnv.get(it).getZones().get(0).getNodes().add(nodeInstance);

                        }
                    }




            try {
                resource.save(null);
            } catch (IOException e) {
                e.printStackTrace();
            }

            MessageDialog.openInformation(window.getShell(),"Test", "--- END ---");


    return null;
}

}