Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Sirius » Loading Model created using Eclipse-Sirius(Loading a model in terms of the metamodel in java)
Loading Model created using Eclipse-Sirius [message #1849571] Tue, 25 January 2022 23:33 Go to next message
Bruno Curzi-Laliberté is currently offline Bruno Curzi-LalibertéFriend
Messages: 14
Registered: January 2022
Junior Member
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 #1849575 is a reply to message #1849571] Wed, 26 January 2022 05:46 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Your code contains no support for loading from XML because your code uses EMF that provides all these facilities to its users.

XML is loaded by the ResourceSet.getResource merthod.

I suggest you follow some tutorials or read the EMF book.

Regards

Ed Willink
Re: Loading Model created using Eclipse-Sirius [message #1849663 is a reply to message #1849575] Fri, 28 January 2022 14:21 Go to previous message
Bruno Curzi-Laliberté is currently offline Bruno Curzi-LalibertéFriend
Messages: 14
Registered: January 2022
Junior Member
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.
Previous Topic:Extract a subpart of the model to an XML file
Next Topic:Undo effects outside of model changes in service calls
Goto Forum:
  


Current Time: Fri Apr 19 18:13:17 GMT 2024

Powered by FUDForum. Page generated in 0.03199 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top