0

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:

2
  • You question does not make much sense, sounds like all you need to do is load an EMF model from XML or wherever, get a EObject that is your UserObject, then pass that reference to another plugin or class that can add or modify the object you loaded and then save it back to disk or do whatever you want with it. Is this what you are asking? Commented Oct 6, 2018 at 21:43
  • I find solution Commented Oct 18, 2018 at 15:22

2 Answers 2

2

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

I want to add the fact that the code given in the answer works also outside any Eclipse plugin, in ordinary non-Eclipse Java projects.
-1

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.