本文介绍了用Xtext进行Meshing Acceleo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正处于一个Acceleo转换的中间,旨在从输入的UML模型生成代码(即Java)。I am in the middle of an Acceleo Transformation aimed at producing code (i.e. Java) from an input UML model.这个UML模型的一些元素(即活动不透明的动作)包含符合Xtext语法的一些文本,并且我想获得等效的AST Ecore表示Acceleo转型。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.为此,我开发了一个Java类,它使用一个方法来输入一个包含DSL摘录的字符串,并生成符合它的Ecore模型(参见 http://www.eclipse.org/forums/index.php/m / 901947 /#msg_901947 )。我已经在一个单独的Java应用程序中进行了测试,似乎它正常工作。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.我已经写了一个简单的Acceleo模块(即getDSLModel)从DSL文本表示中获取Ecore模型。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.假设我的DSL(和等价的Ecore)由一个名为 DSLModel 包含一个(0 .. *)数量的 DSLStatement s(这是一个简化)。 当在Acceleo中,我从包含正确的DSL脚本的字符串调用包装器时,我注意到它正确地返回一个 ModelImpl 。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()/]所以Java服务和Xtext解析正在运行。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/]它返回一个无效字符串。所以我不能在一个for循环中使用它it returns an "invalid" string. So I can't use it in a for loop因此,我试图从模型实例调用eAllContents()OCL服务,即:I have therefore tried to call the eAllContents() OCL service from the model instance i.e.: ['statement1;statement2'.getDSLModel().eAllContents()/],它实际上返回了语句列表。我不明白为什么从Xtext解析器返回的Ecore实体的功能不能正常工作。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.以下是将服务器转换为DSL模型(Ecore AST)实例的Java服务。我已经用独立的Java应用程序测试了它,它的工作正常!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;}}现在我需要主要Acceleo(UML2Text)转换中的AST是Acceleo包装器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 (...... 更新对于Java类主要Acceleo模块我添加了以下行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);}@Overridepublic void initialize(URI modelURI, File folder, java.util.List<?> arguments) throws IOException { preInitialize(); super.initialize(modelURI, folder, arguments);}protected void preInitialize() { DSLStandaloneSetup.doSetup();}和 @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);}但它仍然表现相同。 更新在此链接中,您临时找到示例EMF工作区的压缩文件包含Acceleo和XText项目,重现了这个问题。奇怪的是,如果您以Java应用程序的形式运行它,但是如果您将其作为Acceleo应用程序运行,则不会...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在左上角,您应该可以找到一个文件菜单,您可以从中下载zip文件。In the top left corner you should find a File menu from which you can download the zip file.推荐答案我不太熟悉Xtext ...但我相信你的问题在于你在java服务中加载dsl的方式:你正在运行的Eclipse(acceleo应用程序)的上下文中生成,但是你加载你的模型就好像你是独立的:新的DSLStandaloneSetup,createInjector ...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...我相信这种加载你的模型的方式给你两个Xtext元模型和工厂,使OCL无法在您尝试获取其值时检索功能语句。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.我可以想到的一个可能的方法是绕过这个,就是改变你的服务,把任何一个EObject作为参数,并使用它的资源集加载你的dsl(因此使用资源集,您在启动器中初始化了Acacleo使用的资源集,而不是您自己的资源集):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(); [...]}这样,你不需要您从服务中执行的第二个Xtext初始化...如果不起作用,我真的不需要提出解决方案,而不是去Xtext论坛,并询问如何使可以加载DSL的应用程序并且在独立和插入式环境中工作。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. 这篇关于用Xtext进行Meshing Acceleo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-28 17:11