Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to create genmodel from ecore programmatically without using headless invocation/eclipse plugins
How to create genmodel from ecore programmatically without using headless invocation/eclipse plugins [message #1095968] Tue, 27 August 2013 19:44 Go to next message
juan velez is currently offline juan velezFriend
Messages: 10
Registered: June 2013
Junior Member
I am working on an isolated tool (can't exeucte headless invocations, can't use eclipse installation) that will read/parse a source (e.g. DB, XML, etc) and generate both an ecore model as well as its associated gen model. I have been able to generate successfully the ecore model (model.ecore) programmatically using the dynamic generation feature provided by EMF. I would like to do the same for the model.genmodel as I need to set certain properties/features which are dependent on the source and time of generation of the ecore.

Any advice on how to accomplish this without the use of headless invocation/ant tasks will be appreciated
Re: How to create genmodel from ecore programmatically without using headless invocation/eclipse plu [message #1096207 is a reply to message #1095968] Wed, 28 August 2013 04:43 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30700
Registered: July 2009
Senior Member
Juan,

The best I can do is suggest you look at source code. E.g., look at how
org.eclipse.emf.codegen.ecore.genmodel.presentation.GenModelActionBarContributor.GenerateAction
invokes the generator and at the Ant tasks.


On 27/08/2013 9:44 PM, juan velez wrote:
> I am working on an isolated tool (can't exeucte headless invocations,
> can't use eclipse installation) that will read/parse a source (e.g.
> DB, XML, etc) and generate both an ecore model as well as its
> associated gen model. I have been able to generate successfully the
> ecore model (model.ecore) programmatically using the dynamic
> generation feature provided by EMF. I would like to do the same for
> the model.genmodel as I need to set certain properties/features which
> are dependent on the source and time of generation of the ecore.
>
> Any advice on how to accomplish this without the use of headless
> invocation/ant tasks will be appreciated
Re: How to create genmodel from ecore programmatically without using headless invocation/eclipse plu [message #1096532 is a reply to message #1096207] Wed, 28 August 2013 14:19 Go to previous messageGo to next message
juan velez is currently offline juan velezFriend
Messages: 10
Registered: June 2013
Junior Member
Ed,

Thanks for the advice. I might have misread the code you pointed at, but it looks like this generates code from an existing GenModel. What I am trying to accomplish is to generate the GenModel file itself. Any experience/ideas?

Thanks
Re: How to create genmodel from ecore programmatically without using headless invocation/eclipse plu [message #1096591 is a reply to message #1096532] Wed, 28 August 2013 15:55 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30700
Registered: July 2009
Senior Member
Juan,

I see. The importer framework does that type of thing:
org.eclipse.emf.importer.ecore.EcoreImporter. For simple cases, i.e., a
self-contain Ecore model, it's as simple as creating a GenModel instance
and calling
org.eclipse.emf.codegen.ecore.genmodel.GenModel.initialize(Collection<?
extends EPackage>)...

On 28/08/2013 4:19 PM, juan velez wrote:
> Ed,
>
> Thanks for the advice. I might have misread the code you pointed at,
> but it looks like this generates code from an existing GenModel. What
> I am trying to accomplish is to generate the GenModel file itself. Any
> experience/ideas?
>
> Thanks
Re: How to create genmodel from ecore programmatically without using headless invocation/eclipse plu [message #1096737 is a reply to message #1096591] Wed, 28 August 2013 20:09 Go to previous messageGo to next message
juan velez is currently offline juan velezFriend
Messages: 10
Registered: June 2013
Junior Member
For those who end up here doing a google search this is my solution based on Ed's last advice. Please note that this generates a gen model file based on an existing ecore model which has already been imported into an EPackage by reading it using a Resource. Also, not shown here, the resource associated to the EPackage needs to have its URI set to the correct ecore file.

    private void createGenModel(final EPackage rootPackage, final String ecoreLocation,
        final String genModelLocation) {

        GenModel genModel = GenModelFactory.eINSTANCE.createGenModel();
        genModel.setComplianceLevel(GenJDKLevel.JDK70_LITERAL);
        genModel.setModelDirectory(Constants.GEN_MODEL_MODEL_DIR.getValue());
        genModel.getForeignModel().add(new Path(ecoreLocation).lastSegment());
        genModel.setModelName(Constants.GEN_MODEL_MODEL_NAME.getValue());
        genModel.setRootExtendsInterface(Constants.GEN_MODEL_EXTENDS_INTERFACE.getValue());
        genModel.initialize(Collections.singleton(rootPackage));

        GenPackage genPackage = (GenPackage)genModel.getGenPackages().get(0);
        genPackage.setPrefix(Constants.GEN_MODEL_PACKAGE_PREFIX.getValue());

        try {
            URI genModelURI = URI.createFileURI(genModelLocation);
            final XMIResourceImpl genModelResource = new XMIResourceImpl(genModelURI);
            genModelResource.getDefaultSaveOptions().put(XMLResource.OPTION_ENCODING,
                Constants.GEN_MODEL_XML_ENCODING.getValue());
            genModelResource.getContents().add(genModel);
            genModelResource.save(Collections.EMPTY_MAP);
        } catch (IOException e) {
            String msg = null;
            final String genModelLocationCleaned = LogUtil.stripForgeChars(genModelLocation);
            if (e instanceof FileNotFoundException) {
                msg = "Unable to open output file " + genModelLocationCleaned;
            } else {
                msg = "Unexpected IO Exception writing " + genModelLocationCleaned;
            }
            LOGGER.error(msg, e);
            throw new RuntimeException(msg, e);
        }
    }

Re: How to create genmodel from ecore programmatically without using headless invocation/eclipse plu [message #1097121 is a reply to message #1096737] Thu, 29 August 2013 08:58 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30700
Registered: July 2009
Senior Member
Juan,

Thanks for sharing your results!!

On 28/08/2013 10:09 PM, juan velez wrote:
> For those who end up here doing a google search this is my solution
> based on Ed's last advice. Please note that this generates a gen model
> file based on an existing ecore model which has already been imported
> into an EPackage by reading it using a Resource. Also, not shown here,
> the resource associated to the EPackage needs to have its URI set to
> the correct ecore file.
>
>
> private void createGenModel(final EPackage rootPackage, final
> String ecoreLocation,
> final String genModelLocation) {
>
> GenModel genModel = GenModelFactory.eINSTANCE.createGenModel();
> genModel.setComplianceLevel(GenJDKLevel.JDK70_LITERAL);
> genModel.setModelDirectory(Constants.GEN_MODEL_MODEL_DIR.getValue());
> genModel.getForeignModel().add(new
> Path(ecoreLocation).lastSegment());
> genModel.setModelName(Constants.GEN_MODEL_MODEL_NAME.getValue());
> genModel.setRootExtendsInterface(Constants.GEN_MODEL_EXTENDS_INTERFACE.getValue());
> genModel.initialize(Collections.singleton(rootPackage));
>
> GenPackage genPackage =
> (GenPackage)genModel.getGenPackages().get(0);
> genPackage.setPrefix(Constants.GEN_MODEL_PACKAGE_PREFIX.getValue());
>
> try {
> URI genModelURI = URI.createFileURI(genModelLocation);
> final XMIResourceImpl genModelResource = new
> XMIResourceImpl(genModelURI);
> genModelResource.getDefaultSaveOptions().put(XMLResource.OPTION_ENCODING,
> Constants.GEN_MODEL_XML_ENCODING.getValue());
> genModelResource.getContents().add(genModel);
> genModelResource.save(Collections.EMPTY_MAP);
> } catch (IOException e) {
> String msg = null;
> final String genModelLocationCleaned =
> LogUtil.stripForgeChars(genModelLocation);
> if (e instanceof FileNotFoundException) {
> msg = "Unable to open output file " +
> genModelLocationCleaned;
> } else {
> msg = "Unexpected IO Exception writing " +
> genModelLocationCleaned;
> }
> LOGGER.error(msg, e);
> throw new RuntimeException(msg, e);
> }
> }
>
>
Re: How to create genmodel from ecore programmatically without using headless invocation/eclipse plu [message #1723040 is a reply to message #1096737] Thu, 11 February 2016 08:44 Go to previous messageGo to next message
Zille Huma is currently offline Zille HumaFriend
Messages: 6
Registered: February 2016
Junior Member
Hi Juan,

I am trying to use your code to generate genmodel for my ecore model. Can you please specify what is 'Constants' in this code. I dont see any static class with this name. Can you please elaborate further.
Re: How to create genmodel from ecore programmatically without using headless invocation/eclipse plu [message #1723056 is a reply to message #1723040] Thu, 11 February 2016 10:13 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30700
Registered: July 2009
Senior Member
Apparently that's his own class with hard coded (constant) values that
he wants to use.

On 11.02.2016 09:44, Zille Huma wrote:
> Hi Juan,
>
> I am trying to use your code to generate genmodel for my ecore model.
> Can you please specify what is 'Constants' in this code. I dont see
> any static class with this name. Can you please elaborate further.
Re: How to create genmodel from ecore programmatically without using headless invocation/eclipse plu [message #1811269 is a reply to message #1096737] Thu, 29 August 2019 23:42 Go to previous messageGo to next message
Afe Fer is currently offline Afe FerFriend
Messages: 8
Registered: August 2019
Junior Member
Hi,

I have just tested the code provided bellow but I have the following execption :
Exception in thread "main" java.lang.NoSuchMethodError: org.eclipse.emf.codegen.util.CodeGenUtil.capName(Ljava/lang/String;Ljava/util/Locale;)Ljava/lang/String;
at org.eclipse.emf.codegen.ecore.genmodel.impl.GenBaseImpl.capName(GenBaseImpl.java:319)
at org.eclipse.emf.codegen.ecore.genmodel.impl.GenPackageImpl.initialize(GenPackageImpl.java:3068)
at org.eclipse.emf.codegen.ecore.genmodel.impl.GenModelImpl.initialize(GenModelImpl.java:2672)


I have tried another code snipet that uses the same initalize method and I have the same error.

Note there are two packages implementing this method :
import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
import org.eclipse.uml2.codegen.ecore.genmodel.GenModel;

I have tried importing both (not the same time of course)

Any hints?
Re: How to create genmodel from ecore programmatically without using headless invocation/eclipse plu [message #1811272 is a reply to message #1811269] Fri, 30 August 2019 02:03 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30700
Registered: July 2009
Senior Member
The method it says doesn't exist has existed since EMF 2.4 so I don't know where you got your libraries from. You should get them from a recent version from here:

https://download.eclipse.org/modeling/emf/emf/builds/index.html

It's also easy to set up a development environment with all of EMF's source:

https://ci.eclipse.org/emf/

Then you can more easily look at how other parts of the code do the same things as what you're trying to do.
Re: How to create genmodel from ecore programmatically without using headless invocation/eclipse plu [message #1811277 is a reply to message #1811272] Fri, 30 August 2019 05:08 Go to previous messageGo to next message
Afe Fer is currently offline Afe FerFriend
Messages: 8
Registered: August 2019
Junior Member
Thanks again for your reply.

I downloaded from here : https://jar-download.com/?search_box=org.eclipse.emf.ecore

I will have a look at the links at code and will lets you know.
Re: How to create genmodel from ecore programmatically without using headless invocation/eclipse plu [message #1811289 is a reply to message #1811277] Fri, 30 August 2019 07:17 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 6540
Registered: July 2009
Senior Member
Hi

"Last update 22. July 2009" is a bit of a clue. Use the official version applicable to your version of Eclipse at Help->Install New Software.... Work with e.g. 2019-06 - http://download.eclipse.org/releases/2019-06 and select Modeling->EMF - Eclipse Modeling Framework SDK.

Regards

Ed Willink
Previous Topic:Do I need to generate the code of the meta model to explore/edit existing models
Next Topic:Single references of EObject type in Dynamic Model
Goto Forum:
  


Current Time: Mon Dec 16 12:22:56 GMT 2019

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

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

Back to the top