|
|
|
|
|
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 |
Ed Merks Messages: 33137 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);
> }
> }
>
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02842 seconds