Monday, March 30, 2009

Using Xtext in standalone mode

public static void main(String[] args) {
MyDslStandaloneSetup.doSetup();
ResourceSet resourceSet = new ResourceSetImpl();
URI uri = URI.createFileURI("src/test/test.mydsl");
Resource resource = resourceSet.getResource(uri, true);
Model model = (Model) resource.getContents().get(0);
for (org.xtext.example.myDsl.Class clazz : model.getElements()) {
System.out.println(clazz.getName());
}
}

Today we look at using the new TMF Xtext Framework which is currently available as beta version in standalone mode.  Things got considerably easier since the current version. The example has been built using Eclipse 3.5M6. The DSL is the one which is generated after creating a sample Xtext project. TMF Xtext will be available from June onwards.

Looking at the code snippet above they only thing which is worth mentioning is the class MyDslStandaloneSetup which is generated as part of your Domain Specific Language. It does all the required setup work. Xtext is using guice for its dependency management and configuration, but we do not not need to worry about that. The static doSetup method is doing all the work.

After initialising we can simply create a resource. All the required factories have already been registered using the StandaloneSetup class. Because Xtext is now also using generated classes which are derived from the ecore model, querying the model is even easier than before. No more dynamic EMF is required! The Model and Class classes are examples for the generated classes.
Questions? Post a comment!

7 comments:

  1. Hello,

    I've recently started working with xText and just downloaded the new version to give it a try. After running your code I get the following exception (I can't copy paste in here so I'll give a hint):

    com.google.inject.ProvisionException: Error while locating instance bound to org.eclipse.xtext.parsetree.reconstr.IParseTreeConstructor

    The same exception for ITokenSerializer, IValueConverterService...The exception is raised by this line of code:

    Resource resource = resourceSer.getResource(uri,true);

    Do you have any suggestions please? Thank you!

    Cosmin

    ReplyDelete
  2. Hi Cosmin,

    the xtext build I used for that is slightly older, so the API might have changed, but I guess that you have a different project setup. Where is your main defined, what is the name of your DSL project?

    ReplyDelete
  3. Hi Joheinz,

    I have used the sample grammair which exists in the xtext file after creating a project (the one with Model, Class, Import, Datatype, Property...which I think you've used here). The name of the project is the default one: "org.example.xtext.mydsl". I have created a separate Java project and I've added the reference of my dsl project to the build path. I still keep getting these exceptions though. Thank you!

    Cosmin

    ReplyDelete
  4. Did you add it to the build path or to the manifest. You should create a PDE (simply right click and "convert to PDE project") and then add the xtext project to your list of dependend plugins.

    Alternatively, try to see whether the main class works in the xtext project itself. My suspicion is that the metamodel cannot be found, hence the resourceset cannot be instantiated.

    Please report whether you were successful.

    ReplyDelete
  5. It is working. I've converted the project to PDE and added the necessary imports. It seems it couldn't reach the metamodel. Thank you so much for your help!

    Best regards,
    Cosmin

    ReplyDelete
  6. Hi.

    I wanted to check for some cases, for ex cyclic dependency,(which i have written in MyLanguageJavaValidator). I want this to be called before i pass the Model to generator.

    ReplyDelete
  7. Hi,

    I am a bit confused about this standalone code. Where is the code supposed to be put and how can you run it. I am new to xtext and I cant find anywhere where this is stated explicitly.

    Currently I put the code in my Xtext folder where mydsl.Xtext file is, and in a file called MyDslStandalonesetup.java and it doesn't work. I know this is very basic but I really need some help.

    Salome

    ReplyDelete