|
|
Re: XML model to DSL syntax transformation [message #1739514 is a reply to message #1739506] |
Tue, 02 August 2016 15:07 |
Christoph Rieger Messages: 9 Registered: April 2016 |
Junior Member |
|
|
Hi Ed,
if I understand the papers you mentioned correctly, your approach goes beyond what I want to do. I try to explain with an example: I define an Xtext DSL "MyDSL" with the grammar
MyModel:
greetings+=Greeting*;
Greeting:
'Hello' name=ID '!';
from which Xtext automatically derives the Ecore meta model with class structure
[MyModel]-1----*-[Greeting].
Now I have a different meta model and corresponding model instance which I can transform to the be compliant to the above specification. Unfortunately, because the transformation step knows nothing about Xtext it will persist the converted model using an XML representation such as
<?xml version="1.0" encoding="UTF-8"?>
<MyDSL:MyModel xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:model="...">
<greeting name="Ed"/>
</MyDSL:MyModel>
What I now want is the step from this XML model representation to the respective MyDSL syntax, i.e.
From a technical point of view this seems rather trivial to me (take each element from XML and insert the respective values into the .mydsl file together with the respective terminal symbols) because the XML content already conforms to the MyDSL meta model. So I am wondering whether I miss some essential step, just didn't find an existing tool or why this does not make sense at all.
Regards
Christoph
[Updated on: Tue, 02 August 2016 15:08] Report message to a moderator
|
|
|
|
Re: XML model to DSL syntax transformation [message #1739548 is a reply to message #1739522] |
Tue, 02 August 2016 20:15 |
Christoph Rieger Messages: 9 Registered: April 2016 |
Junior Member |
|
|
Quote:Try opening you XMI file with the Xtext editor. It might just work if you got the XMI right.
Well, probably I get confused with the Xtext text/XMI resources. I can open the XMI with the UML Model editor without errors. But when I open it with the Xtext editor it does no de-serialization and just shows the regular XML content as text.
So my problem apparently boils down to the question if there is an easy way to open an XMI file with the (generated) Xtext editor and see the DSL representation?
PS: I found some posts on de-/serializing the model programatically by loading and saving it to the respective resource file type. But this sounds somehow cumbersome to do, considering that both are simply different representations of one another.
[Updated on: Tue, 02 August 2016 20:19] Report message to a moderator
|
|
|
|
|
Re: XML model to DSL syntax transformation [message #1739632 is a reply to message #1739591] |
Wed, 03 August 2016 12:04 |
Christoph Rieger Messages: 9 Registered: April 2016 |
Junior Member |
|
|
In case someone is interested in the programmatic transformation between a generated Xtext language and XMI:
public static void init() {
new org.eclipse.emf.mwe.utils.StandaloneSetup().setPlatformUri("../");
// Register respective language(s)
if (!EPackage.Registry.INSTANCE.containsKey("<languageUri>")) {
EPackage.Registry.INSTANCE.put("<languageUri>", <DSLname>Package.eINSTANCE);
}
}
public static void DslToXmi(String inputUri, String outputUri) {
init();
// Source
Injector injector = new <DSLname>StandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSetXtext = injector.getInstance(XtextResourceSet.class);
resourceSetXtext.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
Resource resourceInput = resourceSetXtext.getResource(URI.createURI(inputUri), true);
// Create new target file
ResourceSet resourceSet = new ResourceSetImpl();
Resource resourceXmi = resourceSet.createResource(URI.createURI(outputUri));
// Copy content
resourceXmi.getContents().addAll(resourceInput.getContents());
try {
Map<String, String> options = new HashMap<String, String>();
options.put(XMLResource.OPTION_ENCODING, "UTF-8");
resourceXmi.save(options);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void XmiToDsl(String inputUri, String outputUri) {
init();
// Source
ResourceSet resourceSet = new ResourceSetImpl();
Resource resourceXmi = resourceSet.getResource(URI.createURI(inputUri), true);
// Create new target file
Injector injector = new <DSLname>StandaloneSetup().createInjectorAndDoEMFRegistration();
XtextResourceSet resourceSetXtext = injector.getInstance(XtextResourceSet.class);
resourceSetXtext.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
Resource resourceDsl = resourceSetXtext.createResource(URI.createURI(outputUri));
// Copy content
resourceDsl.getContents().addAll(resourceXmi.getContents());
try {
resourceDsl.save(Collections.emptyMap());
} catch (IOException e) {
e.printStackTrace();
}
}
|
|
|
Powered by
FUDForum. Page generated in 0.03935 seconds