Acceleo and Xtext [message #902535] |
Sat, 18 August 2012 08:44 |
|
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.
Any help would be very appreciated! Thank you
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 (......
Some hours later
In 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.
Is there anyone who managed to use Xtext with Acceleo this way?
Follow me on Twitter @andreasindico
|
|
|
Re: Acceleo and Xtext [message #902536 is a reply to message #902535] |
Sat, 18 August 2012 08:49 |
|
Hi,
have no idea what your workflow is. is this code targetted to be run from within eclipse (command/action/...)
or standalone. (since i do not know acceleco that much i cannot answer this)
calling the standalonesetup in an non osgi env is a bad idea.
what is the error message you get... what does debugging say...
~Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
Re: Acceleo and Xtext [message #902537 is a reply to message #902536] |
Sat, 18 August 2012 09:15 |
|
Hi,
i just gave it a standalone try (using the uml example)
@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);
}
MyDslStandaloneSetup.doSetup();
}
package org.eclipse.acceleo.examples.uml2java.files;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.xtext.example.mydsl1.myDsl.DSLModel;
public class GetDSLModel {
public DSLModel getDSLModel(String expression){
ResourceSet resourceSet = new ResourceSetImpl();
Resource resource = resourceSet.createResource(URI.createURI("dummy:/example.mydsl1"));
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;
}
}
and it works like a charm
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
Re: Acceleo and Xtext [message #902541 is a reply to message #902535] |
Sat, 18 August 2012 09:49 |
Ed Willink Messages: 7670 Registered: July 2009 |
Senior Member |
|
|
Hi
You have been asking this question for some time on the M2T mailing
list, which is where it belongs.
However you just provide us parts of your program without any indication
how you invoke them.
The more partial information you provide the less attractive it is to
try to help you.
As Christian demonstrated, the basic functionality work fine, so
somewhere you are doing something odd, and you have not told us about it.
If you want help, you need to help yourself, by helping us. Provide a
simple zipped project and launch configuration that demonstrates the
problem. Do not provide snippets that require us to spend time
mis-re-developing what you have failed to provide.
Regards
Ed Willink
On 18/08/2012 09:44, Andrea Sindico wrote:
> 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.
> Any help would be very appreciated! Thank you
>
>
>
> 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: mailto:mypackage.dsl.impl.DSLModelImpl@a1c7a
>
> input: ['statement1;statement2'.getDSLModel().statements/]
> (Syntactically VALID)
> output: invalid
>
> input: ['statement1;statement2'.getDSLModel().eAllContents()/]
> output: mailto:mypackage.dsl.impl.DSLStatement@e53202 (......
>
> Some hours later
>
> In 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.
> Is there anyone who managed to use Xtext with Acceleo this way?
>
>
|
|
|
Re: Acceleo and Xtext [message #902544 is a reply to message #902541] |
Sat, 18 August 2012 10:35 |
|
Hi Ed, yes I have asked the same question in the M2T forum as well. However as the topic both concerns Acceleo and XText I thought it was a good idea to ask it in the Xtext forum too. I didn't think it was impolite. Is it? If so I do apologize.
Christian I really appreciate your help, unfortunately I guess the problem is in the interaction between Acceleo and Xtext so the best thing to do is indeed to provide you a zipped project showing it, as suggested by Ed.
Thank you
Follow me on Twitter @andreasindico
[Updated on: Sat, 18 August 2012 11:06] Report message to a moderator
|
|
|
Re: Acceleo and Xtext [message #902561 is a reply to message #902535] |
Sat, 18 August 2012 13:00 |
|
Ok I have just finished to create an independent workspace with a simple Acceleo and Xtext project reproducing exactly the same problem. It is about 19MB zipped. How do you want me to share it with you?
Follow me on Twitter @andreasindico
|
|
|
|
|
|
|
Re: Acceleo and Xtext [message #902585 is a reply to message #902584] |
Sat, 18 August 2012 16:41 |
|
Hi,
Running the Generate.java gives me the output that i expect.
the cleaner solution is to change registerPackages as i posted before and do not call it from the utility java class.
maybe something is broken with your installation.
~Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
Re: Acceleo and Xtext [message #902587 is a reply to message #902585] |
Sat, 18 August 2012 17:02 |
|
Do you mean it works?
Here is the text it produces if I run it
UML
test1: Class1
DSL
test1: org.xtext.example.mydsl.myDsl.impl.ModelImpl@1ae6ee8
test2: invalid
I get no errors just some warnings like
Discouraged access: The type IPartialContentAssistParser is not accessible due to restriction on required library ../eclipse-juno/plugins/org.eclipse.xtext.ui.codetemplates.ui_2.3.0.v201206120633.jar PartialMyDslContentAssistParser.java /org.xtext.example.mydsl.ui/src-gen/org/xtext/example/mydsl/ui/contentassist/antlr line 10 Java Problem
and
Description Resource Path Location Type
Discouraged access: The type IPartialContentAssistParser is not accessible due to restriction on required library ../eclipse-juno/plugins/org.eclipse.xtext.ui.codetemplates.ui_2.3.0.v201206120633.jar PartialMyDslContentAssistParser.java /org.xtext.example.mydsl.ui/src-gen/org/xtext/example/mydsl/ui/contentassist/antlr line 18 Java Problem
Follow me on Twitter @andreasindico
|
|
|
|
|
Re: Acceleo and Xtext [message #902593 is a reply to message #902592] |
Sat, 18 August 2012 18:21 |
|
Eclipse Modeling Tools
Version: Juno Release
Build id: 20120620-1657
and
java version "1.7.0_02"
Java(TM) SE Runtime Environment (build 1.7.0_02-b13)
Java HotSpot(TM) Server VM (build 22.0-b10, mixed mode)
Follow me on Twitter @andreasindico
|
|
|
|
|
|
|
|
Re: Acceleo and Xtext [message #902607 is a reply to message #902604] |
Sat, 18 August 2012 21:58 |
|
Hi Ed, I have created an Acceleo configuration as specified in the README. To run it i right click on the template, Run As->Run Configurations then I choose the acceleo one.
Unfortunately the model qualification is not the problem, in the more complex project I am working on (which shows the same behavior), I have a DSL with a root element called in a different way.
I am going to check the UML meta-model.
Follow me on Twitter @andreasindico
|
|
|
|
Re: Acceleo and Xtext [message #902656 is a reply to message #902535] |
Sun, 19 August 2012 05:30 |
|
Hi Ed and Christian,
I have tried, as you suggested, to run the acceleo transformation as a Java application and it works. I guess thus that if you try to run it as an acceleo application (see the latest link with the launch configuration file) you will get the same problem I am experiencing.
Follow me on Twitter @andreasindico
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.08380 seconds