Loading Model created using Eclipse-Sirius [message #1849571] |
Tue, 25 January 2022 18:33  |
Eclipse User |
|
|
|
Using ObeoDesigner, I defined an ECore MetaModel, generated the code and ran the project.
I didn't define a graphical interface using Sirius yet, but using a tree-like view, I can define Models instanciating this MetaModel.
Now I want to load the model using Java. My model is just an XML format inside a file of extension named after my ECore project. For example I named the original MetaModel project "epidemiologyJan11" so my file inside the Sirius editor is "MyEpidemiologyJan11.epidemiologyJan11".
Some of the original generated code contains Factories, but none contain functionalities for loading XML, I expected to be able to do something like: Epidemic epidemic = EpidemicFactory.createFromXML("MyEpidemiologyJan11.epidemiologyJan11") in Java.
Is it possible to load my model in terms of metamodel implementation?
If you're not sure exactly what I mean, but know of other ways to interact with a Sirius model, please le me know through comments, thanks.
|
|
|
|
Re: Loading Model created using Eclipse-Sirius [message #1849663 is a reply to message #1849575] |
Fri, 28 January 2022 09:21  |
Eclipse User |
|
|
|
Loading a model:
the metamodel is named `epidemiologyJan11` and the model is named `MyEpidemiologyJan11.epidemiologyJan11`
Java
// could be program argument for example, this is the path to your model to be loaded
String fn = "D:\\Downloads\\ObeoDesigner-Community-11.5-win32.win32.x86_64\\ObeoDesigner-Community\\runtime-EclipseApplication\\epi111\\MyEpidemiologyJan11.epidemiologyJan11";
// register an factory for XMI format
// your model is an XML file but has the metamodel extension (.epidemiologyJan11, not .xml)
// we simply map that extension to the XMI factory
Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
Map<String, Object> m = reg.getExtensionToFactoryMap();
m.put(
"epidemiologyJan11",
new XMIResourceFactoryImpl()
);
// link the metamodel to let the factory know which package the classes referred to in your model actually represent
ResourceSet resSet = new ResourceSetImpl();
resSet.getPackageRegistry().put(
epidemiologyJan11.EpidemiologyJan11Package.eNS_URI,
epidemiologyJan11.EpidemiologyJan11Package.eINSTANCE
);
// create a uri using the path to the model file and use it to load the resource
URI uri = URI.createFileURI(fn);
Resource resource = resSet.getResource(uri, true);
// our example resource has a single top level node of type Epidemic
Epidemic myEpi = (Epidemic) resource.getContents().get(0);
// then we can make use of the loaded object using a combination of EMF utilities and our defined Model classes
EList<PopulationDimension> dimensions = myEpi.getPopulationdimension();
required imports
import java.util.Map;
import org.eclipse.emf.common.util.EList;
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;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import epidemiologyJan11.Epidemic;
import epidemiologyJan11.PopulationDimension;
```
notes
EMF must be available, my suggestion way is to create an empty EMF project instead of a Java project in Eclipse.
Metamodeling EMF project has to be in build path for imports to resolve.
|
|
|
Powered by
FUDForum. Page generated in 0.03764 seconds