How to transform xText to XMI [message #666060] |
Mon, 18 April 2011 20:29 |
|
Hi,
xText comes with a snazzy ecore inference model that can take a grammar and guess a suitable eCore model for it. However, after I write a short program conforming to this grammar, I found that I still needed to write xpand templates to transform this to text!
Is there a way to turn the textual program in my specified grammar into an XMI (or similar) file conforming to the inferred ecore model?
Thanks!
|
|
|
Re: How to transform xText to XMI [message #666091 is a reply to message #666060] |
Tue, 19 April 2011 05:44 |
|
Hi,
if you work e.g. with a programmatically read model it is actually an model instance based on the generated ecore.
How you can read in the model files standalone you can find in the docs section Processing Xtext Models.
If you still need an XMI Feel free to move the contents of the read Xtext Resource to and XMI Resource.
ResourceSet rs = new ResourceSetImpl();
Resource resource = rs.getResource(URI.createURI("./mymodel.dmodel"), true);
EObject eobject = resource.getContents().get(0);
//create xmi resource
Resource xmiResource = ....
xmiResource.getContents().add(eobject);
//persist xmi resource
....
~Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
[Updated on: Tue, 19 April 2011 05:50] Report message to a moderator
|
|
|
|
Re: How to transform xText to XMI [message #666121 is a reply to message #666101] |
Tue, 19 April 2011 08:13 |
Jan Koehnlein Messages: 760 Registered: July 2009 Location: Hamburg |
Senior Member |
|
|
Hi Ed,
this seems to be a bug in the sample Ecore editor, which renames the
resource's URI rather than creating a new one with the extension from
the new URI, if it's not a conversion between "ecore" and "emof":
(EcoreEditor.doSaveAs)
if (currentExtension.equals(ECORE_FILE_EXTENSION) &&
newExtension.equals(EMOF_FILE_EXTENSION) ||
currentExtension.equals(EMOF_FILE_EXTENSION) &&
newExtension.equals(ECORE_FILE_EXTENSION))
{
Resource newResource = resourceSet.createResource(newURI);
newResource.getContents().addAll(currentResource.getContents ());
resourceSet.getResources().remove(0);
resourceSet.getResources().move(0, newResource);
}
else
{
currentResource.setURI(newURI);
}
Regards
Jan
Am 19.04.11 09:07, schrieb Ed Willink:
> Hi Christian
>
> This is an area where I feel that Xtext has been too clever.
>
> You can open an Xtext CS file in the Sample Ecore Editor and see the
> tree view, but if you then do SaveAs *.xmi, the result is saved as CS
> text rather than XMI text. Somehow Xtext has higher priority than XMI.
> It would be nice to have this interactive approach work fully.
>
> Regards
>
> Ed Willink
>
> On 19/04/2011 06:44, Christian Dietrich wrote:
>> Hi,
>>
>> if you work e.g. with a programmatically read model it is actually an
>> model instance based on the generated ecore.
>>
>> How you can read in the model files standalone you can find in the
>> docs section Processing Xtext Models.
>>
>> If you still need an XMI Feel free to move the contents of the read
>> Xtext Resource to and XMI Resource.
>>
>>
>> ResourceSet rs = new ResourceSetImpl();
>> Resource resource = rs.getResource(URI.createURI("./mymodel.dmodel"),
>> true);
>> EObject eobject = resource.getContents().get(0);
>> Resource xmiResource = ....
>> xmiResource.getContents.add(eobject);
>> ...
>>
>>
>> ~Christian
>
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
---
Get professional support from the Xtext committers at www.typefox.io
|
|
|
Re: How to transform xText to XMI [message #666303 is a reply to message #666091] |
Tue, 19 April 2011 20:02 |
|
Hi Christian,
Thanks a lot for the speedy response!
This works. Would be nice if the feature was part of the generated tooling rather than something you had to implement manually (eg some button that says "Save as XMI" in the generated UI).
Just for completeness this is the code I used with holes left out:
XMIResourceFactoryImpl resFactory = new XMIResourceFactoryImpl();
URI xmiuri = URI.createFileURI("blabla/bla/output.xmi);
XMIResource xmiresource = (XMIResource) resFactory.createResource(xmiuri);
xmiresource.getContents().add(app);
xmiresource.save(new HashMap());
where "app" is the resource that represents my model. Should be obvious to EMF pros but took me some time to figure out.
|
|
|
Re: How to transform xText to XMI [message #666310 is a reply to message #666303] |
Tue, 19 April 2011 20:35 |
|
Hi,
Regarding the "to xmi" Button. Since the model files are a texual persistence of the ecore Based model and the xmi files are a xml based persistence the only difference is the way of persisting. So there is usually no need for a transformation except some special usecases e.g. If you want deploy the Models to somewhere where Xtext is Not available.
Regards, Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
Re: How to transform xText to XMI [message #697619 is a reply to message #666310] |
Sun, 17 July 2011 13:08 |
Marten Sijtema Messages: 67 Registered: November 2009 |
Member |
|
|
Currently I too use this approach, save it as .xmi, which is then read by eg. ATL (in an ant based transformation sequence).
Christian, do you still think there is need for m2m transformations, that read the xtext-generated xmi model, and transform it, followed by an eventual XPand transformation to text?
Because this is my current approach (ie. I have an MDA that generates webapps). Allthough I don't use any expressions in my model, just declarative structural stuff, and generate 'defaults' in terms of behaviours, so for that purpose I'd say the XTend thing is very useful.
I wonder what your opinion is about this.
My company: Sytematic, building business software from models.
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05035 seconds