Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Model Instantiation using EMF Reflective API and Root element in Metamodel
Model Instantiation using EMF Reflective API and Root element in Metamodel [message #1733039] Mon, 23 May 2016 22:48 Go to next message
Eclipse UserFriend
Hello,

I am creating an instance of a metamodel using Java EMF library. The metamodel has only one metaclass, which is EC_A. There are multiple instances of this metaclass to be included in the model. The code I wrote is below:

ResourceSet rs = new ResourceSetImpl();
rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore", new XMIResourceFactoryImpl());
Resource res = rs.createResource( URI.createFileURI( "<some direct path to ecore file>" ));
res.load(null);
EPackage metapackage = (EPackage)res.getContents().get(0);
EFactory metafactory = metapackage.getEFactoryInstance();

EClass EC_A_Class = (EClass) metapackage.getEClassifier("EC_A");
EAttribute stringAttr1 = (EAttribute) timeInfoClass.getEStructuralFeature("stringAttr1");

ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new  XMLResourceFactoryImpl());
Resource resource = resourceSet.createResource(URI.createFileURI("<some direct path to model file to be created>"));

for(int i = 0; i < nElementsToBeCreated ; i++)
{
	EObject newInstance = metafactory.create( EC_A_Class );
	newInstance.eSet( stringAttr1, (String) "Some value");
	resource.getContents().add(newInstance );
}

resource.save(Collections.EMPTY_MAP);


The problem is the following: Even if there are more than one EC_A instances, the created model includes only one instance that is the first one. I also tried the same metamodel with a slight modification: I added a Root metaclass, which has a EC_A containment reference with multiplicity >=0. Then, the model instantiation process worked: The resulted model included only one Root element and all expected EC_A instances.

The question is, do I have to include a root element in my metamodel? Or am I doing something wrong with my code?
Re: Model Instantiation using EMF Reflective API and Root element in Metamodel [message #1733045 is a reply to message #1733039] Tue, 24 May 2016 03:13 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33216
Registered: July 2009
Senior Member
On 24.05.2016 00:48, Bugra M. Yildiz wrote:
> Hello,
> I am creating an instance of a metamodel using Java EMF library. The
> metamodel has only one metaclass, which is EC_A. There are multiple
> instances of this metaclass to be included in the model. The code I
> wrote is below:
>
> ResourceSet rs = new ResourceSetImpl();
> rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore",
> new XMIResourceFactoryImpl());
It's better to use EcoreResourceFactoryImpl for serializing Ecore.
> Resource res = rs.createResource( URI.createFileURI( "<some direct
> path to ecore file>" ));
> res.load(null);
> EPackage metapackage = (EPackage)res.getContents().get(0);
> EFactory metafactory = metapackage.getEFactoryInstance();
>
> EClass EC_A_Class = (EClass) metapackage.getEClassifier("EC_A");
> EAttribute stringAttr1 = (EAttribute)
> timeInfoClass.getEStructuralFeature("stringAttr1");
>
> ResourceSet resourceSet = new ResourceSetImpl();
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*",
> new XMLResourceFactoryImpl());
> Resource resource =
> resourceSet.createResource(URI.createFileURI("<some direct path to
> model file to be created>"));
>
> for(int i = 0; i < nElementsToBeCreated ; i++)
> {
> EObject newInstance = metafactory.create( EC_A_Class );
> newInstance.eSet( stringAttr1, (String) "Some value");
> resource.getContents().add(newInstance );
> }
>
> resource.save(Collections.EMPTY_MAP);
>
> The problem is the following: Even if there are more than one EC_A
> instances, the created model includes only one instance that is the
> first one.
Yes, XMLResourceFactoryImpl's creates an XMLResourceImpl and that's what
it does. You'll either need a single root element if you use that
resource implementation. Or you can use XMIResourceFactoryImpl which
creates an XMIResourceImpl which will serialize an "synthetic" root
element in the XML in order to hold all the contents of the resource.
> I also tried the same metamodel with a slight modification: I added a
> Root metaclass, which has a EC_A containment reference with
> multiplicity >=0. Then, the model instantiation process worked: The
> resulted model included only one Root element and all expected EC_A
> instances.
> The question is, do I have to include a root element in my metamodel?
No.
> Or am I doing something wrong with my code?
You'll have to use XMI if you want to support a resource with multiple
objects in the getContents().


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Model Instantiation using EMF Reflective API and Root element in Metamodel [message #1733062 is a reply to message #1733045] Tue, 24 May 2016 00:27 Go to previous message
Eclipse UserFriend
Thanks, it worked Smile !
Previous Topic:Memory consumption of CompositeChangeDescription and large lists
Next Topic:Get all contents of multiple resources
Goto Forum:
  


Current Time: Thu Sep 19 18:47:42 GMT 2024

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

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

Back to the top