Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » modifying xmi model file(Adding assets model from metamodel xmi file)
modifying xmi model file [message #1856506] Tue, 13 December 2022 15:57 Go to next message
wld blm is currently offline wld blmFriend
Messages: 6
Registered: December 2022
Junior Member
Hello everyone,

I'm new to EMF and I need to adding "assets" to my EMF model that I loaded from an XMI file. I have to pick up that assets from my metamodel XMI file that I also loaded in my code.
I'm not sure where to begin but here is what I'v done so far.

ResourceSet resourceSet = new ResourceSetImpl();
        resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());
        resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new  XMIResourceFactoryImpl());

        Resource myMetaModel = resourceSet.getResource(URI.createFileURI(filePathMetamodel), true);

        EPackage univEPackage = (EPackage) myMetaModel.getContents().get(0);
        resourceSet.getPackageRegistry().put("*", univEPackage);
        Resource myModel = resourceSet.getResource( URI.createURI( filePathModel), true);

        EcoreFactory theCoreFactory = EcoreFactory.eINSTANCE;
        EClass adultEClass= theCoreFactory.createEClass();
        univEPackage.getEClassifiers().add(adultEClass);

        EFactory univInstance = univEPackage.getEFactoryInstance();
        EObject adultObject = univInstance.create(adultEClass);

        EList<EObject> ModelObjects = new BasicEList<EObject>();
        ModelObjects.add(adultObject);

        myModel.getContents().addAll(ModelObjects);

        myModel.save(null);
        myMetaModel.save(null);


The problem here is that it's creating a new Eclass and then add it to my metamodel and then my model but what I want to do is choose it directly from my metamodel and add it to my model is it possible ?

Thanks
Re: modifying xmi model file [message #1856507 is a reply to message #1856506] Tue, 13 December 2022 16:37 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
I think it still needs to be like resourceSet.getPackageRegistry().put(univEPackage.getNsURI(), univEPackage). I assume that univEPackage .getEClassifiers() returns a list of classifiers already in that model. Presumably some of those are EClasses and then presumably you want to use univEPackage .getEFactoryInstance().create(...) or use EcoreUtil.create(EClass).

Don't neglect using the debugger to inspect what's happening and to understand the data available in the data structures.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: modifying xmi model file [message #1856509 is a reply to message #1856507] Tue, 13 December 2022 16:48 Go to previous messageGo to next message
wld blm is currently offline wld blmFriend
Messages: 6
Registered: December 2022
Junior Member
Thanks for your answer,
I forgot to mention that I tried to get an EClass from the univEPackage with
EObject adultObject = univEPackage.getEFactoryInstance().create(univEPackage.getEClassifiers().get(0).eClass());

but when i run my code i have this exception
The class 'EClass' is not a valid classifier


I feel like I'm missing something..

Thanks
Re: modifying xmi model file [message #1856523 is a reply to message #1856509] Wed, 14 December 2022 06:24 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Again, use the debugger. It's very clear you have not done that in an attempt to answer your own question before asking it here. An EClassifier might be an EClass, and EDataType or an EEnum. But in all cases the .eClass() of any EClassifer that you fetched from the univEPackage (or any EPackage), will be an EClass from EcorePackage.eINSTANCE.getClassifiers().

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: modifying xmi model file [message #1856539 is a reply to message #1856523] Wed, 14 December 2022 11:54 Go to previous messageGo to next message
wld blm is currently offline wld blmFriend
Messages: 6
Registered: December 2022
Junior Member
Sorry but with all due respect I did use the debugger. Maybe I use it wrong and you can help me and telling me which attribute of my EClassifier should I look into to know if it is an Eclass, because for me when I look at it on the debugger i can clearly see that its and EClass. Thanks
Re: modifying xmi model file [message #1856542 is a reply to message #1856539] Wed, 14 December 2022 13:33 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
This is how to create an instance of every EClass supported by an EPackage:
    EPackage myPackage = GitPackage.eINSTANCE;
    for (EClassifier eClassifier : myPackage.getEClassifiers())
    {
      if (eClassifier instanceof EClass)
      {
        EClass eClass = (EClass)eClassifier;
        if (eClass.eClass() == EcorePackage.Literals.ECLASS)
        {
          System.out.println("Yes, the eClass() of any EClass is EcorePackage.Literals.ECLASS");
        }
        if (!eClass.isAbstract())
        {
          EObject eObject = EcoreUtil.create(eClass);
          System.out.println(eObject);
        }
      }
    }
For some reason you decided to get the eClass() of your classifier rather than test if your classifier is an EClass or not...


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:failed to read genmodel
Next Topic:Documentation generator for Ecore models
Goto Forum:
  


Current Time: Fri Apr 26 07:55:47 GMT 2024

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

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

Back to the top