Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » XML model to DSL syntax transformation
XML model to DSL syntax transformation [message #1739493] Tue, 02 August 2016 12:37 Go to next message
Christoph Rieger is currently offline Christoph RiegerFriend
Messages: 9
Registered: April 2016
Junior Member
Hello,

I am working on a project that is related to an existing Xtext DSL (referred to as "ONE") but uses a separate meta model ("TWO"), so I have two Ecore meta model descriptions.

What I am trying to achieve is a model-to model transformation from ONE to TWO. This is nothing spectacular and there are tools such as QVT to create a model compliant to TWO's meta model which is saved to an XML file. But I am particularly interested in reaching the defined syntax of the target DSL!

Is there a tool I have not found yet which can convert the XML model representation to the ONE syntax defined in Xtext?

Best regards,
Christoph

PS: Obviously I could write a model-to-text generator from a TWO model to the ONE syntax but from a type safety perspective this seems more error-prone than a model instance conforming to ONE's meta model and just trying to create the proper representation.
Re: XML model to DSL syntax transformation [message #1739506 is a reply to message #1739493] Tue, 02 August 2016 12:50 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

I'm not entirely clear what you mean by "But I am particularly
interested in reaching the defined syntax of the target DSL" but I
suspect that you are looking at the CS2AS preoblem that we face in QVT
and OCL. CS is Xtext-friendly and AS is OMG-defined.

http://www.eclipse.org/modeling/mdt/ocl/docs/publications/OCL2015CS2AS/ow2015.pdf

http://www.eclipse.org/modeling/mdt/ocl/docs/publications/ICMT2016CS2AS_DSTL/icmt2016.pdf

The CS2AS is the hard part to model and tool. The inverse AS2CS should
drop out from the same models.

Regards

Ed Willink

On 02/08/2016 13:37, Christoph Rieger wrote:
> Hello,
>
> I am working on a project that is related to an existing Xtext DSL
> (referred to as "ONE") but uses a separate meta model ("TWO"), so I have
> two Ecore meta model descriptions.
>
> What I am trying to achieve is a model-to model transformation from ONE
> to TWO. This is nothing spectacular and there are tools such as QVT to
> create a model compliant to TWO's meta model which is saved to an XML
> file. But I am particularly interested in reaching the defined syntax of
> the target DSL!
>
> Is there a tool I have not found yet which can convert the XML model
> representation to the ONE syntax defined in Xtext?
>
> Best regards,
> Christoph
>
> PS: Obviously I could write a model-to-text generator from a TWO model
> to the ONE syntax but from a type safety perspective this seems more
> error-prone than a model instance conforming to ONE's meta model and
> just trying to create the proper representation.


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
Re: XML model to DSL syntax transformation [message #1739514 is a reply to message #1739506] Tue, 02 August 2016 15:07 Go to previous messageGo to next message
Christoph Rieger is currently offline Christoph RiegerFriend
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.
Hello Ed !


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 #1739522 is a reply to message #1739514] Tue, 02 August 2016 16:21 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Sorry.I do not understand at all.

Either you have two Ecore model; as you originally said and so you
require a transformation between them. Possibly a trivial one.

Or you don't and you just need to use Xtext text/XMI resources
consistently. Xtext's serializer will automatically create the textual
view of its XMI. You may want to tweak the whitespace formatting.

Try opening you XMI file with the Xtext editor. It might just work if
you got the XMI right.

Regards

Ed Willink



On 02/08/2016 16:07, Christoph Rieger wrote:
> 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.
> Hello Ed !
>
> From a technical point of view this seems rather trivial to me (take
> each element from XML and insert the respective values into the .mymodel
> file together with the respective terminals) 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


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
Re: XML model to DSL syntax transformation [message #1739548 is a reply to message #1739522] Tue, 02 August 2016 20:15 Go to previous messageGo to next message
Christoph Rieger is currently offline Christoph RiegerFriend
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 #1739586 is a reply to message #1739548] Wed, 03 August 2016 07:11 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

You have to get your XMI right.

If it's wrong the serializer may ignore it. If you're lucky you may see
something helpful in the Error Log, or observe an Exception with the
debugger.

If the above doesn't help, then proceed from what works to what doesn't.

If you open a *.xtext with the Ecore Editor you can File->Save AS ...
something.xmi and so see what correct XMI should look like.
Unfortunately if you open this XMI with the Xtext editor you see the
XMI. My recollection is that it used to serialize, so it appears that
this functionality is now only available programmatically.

https://bugs.eclipse.org/bugs/show_bug.cgi?id=499082 raised

Regards

Ed Willink



On 02/08/2016 21:15, Christoph Rieger wrote:
> Well, probably I get confused with the Xtext text/XMI resources.
>
> Quote:
>> Try opening you XMI file with the Xtext editor. It might just work if
>> you got the XMI right.
>
> I indeed had some problem with the XMI and can now open them with the
> UML Model editor. But when I open it with the generated Xtext editor it
> does no de-serialization and 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) DSL editor and see the DSL
> representation?


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
Re: XML model to DSL syntax transformation [message #1739591 is a reply to message #1739586] Wed, 03 August 2016 07:47 Go to previous messageGo to next message
Christoph Rieger is currently offline Christoph RiegerFriend
Messages: 9
Registered: April 2016
Junior Member
Hi

thank you for the fast replies, apparently I will have to find a programmatic way in the meantime.

Regards
Christoph
Re: XML model to DSL syntax transformation [message #1739632 is a reply to message #1739591] Wed, 03 August 2016 12:04 Go to previous message
Christoph Rieger is currently offline Christoph RiegerFriend
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();
    }
  }
Previous Topic:xtext apply terminal rule only on selected parser rule
Next Topic:existent JVMInferrer does not work any more
Goto Forum:
  


Current Time: Fri Apr 26 19:05:23 GMT 2024

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

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

Back to the top