remykif
remykif

Reputation: 9

load EMF instance model and then create a object in it programmatically

I need to load the EMF instance model and then create an object in it just by coding in the separate plugin.

Please, explain to me with a code snippet on how to create this object?

Suppose that we have simple metamodel:

Upvotes: 0

Views: 1182

Answers (2)

Ealrann
Ealrann

Reputation: 397

If you want to load an XML model, as long as you use it in an Eclipse plugin, it should be as simple as this:

ResourceSet resourceSet = new ResourceSetImpl();
Resource resource = resourceSet.getResource(fileURI, true);

To create some objects in your model, you need to use the gactory, generated by EMF from your .ecore. If your .ecore describes a package named Database, your factory will be generated in 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);

To retrieve your User inside this model:

User user = (User) resource.eContents().get(0);

(This is just an example, of course the get(0) is risky.)

Upvotes: 2

remykif
remykif

Reputation: 9

public Object execute(ExecutionEvent event) throws ExecutionException { IWorkbenchWindow window = 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;
}

}

Upvotes: -1

Related Questions