How to create genmodel from ecore programmatically without using headless invocation/eclipse plugins [message #1095968] |
Tue, 27 August 2013 15:44  |
Eclipse User |
|
|
|
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 #1097121 is a reply to message #1096737] |
Thu, 29 August 2013 04:58   |
Eclipse User |
|
|
|
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);
> }
> }
>
>
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04708 seconds