Meshing Acceleo with Xtext

南笙酒味 提交于 2019-12-24 01:08:38

问题


I am in the middle of an Acceleo Transformation aimed at producing code (i.e. Java) from an input UML model.

Some elements of this UML model (i.e. Activities Opaque actions) contain some text which is conform to an Xtext grammar and I'd like to get the equivalent AST Ecore representation in the Acceleo transformation.

To this end I have developed a Java class with a method which takes as input a string, containing the DSL excerpt, and produces an Ecore model conform to it (see http://www.eclipse.org/forums/index.php/m/901947/#msg_901947 for further details). I have tested it in a separate Java application and it seems it works properly.

I have therefore written a simple Acceleo module (i.e. getDSLModel) wrapping that java class and enabling me to get the Ecore model from the DSL textual representation.

Suppose my DSL (and the equivalent Ecore) consist of a root element named DSLModel containing a (0..*) number of DSLStatements (this is a simplification). When in Acceleo I invoke the wrapper from a string, containing a correct DSL script, I have noticed it correctly returns a ModelImpl.

    ['statement1;statement2'.getDSLModel()/]

so the Java service and the Xtext parse is working.

However if I try to get the model statements, i.e.:

  ['statement1;statement2'.getDSLModel().statements/]

it returns an "invalid" string. So I can't use it in a for loop

I have therefore tried to call the eAllContents() OCL service from the model instance i.e.:

  ['statement1;statement2'.getDSLModel().eAllContents()/]

and it actually returns the list of statements. I do not understand why the features of the Ecore entities returned from the Xtext parser are not working properly.


Here is the Java service which turns a string into a instance of my DSL model (Ecore AST). I have tested it with an independent Java application and it works fine!

 public class GetDSLModel {
 public DSLModel getDSLModel(String expression){    
    DSLStandaloneSetupGenerated dslsas = new DSLStandaloneSetupGenerated();
    Injector injector = dslsas.createInjectorAndDoEMFRegistration();
    XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
    resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE);
    Resource resource = resourceSet.createResource(URI.createURI("dummy:/example.dsl"));
    InputStream in = new ByteArrayInputStream(expression.getBytes());
    try {
        resource.load(in, resourceSet.getLoadOptions());
        DSLModel model = (DSLModel) resource.getContents().get(0);
        return model;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;

}

}

Now I need the AST in the main Acceleo (UML2Text) transformation thus here is the Acceleo wrapper

 [query public getDSLModel(str:String): DSLModel =  (invoke('sfg.baleno.src.mloaders.GetDSLModel','getDSLModel(java.lang.String)',Sequence{str})).oclAsType(DSLModel)/]

here is what I get if run it

  input: ['statement1;statement2'.getDSLModel()/]
  output: mypackage.dsl.impl.DSLModelImpl@a1c7a

  input: ['statement1;statement2'.getDSLModel().statements/]  (Syntactically VALID)
  output: invalid

  input: ['statement1;statement2'.getDSLModel().eAllContents()/]
  output: mypackage.dsl.impl.DSLStatement@e53202 (......

UPDATE

To the Java Class of the main Acceleo module I have added the following lines

 @Override
 public void initialize(EObject element, File folder, java.util.List<? extends Object> arguments) throws IOException {    preInitialize();
    super.initialize(element, folder, arguments);
}
@Override
public void initialize(URI modelURI, File folder, java.util.List<?> arguments) throws IOException {
    preInitialize();
    super.initialize(modelURI, folder, arguments);
}

protected void preInitialize() {
   DSLStandaloneSetup.doSetup();
}

and

  @Override
 public void registerPackages(ResourceSet resourceSet) {
    super.registerPackages(resourceSet);
   if (!isInWorkspace(org.eclipse.uml2.uml.UMLPackage.class)) {
         resourceSet.getPackageRegistry().put(org.eclipse.uml2.uml.UMLPackage.eINSTANCE.getNsURI(), org.eclipse.uml2.uml.UMLPackage.eINSTANCE);
    }
    if (!isInWorkspace(mypackages.DSLPackage.class)) {
        resourceSet.getPackageRegistry().put(mypackages.DSLPackage.eINSTANCE.getNsURI(), mypackages.DSLPackage.eINSTANCE);
     }
   EcoreUtil.resolveAll(resourceSet);
}

but it still behaves the same.

UPDATE

At this link you temporary find a zipped file of an example EMF workspace containing an Acceleo and a XText project reproducing the issue. The weird thing is that if you run it as a Java application it works but if you run it as an Acceleo application it does not...

https://docs.google.com/open?id=0B2_ovo8IiZaZaXdNdFdPMTI4Yjg

In the top left corner you should find a File menu from which you can download the zip file.


回答1:


I am unfortunately not that familiar with Xtext ... but I believe your problem lies in the way you load your dsl from within the java service : you are generating in the context of a running Eclipse ("acceleo application"), yet you load your model as if you were in standalone : new DSLStandaloneSetup, createInjector...

I believe that this way of loading your model gives you two instances of the Xtext metamodels and factories, making OCL fails to retrieve the feature "statements" when you try to obtain its values.

One possible way I can think of to bypass this would be to change your service to take an EObject of any sort as parameter and use its resource set to load your dsl (thus using the resource set you've initialized in the launcher, the one Acceleo uses, instead of your own) :

public DSLModel getDSLModel(String expression, EObject eObj){
    ResourceSet rSet = eObj.eResource().getResourceSet();
    [...]
}

With that, you should not need the second Xtext initialization you do from the service ... If it does not work tough, I don't really have a solution to propose other than to go to the Xtext forums and ask there how to make an application that can load a DSL and work both in standalone and in a pluginized environment.



来源:https://stackoverflow.com/questions/11966797/meshing-acceleo-with-xtext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!