0

I have a DSL in Xtext, and I would like to reuse the rules, terminals, etc. defined in my .xtext file to generate a configuration file for some other tool involved in the project. The config file uses syntax similar to BNF, so it is very similar to the actual Xtext content and it requires minimal transformations. In theory I could easily write a script that would parse Xtext and spit out my config...

The question is, how do I go about implementing it so that it fits with the whole ecosystem? In other words - how to do a Model to Model transform in Xtext/EMF?

mirosval
  • 6,671
  • 3
  • 32
  • 46

2 Answers2

1

If you have both metamodels(ecore,xsd,...), your best shot is to use ATL ( http://www.eclipse.org/atl/ ).

xavier.seignard
  • 11,254
  • 13
  • 51
  • 75
1

If I understand you correct you want to go from an xtext model to its EMF model. Here is a code example that achieves this, substitute your model specific where necessary.

    public static BeachScript loadScript(String file) throws BeachScriptLoaderException { 
    try {
        Injector injector = new BeachStandaloneSetup().createInjectorAndDoEMFRegistration();
        XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
        resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
        Resource resource = resourceSet.createResource(URI.createURI("test.beach"));
        InputStream in = new ByteArrayInputStream(file.getBytes());
        resource.load(in, resourceSet.getLoadOptions());
        BeachScript model = (BeachScript) resource.getContents().get(0);
        return model;

    } catch (Exception e) {
        throw new BeachScriptLoaderException("Exception Loading Beach Script " + e.toString(),e );
    }
Hauke P.
  • 2,695
  • 1
  • 20
  • 43
Duncan Krebs
  • 3,366
  • 2
  • 33
  • 53