Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Create Ecore Modelling Project programmatically
Create Ecore Modelling Project programmatically [message #1860831] Tue, 05 September 2023 12:39 Go to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
Hi,

I am creating an .ecore model programmatically in Java. However, I want to put this model in an EcoreModelingProject that should also be created programmatically. Any hints on how to do that?

Thanks!
Re: Create Ecore Modelling Project programmatically [message #1860832 is a reply to message #1860831] Tue, 05 September 2023 13:03 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33143
Registered: July 2009
Senior Member
Best to look at how the wizards do these things. It's easy to set up a development environment for that investigation:

https://github.com/eclipse-emf/org.eclipse.emf/blob/master/CONTRIBUTING.md


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Create Ecore Modelling Project programmatically [message #1860833 is a reply to message #1860832] Tue, 05 September 2023 14:14 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
What would be the natureID for the modeling nature?
Re: Create Ecore Modelling Project programmatically [message #1860835 is a reply to message #1860833] Tue, 05 September 2023 15:18 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33143
Registered: July 2009
Senior Member
EMF itself does use/define such a thing. I'm not sure which nature you mean. You can look in .project files to see the nature IDs (and associated builders). Use the view menu Filters... to uncheck .*resources so that you can see such files in the Explorer.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Create Ecore Modelling Project programmatically [message #1860838 is a reply to message #1860835] Tue, 05 September 2023 17:18 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=532994 suggests that EMF could usefully have a Builder / Nature.

If you want to auto-genmodel, you will need to wrap a builder around GenModel, possibly with the help of MWE2. I don't recommend full automation of this since errors can occur and they deserve manual attention before you get confused by downstream failures from everything else. The history of, and lack of errors in, auto-generated EMF is so important that many projects check the auto-generated files into GIT and treat them as secondary sources. I use manually triggered MWE2 scripts to auto-generate.

If you want auto-validation of *.ecore / *.uml to populate the Problems View (without manually opening an editor) you can configure the OCL nature to validate both the Ecore/UML framework as well as the embedded OCL in *.ecore / *.uml.

Re: Create Ecore Modelling Project programmatically [message #1860856 is a reply to message #1860838] Wed, 06 September 2023 11:06 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
I created the Ecore Modeling Project and I put an ecore model in that project. I then wrote code to build me a genmodel for that ecore model. However, it appears that something is wrong because when trying to generate code for the edit, editor, and test, they cannot be selected. Is there something wrong with the way I am creating the genmodel? Is there something out there to assist with this? This is the code I currently have:

public class GenModelGenerator {

    public void generateGenModel(String ecoreFilePath, String genModelFilePath, String genModelName, IPath ecoreProjectPath) {

        ResourceSet resourceSet = new ResourceSetImpl();

        URI ecoreURI = URI.createURI(ecoreFilePath);
        Resource ecoreResource = resourceSet.getResource(ecoreURI, true);
        EPackage ePackage = (EPackage) ecoreResource.getContents().get(0);

        GenModel genModel = GenModelFactory.eINSTANCE.createGenModel();
        genModel.setModelName(genModelName);
        genModel.setDynamicTemplates(true);

        genModel.setModelDirectory("myproject/src-gen");
        genModel.getGenPackages().add(createGenPackage(ePackage));

        genModel.setEditDirectory("myproject/edit");
        genModel.setEditorDirectory("myproject/editor");
        genModel.setTestsDirectory("myproject/tests");

        URI genModelURI = URI.createURI(genModelFilePath);
        Resource genModelResource = resourceSet.createResource(genModelURI);
        genModelResource.getContents().add(genModel);

        try {
            genModelResource.save(null);
            System.out.println("GenModel generated successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private GenPackage createGenPackage(EPackage ePackage) {
        GenPackage genPackage = GenModelFactory.eINSTANCE.createGenPackage();
        genPackage.setEcorePackage(ePackage);

        return genPackage;
    }
}
Re: Create Ecore Modelling Project programmatically [message #1860865 is a reply to message #1860856] Wed, 06 September 2023 16:51 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33143
Registered: July 2009
Senior Member
I'm traveling for a few days and have to focus on getting the 2023-09 release out. Best you use your EMF IDE to launch a Runtime Workspace (it comes with such a launcher) to debug what's going on. Or compare one made with the wizard with one you made.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Create Ecore Modelling Project programmatically [message #1860909 is a reply to message #1860865] Fri, 08 September 2023 07:12 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Quote:
Or compare one made with the wizard with one you made.
is always a good approach when applicable.

After a quick glance:

I see no ResourceSet registrations for *.genmodel so I doubt you created a valid *.genmodel.

I see no contribution for the GenModel <foreignModel>.

...
Re: Create Ecore Modelling Project programmatically [message #1860946 is a reply to message #1860909] Mon, 11 September 2023 11:42 Go to previous messageGo to next message
John Henbergs is currently offline John HenbergsFriend
Messages: 239
Registered: October 2020
Senior Member
I was able to fix it by locating the issues via comparison. In general, I had issues with the dir and pluginIds. However if anyone wants to do something similar in the future, the code that worked for me is in the following:

public class GenModelGenerator {

    public void generateGenModel(String ecoreFilePath, String genModelFilePath, String genModelName) {
        // Initialize the EMF resource set
        ResourceSet resourceSet = new ResourceSetImpl();

        // Load the Ecore model from the file
        URI ecoreURI = URI.createURI(ecoreFilePath);
        Resource ecoreResource = resourceSet.getResource(ecoreURI, true);
        EPackage ePackage = (EPackage) ecoreResource.getContents().get(0);

        // Create a new GenModel instance
        GenModel genModel = GenModelFactory.eINSTANCE.createGenModel();
        genModel.setModelName(genModelName);
        genModel.setDynamicTemplates(true);

        // Configure the GenModel as needed
        genModel.setModelDirectory("/" +genModelName+ "/src");
        genModel.setModelPluginID(genModelName);
 

        // Enable code generation options for edit code, editor code, and test code
        genModel.setEditDirectory("/" +genModelName+ ".edit/src");
        genModel.setEditPluginID(genModelName+ ".edit");
        genModel.setEditorDirectory("/" +genModelName+ ".editor/src");
        genModel.setEditorPluginID(genModelName+".editor");
        genModel.setTestsDirectory("/" +genModelName+ ".tests/src");
        genModel.setTestsPluginID(genModelName+".tests");
        genModel.getGenPackages().add(createGenPackage(ePackage));
      
      

        // Save the GenModel to a file
        URI genModelURI = URI.createURI(genModelFilePath);
        Resource genModelResource = resourceSet.createResource(genModelURI);
        genModelResource.getContents().add(genModel);

        try {
            genModelResource.save(null);
            System.out.println("GenModel generated successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private GenPackage createGenPackage(EPackage ePackage) {
        GenPackage genPackage = GenModelFactory.eINSTANCE.createGenPackage();
        genPackage.setEcorePackage(ePackage);
        // Configure other GenPackage settings as needed
        return genPackage;
    }
    public static void generate(GenModel genModel) {
    	genModel.reconcile();
    	genModel.setCanGenerate(true);
    	genModel.setValidateModel(true);
       
    	Generator generator = new Generator();
    	generator.run(genModel);
    }
}

[Updated on: Mon, 11 September 2023 11:43]

Report message to a moderator

Re: Create Ecore Modelling Project programmatically [message #1860948 is a reply to message #1860946] Mon, 11 September 2023 13:06 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33143
Registered: July 2009
Senior Member
Thank you for sharing your solution for the benefit of others!

Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Relation doesn't exists when executing CDOServerUtil.addRepository()
Next Topic:Runtime editor
Goto Forum:
  


Current Time: Thu May 02 03:58:58 GMT 2024

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

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

Back to the top