Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext interpreter to instantiate EMF Object
Xtext interpreter to instantiate EMF Object [message #922747] Tue, 25 September 2012 09:40 Go to next message
Charles Bonneau is currently offline Charles BonneauFriend
Messages: 32
Registered: February 2010
Location: Belgium
Member
Hello,
I have problem that I'm trying to solve using Xtext (I already have a working PoC in Groovy):
I have a huge meta model EMF, and a Xtext grammar generated from it.
The problem is that the resulting DSL is too complex for the user.
What I would like to do is, create a new grammar from scratch, that would describe a subset of the generated DSL and some specific keywords and have some kind of interpreter that would actually create EMF objects.

Example :
Wanted language:
Person (name="John" children+="Toto")


Existing language (mapping 1-1 with EMF Objects)
Person {
   name = "John",
   children {
      Person{
        name = "Toto"
      }
   }
}


Thank you.

Charles
Re: Xtext interpreter to instantiate EMF Object [message #923413 is a reply to message #922747] Tue, 25 September 2012 22:28 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2501
Registered: July 2009
Senior Member
Did you have a question?

In general - yes, you can do that.
- henrik

On 2012-25-09 2:40, Charles Bonneau wrote:
> Hello,
> I have problem that I'm trying to solve using Xtext (I already have a
> working PoC in Groovy):
> I have a huge meta model EMF, and a Xtext grammar generated from it.
> The problem is that the resulting DSL is too complex for the user.
> What I would like to do is, create a new grammar from scratch, that
> would describe a subset of the generated DSL and some specific keywords
> and have some kind of interpreter that would actually create EMF objects.
>
> Example : Wanted language:
>
> Person (name="John" children+="Toto")
>
>
> Existing language (mapping 1-1 with EMF Objects)
>
> Person {
> name = "John",
> children {
> Person{
> name = "Toto"
> }
> }
> }
>
>
> Thank you.
>
> Charles
>
Re: Xtext interpreter to instantiate EMF Object [message #923647 is a reply to message #922747] Wed, 26 September 2012 04:38 Go to previous messageGo to next message
Andreas Graf is currently offline Andreas GrafFriend
Messages: 157
Registered: July 2009
Senior Member
The thing that you are looking for is using Xtext with "manually maintained ecore". Since you already have a grammar that has been generated from the ecore, you'd have everything like mwe and import statements set up correctly. You just might throw away all the rules in your grammar and define your own.
Re: Xtext interpreter to instantiate EMF Object [message #923838 is a reply to message #923647] Wed, 26 September 2012 08:36 Go to previous messageGo to next message
Charles Bonneau is currently offline Charles BonneauFriend
Messages: 32
Registered: February 2010
Location: Belgium
Member
@Henrick:
OK, that's what I though. But my question is : How should I do that ?
- Using an IGenerator ? I don't want to generate anything.
- Using an IJvmModelInferrer ? I tried but didn't achieved anything either.
- Using a home-made interpreter that would look like this ?
 
class DSLInterpreter {

   // More of less M2M transformation rules
   public bigModel.EObject interprete(DSLEobject o){
      if (o instanceof Person){
         bigModel.Person p = bigModelFactory.eINSTANCE.createPerson();
         p.setName(((Person)o).getName());
         for (String name : ((Person)o).getChildren()){
            bigModel.Person child = bigModelFactory.eINSTANCE.createPerson();
            child .setName(name);
            p.getChildren().add(child);
         }
         return p;
      }
   }
}


@Andreas:
Indeed, I can set up some "shortcuts" in the grammar, but how about a more clever interpreter. I couldn't find any example of DSL Interpreters in Xtext. I guess I'll have to override the XtextBuilder right ?

[Updated on: Wed, 26 September 2012 08:39]

Report message to a moderator

Re: Xtext interpreter to instantiate EMF Object [message #924160 is a reply to message #923838] Wed, 26 September 2012 15:06 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2501
Registered: July 2009
Senior Member
1. The interpreter
A DSL intepreter is quite straight forward to write (complexities comes
from possible advanced constructs in the language being interpreted).

There is a big interpreter in the Eclipse b3 project (includes handling
aspects, injections, etc. and integrates with Java.)

Here is a literal string evaluator:
org.cloudsmith.geppetto.pp.dsl.eval.PPStringConstantEvaluator
it is in cloudsmith/geppetto @ github.
That shows a simple way of iterating over a model and evaluating.

2. When to interpret
Don't know how you want this to work; what starts the interpretation?

3. Where to put the result
Same here, what do you expect as a result?

Regards
- henrik

On 2012-26-09 1:36, Charles Bonneau wrote:
> @Henrick: OK, that's what I though. But my question is : How should I do
> that ? - Using an IGenerator ? I don't want to generate anything.
> - Using an IJvmModelInferrer ? I tried but didn't achieved anything
> either.
> - Using a home-made interpreter that would look like this ?
>
> class DSLInterpreter {
>
> // More of less M2M transformation rules
> public bigModel.EObject interprete(DSLEobject o){
> if (o instanceof Person){
> bigModel.Person p = bigModelFactory.eINSTANCE.createPerson();
> p.setName(((Person)o).getName());
> for (String name : ((Person)o).getChildren()){
> bigModel.Person child =
> bigModelFactory.eINSTANCE.createPerson();
> child .setName(name);
> p.getChildren().add(child);
> }
> return p;
> }
> }
> }
>
>
> @Andreas:
> Indeed, I can set up some "shortcuts" in the grammar, but how about a
> more clever interpreter. I couldn't find any example of DSL Interpreters
> in Xtext.
Re: Xtext interpreter to instantiate EMF Object [message #924955 is a reply to message #924160] Thu, 27 September 2012 09:05 Go to previous messageGo to next message
Charles Bonneau is currently offline Charles BonneauFriend
Messages: 32
Registered: February 2010
Location: Belgium
Member
OK Thank you very much !
I will take a look at the example you mentioned.

Quote:
2. When to interpret
Don't know how you want this to work; what starts the interpretation?

I would say at modification time or save time (like a Builder). But after some thinking, the interpretation should be triggered by the user.

Quote:
3. Where to put the result
Same here, what do you expect as a result?

In memory.

Thank you very much for the questions, it helped me clarify my problem.

Best regards,

Charles


Re: Xtext interpreter to instantiate EMF Object [message #940063 is a reply to message #922747] Thu, 11 October 2012 09:21 Go to previous messageGo to next message
Charles Bonneau is currently offline Charles BonneauFriend
Messages: 32
Registered: February 2010
Location: Belgium
Member
Hello again,
I've implemented 2 Proof of Concept to iterate over the model and evaluating it.
One using the Switch generated by EMF:
Class MyPersonSwitch extends PersonSwitch<bigModel.Person> {
   bigModel.Person casePerson (Person o){
      bigModel.Person p = bigModelFactory.eINSTANCE.createPerson();
      p.setName(o.getName());
      for (String name : o.getChildren()){
         bigModel.Person child = bigModelFactory.eINSTANCE.createPerson();
         child .setName(name);
         p.getChildren().add(child);
      }
      return p;
   } 
}

and the other one using the PolymorphicDispatcher from Xtext as in cloudsmith/geppetto.


From your point of view what is the best approach ?
Re: Xtext interpreter to instantiate EMF Object [message #940163 is a reply to message #940063] Thu, 11 October 2012 11:15 Go to previous message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2501
Registered: July 2009
Senior Member
On 2012-11-10 11:21, Charles Bonneau wrote:
> Hello again,
> I've implemented 2 Proof of Concept to iterate over the model and
> evaluating it. One using the Switch generated by EMF:
>
> Class MyPersonSwitch extends PersonSwitch<bigModel.Person> {
> bigModel.Person casePerson (Person o){
> bigModel.Person p = bigModelFactory.eINSTANCE.createPerson();
> p.setName(o.getName());
> for (String name : o.getChildren()){
> bigModel.Person child = bigModelFactory.eINSTANCE.createPerson();
> child .setName(name);
> p.getChildren().add(child);
> }
> return p;
> } }
>
> and the other one using the PolymorphicDispatcher from Xtext as in
> cloudsmith/geppetto.
>
>
> From your point of view what is the best approach ?
The switch from EMF may run faster since it is not using reflection, but
it is not as flexible.

- henrik
Previous Topic:Scoping vs Naming
Next Topic:Extracting DSL Metamodels
Goto Forum:
  


Current Time: Sun Dec 21 08:44:24 GMT 2014

Powered by FUDForum. Page generated in 0.01992 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software