Skip to main content



      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 10:57 Go to next message
Eclipse UserFriend
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 11:37 Go to previous messageGo to next message
Eclipse UserFriend
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.
Re: modifying xmi model file [message #1856509 is a reply to message #1856507] Tue, 13 December 2022 11:48 Go to previous messageGo to next message
Eclipse UserFriend
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 01:24 Go to previous messageGo to next message
Eclipse UserFriend
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().
Re: modifying xmi model file [message #1856539 is a reply to message #1856523] Wed, 14 December 2022 06:54 Go to previous messageGo to next message
Eclipse UserFriend
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 08:33 Go to previous message
Eclipse UserFriend
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...
Previous Topic:failed to read genmodel
Next Topic:Documentation generator for Ecore models
Goto Forum:
  


Current Time: Sun Aug 31 07:57:20 EDT 2025

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

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

Back to the top